KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > fortress > impl > handler > AbstractComponentHandler


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.handler;
19
20 import org.apache.avalon.excalibur.logger.LoggerManager;
21 import org.apache.avalon.framework.activity.Disposable;
22 import org.apache.avalon.framework.activity.Initializable;
23 import org.apache.avalon.framework.container.ContainerUtil;
24 import org.apache.avalon.framework.logger.Logger;
25 import org.apache.avalon.framework.service.ServiceException;
26 import org.apache.avalon.framework.service.ServiceManager;
27 import org.apache.avalon.framework.service.Serviceable;
28 import org.apache.excalibur.instrument.AbstractLogEnabledInstrumentable;
29 import org.apache.excalibur.instrument.CounterInstrument;
30 import org.apache.excalibur.instrument.Instrumentable;
31 import org.apache.excalibur.mpool.ObjectFactory;
32
33 /**
34  * AbstractComponentHandler class, ensures components are initialized
35  * and destroyed correctly.
36  *
37  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
38  * @version CVS $Revision: 1.18 $ $Date: 2004/02/28 15:16:25 $
39  * @since 4.0
40  */

41 public abstract class AbstractComponentHandler
42     extends AbstractLogEnabledInstrumentable
43     implements Serviceable, Initializable, Disposable, ComponentHandler
44 {
45     private CounterInstrument m_request = new CounterInstrument( "requests" );
46     private CounterInstrument m_release = new CounterInstrument( "releases" );
47
48     /**
49      * The instance of the ComponentFactory that creates and disposes of the
50      * Component
51      */

52     protected ObjectFactory m_factory;
53
54     /**
55      * State management boolean stating whether the Handler is initialized or
56      * not
57      */

58     protected boolean m_prepared;
59
60     /**
61      * State management boolean stating whether the Handler is disposed or
62      * not
63      */

64     protected boolean m_disposed;
65
66     /** Logger for factory */
67     protected Logger m_logger;
68
69     /** Logger Manager */
70     protected LoggerManager m_loggerManager;
71
72     /**
73      * @avalon.dependency type="LoggerManager"
74      */

75     public void service( final ServiceManager manager )
76         throws ServiceException
77     {
78         m_loggerManager =
79             (LoggerManager) manager.lookup( LoggerManager.ROLE );
80         m_factory =
81             (ObjectFactory) manager.lookup( ObjectFactory.ROLE );
82     }
83
84     public void initialize()
85         throws Exception JavaDoc
86     {
87         final String JavaDoc classname = getClass().getName();
88         final int index = classname.lastIndexOf( '.' );
89         final String JavaDoc name = classname.substring( index + 1 );
90
91         String JavaDoc loggerName = name.toLowerCase();
92         if ( name.endsWith( "ComponentHandler" ) )
93         {
94             final int endIndex = loggerName.length() - 16;
95             loggerName = loggerName.substring( 0, endIndex );
96         }
97
98         final String JavaDoc categoryName = "system.handler." + loggerName;
99         m_logger =
100             m_loggerManager.getLoggerForCategory( categoryName );
101
102         if ( m_factory instanceof Instrumentable )
103         {
104             addChildInstrumentable( (Instrumentable) m_factory );
105         }
106
107         addInstrument( m_request );
108         addInstrument( m_release );
109
110         setInstrumentableName( name );
111     }
112
113     /**
114      * Return the component's class that this handler is trying to create.
115      * Used for deubug information.
116      *
117      * @return the <code>Class</code> object for the component
118      */

119     public Class JavaDoc getComponentClass()
120     {
121         return m_factory.getCreatedClass();
122     }
123
124     /**
125      * Actually prepare the handler and make it ready to
126      * handle component access.
127      *
128      * @throws Exception if unable to prepare handler
129      */

130     public synchronized void prepareHandler()
131         throws Exception JavaDoc
132     {
133         if ( m_prepared )
134         {
135             return;
136         }
137
138         if ( m_disposed )
139         {
140             final String JavaDoc message = "Attempted to prepare disposed ComponentHandler for : " +
141                 m_factory.getCreatedClass().getName();
142             m_logger.warn( message );
143
144             return;
145         }
146
147         doPrepare();
148
149         if ( m_logger.isDebugEnabled() )
150         {
151             final String JavaDoc message = "ComponentHandler initialized for: " +
152                 m_factory.getCreatedClass().getName();
153             m_logger.debug( message );
154         }
155
156         m_prepared = true;
157     }
158
159     /**
160      * Initialize the ComponentHandler.
161      * Subclasses should overide this to do their own initialization.
162      *
163      * @throws Exception if there is a problem
164      */

165     protected void doPrepare() throws Exception JavaDoc
166     {}
167
168     /**
169      * Get a reference of the desired Component
170      * @return the component
171      */

172     public Object JavaDoc get()
173         throws Exception JavaDoc
174     {
175         if ( !m_prepared )
176         {
177             prepareHandler();
178         }
179
180         if ( m_disposed )
181         {
182             final String JavaDoc message =
183                 "You cannot get a component from a disposed holder";
184             throw new IllegalStateException JavaDoc( message );
185         }
186
187         if ( m_request.isActive() )
188         {
189             m_request.increment();
190         }
191
192         return doGet();
193     }
194
195     /**
196      * Subclasses should actually overide this to do the work
197      * of retrieving a service.
198      *
199      * @return the service
200      * @throws Exception if unable to aquire service
201      */

202     protected abstract Object JavaDoc doGet()
203         throws Exception JavaDoc;
204
205     /**
206      * Return a reference of the desired Component
207      * @param component the component
208      */

209     public void put( final Object JavaDoc component )
210     {
211         if ( !m_prepared )
212         {
213             final String JavaDoc message =
214                 "You cannot put a component in an uninitialized holder";
215             throw new IllegalStateException JavaDoc( message );
216         }
217
218         if ( m_release.isActive() )
219         {
220             m_release.increment();
221         }
222
223         doPut( component );
224     }
225
226     /**
227      * Subclasses should overide this to return component to handler.
228      *
229      * @param component the component
230      */

231     protected void doPut( final Object JavaDoc component )
232     {
233     }
234
235     /**
236      * Create a new component for handler.
237      *
238      * @return the new component
239      * @throws Exception if unable to create new component
240      */

241     protected Object JavaDoc newComponent()
242         throws Exception JavaDoc
243     {
244         try
245         {
246             return m_factory.newInstance();
247         }
248         catch ( final Exception JavaDoc e )
249         {
250             if ( m_logger.isDebugEnabled() )
251             {
252                 final String JavaDoc message = "Unable to create new instance";
253                 m_logger.debug( message, e );
254             }
255
256             throw e;
257         }
258     }
259
260     /**
261      * Dispose of the specified component.
262      *
263      * @param component the component
264      */

265     protected void disposeComponent( final Object JavaDoc component )
266     {
267         if ( null == component )
268         {
269             return;
270         }
271         try
272         {
273             m_factory.dispose( component );
274         }
275         catch ( final Exception JavaDoc e )
276         {
277             if ( m_logger.isWarnEnabled() )
278             {
279                 m_logger.warn( "Error disposing component", e );
280             }
281         }
282     }
283
284     /**
285      * Dispose of the ComponentHandler and any associated Pools and Factories.
286      */

287     public void dispose()
288     {
289         doDispose();
290         try
291         {
292             ContainerUtil.dispose( m_factory );
293         }
294         catch ( RuntimeException JavaDoc e )
295         {
296             if ( m_logger.isWarnEnabled() )
297             {
298                 final String JavaDoc message = "Error decommissioning component: " +
299                     m_factory.getCreatedClass().getName();
300                 m_logger.warn( message, e );
301             }
302         }
303
304         m_disposed = true;
305     }
306
307     /**
308      * Dispose handler specific resources.
309      * Subclasses should overide this to provide their own funcitonality.
310      */

311     protected void doDispose()
312     {
313     }
314
315     /**
316      * Represents the handler as a string.
317      * @return the string representation of the handler
318      */

319     public String JavaDoc toString()
320     {
321         return getClass().getName() + "[for: " + m_factory.getCreatedClass().getName() + "]";
322     }
323 }
324
Popular Tags