Coverage report

  %line %branch
org.apache.turbine.services.avaloncomponent.TurbineAvalonComponentService
84% 
100% 

 1  
 package org.apache.turbine.services.avaloncomponent;
 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 java.util.List;
 21  
 import java.util.Vector;
 22  
 
 23  
 import org.apache.commons.logging.Log;
 24  
 import org.apache.commons.logging.LogFactory;
 25  
 
 26  
 import org.apache.avalon.excalibur.component.DefaultRoleManager;
 27  
 import org.apache.avalon.excalibur.component.ExcaliburComponentManager;
 28  
 import org.apache.avalon.excalibur.logger.Log4JLoggerManager;
 29  
 import org.apache.avalon.excalibur.logger.LoggerManager;
 30  
 import org.apache.avalon.framework.activity.Disposable;
 31  
 import org.apache.avalon.framework.activity.Initializable;
 32  
 import org.apache.avalon.framework.component.Component;
 33  
 import org.apache.avalon.framework.component.ComponentException;
 34  
 import org.apache.avalon.framework.configuration.Configuration;
 35  
 import org.apache.avalon.framework.configuration.DefaultConfigurationBuilder;
 36  
 import org.apache.avalon.framework.context.DefaultContext;
 37  
 import org.apache.avalon.framework.logger.Logger;
 38  
 
 39  
 import org.apache.turbine.Turbine;
 40  
 import org.apache.turbine.services.InitializationException;
 41  
 import org.apache.turbine.services.TurbineBaseService;
 42  
 
 43  
 /**
 44  
  * An implementation of AvalonComponentService which loads all the
 45  
  * components given in the TurbineResources.properties File.
 46  
  * <p>
 47  
  * For component which require the location of the application or
 48  
  * context root, there are two ways to get it.
 49  
  * <ol>
 50  
  * <li>
 51  
  *   Implement the Contextualizable interface.  The full path to the
 52  
  *   correct OS directory can be found under the ComponentAppRoot key.
 53  
  * </li>
 54  
  * <li>
 55  
  *   The system property "applicationRoot" is also set to the full path
 56  
  *   of the correct OS directory.
 57  
  * </li>
 58  
  * </ol>
 59  
  * If you want to initialize Torque by using the AvalonComponentService, you
 60  
  * must activate Torque at initialization time by specifying
 61  
  *
 62  
  * services.AvalonComponentService.lookup = org.apache.torque.avalon.Torque
 63  
  *
 64  
  * in your TurbineResources.properties.
 65  
  *
 66  
  * @author <a href="mailto:quintonm@bellsouth.net">Quinton McCombs</a>
 67  
  * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
 68  
  * @version $Id: TurbineAvalonComponentService.java 280565 2005-09-13 14:39:33Z henning $
 69  
  */
 70  56
 public class TurbineAvalonComponentService
 71  
         extends TurbineBaseService
 72  
         implements AvalonComponentService, Initializable, Disposable
 73  
 {
 74  
     /** Logging */
 75  28
     private static Log log = LogFactory.getLog(
 76  14
             TurbineAvalonComponentService.class);
 77  
 
 78  
     /** Component manager */
 79  28
     private ExcaliburComponentManager manager = null;
 80  
 
 81  
     // -------------------------------------------------------------
 82  
     // Service initialization
 83  
     // -------------------------------------------------------------
 84  
 
 85  
     /**
 86  
      * Load all configured components and initialize them. This is
 87  
      * a zero parameter variant which queries the Turbine Servlet
 88  
      * for its config.
 89  
      *
 90  
      * @throws InitializationException Something went wrong in the init
 91  
      *         stage
 92  
      */
 93  
     public void init()
 94  
             throws InitializationException
 95  
     {
 96  
         try
 97  
         {
 98  28
             initialize();
 99  
 
 100  28
             setInit(true);
 101  14
         }
 102  0
         catch (Exception e)
 103  
         {
 104  0
             throw new InitializationException("init failed", e);
 105  14
         }
 106  28
     }
 107  
 
 108  
     /**
 109  
      * Shuts the Component Service down, calls dispose on the components that
 110  
      * implement this interface
 111  
      *
 112  
      */
 113  
     public void shutdown()
 114  
     {
 115  0
         dispose();
 116  0
         setInit(false);
 117  0
     }
 118  
 
 119  
     // -------------------------------------------------------------
 120  
     // Avalon lifecycle interfaces
 121  
     // -------------------------------------------------------------
 122  
 
 123  
     /**
 124  
      * Initializes the container
 125  
      *
 126  
      * @throws Exception generic exception
 127  
      */
 128  
     public void initialize() throws Exception
 129  
     {
 130  28
         org.apache.commons.configuration.Configuration conf
 131  
                 = getConfiguration();
 132  
 
 133  
         // get the filenames and expand them relative to webapp root
 134  28
         String sysConfigFilename = Turbine.getRealPath(
 135  
                 conf.getString(COMPONENT_CONFIG_KEY, COMPONENT_CONFIG_VALUE));
 136  28
         String roleConfigFilename = Turbine.getRealPath(
 137  
                 conf.getString(COMPONENT_ROLE_KEY, COMPONENT_ROLE_VALUE));
 138  
 
 139  28
         log.debug("Config File: " + sysConfigFilename);
 140  28
         log.debug("Role File:   " + roleConfigFilename);
 141  
 
 142  
         // process configuration files
 143  
 
 144  28
         DefaultConfigurationBuilder builder = new DefaultConfigurationBuilder();
 145  28
         Configuration sysConfig  = builder.buildFromFile(sysConfigFilename);
 146  28
         Configuration roleConfig = builder.buildFromFile(roleConfigFilename);
 147  
 
 148  
         // Create the LoggerManager for Log4J
 149  28
         LoggerManager lm = new Log4JLoggerManager();
 150  
 
 151  
         // Setup the RoleManager
 152  28
         DefaultRoleManager roles = new DefaultRoleManager();
 153  
 
 154  28
         Logger logger = lm.getLoggerForCategory(AVALON_LOG_CATEGORY);
 155  
 
 156  28
         roles.enableLogging(logger);
 157  28
         roles.configure(roleConfig);
 158  
 
 159  
         // Setup ECM
 160  28
         manager = new ExcaliburComponentManager();
 161  
 
 162  28
         manager.setLoggerManager(lm);
 163  28
         manager.enableLogging(logger);
 164  
 
 165  28
         DefaultContext context = new DefaultContext();
 166  28
         String realPath = Turbine.getRealPath("/");
 167  
 
 168  28
         context.put(AvalonComponentService.COMPONENT_APP_ROOT, realPath);
 169  28
         System.setProperty("applicationRoot", realPath);
 170  
 
 171  28
         log.debug("Application Root is " + realPath);
 172  
 
 173  28
         manager.contextualize(context);
 174  28
         manager.setRoleManager(roles);
 175  28
         manager.configure(sysConfig);
 176  
 
 177  
         // Init ECM!!!!
 178  28
         manager.initialize();
 179  
 
 180  28
         List lookupComponents = conf.getList(COMPONENT_LOOKUP_KEY,
 181  
                 new Vector());
 182  
 
 183  55
         for (Iterator it = lookupComponents.iterator(); it.hasNext();)
 184  
         {
 185  26
             String component = (String) it.next();
 186  
             try
 187  
             {
 188  26
                 Component c = manager.lookup(component);
 189  26
                 log.info("Lookup for Component " + component + " successful");
 190  26
                 manager.release(c);
 191  13
             }
 192  0
             catch (Exception e)
 193  
             {
 194  0
                 log.error("Lookup for Component " + component + " failed!");
 195  26
             }
 196  
         }
 197  28
     }
 198  
 
 199  
     /**
 200  
      * Disposes of the container and releases resources
 201  
      */
 202  
     public void dispose()
 203  
     {
 204  0
         manager.dispose();
 205  0
     }
 206  
 
 207  
     /**
 208  
      * Returns an instance of the named component
 209  
      *
 210  
      * @param roleName Name of the role the component fills.
 211  
      * @return an instance of the named component
 212  
      * @throws ComponentException generic exception
 213  
      */
 214  
     public Component lookup(String roleName)
 215  
             throws ComponentException
 216  
     {
 217  2
         return manager.lookup(roleName);
 218  
     }
 219  
 
 220  
     /**
 221  
      * Releases the component
 222  
      *
 223  
      * @param component the component to release
 224  
      */
 225  
     public void release(Component component)
 226  
     {
 227  2
         manager.release(component);
 228  2
     }
 229  
 
 230  
 }

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