| 1 | |
package org.apache.ojb.broker.util.interceptor; |
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
import java.lang.reflect.Proxy; |
| 20 | |
import java.lang.reflect.InvocationHandler; |
| 21 | |
|
| 22 | |
|
| 23 | |
|
| 24 | |
|
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
import java.util.HashMap; |
| 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 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
public class InterceptorFactory implements Configurable |
| 42 | |
{ |
| 43 | |
|
| 44 | |
private static InterceptorFactory instance = null; |
| 45 | |
|
| 46 | |
private Class interceptorClassToBeUsed = null; |
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 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 | |
|
| 65 | |
|
| 66 | |
public void configure(Configuration pConfig) throws ConfigurationException |
| 67 | |
{ |
| 68 | |
Class clazz = pConfig.getClass("InterceptorClass", Object.class); |
| 69 | |
if(!clazz.equals(Object.class)) setInterceptorClassToBeUsed(clazz); |
| 70 | |
} |
| 71 | |
|
| 72 | |
public Object createInterceptorFor(Object instanceToIntercept) |
| 73 | |
{ |
| 74 | |
if (getInterceptorClassToBeUsed() != null) |
| 75 | |
{ |
| 76 | |
try |
| 77 | |
{ |
| 78 | |
|
| 79 | |
|
| 80 | |
|
| 81 | |
|
| 82 | |
|
| 83 | |
|
| 84 | |
|
| 85 | |
InvocationHandler handler = (InvocationHandler) ClassHelper.newInstance( |
| 86 | |
getInterceptorClassToBeUsed(), Object.class, instanceToIntercept); |
| 87 | |
Class[] interfaces = computeInterfaceArrayFor(instanceToIntercept.getClass()); |
| 88 | |
Object result = |
| 89 | |
Proxy.newProxyInstance( |
| 90 | |
ClassHelper.getClassLoader(), |
| 91 | |
interfaces, |
| 92 | |
handler); |
| 93 | |
return result; |
| 94 | |
} |
| 95 | |
catch (Throwable 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[] computeInterfaceArrayFor(Class clazz) |
| 111 | |
{ |
| 112 | |
Class superClass = clazz; |
| 113 | |
Class[] interfaces = clazz.getInterfaces(); |
| 114 | |
|
| 115 | |
|
| 116 | |
|
| 117 | |
|
| 118 | |
if (clazz.isInterface()) |
| 119 | |
{ |
| 120 | |
Class[] tempInterfaces = new Class[interfaces.length + 1]; |
| 121 | |
tempInterfaces[0] = clazz; |
| 122 | |
|
| 123 | |
System.arraycopy(interfaces, 0, tempInterfaces, 1, interfaces.length); |
| 124 | |
interfaces = tempInterfaces; |
| 125 | |
} |
| 126 | |
|
| 127 | |
|
| 128 | |
while ((superClass = superClass.getSuperclass()) != null) |
| 129 | |
{ |
| 130 | |
Class[] superInterfaces = superClass.getInterfaces(); |
| 131 | |
Class[] combInterfaces = new Class[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 | |
|
| 144 | |
|
| 145 | |
|
| 146 | |
|
| 147 | |
HashMap unique = new HashMap(); |
| 148 | |
for (int i = 0; i < interfaces.length; i++) |
| 149 | |
{ |
| 150 | |
unique.put(interfaces[i].getName(), interfaces[i]); |
| 151 | |
} |
| 152 | |
interfaces = (Class[]) unique.values().toArray(new Class[unique.size()]); |
| 153 | |
|
| 154 | |
return interfaces; |
| 155 | |
} |
| 156 | |
|
| 157 | |
|
| 158 | |
|
| 159 | |
|
| 160 | |
|
| 161 | |
|
| 162 | |
|
| 163 | |
public Class getInterceptorClassToBeUsed() |
| 164 | |
{ |
| 165 | |
return interceptorClassToBeUsed; |
| 166 | |
} |
| 167 | |
|
| 168 | |
|
| 169 | |
|
| 170 | |
|
| 171 | |
|
| 172 | |
public void setInterceptorClassToBeUsed(Class interceptorClassToBeUsed) |
| 173 | |
{ |
| 174 | |
this.interceptorClassToBeUsed = interceptorClassToBeUsed; |
| 175 | |
} |
| 176 | |
|
| 177 | |
} |