KickJava   Java API By Example, From Geeks To Geeks.

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


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.excalibur.mpool.ObjectFactory;
21
22 /**
23  * An object factory that delegates all calls to another object factory and
24  * wraps the returned object into another object that exposes only the wrapped
25  * object's work interface(s).
26  *
27  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
28  * @version CVS $ Revision: 1.1 $
29  */

30 public final class WrapperObjectFactory extends AbstractObjectFactory
31 {
32
33     /**
34      * The {@link BCELWrapperGenerator} to use for creating the wrapper.
35      */

36     private final BCELWrapperGenerator m_wrapperGenerator;
37
38     /**
39      * Creates a {@link WrapperObjectFactory} with the specified
40      * {@link org.apache.excalibur.mpool.ObjectFactory ObjectFactory} as the
41      * object factory to delegate all calls for new instances to.
42      *
43      * @param objectFactory The {@link org.apache.excalibur.mpool.ObjectFactory
44      * ObjectFactory} to sue when creating new instances
45      * @throws IllegalArgumentException If <code>objFactory</code> is
46      * <code>null</code>
47      */

48     public WrapperObjectFactory( final ObjectFactory objectFactory )
49         throws IllegalArgumentException JavaDoc
50     {
51         super( objectFactory );
52         m_wrapperGenerator = new BCELWrapperGenerator();
53     }
54
55     /**
56      * @see org.apache.excalibur.mpool.ObjectFactory#newInstance()
57      */

58     public Object JavaDoc newInstance() throws Exception JavaDoc
59     {
60         final Object JavaDoc obj = m_delegateFactory.newInstance();
61
62         final Class JavaDoc wrappedClass =
63             m_wrapperGenerator.createWrapper( obj.getClass() );
64
65         return wrappedClass.getConstructor(
66             new Class JavaDoc[]{obj.getClass()} ).newInstance(
67                 new Object JavaDoc[]{obj} );
68     }
69
70     /**
71      * @see org.apache.excalibur.mpool.ObjectFactory#dispose(java.lang.Object)
72      */

73     public void dispose( final Object JavaDoc object ) throws Exception JavaDoc
74     {
75         if ( object == null )
76         {
77             final String JavaDoc error = "Supplied argument is <null>";
78             throw new IllegalArgumentException JavaDoc( error );
79         }
80         if ( !( object instanceof WrapperClass ) )
81         {
82             final String JavaDoc error =
83                 "Supplied argument is no instance of \""
84                 + WrapperClass.class.getName()
85                 + "\"";
86             throw new IllegalArgumentException JavaDoc( error );
87         }
88
89         final Object JavaDoc target = ( (WrapperClass) object ).getWrappedObject();
90
91         m_delegateFactory.dispose( target );
92     }
93 }
94
Popular Tags