KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.File JavaDoc;
11 import java.io.FileInputStream JavaDoc;
12 import java.io.InputStream JavaDoc;
13 import java.io.FileNotFoundException JavaDoc;
14 import java.util.HashMap JavaDoc;
15 import org.apache.avalon.excalibur.i18n.ResourceManager;
16 import org.apache.avalon.excalibur.i18n.Resources;
17 import org.apache.avalon.framework.activity.Initializable;
18 import org.apache.avalon.framework.configuration.Configuration;
19 import org.apache.avalon.framework.configuration.ConfigurationException;
20 import org.apache.avalon.framework.logger.AbstractLogEnabled;
21 import org.apache.avalon.framework.logger.Logger;
22 import org.apache.avalon.framework.service.ServiceException;
23 import org.apache.avalon.framework.service.ServiceManager;
24 import org.apache.avalon.framework.service.Serviceable;
25 import org.apache.avalon.phoenix.components.util.ResourceUtil;
26 import org.apache.avalon.phoenix.interfaces.ApplicationContext;
27 import org.apache.avalon.phoenix.interfaces.ConfigurationRepository;
28 import org.apache.avalon.phoenix.interfaces.ConfigurationValidator;
29 import org.apache.avalon.phoenix.interfaces.Kernel;
30 import org.apache.avalon.phoenix.interfaces.ManagerException;
31 import org.apache.avalon.phoenix.interfaces.SystemManager;
32 import org.apache.avalon.phoenix.metadata.BlockListenerMetaData;
33 import org.apache.avalon.phoenix.metadata.SarMetaData;
34 import org.apache.excalibur.threadcontext.ThreadContext;
35 import org.apache.excalibur.threadcontext.impl.DefaultThreadContextPolicy;
36
37 /**
38  * Manage the "frame" in which Applications operate.
39  *
40  * @author <a HREF="mailto:peter at apache.org">Peter Donald</a>
41  * @author <a HREF="mailto:proyal@apache.org">Peter Royal</a>
42  */

43 class DefaultApplicationContext
44     extends AbstractLogEnabled
45     implements ApplicationContext, Serviceable, Initializable
46 {
47     private static final Resources REZ =
48         ResourceManager.getPackageResources( DefaultApplicationContext.class );
49
50     //Log Hierarchy for application
51
private final Logger m_hierarchy;
52
53     ///ClassLoader for application
54
private final ClassLoader JavaDoc m_classLoader;
55
56     ///ThreadContext for application
57
private final ThreadContext m_threadContext;
58
59     //Repository of configuration data to access
60
private ConfigurationRepository m_repository;
61
62     //Validator to validate configuration against
63
private ConfigurationValidator m_validator;
64
65     ///Place to expose Management beans
66
private SystemManager m_systemManager;
67
68     private SystemManager m_blockManager;
69
70     private final SarMetaData m_metaData;
71     private final File JavaDoc m_workDirectory;
72
73     /**
74      * The kernel associate with context
75      */

76     private Kernel m_kernel;
77
78     protected DefaultApplicationContext( final SarMetaData metaData,
79                                          final File JavaDoc workDirectory,
80                                          final ClassLoader JavaDoc classLoader,
81                                          final Logger hierarchy )
82     {
83         if( null == metaData )
84         {
85             throw new NullPointerException JavaDoc( "metaData" );
86         }
87         if( null == classLoader )
88         {
89             throw new NullPointerException JavaDoc( "classLoader" );
90         }
91         if( null == hierarchy )
92         {
93             throw new NullPointerException JavaDoc( "hierarchy" );
94         }
95         if( null == workDirectory )
96         {
97             throw new NullPointerException JavaDoc( "workDirectory" );
98         }
99
100         m_metaData = metaData;
101         m_classLoader = classLoader;
102         m_hierarchy = hierarchy;
103         m_workDirectory = workDirectory;
104
105         final DefaultThreadContextPolicy policy = new DefaultThreadContextPolicy();
106         final HashMap JavaDoc map = new HashMap JavaDoc( 1 );
107         map.put( DefaultThreadContextPolicy.CLASSLOADER, m_classLoader );
108         m_threadContext = new ThreadContext( policy, map );
109     }
110
111     public void service( final ServiceManager serviceManager )
112         throws ServiceException
113     {
114         m_repository = (ConfigurationRepository)serviceManager.
115             lookup( ConfigurationRepository.ROLE );
116         m_systemManager = (SystemManager)serviceManager.
117             lookup( SystemManager.ROLE );
118         m_validator = (ConfigurationValidator)serviceManager.
119             lookup( ConfigurationValidator.ROLE );
120         m_kernel = (Kernel)serviceManager.lookup( Kernel.ROLE );
121     }
122
123     public void initialize()
124         throws Exception JavaDoc
125     {
126         m_blockManager = getManagementContext();
127     }
128
129     public InputStream JavaDoc getResourceAsStream( final String JavaDoc name )
130     {
131         final File JavaDoc file =
132             ResourceUtil.getFileForResource( name,
133                                              m_metaData.getHomeDirectory(),
134                                              m_workDirectory );
135         if( !file.exists() )
136         {
137             return null;
138         }
139         else
140         {
141             try
142             {
143                 return new FileInputStream JavaDoc( file );
144             }
145             catch( FileNotFoundException JavaDoc e )
146             {
147                 //Should never happen
148
return null;
149             }
150         }
151     }
152
153     public SarMetaData getMetaData()
154     {
155         return m_metaData;
156     }
157
158     public ThreadContext getThreadContext()
159     {
160         return m_threadContext;
161     }
162
163     public void requestShutdown()
164     {
165         final Thread JavaDoc thread = new Thread JavaDoc( "AppShutdown" )
166         {
167             public void run()
168             {
169                 schedulShutdown();
170             }
171         };
172         thread.start();
173     }
174
175     private void schedulShutdown()
176     {
177         try
178         {
179             //Sleep for a little bit so that the
180
//thread that requested this method can
181
//return and do whatever it needs to be
182
//done
183
Thread.sleep( 2 );
184             m_kernel.removeApplication( m_metaData.getName() );
185         }
186         catch( Exception JavaDoc e )
187         {
188             final String JavaDoc message =
189                 REZ.getString( "applicationcontext.error.noremove",
190                                m_metaData.getName() );
191             getLogger().error( message, e );
192         }
193     }
194
195     /**
196      * Get ClassLoader for the current application.
197      *
198      * @return the ClassLoader
199      */

200     public ClassLoader JavaDoc getClassLoader()
201     {
202         return m_classLoader;
203     }
204
205     /**
206      * Get logger with category for application.
207      * Note that this name may not be the absolute category.
208      *
209      * @param category the logger category
210      * @return the Logger
211      */

212     public Logger getLogger( final String JavaDoc category )
213     {
214         return m_hierarchy.getChildLogger( category );
215     }
216
217     /**
218      * Export specified object into management system.
219      * The object is exported using specifed interface
220      * and using the specified name.
221      *
222      * @param name the name of object to export
223      * @param services the interface of object with which to export
224      * @param object the actual object to export
225      */

226     public void exportObject( final String JavaDoc name,
227                               final Class JavaDoc[] services,
228                               final Object JavaDoc object )
229         throws Exception JavaDoc
230     {
231         m_blockManager.register( name, object, services );
232     }
233
234     /**
235      * Unexport specified object from management system.
236      *
237      * @param name the name of object to unexport
238      */

239     public void unexportObject( final String JavaDoc name )
240         throws Exception JavaDoc
241     {
242         m_blockManager.unregister( name );
243     }
244
245     /**
246      * Get the Configuration for specified component.
247      *
248      * @param component the component
249      * @return the Configuration
250      */

251     public Configuration getConfiguration( final String JavaDoc component )
252         throws ConfigurationException
253     {
254         final Configuration configuration =
255             m_repository.getConfiguration( m_metaData.getName(),
256                                            component );
257
258         //no validation of listeners just yet..
259
if( hasBlockListener( component, this.m_metaData.getListeners() ) )
260         {
261             return configuration;
262         }
263         else if( m_validator.isValid( m_metaData.getName(),
264                                       component,
265                                       configuration ) )
266         {
267
268             return configuration;
269         }
270         else
271         {
272             final String JavaDoc message =
273                 REZ.getString( "applicationcontext.error.invalidconfig",
274                                component );
275             throw new ConfigurationException( message );
276         }
277     }
278
279     /**
280      * Return true if specified array contains entry with specified name.
281      *
282      * @param name the blocks name
283      * @param listeners the set of BlockListenerMetaData objects to search
284      * @return true if block present, false otherwise
285      */

286     private boolean hasBlockListener( final String JavaDoc name,
287                                       final BlockListenerMetaData[] listeners )
288     {
289         for( int i = 0; i < listeners.length; i++ )
290         {
291             if( listeners[ i ].getName().equals( name ) )
292             {
293                 return true;
294             }
295         }
296
297         return false;
298     }
299
300     /**
301      * Returns the local SystemManager where the blocks should be registered
302      * for management.
303      *
304      * TODO: context should probably be passed in by reference from the kernel
305      */

306     private SystemManager getManagementContext()
307         throws ManagerException
308     {
309         final SystemManager appContext =
310             m_systemManager.getSubContext( null, "application" );
311         return appContext.getSubContext( m_metaData.getName(), "block" );
312     }
313 }
314
Popular Tags