KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > broker > util > interceptor > InterceptorFactory


1 package org.apache.ojb.broker.util.interceptor;
2
3 /* Copyright 2002-2005 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 //#ifdef JDK13
19
import java.lang.reflect.Proxy JavaDoc;
20 import java.lang.reflect.InvocationHandler JavaDoc;
21 //#else
22
/*
23 import net.sf.cglib.proxy.Proxy;
24 import net.sf.cglib.proxy.InvocationHandler;
25 */

26 //#endif
27

28 import java.util.HashMap JavaDoc;
29
30 import org.apache.ojb.broker.util.configuration.Configurable;
31 import org.apache.ojb.broker.util.configuration.Configuration;
32 import org.apache.ojb.broker.util.configuration.ConfigurationException;
33 import org.apache.ojb.broker.util.configuration.impl.OjbConfigurator;
34 import org.apache.ojb.broker.util.logging.LoggerFactory;
35 import org.apache.ojb.broker.util.ClassHelper;
36
37 /**
38  * @author <a HREF="mailto:thma@apache.org">Thomas Mahler<a>
39  * @version $Id: InterceptorFactory.java,v 1.10.2.3 2005/12/21 22:28:16 tomdz Exp $
40  */

41 public class InterceptorFactory implements Configurable
42 {
43
44     private static InterceptorFactory instance = null;
45
46     private Class JavaDoc interceptorClassToBeUsed = null;
47
48     /**
49      * Returns the instance.
50      * @return InterceptorFactory
51      */

52     public static InterceptorFactory getInstance()
53     {
54         if (instance == null)
55         {
56             instance = new InterceptorFactory();
57             OjbConfigurator.getInstance().configure(instance);
58         }
59         return instance;
60     }
61
62
63     /**
64      * @see org.apache.ojb.broker.util.configuration.Configurable#configure(Configuration)
65      */

66     public void configure(Configuration pConfig) throws ConfigurationException
67     {
68         Class JavaDoc clazz = pConfig.getClass("InterceptorClass", Object JavaDoc.class);
69         if(!clazz.equals(Object JavaDoc.class)) setInterceptorClassToBeUsed(clazz);
70     }
71
72     public Object JavaDoc createInterceptorFor(Object JavaDoc instanceToIntercept)
73     {
74         if (getInterceptorClassToBeUsed() != null)
75         {
76             try
77             {
78
79
80 // Class[] parameterTypes = {Object.class};
81
// Object[] parameters = {instanceToIntercept};
82
// Constructor constructor = getInterceptorClassToBeUsed().getConstructor(parameterTypes);
83
// InvocationHandler handler = (InvocationHandler) constructor.newInstance(parameters);
84
// use helper class to instantiate
85
InvocationHandler JavaDoc handler = (InvocationHandler JavaDoc) ClassHelper.newInstance(
86                                             getInterceptorClassToBeUsed(), Object JavaDoc.class, instanceToIntercept);
87                 Class JavaDoc[] interfaces = computeInterfaceArrayFor(instanceToIntercept.getClass());
88                 Object JavaDoc result =
89                     Proxy.newProxyInstance(
90                         ClassHelper.getClassLoader(),
91                         interfaces,
92                         handler);
93                 return result;
94             }
95             catch (Throwable JavaDoc t)
96             {
97                 LoggerFactory.getDefaultLogger().error("can't use Interceptor " + getInterceptorClassToBeUsed().getName() +
98                     "for " + instanceToIntercept.getClass().getName(), t);
99                 return instanceToIntercept;
100             }
101         }
102         else
103         {
104             return instanceToIntercept;
105         }
106     }
107
108
109
110     public Class JavaDoc[] computeInterfaceArrayFor(Class JavaDoc clazz)
111     {
112         Class JavaDoc superClass = clazz;
113         Class JavaDoc[] interfaces = clazz.getInterfaces();
114
115         // clazz can be an interface itself and when getInterfaces()
116
// is called on an interface it returns only the extending
117
// interfaces, not the interface itself.
118
if (clazz.isInterface())
119         {
120             Class JavaDoc[] tempInterfaces = new Class JavaDoc[interfaces.length + 1];
121             tempInterfaces[0] = clazz;
122
123             System.arraycopy(interfaces, 0, tempInterfaces, 1, interfaces.length);
124             interfaces = tempInterfaces;
125         }
126
127         // add all interfaces implemented by superclasses to the interfaces array
128
while ((superClass = superClass.getSuperclass()) != null)
129         {
130             Class JavaDoc[] superInterfaces = superClass.getInterfaces();
131             Class JavaDoc[] combInterfaces = new Class JavaDoc[interfaces.length + superInterfaces.length];
132             System.arraycopy(interfaces, 0, combInterfaces, 0, interfaces.length);
133             System.arraycopy(
134                 superInterfaces,
135                 0,
136                 combInterfaces,
137                 interfaces.length,
138                 superInterfaces.length);
139             interfaces = combInterfaces;
140         }
141
142         /**
143          * Must remove duplicate interfaces before calling Proxy.getProxyClass().
144          * Duplicates can occur if a subclass re-declares that it implements
145          * the same interface as one of its ancestor classes.
146         **/

147         HashMap JavaDoc unique = new HashMap JavaDoc();
148         for (int i = 0; i < interfaces.length; i++)
149         {
150             unique.put(interfaces[i].getName(), interfaces[i]);
151         }
152         interfaces = (Class JavaDoc[]) unique.values().toArray(new Class JavaDoc[unique.size()]);
153
154         return interfaces;
155     }
156
157
158
159     /**
160      * Returns the interceptorClassToBeUsed.
161      * @return Class
162      */

163     public Class JavaDoc getInterceptorClassToBeUsed()
164     {
165         return interceptorClassToBeUsed;
166     }
167
168     /**
169      * Sets the interceptorClassToBeUsed.
170      * @param interceptorClassToBeUsed The interceptorClassToBeUsed to set
171      */

172     public void setInterceptorClassToBeUsed(Class JavaDoc interceptorClassToBeUsed)
173     {
174         this.interceptorClassToBeUsed = interceptorClassToBeUsed;
175     }
176
177 }
178
Popular Tags