KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fulcrum > yaafi > framework > factory > ServiceManagerFactory


1 package org.apache.fulcrum.yaafi.framework.factory;
2
3 /*
4  * Copyright 2004 Apache Software Foundation
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
14  * implied.
15  *
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */

19
20 import org.apache.avalon.framework.configuration.DefaultConfiguration;
21 import org.apache.avalon.framework.context.Context;
22 import org.apache.avalon.framework.logger.ConsoleLogger;
23 import org.apache.avalon.framework.logger.Logger;
24
25 import org.apache.fulcrum.yaafi.framework.container.ServiceContainer;
26
27
28 /**
29  * A factory to hide how to initialize YAFFI since this might change
30  * over the time
31  *
32  * @author <a HREF="mailto:siegfried.goeschl@it20one.at">Siegfried Goeschl</a>
33  */

34
35 public class ServiceManagerFactory
36 {
37     /** Our default implementation class of the service container */
38     private static String JavaDoc serviceContainerClazzName = "org.apache.fulcrum.yaafi.framework.container.ServiceContainerImpl";
39
40     /** Our logger */
41     private static Logger logger;
42     
43     static
44     {
45         // as default use the console logger
46
logger = new ConsoleLogger();
47     }
48     
49     /**
50      * Create a fully initialized YAFFI service container
51      */

52     public static ServiceContainer create(
53         Logger logger,
54         String JavaDoc componentRolesLocation,
55         String JavaDoc componentConfigurationLocation,
56         String JavaDoc parametersLocation)
57         throws Exception JavaDoc
58     {
59         return create(
60             logger,
61             componentRolesLocation,
62             componentConfigurationLocation,
63             parametersLocation,
64             null
65             );
66     }
67
68     /**
69      * Create a fully initialized YAFFI service container
70      */

71     public static ServiceContainer create(
72         Logger logger,
73         String JavaDoc componentRolesLocation,
74         String JavaDoc componentConfigurationLocation,
75         String JavaDoc parametersLocation,
76         Context context)
77         throws Exception JavaDoc
78     {
79         Class JavaDoc clazz = null;
80         ServiceContainer result = null;
81         
82         // Enforce a logger from the caller
83

84         if( logger == null )
85         {
86             String JavaDoc msg = "An instance of a logger is required";
87             ServiceManagerFactory.logger.error(msg);
88             throw new IllegalArgumentException JavaDoc(msg);
89         }
90         
91         try
92         {
93             // bootstrap the logging
94

95             ServiceManagerFactory.logger = logger.getChildLogger( ServiceManagerFactory.class.getName() );
96             ServiceManagerFactory.logger.debug( "Loading the service container class " + serviceContainerClazzName );
97             
98             // bootstrap the service container
99

100             clazz = ServiceManagerFactory.class.getClassLoader().loadClass(serviceContainerClazzName);
101             ServiceManagerFactory.logger.debug( "Instantiating the service container class " + serviceContainerClazzName );
102             result = (ServiceContainer) clazz.newInstance();
103         }
104         catch( Exception JavaDoc e )
105         {
106             String JavaDoc msg = "Creating the ServiceContainer failed";
107             ServiceManagerFactory.logger.error( msg, e );
108             throw e;
109         }
110         
111         // LogEnabled.enableLogging() to set the logger
112

113         Logger serviceContainerLogger = ServiceManagerFactory.logger.getChildLogger(
114             ServiceContainer.class.getName()
115             );
116         
117         result.enableLogging( serviceContainerLogger );
118         
119         // Contextualizable.contextualize() to set the context
120

121         if( context != null )
122         {
123             result.contextualize(context);
124         }
125         
126         // Configurable.configure() to set the configurattion files
127

128         DefaultConfiguration configuration = new DefaultConfiguration(
129             ServiceContainer.ROLE_NAME
130             );
131         
132         configuration = createConfiguration(
133             componentRolesLocation,
134             componentConfigurationLocation,
135             parametersLocation );
136             
137         result.configure( configuration );
138         
139         // Initializable.initialie() to start the container
140

141         result.initialize();
142         
143         return result;
144     }
145
146     /**
147      * @return A configuration to be passed to the service container
148      */

149     private static DefaultConfiguration createConfiguration(
150         String JavaDoc componentRolesLocation,
151         String JavaDoc componentConfigurationLocation,
152         String JavaDoc parametersLocation )
153     {
154         DefaultConfiguration result = new DefaultConfiguration(
155             ServiceContainer.ROLE_NAME
156             );
157         
158         // 1) componentRolesLocation
159

160         DefaultConfiguration componentRolesLocationConfig = new DefaultConfiguration(
161             ServiceContainer.COMPONENT_ROLE_KEYS
162             );
163            
164         componentRolesLocationConfig.setValue(
165             componentRolesLocation
166             );
167         
168         result.addChild( componentRolesLocationConfig );
169             
170         // 2) componentConfigurationLocation
171

172         DefaultConfiguration componentConfigurationLocationConfig = new DefaultConfiguration(
173             ServiceContainer.COMPONENT_CONFIG_KEY
174             );
175            
176         componentConfigurationLocationConfig.setValue(
177             componentConfigurationLocation
178             );
179         
180         result.addChild( componentConfigurationLocationConfig );
181         
182         // 3) parametersLocation
183

184         DefaultConfiguration parametersLocationConfig = new DefaultConfiguration(
185             ServiceContainer.COMPONENT_PARAMETERS_KEY
186             );
187            
188         parametersLocationConfig.setValue(
189             parametersLocation
190             );
191         
192         result.addChild( parametersLocationConfig );
193
194         return result;
195     }
196 }
197
Popular Tags