[weld-commits] Weld SVN: r6309 - in core/trunk/impl/src/main/java/org/jboss/weld: bean/builtin/ee and 3 other directories.
weld-commits at lists.jboss.org
weld-commits at lists.jboss.org
Wed May 26 06:02:44 EDT 2010
Author: dallen6
Date: 2010-05-26 06:02:43 -0400 (Wed, 26 May 2010)
New Revision: 6309
Modified:
core/trunk/impl/src/main/java/org/jboss/weld/bean/AbstractClassBean.java
core/trunk/impl/src/main/java/org/jboss/weld/bean/ManagedBean.java
core/trunk/impl/src/main/java/org/jboss/weld/bean/builtin/ee/AbstractEEBean.java
core/trunk/impl/src/main/java/org/jboss/weld/bean/builtin/ee/EEResourceProducerField.java
core/trunk/impl/src/main/java/org/jboss/weld/bean/proxy/ClientProxyProvider.java
core/trunk/impl/src/main/java/org/jboss/weld/bean/proxy/DecoratorProxyFactory.java
core/trunk/impl/src/main/java/org/jboss/weld/bean/proxy/EnterpriseProxyFactory.java
core/trunk/impl/src/main/java/org/jboss/weld/bean/proxy/ProxyFactory.java
core/trunk/impl/src/main/java/org/jboss/weld/bean/proxy/util/SerializableProxy.java
core/trunk/impl/src/main/java/org/jboss/weld/util/collections/ArraySet.java
Log:
Redesigned proxies to use only interfaces where possible and fixed serialization issues
Modified: core/trunk/impl/src/main/java/org/jboss/weld/bean/AbstractClassBean.java
===================================================================
--- core/trunk/impl/src/main/java/org/jboss/weld/bean/AbstractClassBean.java 2010-05-25 22:03:31 UTC (rev 6308)
+++ core/trunk/impl/src/main/java/org/jboss/weld/bean/AbstractClassBean.java 2010-05-26 10:02:43 UTC (rev 6309)
@@ -200,7 +200,7 @@
{
T proxy = null;
TargetBeanInstance beanInstance = new TargetBeanInstance(this, instance);
- ProxyFactory<T> proxyFactory = new ProxyFactory<T>(beanInstance);
+ ProxyFactory<T> proxyFactory = new ProxyFactory<T>(getType(), getTypes());
DecorationHelper<T> decorationHelper = new DecorationHelper<T>(beanInstance, proxyFactory.getProxyClass(), beanManager, decorators);
DecorationHelper.getHelperStack().push(decorationHelper);
Modified: core/trunk/impl/src/main/java/org/jboss/weld/bean/ManagedBean.java
===================================================================
--- core/trunk/impl/src/main/java/org/jboss/weld/bean/ManagedBean.java 2010-05-25 22:03:31 UTC (rev 6308)
+++ core/trunk/impl/src/main/java/org/jboss/weld/bean/ManagedBean.java 2010-05-26 10:02:43 UTC (rev 6309)
@@ -585,7 +585,7 @@
MethodHandler methodHandler = interceptorProxyCreator.createMethodHandler(instance, getType(), getBeanManager().getServices().get(InterceptionMetadataService.class).getInterceptorMetadataRegistry().getInterceptorClassMetadata(WeldClassReference.of(getWeldAnnotated()), true));
TargetBeanInstance targetInstance = new TargetBeanInstance(this, instance);
targetInstance.setInterceptorsHandler(methodHandler);
- instance = new ProxyFactory<T>(targetInstance).create(targetInstance);
+ instance = new ProxyFactory<T>(getType(), getTypes()).create(targetInstance);
}
}
Modified: core/trunk/impl/src/main/java/org/jboss/weld/bean/builtin/ee/AbstractEEBean.java
===================================================================
--- core/trunk/impl/src/main/java/org/jboss/weld/bean/builtin/ee/AbstractEEBean.java 2010-05-25 22:03:31 UTC (rev 6308)
+++ core/trunk/impl/src/main/java/org/jboss/weld/bean/builtin/ee/AbstractEEBean.java 2010-05-26 10:02:43 UTC (rev 6309)
@@ -43,7 +43,7 @@
this.types = new HashSet<Type>();
this.types.add(Object.class);
this.types.add(type);
- this.proxy = new ProxyFactory<T>(type).create(new EnterpriseTargetBeanInstance(type, new CallableMethodHandler(callable)));
+ this.proxy = new ProxyFactory<T>(type, types).create(new EnterpriseTargetBeanInstance(type, new CallableMethodHandler(callable)));
}
public T create(CreationalContext<T> creationalContext)
Modified: core/trunk/impl/src/main/java/org/jboss/weld/bean/builtin/ee/EEResourceProducerField.java
===================================================================
--- core/trunk/impl/src/main/java/org/jboss/weld/bean/builtin/ee/EEResourceProducerField.java 2010-05-25 22:03:31 UTC (rev 6308)
+++ core/trunk/impl/src/main/java/org/jboss/weld/bean/builtin/ee/EEResourceProducerField.java 2010-05-26 10:02:43 UTC (rev 6309)
@@ -140,7 +140,7 @@
else
{
BeanInstance proxyBeanInstance = new EnterpriseTargetBeanInstance(getTypes(), new CallableMethodHandler(new EEResourceCallable<T>(getBeanManager(), this, creationalContext)));
- return new ProxyFactory<T>(proxyBeanInstance).create(proxyBeanInstance);
+ return new ProxyFactory<T>(getType(), getTypes()).create(proxyBeanInstance);
}
}
Modified: core/trunk/impl/src/main/java/org/jboss/weld/bean/proxy/ClientProxyProvider.java
===================================================================
--- core/trunk/impl/src/main/java/org/jboss/weld/bean/proxy/ClientProxyProvider.java 2010-05-25 22:03:31 UTC (rev 6308)
+++ core/trunk/impl/src/main/java/org/jboss/weld/bean/proxy/ClientProxyProvider.java 2010-05-26 10:02:43 UTC (rev 6309)
@@ -84,28 +84,7 @@
private static <T> T createClientProxy(Bean<T> bean, String id) throws RuntimeException
{
ContextBeanInstance<T> beanInstance = new ContextBeanInstance<T>(bean, id);
- return new ProxyFactory<T>(beanInstance).create(beanInstance);
-// try
-// {
-// TypeInfo typeInfo;
-// if ((bean instanceof AbstractClassBean) && ((AbstractClassBean)bean).hasInterceptors())
-// {
-// typeInfo = TypeInfo.of(bean.getTypes()).add(Serializable.class).add(LifecycleMixin.class);
-// }
-// else
-// {
-// typeInfo = TypeInfo.of(bean.getTypes()).add(Serializable.class);
-// }
-// return Proxies.<T>createProxy(new ClientProxyMethodHandler(bean, id), typeInfo);
-// }
-// catch (InstantiationException e)
-// {
-// throw new WeldException(PROXY_INSTANTIATION_FAILED, e, bean);
-// }
-// catch (IllegalAccessException e)
-// {
-// throw new WeldException(PROXY_INSTANTIATION_BEAN_ACCESS_FAILED, e, bean);
-// }
+ return new ProxyFactory<T>(bean.getBeanClass(), bean.getTypes()).create(beanInstance);
}
/**
Modified: core/trunk/impl/src/main/java/org/jboss/weld/bean/proxy/DecoratorProxyFactory.java
===================================================================
--- core/trunk/impl/src/main/java/org/jboss/weld/bean/proxy/DecoratorProxyFactory.java 2010-05-25 22:03:31 UTC (rev 6308)
+++ core/trunk/impl/src/main/java/org/jboss/weld/bean/proxy/DecoratorProxyFactory.java 2010-05-26 10:02:43 UTC (rev 6309)
@@ -20,6 +20,7 @@
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
+import java.util.Collections;
import javassist.CtClass;
import javassist.CtConstructor;
@@ -52,7 +53,7 @@
public DecoratorProxyFactory(Class<T> proxyType, WeldInjectionPoint<?, ?> delegateInjectionPoint)
{
- super(proxyType);
+ super(proxyType, Collections.EMPTY_SET);
this.delegateInjectionPoint = delegateInjectionPoint;
try
{
Modified: core/trunk/impl/src/main/java/org/jboss/weld/bean/proxy/EnterpriseProxyFactory.java
===================================================================
--- core/trunk/impl/src/main/java/org/jboss/weld/bean/proxy/EnterpriseProxyFactory.java 2010-05-25 22:03:31 UTC (rev 6308)
+++ core/trunk/impl/src/main/java/org/jboss/weld/bean/proxy/EnterpriseProxyFactory.java 2010-05-26 10:02:43 UTC (rev 6309)
@@ -25,19 +25,16 @@
import javassist.CtNewMethod;
import org.jboss.weld.exceptions.WeldException;
-import org.jboss.weld.util.reflection.Reflections;
/**
- * This factory produces proxies specific for enterprise beans, in particular
- * session beans. It adds the interface {@link EnterpriseBeanInstance} to
- * each proxy class.
+ * This factory produces client proxies specific for enterprise beans, in
+ * particular session beans. It adds the interface
+ * {@link EnterpriseBeanInstance} on the proxy.
*
* @author David Allen
*/
public class EnterpriseProxyFactory<T> extends ProxyFactory<T>
{
- public static final String PROXY_SUFFIX = "EnterpriseProxy";
-
/**
* Produces a factory for a specific bean implementation.
*
@@ -45,29 +42,14 @@
*/
public EnterpriseProxyFactory(Class<T> proxiedBeanType, Set<Type> localBusinessInterfaces)
{
- super(proxiedBeanType);
- for (Type type : localBusinessInterfaces)
- {
- Class<?> c = Reflections.getRawType(type);
- // Ignore no-interface views, they are dealt with proxiedBeanType (pending redesign)
- if (c.isInterface())
- {
- addInterface(c);
- }
- }
+ super(proxiedBeanType, localBusinessInterfaces);
}
@Override
- protected String getProxyNameSuffix()
- {
- return PROXY_SUFFIX;
- }
-
- @Override
protected void addSpecialMethods(CtClass proxyClassType)
{
super.addSpecialMethods(proxyClassType);
-
+
// Add methods for the EnterpriseBeanInstance interface
try
{
@@ -83,6 +65,6 @@
{
throw new WeldException(e);
}
-
+
}
}
Modified: core/trunk/impl/src/main/java/org/jboss/weld/bean/proxy/ProxyFactory.java
===================================================================
--- core/trunk/impl/src/main/java/org/jboss/weld/bean/proxy/ProxyFactory.java 2010-05-25 22:03:31 UTC (rev 6308)
+++ core/trunk/impl/src/main/java/org/jboss/weld/bean/proxy/ProxyFactory.java 2010-05-26 10:02:43 UTC (rev 6309)
@@ -27,9 +27,11 @@
import java.io.ObjectStreamException;
import java.io.Serializable;
import java.lang.reflect.Modifier;
+import java.lang.reflect.Type;
import java.security.ProtectionDomain;
-import java.util.ArrayList;
import java.util.Arrays;
+import java.util.HashSet;
+import java.util.Set;
import javassist.ClassPool;
import javassist.CtClass;
@@ -47,6 +49,9 @@
import org.jboss.weld.exceptions.DefinitionException;
import org.jboss.weld.exceptions.WeldException;
import org.jboss.weld.serialization.spi.ProxyServices;
+import org.jboss.weld.util.Proxies.TypeInfo;
+import org.jboss.weld.util.collections.ArraySet;
+import org.jboss.weld.util.reflection.Reflections;
import org.jboss.weld.util.reflection.SecureReflections;
import org.jboss.weld.util.reflection.instantiation.InstantiatorFactory;
import org.slf4j.cal10n.LocLogger;
@@ -61,41 +66,55 @@
public class ProxyFactory<T>
{
// The log provider
- protected static final LocLogger log = loggerFactory().getLogger(BEAN);
+ protected static final LocLogger log = loggerFactory().getLogger(BEAN);
// Default proxy class name suffix
- public static final String PROXY_SUFFIX = "Proxy";
+ public static final String PROXY_SUFFIX = "Proxy";
- private final Class<?> beanType;
- private final ArrayList<Class<?>> additionalInterfaces = new ArrayList<Class<?>>();
- private final ClassLoader classLoader;
- private final ProtectionDomain protectionDomain;
- private final ClassPool classPool;
+ private final Class<?> beanType;
+ private final Set<Class<?>> additionalInterfaces = new HashSet<Class<?>>();
+ private final ClassLoader classLoader;
+ private final ProtectionDomain protectionDomain;
+ private final ClassPool classPool;
+ private final String baseProxyName;
/**
- * Creates a new proxy factory from any type of BeanInstance. This bean
- * instance is only used for initialization information and is not associated
- * with this factory once created.
- *
- * @param instance a bean instance that will be used with the proxy
- */
- public ProxyFactory(BeanInstance beanInstance)
- {
- this(beanInstance.getInstanceType());
- }
-
- /**
* Creates a new proxy factory with only the type of proxy specified.
*
* @param proxiedBeanType the super-class for this proxy class
*/
- public ProxyFactory(Class<?> proxiedBeanType)
+ public ProxyFactory(Class<?> proxiedBeanType, Set<Type> businessInterfaces)
{
- this.beanType = proxiedBeanType;
- this.classLoader = Container.instance().services().get(ProxyServices.class).getClassLoader(beanType);
+ for (Type type : businessInterfaces)
+ {
+ Class<?> c = Reflections.getRawType(type);
+ // Ignore no-interface views, they are dealt with proxiedBeanType
+ // (pending redesign)
+ if (c.isInterface())
+ {
+ addInterface(c);
+ }
+ }
+ Class<?> superClass = TypeInfo.of(businessInterfaces).getSuperClass();
+ superClass = superClass == null ? Object.class : superClass;
+ if (superClass.equals(Object.class))
+ {
+ if (additionalInterfaces.isEmpty())
+ {
+ // No interface beans must use the bean impl as superclass
+ superClass = proxiedBeanType;
+ }
+ this.classLoader = Container.instance().services().get(ProxyServices.class).getClassLoader(proxiedBeanType);
+ }
+ else
+ {
+ this.classLoader = Container.instance().services().get(ProxyServices.class).getClassLoader(superClass);
+ }
+ this.beanType = superClass;
this.protectionDomain = Container.instance().services().get(ProxyServices.class).getProtectionDomain(beanType);
this.classPool = new ClassPool();
this.classPool.appendClassPath(new ClassloaderClassPath(classLoader));
addDefaultAdditionalInterfaces();
+ baseProxyName = proxiedBeanType.getName();
}
/**
@@ -153,7 +172,7 @@
@SuppressWarnings("unchecked")
public Class<T> getProxyClass()
{
- String proxyClassName = beanType.getName() + "_$$_Weld" + getProxyNameSuffix();
+ String proxyClassName = getBaseProxyName() + "_$$_Weld" + getProxyNameSuffix();
if (proxyClassName.startsWith("java"))
{
proxyClassName = proxyClassName.replaceFirst("java", "org.jboss.weld");
@@ -181,6 +200,16 @@
}
/**
+ * Returns the package and base name for the proxy class.
+ *
+ * @return base name without suffixes
+ */
+ protected String getBaseProxyName()
+ {
+ return baseProxyName;
+ }
+
+ /**
* Convenience method to determine if an object is a proxy generated by this
* factory or any derived factory.
*
@@ -227,6 +256,13 @@
@SuppressWarnings("unchecked")
private Class<T> createProxyClass(String proxyClassName) throws Exception
{
+ ArraySet<Class<?>> specialInterfaces = new ArraySet<Class<?>>(3);
+ specialInterfaces.add(Proxy.class);
+ specialInterfaces.add(LifecycleMixin.class);
+ specialInterfaces.add(TargetInstanceProxy.class);
+ // Remove special interfaces from main set (deserialization scenario)
+ additionalInterfaces.removeAll(specialInterfaces);
+
CtClass instanceType = classPool.get(beanType.getName());
CtClass proxyClassType = null;
if (instanceType.isInterface())
@@ -249,9 +285,10 @@
addMethods(proxyClassType);
// Additional interfaces whose methods require special handling
- proxyClassType.addInterface(classPool.get(Proxy.class.getName()));
- proxyClassType.addInterface(classPool.get(LifecycleMixin.class.getName()));
- proxyClassType.addInterface(classPool.get(TargetInstanceProxy.class.getName()));
+ for (Class<?> specialInterface : specialInterfaces)
+ {
+ proxyClassType.addInterface(classPool.get(specialInterface.getName()));
+ }
Class<T> proxyClass = proxyClassType.toClass(classLoader, protectionDomain);
proxyClassType.detach();
@@ -318,10 +355,10 @@
/**
* Adds special serialization code by providing a writeReplace() method on
- * the proxy. This method when first called will substitute the proxy
- * object with an instance of {@link org.jboss.weld.proxy.util.SerializableProxy}.
- * The next call will receive the proxy object itself permitting the substitute
- * object to serialize the proxy.
+ * the proxy. This method when first called will substitute the proxy object
+ * with an instance of {@link org.jboss.weld.proxy.util.SerializableProxy}.
+ * The next call will receive the proxy object itself permitting the
+ * substitute object to serialize the proxy.
*
* @param proxyClassType the Javassist class for the proxy class
*/
@@ -333,23 +370,18 @@
// replacement object and the subsequent call get the proxy object.
CtClass exception = classPool.get(ObjectStreamException.class.getName());
CtClass objectClass = classPool.get(Object.class.getName());
- String writeReplaceBody = "{ " +
- " if (firstSerializationPhaseComplete) {" +
- " firstSerializationPhaseComplete = false; " +
- " return $0; " +
- " } else {" +
- " firstSerializationPhaseComplete = true; " +
- " return ((org.jboss.weld.serialization.spi.ProxyServices)org.jboss.weld.Container.instance().services().get(org.jboss.weld.serialization.spi.ProxyServices.class)).wrapForSerialization($0);" +
- " } }";
+ String writeReplaceBody = "{ " + " if (firstSerializationPhaseComplete) {" + " firstSerializationPhaseComplete = false; " + " return $0; " + " } else {" + " firstSerializationPhaseComplete = true; " + " return ((org.jboss.weld.serialization.spi.ProxyServices)org.jboss.weld.Container.instance().services().get(org.jboss.weld.serialization.spi.ProxyServices.class)).wrapForSerialization($0);" + " } }";
proxyClassType.addMethod(CtNewMethod.make(objectClass, "writeReplace", null, new CtClass[] { exception }, writeReplaceBody, proxyClassType));
-
- // Also add a static method that can be used to deserialize a proxy object.
- // This causes the OO input stream to use the class loader from this class.
+
+ // Also add a static method that can be used to deserialize a proxy
+ // object.
+ // This causes the OO input stream to use the class loader from this
+ // class.
CtClass objectInputStreamClass = classPool.get(ObjectInputStream.class.getName());
CtClass cnfe = classPool.get(ClassNotFoundException.class.getName());
CtClass ioe = classPool.get(IOException.class.getName());
String deserializeProxyBody = "{ return $1.readObject(); }";
- proxyClassType.addMethod(CtNewMethod.make(Modifier.STATIC | Modifier.PUBLIC, objectClass, "deserializeProxy", new CtClass[]{objectInputStreamClass}, new CtClass[]{cnfe, ioe}, deserializeProxyBody, proxyClassType));
+ proxyClassType.addMethod(CtNewMethod.make(Modifier.STATIC | Modifier.PUBLIC, objectClass, "deserializeProxy", new CtClass[] { objectInputStreamClass }, new CtClass[] { cnfe, ioe }, deserializeProxyBody, proxyClassType));
}
catch (Exception e)
{
@@ -402,14 +434,14 @@
}
bodyString.append("beanInstance.invoke(");
+ bodyString.append(method.getDeclaringClass().getName());
if (Modifier.isPublic(method.getModifiers()))
{
- bodyString.append("beanInstance.getInstanceType().getMethod(\"");
+ bodyString.append(".class.getMethod(\"");
log.trace("Using getMethod in proxy for method " + method.getLongName());
}
else
{
- bodyString.append(method.getDeclaringClass().getName());
bodyString.append(".class.getDeclaredMethod(\"");
log.trace("Using getDeclaredMethod in proxy for method " + method.getLongName());
}
Modified: core/trunk/impl/src/main/java/org/jboss/weld/bean/proxy/util/SerializableProxy.java
===================================================================
--- core/trunk/impl/src/main/java/org/jboss/weld/bean/proxy/util/SerializableProxy.java 2010-05-25 22:03:31 UTC (rev 6308)
+++ core/trunk/impl/src/main/java/org/jboss/weld/bean/proxy/util/SerializableProxy.java 2010-05-26 10:02:43 UTC (rev 6309)
@@ -25,12 +25,16 @@
import java.io.ObjectOutputStream;
import java.io.ObjectStreamException;
import java.io.Serializable;
+import java.lang.reflect.Type;
+import java.util.Arrays;
+import java.util.Set;
import org.jboss.weld.Container;
import org.jboss.weld.bean.proxy.ProxyFactory;
import org.jboss.weld.exceptions.ForbiddenStateException;
import org.jboss.weld.exceptions.WeldException;
import org.jboss.weld.serialization.spi.ProxyServices;
+import org.jboss.weld.util.collections.ArraySet;
/**
* A wrapper mostly for client proxies which provides header information useful
@@ -49,6 +53,7 @@
// Information required to generate client proxy classes
private final String proxyClassName;
private final String proxySuperClassName;
+ private final ArraySet<String> proxyInterfaces;
// The wrapped proxy object not serialized by default actions
private transient Object proxyObject;
@@ -62,6 +67,13 @@
}
this.proxyClassName = proxyObject.getClass().getName();
this.proxySuperClassName = proxyObject.getClass().getSuperclass().getName();
+ Class<?>[] proxyInterfaceClasses = proxyObject.getClass().getInterfaces();
+ proxyInterfaces = new ArraySet<String>(proxyInterfaceClasses.length);
+ for (int i = 0; i < proxyInterfaceClasses.length; i++)
+ {
+ proxyInterfaces.add(proxyInterfaceClasses[i].getName());
+ }
+ proxyInterfaces.add(proxySuperClassName);
this.proxyObject = proxyObject;
}
@@ -92,10 +104,11 @@
{
in.defaultReadObject();
Class<?> proxyBeanType = Container.instance().services().get(ProxyServices.class).loadProxySuperClass(proxySuperClassName);
+ ArraySet<Type> proxyBeanInterfaces = loadInterfaces();
Class<?> proxyClass = null;
if (proxyClassName.endsWith(ProxyFactory.PROXY_SUFFIX))
{
- proxyClass = generateClientProxyClass(proxyBeanType);
+ proxyClass = generateClientProxyClass(proxyBeanType, proxyBeanInterfaces);
}
else
{
@@ -112,6 +125,17 @@
}
}
+ private ArraySet<Type> loadInterfaces() throws ClassNotFoundException
+ {
+ ProxyServices proxyServices = Container.instance().services().get(ProxyServices.class);
+ ArraySet<Type> interfaceClasses = new ArraySet<Type>(proxyInterfaces.size());
+ for (String interfaceName : proxyInterfaces)
+ {
+ interfaceClasses.add(proxyServices.loadProxySuperClass(interfaceName));
+ }
+ return interfaceClasses;
+ }
+
/**
* Always returns the original proxy object that was serialized.
*
@@ -128,8 +152,8 @@
return writeProxy ? proxyObject : this;
}
- private <T> Class<?> generateClientProxyClass(Class<T> beanType)
+ private <T> Class<?> generateClientProxyClass(Class<T> beanType, Set<Type> interfaces)
{
- return new ProxyFactory<T>(beanType).getProxyClass();
+ return new ProxyFactory<T>(beanType, interfaces).getProxyClass();
}
}
Modified: core/trunk/impl/src/main/java/org/jboss/weld/util/collections/ArraySet.java
===================================================================
--- core/trunk/impl/src/main/java/org/jboss/weld/util/collections/ArraySet.java 2010-05-25 22:03:31 UTC (rev 6308)
+++ core/trunk/impl/src/main/java/org/jboss/weld/util/collections/ArraySet.java 2010-05-26 10:02:43 UTC (rev 6309)
@@ -17,6 +17,7 @@
package org.jboss.weld.util.collections;
+import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
@@ -38,7 +39,7 @@
*
* @author David Allen
*/
-public class ArraySet<E> implements Set<E>
+public class ArraySet<E> implements Set<E>, Serializable
{
// Underlying array of set elements
private ArrayList<E> elements;
More information about the weld-commits
mailing list