KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > codehaus > loom > components > kernel > DefaultApplicationContext


1 /* ====================================================================
2  * Loom Software License, version 1.1
3  *
4  * Copyright (c) 2003, Loom Group. All rights reserved.
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in
14  * the documentation and/or other materials provided with the
15  * distribution.
16  *
17  * 3. Neither the name of the Loom Group nor the name "Loom" nor
18  * the names of its contributors may be used to endorse or promote
19  * products derived from this software without specific prior
20  * written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
30  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  *
35  * ====================================================================
36  *
37  * Loom includes code from the Apache Software Foundation
38  *
39  * ====================================================================
40  * The Apache Software License, Version 1.1
41  *
42  * Copyright (c) 1997-2003 The Apache Software Foundation. All rights
43  * reserved.
44  *
45  * Redistribution and use in source and binary forms, with or without
46  * modification, are permitted provided that the following conditions
47  * are met:
48  *
49  * 1. Redistributions of source code must retain the above copyright
50  * notice, this list of conditions and the following disclaimer.
51  *
52  * 2. Redistributions in binary form must reproduce the above copyright
53  * notice, this list of conditions and the following disclaimer in
54  * the documentation and/or other materials provided with the
55  * distribution.
56  *
57  * 3. The end-user documentation included with the redistribution,
58  * if any, must include the following acknowledgment:
59  * "This product includes software developed by the
60  * Apache Software Foundation (http://www.apache.org/)."
61  * Alternately, this acknowledgment may appear in the software
62  * itself, if and wherever such third-party acknowledgments
63  * normally appear.
64  *
65  * 4. The names "Jakarta", "Avalon", and "Apache Software Foundation"
66  * must not be used to endorse or promote products derived from this
67  * software without prior written permission. For written
68  * permission, please contact apache@apache.org.
69  *
70  * 5. Products derived from this software may not be called "Apache",
71  * nor may "Apache" appear in their name, without prior written
72  * permission of the Apache Software Foundation.
73  *
74  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
75  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
76  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
77  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
78  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
79  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
80  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
81  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
82  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
83  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
84  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
85  * SUCH DAMAGE.
86  */

87 package org.codehaus.loom.components.kernel;
88
89 import java.io.File JavaDoc;
90 import java.io.FileInputStream JavaDoc;
91 import java.io.FileNotFoundException JavaDoc;
92 import java.io.InputStream JavaDoc;
93 import org.apache.avalon.framework.logger.Logger;
94 import org.apache.excalibur.instrument.InstrumentManager;
95 import org.codehaus.loom.components.util.ResourceUtil;
96 import org.codehaus.loom.components.util.profile.PartitionProfile;
97 import org.codehaus.loom.interfaces.ApplicationContext;
98 import org.codehaus.loom.interfaces.ContainerConstants;
99 import org.codehaus.loom.interfaces.Kernel;
100 import org.codehaus.loom.interfaces.LoomException;
101 import org.codehaus.loom.interfaces.SystemManager;
102 import org.codehaus.spice.alchemist.logger.LoggerAlchemist;
103 import org.codehaus.spice.loggerstore.LoggerStore;
104 import org.codehaus.spice.salt.i18n.ResourceManager;
105 import org.codehaus.spice.salt.i18n.Resources;
106 import org.codehaus.dna.AbstractLogEnabled;
107 import org.codehaus.dna.Active;
108 import org.codehaus.dna.Composable;
109 import org.codehaus.dna.MissingResourceException;
110 import org.codehaus.dna.ResourceLocator;
111
112 /**
113  * Manage the "frame" in which Applications operate.
114  *
115  * @author Peter Donald
116  * @author <a HREF="mailto:proyal@apache.org">Peter Royal</a>
117  */

118 class DefaultApplicationContext
119     extends AbstractLogEnabled
120     implements ApplicationContext, Composable, Active
121 {
122     private static final Resources REZ =
123         ResourceManager.getPackageResources( DefaultApplicationContext.class );
124
125     //Log Hierarchy for application
126
private final LoggerStore m_store;
127
128     ///ClassLoader for application
129
private final ClassLoader JavaDoc m_classLoader;
130
131     //InstrumentManager to register instruments with
132
private InstrumentManager m_instrumentManager;
133
134     ///Place to expose Management beans
135
private SystemManager m_systemManager;
136
137     private SystemManager m_blockManager;
138
139     private final PartitionProfile m_profile;
140     private final File JavaDoc m_workDirectory;
141     private final File JavaDoc m_homeDirectory;
142
143     /** The kernel associate with context */
144     private Kernel m_kernel;
145
146     protected DefaultApplicationContext( final PartitionProfile profile,
147                                          final File JavaDoc homeDirectory,
148                                          final File JavaDoc workDirectory,
149                                          final ClassLoader JavaDoc classLoader,
150                                          final LoggerStore store )
151     {
152         if( null == profile )
153         {
154             throw new NullPointerException JavaDoc( "profile" );
155         }
156         if( null == classLoader )
157         {
158             throw new NullPointerException JavaDoc( "classLoader" );
159         }
160         if( null == store )
161         {
162             throw new NullPointerException JavaDoc( "store" );
163         }
164         if( null == workDirectory )
165         {
166             throw new NullPointerException JavaDoc( "workDirectory" );
167         }
168         if( null == homeDirectory )
169         {
170             throw new NullPointerException JavaDoc( "homeDirectory" );
171         }
172         m_profile = profile;
173         m_classLoader = classLoader;
174         m_store = store;
175         m_workDirectory = workDirectory;
176         m_homeDirectory = homeDirectory;
177     }
178
179     /**
180      * @dna.dependency type="Kernel"
181      * @dna.dependency type="InstrumentManager"
182      * @dna.dependency type="SystemManager"
183      */

184     public void compose( final ResourceLocator locator )
185         throws MissingResourceException
186     {
187         m_systemManager = (SystemManager)locator.
188             lookup( SystemManager.class.getName() );
189         m_kernel = (Kernel)locator.lookup( Kernel.class.getName() );
190         m_instrumentManager = (InstrumentManager)locator.
191             lookup( InstrumentManager.class.getName() );
192     }
193
194     public void initialize()
195         throws Exception JavaDoc
196     {
197         m_blockManager = getManagementContext();
198     }
199
200     public void dispose()
201         throws Exception JavaDoc
202     {
203     }
204
205     public InputStream JavaDoc getResourceAsStream( final String JavaDoc name )
206     {
207         final File JavaDoc file =
208             ResourceUtil.getFileForResource( name,
209                                              getHomeDirectory(),
210                                              m_workDirectory );
211         if( !file.exists() )
212         {
213             return null;
214         }
215         else
216         {
217             try
218             {
219                 return new FileInputStream JavaDoc( file );
220             }
221             catch( FileNotFoundException JavaDoc e )
222             {
223                 //Should never happen
224
return null;
225             }
226         }
227     }
228
229     public PartitionProfile getPartitionProfile()
230     {
231         return m_profile;
232     }
233
234     public void requestShutdown()
235     {
236         final Thread JavaDoc thread = new Thread JavaDoc( "AppShutdown" )
237         {
238             public void run()
239             {
240                 scheduleShutdown();
241             }
242         };
243         thread.start();
244     }
245
246     private void scheduleShutdown()
247     {
248         try
249         {
250             //Sleep for a little bit so that the
251
//thread that requested this method can
252
//return and do whatever it needs to be
253
//done
254
Thread.sleep( 2 );
255             m_kernel.removeApplication( getName() );
256         }
257         catch( Exception JavaDoc e )
258         {
259             final String JavaDoc message =
260                 REZ.format( "applicationcontext.error.noremove",
261                             getName() );
262             getLogger().error( message, e );
263         }
264     }
265
266     public File JavaDoc getHomeDirectory()
267     {
268         return m_homeDirectory;
269     }
270
271     /**
272      * Get ClassLoader for the current application.
273      *
274      * @return the ClassLoader
275      */

276     public ClassLoader JavaDoc getClassLoader()
277     {
278         return m_classLoader;
279     }
280
281     /**
282      * Get logger with category for application. Note that this name may not be
283      * the absolute category.
284      *
285      * @param category the logger category
286      * @return the Logger
287      */

288     public Logger getLogger( final String JavaDoc category )
289         throws Exception JavaDoc
290     {
291         return LoggerAlchemist.toAvalonLogger( m_store.getLogger( category ) );
292     }
293
294     /**
295      * Export specified object into management system. The object is exported
296      * using specifed interface and using the specified name.
297      *
298      * @param name the name of object to export
299      * @param object the actual object to export
300      */

301     public void exportObject( final String JavaDoc name,
302                               final Object JavaDoc object )
303         throws Exception JavaDoc
304     {
305         m_blockManager.register( name, object );
306     }
307
308     /**
309      * Unexport specified object from management system.
310      *
311      * @param name the name of object to unexport
312      */

313     public void unexportObject( final String JavaDoc name )
314         throws Exception JavaDoc
315     {
316         m_blockManager.unregister( name );
317     }
318
319     /**
320      * Returns the local SystemManager where the blocks should be registered for
321      * management.
322      *
323      * TODO: context should probably be passed in by reference from the kernel
324      */

325     private SystemManager getManagementContext()
326         throws LoomException
327     {
328         final SystemManager appContext =
329             m_systemManager.getSubContext( null, "application" );
330         return appContext.getSubContext( getName(), "block" );
331     }
332
333     /**
334      * Get the instrument manager to use for this application
335      *
336      * @return the InstrumentManager
337      */

338     public InstrumentManager getInstrumentManager()
339     {
340         return m_instrumentManager;
341     }
342
343     /**
344      * Get the name to use for the instrumentables for the specified component
345      *
346      * @param component the component
347      * @return the name to use for Instrumentables
348      */

349     public String JavaDoc getInstrumentableName( String JavaDoc component )
350     {
351         return ContainerConstants.ROOT_INSTRUMENT_CATEGORY +
352             "." +
353             getName() +
354             "." +
355             component;
356     }
357
358     private String JavaDoc getName()
359     {
360         return m_profile.getMetaData().getName();
361     }
362 }
363
Popular Tags