KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > fortress > impl > factory > AbstractObjectFactory


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.factory;
19
20 import org.apache.avalon.framework.activity.Disposable;
21 import org.apache.avalon.framework.activity.Initializable;
22 import org.apache.avalon.framework.activity.Startable;
23 import org.apache.avalon.framework.activity.Suspendable;
24 import org.apache.avalon.framework.component.Component;
25 import org.apache.avalon.framework.component.Composable;
26 import org.apache.avalon.framework.component.Recomposable;
27 import org.apache.avalon.framework.configuration.Configurable;
28 import org.apache.avalon.framework.configuration.Reconfigurable;
29 import org.apache.avalon.framework.context.Contextualizable;
30 import org.apache.avalon.framework.context.Recontextualizable;
31 import org.apache.avalon.framework.logger.LogEnabled;
32 import org.apache.avalon.framework.logger.Loggable;
33 import org.apache.avalon.framework.parameters.Parameterizable;
34 import org.apache.avalon.framework.parameters.Reparameterizable;
35 import org.apache.avalon.framework.service.Serviceable;
36 import org.apache.excalibur.instrument.Instrument;
37 import org.apache.excalibur.instrument.Instrumentable;
38 import org.apache.excalibur.mpool.ObjectFactory;
39
40 import java.io.Serializable JavaDoc;
41 import java.util.HashSet JavaDoc;
42 import java.util.Set JavaDoc;
43
44 /**
45  * AbstractObjectFactory does XYZ
46  *
47  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
48  * @version CVS $ Revision: 1.1 $
49  */

50 public abstract class AbstractObjectFactory implements ObjectFactory, Instrumentable
51 {
52     /**
53      * The list interfaces that will not be proxied.
54      */

55     private static final Class JavaDoc[] INVALID_INTERFACES = new Class JavaDoc[]
56     {
57         Loggable.class,
58         LogEnabled.class,
59         Contextualizable.class,
60         Recontextualizable.class,
61         Composable.class,
62         Recomposable.class,
63         Serviceable.class,
64         Configurable.class,
65         Reconfigurable.class,
66         Parameterizable.class,
67         Reparameterizable.class,
68         Initializable.class,
69         Startable.class,
70         Suspendable.class,
71         Disposable.class,
72         Serializable JavaDoc.class
73     };
74
75     /**
76      * The {@link ObjectFactory ObjectFactory} proper
77      * we delegate all calls to.
78      */

79     protected final ObjectFactory m_delegateFactory;
80
81     public AbstractObjectFactory( final ObjectFactory objectFactory )
82     {
83         if ( null == objectFactory )
84         {
85             throw new NullPointerException JavaDoc( "objectFactory" );
86         }
87
88         m_delegateFactory = objectFactory;
89     }
90
91     /**
92      * @see ObjectFactory#newInstance()
93      */

94     public abstract Object JavaDoc newInstance() throws Exception JavaDoc;
95
96     /**
97      * @see ObjectFactory#getCreatedClass()
98      */

99     public final Class JavaDoc getCreatedClass()
100     {
101         return m_delegateFactory.getCreatedClass();
102     }
103
104     /**
105      * @see ObjectFactory#dispose(Object)
106      */

107     public abstract void dispose( Object JavaDoc object ) throws Exception JavaDoc;
108
109     /* (non-Javadoc)
110      * @see org.apache.excalibur.instrument.Instrumentable#setInstrumentableName(java.lang.String)
111      */

112     public final void setInstrumentableName( final String JavaDoc name )
113     {
114         if ( m_delegateFactory instanceof Instrumentable )
115         {
116             ( (Instrumentable) m_delegateFactory ).setInstrumentableName( name );
117         }
118     }
119
120     /* (non-Javadoc)
121      * @see org.apache.excalibur.instrument.Instrumentable#getInstrumentableName()
122      */

123     public final String JavaDoc getInstrumentableName()
124     {
125         if ( m_delegateFactory instanceof Instrumentable )
126         {
127             return ( (Instrumentable) m_delegateFactory ).getInstrumentableName();
128         }
129
130         return "";
131     }
132
133     /* (non-Javadoc)
134      * @see org.apache.excalibur.instrument.Instrumentable#getInstruments()
135      */

136     public final Instrument[] getInstruments()
137     {
138         if ( m_delegateFactory instanceof Instrumentable )
139         {
140             return ( (Instrumentable) m_delegateFactory ).getInstruments();
141         }
142
143         return new Instrument[]{};
144     }
145
146     /* (non-Javadoc)
147      * @see org.apache.excalibur.instrument.Instrumentable#getChildInstrumentables()
148      */

149     public final Instrumentable[] getChildInstrumentables()
150     {
151         if ( m_delegateFactory instanceof Instrumentable )
152         {
153             return ( (Instrumentable) m_delegateFactory ).getChildInstrumentables();
154         }
155
156         return new Instrumentable[]{};
157     }
158
159     /**
160      * Get a list of interfaces to proxy by scanning through
161      * all interfaces a class implements and skipping invalid interfaces
162      * (as defined in {@link #INVALID_INTERFACES}).
163      *
164      * @param clazz the class
165      * @return the list of interfaces to proxy
166      */

167     protected static Class JavaDoc[] guessWorkInterfaces( final Class JavaDoc clazz )
168     {
169         final HashSet JavaDoc workInterfaces = new HashSet JavaDoc();
170
171         // Get *all* interfaces
172
guessWorkInterfaces( clazz, workInterfaces );
173
174         // Make sure we have Component in there.
175
workInterfaces.add( Component.class );
176
177         // Remove the invalid ones.
178
for ( int j = 0; j < INVALID_INTERFACES.length; j++ )
179         {
180             workInterfaces.remove(INVALID_INTERFACES[j]);
181         }
182
183         return (Class JavaDoc[]) workInterfaces.toArray( new Class JavaDoc[workInterfaces.size()] );
184     }
185
186     /**
187      * Get a list of interfaces to proxy by scanning through
188      * all interfaces a class implements.
189      *
190      * @param clazz the class
191      * @param workInterfaces the set of current work interfaces
192      */

193     private static void guessWorkInterfaces( final Class JavaDoc clazz,
194                                              final Set JavaDoc workInterfaces )
195     {
196         if ( null != clazz )
197         {
198             addInterfaces( clazz.getInterfaces(), workInterfaces );
199
200             guessWorkInterfaces( clazz.getSuperclass(), workInterfaces );
201         }
202     }
203
204     /**
205      * Get a list of interfaces to proxy by scanning through
206      * all interfaces a class implements.
207      *
208      * @param interfaces the array of interfaces
209      * @param workInterfaces the set of current work interfaces
210      */

211     private static void addInterfaces( final Class JavaDoc[] interfaces,
212                                              final Set JavaDoc workInterfaces )
213     {
214         for ( int i = 0; i < interfaces.length; i++ )
215         {
216             workInterfaces.add( interfaces[i] );
217             addInterfaces(interfaces[i].getInterfaces(), workInterfaces);
218         }
219     }
220 }
221
Popular Tags