KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > fortress > impl > DefaultContainerManager


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.impl;
19
20 import org.apache.avalon.fortress.ContainerManager;
21 import org.apache.avalon.fortress.ContainerManagerConstants;
22 import org.apache.avalon.fortress.InitializationException;
23 import org.apache.avalon.fortress.util.ContextManager;
24 import org.apache.avalon.framework.activity.Disposable;
25 import org.apache.avalon.framework.activity.Initializable;
26 import org.apache.avalon.framework.component.Composable;
27 import org.apache.avalon.framework.configuration.Configuration;
28 import org.apache.avalon.framework.container.ContainerUtil;
29 import org.apache.avalon.framework.context.Context;
30 import org.apache.avalon.framework.context.ContextException;
31 import org.apache.avalon.framework.logger.ConsoleLogger;
32 import org.apache.avalon.framework.logger.Loggable;
33 import org.apache.avalon.framework.logger.Logger;
34 import org.apache.avalon.framework.parameters.Parameters;
35 import org.apache.avalon.framework.service.ServiceManager;
36 import org.apache.avalon.excalibur.logger.LoggerManager;
37
38 /**
39  * This is the default implementation of the
40  * {@link org.apache.avalon.fortress.ContainerManager} interface.
41  * See that interface for a description.
42  *
43  * @author <a HREF="mailto:dev@avalon.apache.org">The Avalon Team</a>
44  * @version CVS $Revision: 1.29 $ $Date: 2004/02/28 15:16:24 $
45  */

46 public class DefaultContainerManager
47     implements Initializable, Disposable, ContainerManager, ContainerManagerConstants
48 {
49     private final ContextManager m_contextManager;
50     private final Logger m_logger;
51     private Object JavaDoc m_containerInstance;
52
53     public DefaultContainerManager( final ContextManager contextManager )
54     {
55         this( contextManager, null );
56     }
57
58     public DefaultContainerManager( final ContextManager contextManager,
59                                     final Logger logger )
60     {
61         m_contextManager = contextManager;
62         m_logger = ( logger == null ?
63             createLoggerFromContext( m_contextManager.getContainerManagerContext() ) : logger );
64     }
65
66     public DefaultContainerManager( final Context initParameters ) throws Exception JavaDoc
67     {
68         this( initParameters, null );
69     }
70
71     public DefaultContainerManager( final Context initParameters,
72                                     final Logger logger ) throws Exception JavaDoc
73     {
74         this( getInitializedContextManager( initParameters, logger ), logger );
75     }
76
77     /**
78      * Creates and initializes a contextManager given an initialization context.
79      * This is necessary so that these operations can complete before the
80      * super constructor has been executed.
81      */

82     private static ContextManager getInitializedContextManager( final Context initParameters,
83                                                                 Logger logger ) throws Exception JavaDoc
84     {
85         // The context manager will use an internal coonsole logger if logger is null.
86
final ContextManager contextManager = new ContextManager( initParameters, logger );
87         contextManager.initialize();
88         return contextManager;
89     }
90
91     protected Logger createLoggerFromContext( final Context initParameters )
92     {
93         try
94         {
95             final Logger logger = (Logger) initParameters.get( LOGGER );
96             return logger;
97         }
98         catch ( ContextException ce )
99         {
100             final Logger consoleLogger = new ConsoleLogger();
101             consoleLogger.error( "ContainerManager could not obtain logger manager from context "
102                 + "(this should not happen). Using console instead." );
103             return consoleLogger;
104         }
105     }
106
107     /**
108      * Initialize the ContainerManager
109      */

110     public void initialize() throws Exception JavaDoc
111     {
112         initializeContainer();
113     }
114
115     protected void initializeContainer() throws InitializationException
116     {
117         if ( null == m_containerInstance )
118         {
119             createContainer();
120         }
121     }
122
123     private void createContainer()
124         throws InitializationException
125     {
126         final Context managerContext =
127             m_contextManager.getContainerManagerContext();
128         final Object JavaDoc instance;
129         try
130         {
131             final Class JavaDoc clazz = (Class JavaDoc) managerContext.get( CONTAINER_CLASS );
132             instance = clazz.newInstance();
133         }
134         catch ( Exception JavaDoc e )
135         {
136             final String JavaDoc message =
137                 "Cannot set up impl. Unable to create impl class";
138             throw new InitializationException( message, e );
139         }
140
141         if ( instance instanceof Loggable )
142         {
143             throw new InitializationException( "Loggable containers are not supported" );
144         }
145
146         if ( instance instanceof Composable )
147         {
148             throw new InitializationException( "Composable containers are not supported" );
149         }
150
151         try
152         {
153             final Context implContext = m_contextManager.getChildContext();
154
155             final ServiceManager serviceManager =
156                     (ServiceManager) getContextEntry( managerContext, SERVICE_MANAGER );
157             final LoggerManager loggerManager =
158                     (LoggerManager) serviceManager.lookup( LoggerManager.ROLE );
159
160             ContainerUtil.enableLogging( instance, loggerManager.getDefaultLogger() );
161             ContainerUtil.contextualize( instance, implContext );
162
163             ContainerUtil.service( instance, serviceManager );
164
165             final Configuration config =
166                 (Configuration) getContextEntry( managerContext, CONFIGURATION );
167             ContainerUtil.configure( instance, config );
168
169             final Parameters parameters =
170                 (Parameters) getContextEntry( managerContext, PARAMETERS );
171             ContainerUtil.parameterize( instance, parameters );
172
173             ContainerUtil.initialize( instance );
174             ContainerUtil.start( instance );
175
176             m_containerInstance = instance;
177         }
178         catch ( Exception JavaDoc e )
179         {
180             final String JavaDoc message =
181                 "Cannot set up Container. Startup lifecycle failure";
182             throw new InitializationException( message, e );
183         }
184     }
185
186     /**
187      * Retrieve an entry from context if it exists, else return null.
188      *
189      * @param context the context
190      * @param key the key
191      * @return the entry
192      */

193     private Object JavaDoc getContextEntry( final Context context, final String JavaDoc key )
194     {
195         try
196         {
197             return context.get( key );
198         }
199         catch ( ContextException e )
200         {
201             return null;
202         }
203     }
204
205     protected void disposeContainer()
206     {
207         if ( null != m_containerInstance )
208         {
209             try
210             {
211                 ContainerUtil.stop( m_containerInstance );
212             }
213             catch ( Exception JavaDoc e )
214             {
215                 if ( getLogger().isWarnEnabled() )
216                 {
217                     getLogger().warn( "Caught an exception when stopping the Container, "
218                         + "continuing with shutdown", e );
219                 }
220             }
221
222             ContainerUtil.dispose( m_containerInstance );
223             m_containerInstance = null;
224         }
225     }
226
227     /**
228      * Dispose of the ContainerManager and managed Container
229      */

230     public void dispose()
231     {
232         disposeContainer();
233         m_contextManager.dispose();
234     }
235
236     /**
237      * Get a reference to your Container. Typically, you would cast this to
238      * whatever interface you will use to interact with it.
239      */

240     public Object JavaDoc getContainer()
241     {
242         return m_containerInstance;
243     }
244
245     /**
246      * Allows to get the logger and associated hierarchy for logging.
247      * @return Logger
248      */

249     public final Logger getLogger()
250     {
251         // (mschier) was protected.
252
// Made public to get to the logger at the impl setup level.
253
return m_logger;
254     }
255 }
256
Popular Tags