Coverage report

  %line %branch
org.apache.turbine.services.component.TurbineComponentService
85% 
94% 

 1  
 package org.apache.turbine.services.component;
 2  
 
 3  
 /*
 4  
  * Copyright 2001-2005 The Apache Software Foundation.
 5  
  *
 6  
  * Licensed under the Apache License, Version 2.0 (the "License")
 7  
  * you may not use this file except in compliance with the License.
 8  
  * You may obtain a copy of the License at
 9  
  *
 10  
  *     http://www.apache.org/licenses/LICENSE-2.0
 11  
  *
 12  
  * Unless required by applicable law or agreed to in writing, software
 13  
  * distributed under the License is distributed on an "AS IS" BASIS,
 14  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 15  
  * See the License for the specific language governing permissions and
 16  
  * limitations under the License.
 17  
  */
 18  
 
 19  
 import java.util.Iterator;
 20  
 import javax.servlet.ServletConfig;
 21  
 
 22  
 import org.apache.commons.configuration.BaseConfiguration;
 23  
 import org.apache.commons.configuration.Configuration;
 24  
 import org.apache.commons.logging.Log;
 25  
 import org.apache.commons.logging.LogFactory;
 26  
 import org.apache.stratum.component.ComponentLoader;
 27  
 import org.apache.stratum.lifecycle.Disposable;
 28  
 import org.apache.turbine.Turbine;
 29  
 import org.apache.turbine.services.InitializationException;
 30  
 import org.apache.turbine.services.TurbineBaseService;
 31  
 
 32  
 /**
 33  
  * An implementation of ComponentService which loads all the
 34  
  * components given in the TurbineResources.properties File
 35  
  *
 36  
  * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
 37  
  * @version $Id: TurbineComponentService.java 264148 2005-08-29 14:21:04Z henning $
 38  
  * @deprecated torque is now loaded using the AvalonComponentService
 39  
  */
 40  4
 public class TurbineComponentService
 41  
         extends TurbineBaseService
 42  
         implements ComponentService
 43  
 {
 44  
 
 45  
     /** Logging */
 46  4
     private static Log log = LogFactory.getLog(TurbineComponentService.class);
 47  
 
 48  
     /** Extension used for Configuration files. */
 49  2
     private static String CONFIG = "config";
 50  
 
 51  
     /** Name tag used in Configurations */
 52  2
     private static String NAME = "name";
 53  
 
 54  
     /** Prefix used by the Component Loader */
 55  2
     private static String COMPONENT = "component";
 56  
 
 57  
     /** List of Components that was initialized */
 58  2
     private Object[] components = null;
 59  
 
 60  
     /**
 61  
      * Load all configured components and initialize them. This is
 62  
      * a zero parameter variant which queries the Turbine Servlet
 63  
      * for its config.
 64  
      *
 65  
      * @throws InitializationException Something went wrong in the init
 66  
      *         stage
 67  
      */
 68  
     public void init()
 69  
             throws InitializationException
 70  
     {
 71  2
         ServletConfig config = Turbine.getTurbineServletConfig();
 72  2
         Configuration loaderConf = new BaseConfiguration();
 73  
 
 74  2
         String[] names = getConfiguration().getStringArray(NAME);
 75  
 
 76  2
         log.warn("The ComponentService is deprecated!");
 77  
 
 78  4
         for (int i = 0; i < names.length; i++)
 79  
         {
 80  2
             String key = names[i];
 81  
 
 82  2
             loaderConf.addProperty(COMPONENT + "." + NAME, key);
 83  
 
 84  2
             String subProperty = COMPONENT + "." + key;
 85  2
             Configuration subConf = getConfiguration().subset(key);
 86  
 
 87  5
             for (Iterator it = subConf.getKeys(); it.hasNext();)
 88  
             {
 89  4
                 String subKey = (String) it.next();
 90  4
                 Object subVal = subConf.getProperty(subKey);
 91  
 
 92  4
                 if (subKey.equals(CONFIG))
 93  
                 {
 94  2
                     log.debug("Fixing up " + subVal);
 95  2
                     String newPath =
 96  
                             config.getServletContext().getRealPath((String) subVal);
 97  
 
 98  2
                     if (newPath == null)
 99  
                     {
 100  0
                       throw new InitializationException("Could not translate path " + subVal);
 101  
                     }
 102  
 
 103  2
                     subVal = newPath;
 104  2
                     log.debug("Now: " + subVal);
 105  
                 }
 106  
 
 107  4
                 loaderConf.addProperty(subProperty + "." + subKey,
 108  
                         subVal);
 109  
             }
 110  
 
 111  2
             log.info("Added " + key + " as a component");
 112  
         }
 113  
 
 114  
         try
 115  
         {
 116  2
             ComponentLoader cl = new ComponentLoader(loaderConf);
 117  2
             components = cl.load();
 118  2
             setInit(true);
 119  1
         }
 120  0
         catch (Exception e)
 121  
         {
 122  0
             log.error("Component Service failed: ", e);
 123  0
             throw new InitializationException("ComponentService failed: ", e);
 124  1
         }
 125  2
     }
 126  
 
 127  
     /**
 128  
      * Inits the service using servlet parameters to obtain path to the
 129  
      * configuration file. Change relatives paths.
 130  
      *
 131  
      * @param config The ServletConfiguration from Turbine
 132  
      *
 133  
      * @throws InitializationException Something went wrong when starting up.
 134  
      * @deprecated use init() instead.
 135  
      */
 136  
     public void init(ServletConfig config)
 137  
             throws InitializationException
 138  
     {
 139  0
         init();
 140  0
     }
 141  
 
 142  
     /**
 143  
      * Shuts the Component Service down, calls dispose on the components that
 144  
      * implement this interface
 145  
      *
 146  
      */
 147  
 
 148  
     public void shutdown()
 149  
     {
 150  2
         if (components != null)
 151  
         {
 152  4
             for (int i = 0; i < components.length; i++)
 153  
             {
 154  2
                 if (components[i] instanceof Disposable)
 155  
                 {
 156  2
                     log.debug("Disposing a " + components[i].getClass().getName() + " object");
 157  2
                     ((Disposable) components[i]).dispose();
 158  
                 }
 159  
                 else
 160  
                 {
 161  0
                     log.debug("Not disposing " + components[i].getClass().getName() + ", not a Disposable Object");
 162  
                 }
 163  
             }
 164  
         }
 165  2
         setInit(false);
 166  2
     }
 167  
 }

This report is generated by jcoverage, Maven and Maven JCoverage Plugin.