KickJava   Java API By Example, From Geeks To Geeks.

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


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.fortress.util.LifecycleExtensionManager;
21 import org.apache.avalon.framework.activity.Disposable;
22 import org.apache.avalon.framework.container.ContainerUtil;
23 import org.apache.avalon.framework.context.Context;
24
25 /**
26  * A ComponentHandler that delegates to underlying handler but also
27  * calls relevent Lifecycle Extension handlers at the right time.
28  *
29  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
30  * @version CVS $Revision: 1.18 $ $Date: 2004/02/28 15:16:25 $
31  */

32 public final class LEAwareComponentHandler
33     implements ComponentHandler, Disposable
34 {
35     private final ComponentHandler m_componentHandler;
36     private final LifecycleExtensionManager m_extManager;
37     private final Context m_context;
38
39     /**
40      * Creation of a new handler.
41      * @param componentHandler the handler
42      * @param extManager the extension manager
43      * @param context the context
44      */

45     public LEAwareComponentHandler( final ComponentHandler componentHandler,
46                                     final LifecycleExtensionManager extManager,
47                                     final Context context )
48     {
49         if ( null == componentHandler )
50         {
51             throw new NullPointerException JavaDoc( "componentHandler" );
52         }
53         if ( null == extManager )
54         {
55             throw new NullPointerException JavaDoc( "extManager" );
56         }
57         if ( null == context )
58         {
59             throw new NullPointerException JavaDoc( "context" );
60         }
61
62         m_componentHandler = componentHandler;
63         m_extManager = extManager;
64         m_context = context;
65     }
66
67     /**
68      * Return the component's class that this handler is trying to create.
69      * Used for deubug information.
70      *
71      * @return the <code>Class</code> object for the component
72      */

73     public Class JavaDoc getComponentClass()
74     {
75         return m_componentHandler.getComponentClass();
76     }
77
78     /**
79      * Prepare the handler.
80      * @exception Exception if a handler preparation error occurs
81      */

82     public void prepareHandler()
83         throws Exception JavaDoc
84     {
85         m_componentHandler.prepareHandler();
86     }
87
88     /**
89      * Retrieve the object and execute access extensions.
90      *
91      * @return the object
92      * @throws Exception if unable to aquire object
93      */

94     public Object JavaDoc get() throws Exception JavaDoc
95     {
96         final Object JavaDoc object = m_componentHandler.get();
97         m_extManager.executeAccessExtensions( object, m_context );
98         return object;
99     }
100
101     /**
102      * Return component and execute Release extensions.
103      *
104      * @param component the component
105      */

106     public void put( final Object JavaDoc component )
107     {
108         try
109         {
110             m_extManager.executeReleaseExtensions( component, m_context );
111         }
112         catch ( Exception JavaDoc e )
113         {
114             // REVISIT(MC): we need to log this somewhere
115
}
116         m_componentHandler.put( component );
117     }
118
119     /**
120      * Disposal of the handler.
121      */

122     public void dispose()
123     {
124         ContainerUtil.dispose( m_componentHandler );
125     }
126 }
127
Popular Tags