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         ContainerUtil.configure( result, configuration );
125         ContainerUtil.initialize( result );
126 
127         return result;
128     }
129 
130     /**
131      * Disposes the container.
132      *
133      * @param container the container to be disposed
134      * @return true if the disposal was successful or false otherwise
135      */
136     public static boolean dispose( ServiceContainer container )
137     {
138         try
139         {
140             if( container != null )
141             {
142                 container.dispose();
143             }
144 
145             return true;
146         }
147         catch( Throwable t )
148         {
149             String msg = "Disposing the container failed : " + t.getMessage();
150             System.err.println(msg);
151             t.printStackTrace();
152             return false;
153         }
154     }
155 
156     /**
157      * Reads the implementation class of the YAAFI container
158      */
159     private static String getServiceContainerClazzName( Configuration configuration )
160     {
161         Configuration containerClazzNameConfig = configuration.getChild(
162             ServiceConstants.CONTAINERCLAZZNAME_CONFIG_KEY
163             );
164 
165         if( containerClazzNameConfig != null )
166         {
167             return containerClazzNameConfig.getValue(ServiceConstants.CLAZZ_NAME);
168         }
169         else
170         {
171             return ServiceConstants.CLAZZ_NAME;
172         }
173     }
174 
175 }