1 package org.apache.ojb.broker.util.interceptor; 2 3 17 18 import java.lang.reflect.Proxy ; 20 import java.lang.reflect.InvocationHandler ; 21 26 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 41 public class InterceptorFactory implements Configurable 42 { 43 44 private static InterceptorFactory instance = null; 45 46 private Class interceptorClassToBeUsed = null; 47 48 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 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 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 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 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 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 163 public Class getInterceptorClassToBeUsed() 164 { 165 return interceptorClassToBeUsed; 166 } 167 168 172 public void setInterceptorClassToBeUsed(Class interceptorClassToBeUsed) 173 { 174 this.interceptorClassToBeUsed = interceptorClassToBeUsed; 175 } 176 177 } 178 | Popular Tags |