KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > phoenix > components > kernel > DefaultKernel


1 /*
2  * Copyright (C) The Apache Software Foundation. All rights reserved.
3  *
4  * This software is published under the terms of the Apache Software License
5  * version 1.1, a copy of which has been included with this distribution in
6  * the LICENSE.txt file.
7  */

8 package org.apache.avalon.phoenix.components.kernel;
9
10 import java.util.HashMap JavaDoc;
11 import java.io.File JavaDoc;
12 import org.apache.avalon.excalibur.i18n.ResourceManager;
13 import org.apache.avalon.excalibur.i18n.Resources;
14 import org.apache.avalon.framework.CascadingException;
15 import org.apache.avalon.framework.activity.Disposable;
16 import org.apache.avalon.framework.activity.Initializable;
17 import org.apache.avalon.framework.configuration.Configurable;
18 import org.apache.avalon.framework.configuration.Configuration;
19 import org.apache.avalon.framework.configuration.ConfigurationException;
20 import org.apache.avalon.framework.container.ContainerUtil;
21 import org.apache.avalon.framework.logger.AbstractLogEnabled;
22 import org.apache.avalon.framework.logger.Logger;
23 import org.apache.avalon.framework.service.DefaultServiceManager;
24 import org.apache.avalon.framework.service.ServiceException;
25 import org.apache.avalon.framework.service.ServiceManager;
26 import org.apache.avalon.framework.service.Serviceable;
27 import org.apache.avalon.phoenix.components.application.DefaultApplication;
28 import org.apache.avalon.phoenix.interfaces.Application;
29 import org.apache.avalon.phoenix.interfaces.ApplicationContext;
30 import org.apache.avalon.phoenix.interfaces.ApplicationMBean;
31 import org.apache.avalon.phoenix.interfaces.ConfigurationRepository;
32 import org.apache.avalon.phoenix.interfaces.ConfigurationValidator;
33 import org.apache.avalon.phoenix.interfaces.Kernel;
34 import org.apache.avalon.phoenix.interfaces.KernelMBean;
35 import org.apache.avalon.phoenix.interfaces.SystemManager;
36 import org.apache.avalon.phoenix.metadata.SarMetaData;
37
38 /**
39  * The ServerKernel is the core of the Phoenix system.
40  * The kernel is responsible for orchestrating low level services
41  * such as loading, configuring and destroying blocks. It also
42  * gives access to basic facilities such as scheduling sub-systems,
43  * protected execution contexts, naming and directory services etc.
44  *
45  * Note that no facilities are available until after the Kernel has been
46  * configured and initialized.
47  *
48  * @author <a HREF="mailto:peter at apache.org">Peter Donald</a>
49  * @author <a HREF="mailto:leosimons@apache.org">Leo Simons</a>
50  */

51 public class DefaultKernel
52     extends AbstractLogEnabled
53     implements Kernel, KernelMBean, Initializable, Serviceable, Disposable, Configurable
54 {
55     private static final Resources REZ =
56         ResourceManager.getPackageResources( DefaultKernel.class );
57
58     ///SystemManager provided by Embeddor
59
private SystemManager m_systemManager;
60
61     private SystemManager m_applicationManager;
62
63     ///Configuration Repository
64
private ConfigurationRepository m_repository;
65
66     //Configuration Validator
67
private ConfigurationValidator m_validator;
68
69     private HashMap JavaDoc m_entries = new HashMap JavaDoc();
70
71     private boolean m_addInvalidApplications;
72
73     public void service( final ServiceManager serviceManager )
74         throws ServiceException
75     {
76         m_systemManager = (SystemManager)serviceManager.lookup( SystemManager.ROLE );
77         m_repository = (ConfigurationRepository)serviceManager.
78             lookup( ConfigurationRepository.ROLE );
79         m_validator = (ConfigurationValidator)serviceManager.lookup( ConfigurationValidator.ROLE );
80     }
81
82     public void configure( Configuration configuration )
83         throws ConfigurationException
84     {
85         m_addInvalidApplications =
86             configuration.getChild( "add-invalid-applications" ).getValueAsBoolean( false );
87     }
88
89     public void initialize()
90         throws Exception JavaDoc
91     {
92         m_applicationManager =
93             m_systemManager.getSubContext( null, "application" );
94     }
95
96     public void dispose()
97     {
98         final String JavaDoc[] names = getApplicationNames();
99         for( int i = 0; i < names.length; i++ )
100         {
101             try
102             {
103                 final SarEntry entry = (SarEntry)m_entries.get( names[ i ] );
104                 shutdown( entry );
105             }
106             catch( final Exception JavaDoc e )
107             {
108                 final String JavaDoc message = REZ.getString( "kernel.error.entry.dispose", names[ i ] );
109                 getLogger().warn( message, e );
110             }
111         }
112     }
113
114     public String JavaDoc[] getApplicationNames()
115     {
116         return (String JavaDoc[])m_entries.keySet().toArray( new String JavaDoc[ 0 ] );
117     }
118
119     public Application getApplication( final String JavaDoc name )
120     {
121         final SarEntry entry = (SarEntry)m_entries.get( name );
122         if( null == entry )
123         {
124             return null;
125         }
126         else
127         {
128             return entry.getApplication();
129         }
130     }
131
132     /**
133      * Create and initialize the application instance if it is not already initialized.
134      *
135      * @param entry the entry for application
136      * @throws Exception if an error occurs
137      */

138     private void startup( final SarEntry entry )
139         throws Exception JavaDoc
140     {
141         //lock for application startup and shutdown
142
synchronized( entry )
143         {
144             final String JavaDoc name = entry.getMetaData().getName();
145
146             Application application = entry.getApplication();
147             if( null == application )
148             {
149                 try
150                 {
151                     final Application newApp = new DefaultApplication();
152                     final Logger childLogger =
153                         getLogger().getChildLogger( name );
154                     ContainerUtil.enableLogging( newApp, childLogger );
155
156                     final ApplicationContext context =
157                         createApplicationContext( entry );
158                     newApp.setApplicationContext( context );
159
160                     ContainerUtil.initialize( newApp );
161
162                     application = newApp;
163                 }
164                 catch( final Throwable JavaDoc t )
165                 {
166                     //Initialization failed so clean entry
167
//so invalid instance is not used
168
entry.setApplication( null );
169
170                     final String JavaDoc message =
171                         REZ.getString( "kernel.error.entry.initialize",
172                                        entry.getMetaData().getName() );
173                     throw new CascadingException( message, t );
174                 }
175
176                 try
177                 {
178                     ContainerUtil.start( application );
179                 }
180                 catch( final Throwable JavaDoc t )
181                 {
182                     final String JavaDoc message =
183                         REZ.getString( "kernel.error.entry.start", entry.getMetaData().getName() );
184
185                     if( m_addInvalidApplications )
186                     {
187                         getLogger().warn( message, t );
188                     }
189                     else
190                     {
191                         //Initialization failed so clean entry
192
//so invalid instance is not used
193
entry.setApplication( null );
194
195                         throw new CascadingException( message, t );
196                     }
197                 }
198
199                 entry.setApplication( application );
200
201                 // manage application
202
try
203                 {
204                     m_applicationManager.register( name,
205                                                    application,
206                                                    new Class JavaDoc[]{ApplicationMBean.class} );
207                 }
208                 catch( final Throwable JavaDoc t )
209                 {
210                     final String JavaDoc message =
211                         REZ.getString( "kernel.error.entry.manage", name );
212                     throw new CascadingException( message, t );
213                 }
214             }
215         }
216     }
217
218     private void shutdown( final SarEntry entry )
219         throws Exception JavaDoc
220     {
221         //lock for application startup and shutdown
222
synchronized( entry )
223         {
224             final Application application = entry.getApplication();
225             if( null != application )
226             {
227                 entry.setApplication( null );
228                 ContainerUtil.shutdown( application );
229             }
230             else
231             {
232                 final String JavaDoc message =
233                     REZ.getString( "kernel.error.entry.nostop",
234                                    entry.getMetaData().getName() );
235                 getLogger().warn( message );
236             }
237         }
238     }
239
240     public void addApplication( final SarMetaData metaData,
241                                 final File JavaDoc workDirectory,
242                                 final ClassLoader JavaDoc classLoader,
243                                 final Logger logger,
244                                 final Configuration server )
245         throws Exception JavaDoc
246     {
247         final String JavaDoc name = metaData.getName();
248         final SarEntry entry =
249             new SarEntry( metaData, workDirectory, classLoader, logger, server );
250         m_entries.put( name, entry );
251
252         try
253         {
254             startup( (SarEntry)entry );
255         }
256         catch( final Exception JavaDoc e )
257         {
258             final String JavaDoc message = REZ.getString( "kernel.error.entry.start", name );
259             getLogger().warn( message, e );
260             throw e;
261         }
262     }
263
264     private ApplicationContext createApplicationContext( final SarEntry entry )
265         throws Exception JavaDoc
266     {
267         final SarMetaData metaData = entry.getMetaData();
268         final String JavaDoc name = metaData.getName();
269
270         final DefaultApplicationContext context =
271             new DefaultApplicationContext( metaData,
272                                            entry.getWorkDirectory(),
273                                            entry.getClassLoader(),
274                                            entry.getLogger() );
275
276         ContainerUtil.enableLogging( context, createContextLogger( name ) );
277         ContainerUtil.service( context, createServiceManager() );
278         ContainerUtil.initialize( context );
279         return context;
280     }
281
282     /**
283      * Create a logger for specified ApplicationContext.
284      *
285      * @param name the name of application name
286      * @return the Logger for context
287      */

288     private Logger createContextLogger( final String JavaDoc name )
289     {
290         final String JavaDoc loggerName = name + ".frame";
291         final Logger childLogger =
292             getLogger().getChildLogger( loggerName );
293         return childLogger;
294     }
295
296     private ServiceManager createServiceManager()
297     {
298         final DefaultServiceManager componentManager = new DefaultServiceManager();
299         componentManager.put( SystemManager.ROLE, m_systemManager );
300         componentManager.put( ConfigurationRepository.ROLE, m_repository );
301         componentManager.put( ConfigurationValidator.ROLE, m_validator );
302         componentManager.put( Kernel.ROLE, this );
303         componentManager.makeReadOnly();
304         return componentManager;
305     }
306
307     public void removeApplication( String JavaDoc name )
308         throws Exception JavaDoc
309     {
310         final SarEntry entry = (SarEntry)m_entries.remove( name );
311         if( null == entry )
312         {
313             final String JavaDoc message =
314                 REZ.getString( "kernel.error.entry.initialize", name );
315             throw new Exception JavaDoc( message );
316         }
317         else
318         {
319             // un-manage application
320
try
321             {
322                 m_applicationManager.unregister( name );
323             }
324             catch( final Throwable JavaDoc t )
325             {
326                 final String JavaDoc message =
327                     REZ.getString( "kernel.error.entry.unmanage", name );
328                 throw new CascadingException( message, t );
329             }
330
331             shutdown( entry );
332         }
333     }
334 }
335
Popular Tags