KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > fortress > util > FortressConfig


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

17
18 package org.apache.avalon.fortress.util;
19
20 import org.apache.avalon.excalibur.logger.LoggerManager;
21 import org.apache.avalon.fortress.impl.DefaultContainer;
22 import org.apache.avalon.framework.configuration.Configuration;
23 import org.apache.avalon.framework.context.Context;
24 import org.apache.avalon.framework.context.ContextException;
25 import org.apache.avalon.framework.context.DefaultContext;
26 import org.apache.avalon.framework.service.ServiceManager;
27 import org.apache.excalibur.event.Sink;
28 import org.apache.excalibur.event.command.ThreadManager;
29 import org.apache.excalibur.instrument.InstrumentManager;
30 import org.apache.excalibur.mpool.PoolManager;
31
32 import java.io.File JavaDoc;
33 import java.net.URL JavaDoc;
34
35 /**
36  * Helper class to create a m_context for the ContextManager.
37  * @version CVS $Revision: 1.22 $ $Date: 2004/02/28 15:16:26 $
38  */

39 public final class FortressConfig
40 {
41     private final DefaultContext m_context;
42
43     /**
44      * Creates a m_context builder and initializes it with default values.
45      * The default values are:
46      *
47      * <ul>
48      * <li>CONTAINER_CLASS = "org.apache.avalon.fortress.impl.DefaultContainer" </li>
49      * <li>THREADS_CPU = 2</li>
50      * <li>THREAD_TIMEOUT = 1000</li>
51      * <li>CONTEXT_DIRECTORY = "../"</li>
52      * <li>WORK_DIRECTORY = "/tmp"</li>
53      * <li>LOG_CATEGORY = "fortress"</li>
54      * <li>CONTEXT_CLASSLOADER = the thread m_context class loader</li>
55      * </ul>
56      */

57     public FortressConfig()
58     {
59         this( createDefaultConfig() );
60     }
61
62     /**
63      * Creates a m_context builder and initializes it with default values.
64      *
65      * @param parent parent m_context with default values.
66      */

67     public FortressConfig( final Context parent )
68     {
69         m_context = new OverridableContext( parent );
70     }
71
72     /**
73      * Creates a default m_context.
74      */

75     public static final Context createDefaultConfig()
76     {
77         return createDefaultConfig( Thread.currentThread().getContextClassLoader() );
78     }
79
80     /**
81      * Creates a default m_context.
82      */

83     public static final Context createDefaultConfig( final ClassLoader JavaDoc classLoader )
84     {
85         final DefaultContext defaultContext = new DefaultContext();
86
87         try
88         {
89             defaultContext.put( ContextManagerConstants.CONTAINER_CLASS,
90                 DefaultContainer.class );
91             defaultContext.put( ContextManagerConstants.COMMAND_FAILURE_HANDLER_CLASS,
92                 FortressCommandFailureHandler.class );
93         }
94         catch ( Exception JavaDoc e )
95         {
96             // ignore
97
}
98
99         final File JavaDoc contextDir = new File JavaDoc( System.getProperty( "user.dir" ) );
100         final File JavaDoc workDir = new File JavaDoc( System.getProperty( "java.io.tmpdir" ) );
101
102         defaultContext.put( ContextManagerConstants.THREADS_CPU, new Integer JavaDoc( 2 ) );
103         defaultContext.put( ContextManagerConstants.THREAD_TIMEOUT, new Long JavaDoc( 1000 ) );
104         defaultContext.put( ContextManagerConstants.CONTEXT_DIRECTORY, contextDir );
105         defaultContext.put( ContextManagerConstants.WORK_DIRECTORY, workDir );
106         defaultContext.put( ContextManagerConstants.LOG_CATEGORY, "fortress" );
107         defaultContext.put( ClassLoader JavaDoc.class.getName(), classLoader );
108         defaultContext.put( ContextManagerConstants.CONFIGURATION_URI, "conf/system.xconf" );
109         defaultContext.put( ContextManagerConstants.LOGGER_MANAGER_CONFIGURATION_URI, "conf/logkit.xconf" );
110
111         defaultContext.makeReadOnly();
112
113         return defaultContext;
114     }
115
116     /**
117      * Finalizes the m_context and returns it.
118      */

119     public Context getContext()
120     {
121         m_context.makeReadOnly();
122         return m_context;
123     }
124
125     public void setCommandSink( final Sink commandSink )
126     {
127         m_context.put( Sink.ROLE, commandSink );
128     }
129
130     public void setServiceManager( final ServiceManager componentManager )
131     {
132         m_context.put( ContextManagerConstants.SERVICE_MANAGER, componentManager );
133     }
134
135     public void setLifecycleExtensionManager( final LifecycleExtensionManager extensionManager )
136     {
137         m_context.put( LifecycleExtensionManager.ROLE, extensionManager );
138     }
139
140     public void setContainerClass( final String JavaDoc containerClass )
141         throws ClassNotFoundException JavaDoc
142     {
143         ClassLoader JavaDoc classLoader;
144         try
145         {
146             classLoader = (ClassLoader JavaDoc) m_context.get( ClassLoader JavaDoc.class.getName() );
147         }
148         catch ( ContextException ce )
149         {
150             classLoader = Thread.currentThread().getContextClassLoader();
151         }
152
153         setContainerClass( classLoader.loadClass( containerClass ) );
154     }
155
156     public void setContainerClass( final Class JavaDoc containerClass )
157     {
158         m_context.put( ContextManagerConstants.CONTAINER_CLASS, containerClass );
159     }
160     
161     /**
162      * Sets a class whose instance will be used to override the default
163      * CommandFailureHandler used by the container. This makes it possible
164      * for applications to decide how they wish to handle failures.
165      *
166      * @param commandFailureHandlerClass Name of the CommandFailureHandler class to use.
167      */

168     public void setCommandFailureHandlerClass( final String JavaDoc commandFailureHandlerClass )
169         throws ClassNotFoundException JavaDoc
170     {
171         ClassLoader JavaDoc classLoader;
172         try
173         {
174             classLoader = (ClassLoader JavaDoc) m_context.get( ClassLoader JavaDoc.class.getName() );
175         }
176         catch ( ContextException ce )
177         {
178             classLoader = Thread.currentThread().getContextClassLoader();
179         }
180
181         setCommandFailureHandlerClass( classLoader.loadClass( commandFailureHandlerClass ) );
182     }
183
184     /**
185      * Sets a class whose instance will be used to override the default
186      * CommandFailureHandler used by the container. This makes it possible
187      * for applications to decide how they wish to handle failures.
188      *
189      * @param commandFailureHandlerClass The CommandFailureHandler class to use.
190      */

191     public void setCommandFailureHandlerClass( final Class JavaDoc commandFailureHandlerClass )
192     {
193         m_context.put(
194             ContextManagerConstants.COMMAND_FAILURE_HANDLER_CLASS, commandFailureHandlerClass );
195     }
196
197     public void setContainerConfiguration( final Configuration config )
198     {
199         m_context.put( ContextManagerConstants.CONFIGURATION, config );
200         m_context.put( ContextManagerConstants.CONFIGURATION_URI, null );
201     }
202
203     public void setContainerConfiguration( final String JavaDoc location )
204     {
205         m_context.put( ContextManagerConstants.CONFIGURATION_URI, location );
206     }
207
208     public void setContextClassLoader( final ClassLoader JavaDoc loader )
209     {
210         m_context.put( ClassLoader JavaDoc.class.getName(), loader );
211     }
212
213     public void setContextDirectory( final File JavaDoc file )
214     {
215         m_context.put( ContextManagerConstants.CONTEXT_DIRECTORY, file );
216     }
217
218     public void setContextDirectory( final String JavaDoc directory )
219     {
220         m_context.put( ContextManagerConstants.CONTEXT_DIRECTORY, new File JavaDoc( directory ) );
221     }
222
223     public void setContextRootURL( final URL JavaDoc url )
224     {
225         m_context.put( ContextManagerConstants.CONTEXT_DIRECTORY, url );
226     }
227
228     public void setLoggerCategory( final String JavaDoc category )
229     {
230         m_context.put( ContextManagerConstants.LOG_CATEGORY, category );
231     }
232
233     public void setLoggerManager( final LoggerManager logManager )
234     {
235         m_context.put( LoggerManager.ROLE, logManager );
236         m_context.put( ContextManagerConstants.LOGGER_MANAGER_CONFIGURATION, null );
237         m_context.put( ContextManagerConstants.LOGGER_MANAGER_CONFIGURATION_URI, null );
238     }
239
240     public void setLoggerManagerConfiguration( final Configuration config )
241     {
242         m_context.put( ContextManagerConstants.LOGGER_MANAGER_CONFIGURATION, config );
243         m_context.put( ContextManagerConstants.LOGGER_MANAGER_CONFIGURATION_URI, null );
244     }
245
246     public void setLoggerManagerConfiguration( final String JavaDoc location )
247     {
248         m_context.put( ContextManagerConstants.LOGGER_MANAGER_CONFIGURATION_URI, location );
249     }
250
251     public void setInstrumentManager( final InstrumentManager profiler )
252     {
253         m_context.put( InstrumentManager.ROLE, profiler );
254         m_context.put( ContextManagerConstants.INSTRUMENT_MANAGER_CONFIGURATION, null );
255         m_context.put( ContextManagerConstants.INSTRUMENT_MANAGER_CONFIGURATION_URI, null );
256     }
257
258     public void setInstrumentManagerConfiguration( final Configuration config )
259     {
260         m_context.put( ContextManagerConstants.INSTRUMENT_MANAGER_CONFIGURATION, config );
261         m_context.put( ContextManagerConstants.INSTRUMENT_MANAGER_CONFIGURATION_URI, null );
262     }
263
264     public void setInstrumentManagerConfiguration( final String JavaDoc location )
265     {
266         m_context.put( ContextManagerConstants.INSTRUMENT_MANAGER_CONFIGURATION_URI, location );
267     }
268
269     public void setNumberOfThreadsPerCPU( final int numberOfThreads )
270     {
271         m_context.put( ContextManagerConstants.THREADS_CPU, new Integer JavaDoc( numberOfThreads ) );
272     }
273
274     public void setPoolManager( final PoolManager poolManager )
275     {
276         m_context.put( PoolManager.ROLE, poolManager );
277     }
278
279     public void setRoleManager( final org.apache.avalon.fortress.RoleManager roleManager )
280     {
281         m_context.put( org.apache.avalon.fortress.RoleManager.ROLE, roleManager );
282     }
283
284     public void setRoleManagerConfiguration( final Configuration config )
285     {
286         m_context.put( ContextManagerConstants.ROLE_MANAGER_CONFIGURATION, config );
287         m_context.put( ContextManagerConstants.ROLE_MANAGER_CONFIGURATION_URI, null );
288     }
289
290     public void setRoleManagerConfiguration( final String JavaDoc location )
291     {
292         m_context.put( ContextManagerConstants.ROLE_MANAGER_CONFIGURATION_URI, location );
293     }
294
295     public void setThreadTimeout( final long timeout )
296     {
297         m_context.put( ContextManagerConstants.THREAD_TIMEOUT, new Long JavaDoc( timeout ) );
298     }
299
300     public void setWorkDirectory( final File JavaDoc file )
301     {
302         m_context.put( ContextManagerConstants.WORK_DIRECTORY, file );
303     }
304
305     public void setWorkDirectory( final String JavaDoc directory )
306     {
307         setWorkDirectory( new File JavaDoc( directory ) );
308     }
309
310     public void setThreadManager( final ThreadManager threadManager )
311     {
312         m_context.put( ThreadManager.ROLE, threadManager );
313     }
314 }
315
Popular Tags