KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.lang.reflect.Constructor JavaDoc;
23
24 /**
25  * ProxyManager is used to abstract away the plumbing for the underlying
26  * proxy generator. Each proxy solution has to implement the ObjectFactory interface,
27  * that way we can keep a soft dependency on things like BCEL.
28  *
29  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
30  * @version CVS $ Revision: 1.1 $
31  */

32 public final class ProxyManager
33 {
34     public static final int DISCOVER = 0;
35     public static final int NONE = 1;
36     public static final int BCEL = 2;
37     public static final int PROXY = 3;
38     private static final String JavaDoc BCEL_CLASS = "org.apache.bcel.classfile.JavaClass";
39     private static final String JavaDoc BCEL_WRAPPER = "org.apache.avalon.fortress.impl.factory.WrapperObjectFactory";
40     private static final String JavaDoc PROXY_WRAPPER = "org.apache.avalon.fortress.impl.factory.ProxyObjectFactory";
41     private static final String JavaDoc NOOP_WRAPPER = "org.apache.avalon.fortress.impl.factory.NoopObjectFactory";
42     private static final boolean m_isBCELPresent;
43     private final String JavaDoc m_wrapperClassName;
44     private Class JavaDoc m_factoryClass;
45
46     static
47     {
48         boolean bcelPresent = false;
49         try
50         {
51             Thread.currentThread().getContextClassLoader().loadClass( BCEL_CLASS );
52             Thread.currentThread().getContextClassLoader().loadClass( BCEL_WRAPPER );
53             bcelPresent = true;
54         }
55         catch ( ClassNotFoundException JavaDoc cfne )
56         {
57             //ignore because we already have the proper value
58
}
59
60         m_isBCELPresent = bcelPresent;
61     }
62
63     public ProxyManager( final int type ) throws Exception JavaDoc
64     {
65         switch (type)
66         {
67             case NONE:
68                 m_wrapperClassName = NOOP_WRAPPER;
69                 break;
70
71             case BCEL:
72                 if ( ! m_isBCELPresent ) throw new IllegalStateException JavaDoc("BCEL is not available");
73                 m_wrapperClassName = BCEL_WRAPPER;
74                 break;
75
76             case PROXY:
77                 m_wrapperClassName = PROXY_WRAPPER;
78                 break;
79
80             default: // DISCOVER
81
if ( m_isBCELPresent )
82                 {
83                     m_wrapperClassName = BCEL_WRAPPER;
84                 }
85                 else
86                 {
87                     m_wrapperClassName = PROXY_WRAPPER;
88                 }
89                 break;
90         }
91     }
92
93     public ObjectFactory getWrappedObjectFactory( final ObjectFactory source ) throws Exception JavaDoc
94     {
95         if ( null == m_factoryClass )
96         {
97             final ClassLoader JavaDoc loader = source.getClass().getClassLoader();
98
99             m_factoryClass = loader.loadClass( m_wrapperClassName );
100         }
101
102         final Constructor JavaDoc constr = m_factoryClass.getConstructor( new Class JavaDoc[]{ObjectFactory.class} );
103         return (ObjectFactory) constr.newInstance( new Object JavaDoc[]{source} );
104     }
105 }
106
Popular Tags