Coverage Report - org.apache.turbine.services.avaloncomponent.TurbineYaafiComponentService
 
Classes in this File Line Coverage Branch Coverage Complexity
TurbineYaafiComponentService
70%
45/64
66%
4/6
2
 
 1  
 package org.apache.turbine.services.avaloncomponent;
 2  
 
 3  
 /*
 4  
  * Licensed to the Apache Software Foundation (ASF) under one
 5  
  * or more contributor license agreements.  See the NOTICE file
 6  
  * distributed with this work for additional information
 7  
  * regarding copyright ownership.  The ASF licenses this file
 8  
  * to you under the Apache License, Version 2.0 (the
 9  
  * "License"); you may not use this file except in compliance
 10  
  * with the License.  You may obtain a copy of the License at
 11  
  *
 12  
  *   http://www.apache.org/licenses/LICENSE-2.0
 13  
  *
 14  
  * Unless required by applicable law or agreed to in writing,
 15  
  * software distributed under the License is distributed on an
 16  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 17  
  * KIND, either express or implied.  See the License for the
 18  
  * specific language governing permissions and limitations
 19  
  * under the License.
 20  
  */
 21  
 
 22  
 import java.io.IOException;
 23  
 
 24  
 import org.apache.avalon.framework.activity.Disposable;
 25  
 import org.apache.avalon.framework.activity.Initializable;
 26  
 import org.apache.avalon.framework.logger.CommonsLogger;
 27  
 import org.apache.avalon.framework.logger.Logger;
 28  
 import org.apache.avalon.framework.service.ServiceException;
 29  
 import org.apache.commons.configuration.Configuration;
 30  
 import org.apache.commons.logging.Log;
 31  
 import org.apache.commons.logging.LogFactory;
 32  
 import org.apache.fulcrum.yaafi.framework.container.ServiceContainer;
 33  
 import org.apache.fulcrum.yaafi.framework.factory.ServiceContainerConfiguration;
 34  
 import org.apache.fulcrum.yaafi.framework.factory.ServiceContainerFactory;
 35  
 import org.apache.turbine.Turbine;
 36  
 import org.apache.turbine.services.InitializationException;
 37  
 import org.apache.turbine.services.InstantiationException;
 38  
 import org.apache.turbine.services.TurbineBaseService;
 39  
 
 40  
 /**
 41  
  * An implementation of Turbine service initializing the YAAFI container
 42  
  *
 43  
  * @author <a href="mailto:siegfried.goescfl@it20one.at">Siegfried Goeschl</a>
 44  
  */
 45  
 public class TurbineYaafiComponentService
 46  
         extends TurbineBaseService
 47  
         implements AvalonComponentService, Initializable, Disposable
 48  
 {
 49  
     /** the logger to be used */
 50  60
     private static Log log = LogFactory.getLog(AVALON_LOG_CATEGORY);
 51  
 
 52  
     /** property to lookup the container configuration file */
 53  
     public static final String CONTAINER_CONFIGURATION_KEY = "containerConfiguration";
 54  
 
 55  
     /** the default value for the container configuration file */
 56  
     public static final String CONTAINER_CONFIGURATION_VALUE = "/WEB-INF/conf/containerConfiguration.xml";
 57  
 
 58  
     /** property to lookup the properties file */
 59  
     public static final String COMPONENT_PARAMETERS_KEY = "parameters";
 60  
 
 61  
     /** the default value for the parameter file */
 62  
     public static final String COMPONENT_PARAMETERS_VALUE = "/WEB-INF/conf/parameters.properties";
 63  
 
 64  
     /** YAFFI container */
 65  
     private ServiceContainer container;
 66  
 
 67  
     // -------------------------------------------------------------
 68  
     // Service initialization
 69  
     // -------------------------------------------------------------
 70  
 
 71  
     public TurbineYaafiComponentService()
 72  60
     {
 73  
         // nothing to do
 74  60
     }
 75  
 
 76  
     /**
 77  
      * Load all configured components and initialize them. This is a zero parameter variant which
 78  
      * queries the Turbine Servlet for its config.
 79  
      *
 80  
      * @throws InitializationException Something went wrong in the init stage
 81  
      */
 82  
     public void init() throws InitializationException
 83  
     {
 84  
         try
 85  
         {
 86  130
             log.info( "Initializing TurbineYaafiComponentService ..." );
 87  130
             initialize();
 88  130
             setInit(true);
 89  
         }
 90  0
         catch (Exception e)
 91  
         {
 92  0
             log.error("Exception caught initialising service: ", e);
 93  0
             throw new InitializationException("Initializing TurbineYaafiComponentService failed", e);
 94  130
         }
 95  130
     }
 96  
 
 97  
     /**
 98  
      * Shuts the Component Service down, calls dispose on the components that implement this
 99  
      * interface
 100  
      *
 101  
      */
 102  
     public void shutdown()
 103  
     {
 104  98
         log.info( "Disposing TurbineYaafiComponentService ..." );
 105  98
         dispose();
 106  98
         setInit(false);
 107  98
     }
 108  
 
 109  
     // -------------------------------------------------------------
 110  
     // Avalon lifecycle interfaces
 111  
     // -------------------------------------------------------------
 112  
 
 113  
     /**
 114  
      * Initializes the container
 115  
      *
 116  
      * @throws Exception generic exception
 117  
      */
 118  
     public void initialize() throws Exception
 119  
     {
 120  
         // get the configuration from the baseclass
 121  
 
 122  130
         Configuration conf = this.getConfiguration();
 123  
 
 124  
         // determine the home directory
 125  
 
 126  130
         String homePath = Turbine.getRealPath("/");
 127  130
         log.info( "Using the following home : " + homePath );
 128  
 
 129  
         // create the configuration for YAAFI
 130  
 
 131  130
         ServiceContainerConfiguration config =
 132  
             this.createServiceContainerConfiguration(conf);
 133  
 
 134  130
         config.setLogger( this.createAvalonLogger() );
 135  130
         config.setApplicationRootDir( homePath );
 136  
 
 137  
         // initialize the container
 138  
 
 139  
         try
 140  
         {
 141  130
             this.container = ServiceContainerFactory.create(
 142  
                 config
 143  
                 );
 144  
         }
 145  0
         catch (Exception e)
 146  
         {
 147  0
             String msg = "Initializing YAAFI failed";
 148  0
             log.error(msg,e);
 149  0
             throw e;
 150  130
         }
 151  130
     }
 152  
 
 153  
     /**
 154  
      * Disposes of the container and releases resources
 155  
      */
 156  
     public void dispose()
 157  
     {
 158  98
         if (this.container != null)
 159  
         {
 160  98
             this.container.dispose();
 161  98
             this.container = null;
 162  
         }
 163  98
     }
 164  
 
 165  
     /**
 166  
      * Returns an instance of the named component
 167  
      *
 168  
      * @param roleName Name of the role the component fills.
 169  
      * @return an instance of the named component
 170  
      * @throws Exception generic exception
 171  
      */
 172  
     public Object lookup(String roleName) throws ServiceException
 173  
     {
 174  1092
         return this.container.lookup(roleName);
 175  
     }
 176  
 
 177  
     /**
 178  
      * Releases the component.
 179  
      *
 180  
      * @param component the component to release
 181  
      */
 182  
     public void release(Object component)
 183  
     {
 184  0
         this.container.release( component );
 185  0
     }
 186  
 
 187  
     /**
 188  
      * @see org.apache.avalon.framework.service.ServiceManager#hasService(java.lang.String)
 189  
      */
 190  
     public boolean hasService(String roleName)
 191  
     {
 192  2194
         return this.container.hasService(roleName);
 193  
     }
 194  
 
 195  
     /**
 196  
      * Create a ServiceContainerConfiguration based on the Turbine configuration
 197  
      *
 198  
      * @param conf the Turbine configuration
 199  
      * @return the YAAFI configuration
 200  
      * @throws IOException creating the YAAFI configuration failed
 201  
      */
 202  
     protected ServiceContainerConfiguration createServiceContainerConfiguration( Configuration conf )
 203  
         throws IOException
 204  
     {
 205  130
         ServiceContainerConfiguration result = new ServiceContainerConfiguration();
 206  
 
 207  
         // are we using a "containerConfiguration.xml" ?!
 208  
 
 209  130
         if( conf.containsKey(CONTAINER_CONFIGURATION_KEY) )
 210  
         {
 211  
             // determine the container configuration file
 212  
 
 213  78
             String containerConfiguration = conf.getString(
 214  
                 CONTAINER_CONFIGURATION_KEY
 215  
                 );
 216  
 
 217  78
             result.loadContainerConfiguration(containerConfiguration);
 218  78
         }
 219  52
         else if( conf.containsKey(COMPONENT_ROLE_KEY) )
 220  
         {
 221  
             // determine the location of the role configuraton file
 222  
 
 223  52
             String roleConfigurationFileName = conf.getString(
 224  
                 COMPONENT_ROLE_KEY,
 225  
                 COMPONENT_ROLE_VALUE
 226  
                 );
 227  
 
 228  
             // determine the location of component configuration file
 229  
 
 230  52
             String componentConfigurationFileName = conf.getString(
 231  
                 COMPONENT_CONFIG_KEY,
 232  
                 COMPONENT_CONFIG_VALUE
 233  
                 );
 234  
 
 235  
             // determine the location of parameters file
 236  
 
 237  52
             String parametersFileName = conf.getString(
 238  
                 COMPONENT_PARAMETERS_KEY,
 239  
                 COMPONENT_PARAMETERS_VALUE
 240  
                 );
 241  
 
 242  52
             result.setComponentRolesLocation( roleConfigurationFileName );
 243  52
             result.setComponentConfigurationLocation( componentConfigurationFileName );
 244  52
             result.setParametersLocation( parametersFileName );
 245  52
         }
 246  
         else
 247  
         {
 248  
             // determine the container configuration file
 249  
 
 250  0
             String containerConfiguration = conf.getString(
 251  
                 CONTAINER_CONFIGURATION_KEY,
 252  
                 CONTAINER_CONFIGURATION_VALUE
 253  
                 );
 254  
 
 255  0
             result.loadContainerConfiguration(containerConfiguration);
 256  
         }
 257  
 
 258  130
         return result;
 259  
     }
 260  
 
 261  
     /**
 262  
      * Create the Avalon logger to be passed to YAAFI
 263  
      * @return an Avalon Logger
 264  
      */
 265  
     protected Logger createAvalonLogger()
 266  
     {
 267  130
         Logger result = new CommonsLogger(log, AVALON_LOG_CATEGORY);
 268  130
         return result;
 269  
     }
 270  
 
 271  
     // -------------------------------------------------------------
 272  
     // TurbineServiceProvider
 273  
     // -------------------------------------------------------------
 274  
 
 275  
     /**
 276  
      * @see org.apache.turbine.services.TurbineServiceProvider#exists(java.lang.String)
 277  
      */
 278  
     public boolean exists(String roleName)
 279  
     {
 280  2194
         return this.hasService(roleName);
 281  
     }
 282  
 
 283  
     /**
 284  
      * @see org.apache.turbine.services.TurbineServiceProvider#get(java.lang.String)
 285  
      */
 286  
     public Object get(String roleName) throws InstantiationException
 287  
     {
 288  
         try
 289  
         {
 290  1080
             return this.lookup(roleName);
 291  
         }
 292  0
         catch (ServiceException e)
 293  
         {
 294  0
             String msg = "Unable to get the following service : " + roleName;
 295  0
             log.error(msg);
 296  0
             throw new InstantiationException(msg);
 297  
         }
 298  0
         catch (Throwable t)
 299  
         {
 300  0
             String msg = "Unable to get the following service : " + roleName;
 301  0
             log.error(msg,t);
 302  0
             throw new InstantiationException(msg,t);
 303  
         }
 304  
     }
 305  
 }