View Javadoc
1   package org.apache.fulcrum.yaafi.framework.factory;
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 org.apache.avalon.framework.configuration.Configuration;
23  import org.apache.avalon.framework.configuration.ConfigurationUtil;
24  import org.apache.avalon.framework.container.ContainerUtil;
25  import org.apache.avalon.framework.context.Context;
26  import org.apache.avalon.framework.logger.Logger;
27  import org.apache.fulcrum.yaafi.framework.container.ServiceConstants;
28  import org.apache.fulcrum.yaafi.framework.container.ServiceContainer;
29  import org.apache.fulcrum.yaafi.framework.util.Validate;
30  
31  /**
32   * A factory to hide how to initialize YAFFI since this might change over the time
33   *
34   * @author <a href="mailto:siegfried.goeschl@it20one.at">Siegfried Goeschl </a>
35   */
36  
37  public class ServiceContainerFactory
38  {
39      /** The logger to be used */
40      private static Logger logger;
41  
42      /**
43       * Create a fully initialized YAFFI service container.
44       *
45       * @param serviceManagerConfig the configuration to use
46       * @return the service container
47       * @throws Exception the creation failed
48       */
49      public static ServiceContainer create(
50          ServiceContainerConfiguration serviceManagerConfig)
51          throws Exception
52      {
53          Validate.notNull(serviceManagerConfig,"serviceManagerConfig");
54          Context context = serviceManagerConfig.createFinalContext();
55          return ServiceContainerFactory.create( serviceManagerConfig, context );
56      }
57  
58      /**
59       * Create a fully initialized YAFFI service container
60       *
61       * @param serviceManagerConfig the configuration to use
62       * @param context the context to use
63       * @return the service container
64       * @throws Exception the creation failed
65       */
66      public static ServiceContainer create(
67          ServiceContainerConfiguration serviceManagerConfig, Context context )
68          throws Exception
69      {
70          Validate.notNull(serviceManagerConfig,"serviceManagerConfig");
71          Validate.notNull(context,"context");
72  
73          String clazzName;
74          Class<?> clazz = null;
75          Configuration configuration = null;
76          ServiceContainer result = null;
77  
78          // Enforce a logger from the caller
79  
80          try
81          {
82              // bootstrap the logging
83  
84              ServiceContainerFactory.logger = serviceManagerConfig.getLogger();
85  
86              // bootstrap the configuration settings
87  
88              configuration = serviceManagerConfig.createFinalConfiguration();
89  
90              // bootstrap the service container
91  
92              clazzName = getServiceContainerClazzName(configuration);
93  
94              ServiceContainerFactory.logger.debug(
95                  "Loading the service container class " + clazzName
96                  );
97  
98              clazz = ServiceContainerFactory.class.getClassLoader().loadClass(
99                  clazzName
100                 );
101 
102             ServiceContainerFactory.logger.debug(
103                 "Instantiating the service container class " + clazzName
104                 );
105 
106             result = (ServiceContainer) clazz.newInstance();
107         }
108         catch (Exception e)
109         {
110             String msg = "Creating the ServiceContainer failed";
111             ServiceContainerFactory.logger.error( msg, e );
112             throw e;
113         }
114 
115         Logger serviceContainerLogger = serviceManagerConfig.getLogger();
116 
117         serviceContainerLogger.debug(
118             "Using the following configuration : "
119             + ConfigurationUtil.toString( configuration )
120             );
121 
122         ContainerUtil.enableLogging( result, serviceManagerConfig.getLogger() );
123         ContainerUtil.contextualize( result, context );
124 
125         if(serviceManagerConfig.getParentServiceManager() != null)
126         {
127             ContainerUtil.service(result, serviceManagerConfig.getParentServiceManager());
128         }
129         
130         ContainerUtil.configure( result, configuration );
131         ContainerUtil.initialize( result );
132 
133         return result;
134     }
135 
136     /**
137      * Disposes the container.
138      *
139      * @param container the container to be disposed
140      * @return true if the disposal was successful or false otherwise
141      */
142     public static boolean dispose( ServiceContainer container )
143     {
144         try
145         {
146             if( container != null )
147             {
148                 container.dispose();
149             }
150 
151             return true;
152         }
153         catch( Throwable t )
154         {
155             String msg = "Disposing the container failed : " + t.getMessage();
156             System.err.println(msg);
157             t.printStackTrace();
158             return false;
159         }
160     }
161 
162     /**
163      * Reads the implementation class of the YAAFI container.
164      *
165      * @param configuration the Avalon configuration
166      * @return the implementation class name of the container
167      */
168     private static String getServiceContainerClazzName( Configuration configuration )
169     {
170         Configuration containerClazzNameConfig = configuration.getChild(
171             ServiceConstants.CONTAINERCLAZZNAME_CONFIG_KEY
172             );
173 
174         if( containerClazzNameConfig != null )
175         {
176             return containerClazzNameConfig.getValue(ServiceConstants.CLAZZ_NAME);
177         }
178         else
179         {
180             return ServiceConstants.CLAZZ_NAME;
181         }
182     }
183 
184 }