Author: nickarls
Date: 2008-11-02 15:24:21 -0500 (Sun, 02 Nov 2008)
New Revision: 216
Added:
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/util/ProxyMethodHandler.java
Modified:
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/ManagerImpl.java
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/introspector/SimpleAnnotatedItem.java
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/util/ClientProxyFactory.java
ri/trunk/webbeans-ri/src/test/java/org/jboss/webbeans/test/ClientProxyTest.java
ri/trunk/webbeans-ri/src/test/java/org/jboss/webbeans/test/beans/Tuna.java
Log:
Client proxys
Modified: ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/ManagerImpl.java
===================================================================
--- ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/ManagerImpl.java 2008-11-02
18:58:32 UTC (rev 215)
+++ ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/ManagerImpl.java 2008-11-02
20:24:21 UTC (rev 216)
@@ -53,8 +53,8 @@
{
return (List<Context>) super.get(key);
}
-
- public DependentContext getDependentContext()
+
+ public DependentContext getDependentContext()
{
return (DependentContext) get(Dependent.class).get(0);
}
@@ -80,18 +80,16 @@
private ResolutionManager resolutionManager;
private ContextMap contextMap;
private ClientProxyCache clientProxyCache;
- // TODO This could be a static method?
private ClientProxyFactory clientProxyFactory;
+ private List<Bean<?>> beans;
- private Set<Bean<?>> beans;
-
public ManagerImpl()
{
initEnabledDeploymentTypes(null);
initContexts(null);
this.modelManager = new ModelManager();
this.ejbLookupManager = new EjbManager();
- this.beans = new HashSet<Bean<?>>();
+ this.beans = new ArrayList<Bean<?>>();
this.eventBus = new EventBus();
this.resolutionManager = new ResolutionManager(this);
this.clientProxyCache = new ClientProxyCache();
@@ -137,6 +135,10 @@
public Manager addBean(Bean<?> bean)
{
+ if (beans.contains(bean))
+ {
+ return this;
+ }
getResolutionManager().clear();
beans.add(bean);
return this;
@@ -201,9 +203,17 @@
return resolutionManager;
}
+ public Bean<?> getBean(int beanIndex)
+ {
+ return beans.get(beanIndex);
+ }
+
public Set<Bean<? extends Object>> getBeans()
{
- return beans;
+ // TODO List to Set?
+ Set<Bean<?>> beanSet = new HashSet<Bean<?>>();
+ beanSet.addAll(beans);
+ return beanSet;
}
public Manager addContext(Context context)
@@ -266,8 +276,6 @@
{
throw new ContextNotActiveException("No active contexts for " +
scopeType.getName());
}
- // TODO performance? Just flag found=true and continue loop, failing when
- // found=already true etc?
List<Context> activeContexts = new ArrayList<Context>();
for (Context context : contexts)
{
@@ -294,7 +302,6 @@
contextMap.getDependentContext().setActive(true);
if (getModelManager().getScopeModel(bean.getScopeType()).isNormal())
{
- // TODO What *really* to proxy? The bean? The instance?
return (T) getClientProxy(bean);
}
else
@@ -396,10 +403,12 @@
Object clientProxy = clientProxyCache.get(bean);
if (clientProxy == null)
{
- try
+ try
{
- clientProxy = clientProxyFactory.createClientProxy(bean);
- } catch (Exception e) {
+ clientProxy = clientProxyFactory.createClientProxy(bean,
beans.indexOf(bean));
+ }
+ catch (Exception e)
+ {
// TODO: What to *really* do here?
throw new UnproxyableDependencyException("Could not create client proxy
for " + bean.getName(), e);
}
Modified:
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/introspector/SimpleAnnotatedItem.java
===================================================================
---
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/introspector/SimpleAnnotatedItem.java 2008-11-02
18:58:32 UTC (rev 215)
+++
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/introspector/SimpleAnnotatedItem.java 2008-11-02
20:24:21 UTC (rev 216)
@@ -10,7 +10,7 @@
public class SimpleAnnotatedItem<T, S> extends AbstractAnnotatedItem<T, S>
{
- private Type[] actualTypeArguements = new Type[0];
+ private Type[] actualTypeArguments = new Type[0];
private Class<? extends T> type;
private Annotation[] actualAnnotations;
@@ -31,14 +31,14 @@
this.type = apiType.getRawType();
if (apiType.getType() instanceof ParameterizedType)
{
- actualTypeArguements = ((ParameterizedType)
apiType.getType()).getActualTypeArguments();
+ actualTypeArguments = ((ParameterizedType)
apiType.getType()).getActualTypeArguments();
}
}
private SimpleAnnotatedItem(Map<Class<? extends Annotation>, Annotation>
annotationMap, Class<? extends T> type, Type[] actualTypeArguements)
{
this(annotationMap, type);
- this.actualTypeArguements = actualTypeArguements;
+ this.actualTypeArguments = actualTypeArguements;
}
public SimpleAnnotatedItem(Annotation[] annotations)
@@ -77,7 +77,7 @@
public Type[] getActualTypeArguements()
{
- return actualTypeArguements;
+ return actualTypeArguments;
}
public Annotation[] getActualAnnotations()
Modified:
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/util/ClientProxyFactory.java
===================================================================
---
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/util/ClientProxyFactory.java 2008-11-02
18:58:32 UTC (rev 215)
+++
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/util/ClientProxyFactory.java 2008-11-02
20:24:21 UTC (rev 216)
@@ -1,17 +1,14 @@
package org.jboss.webbeans.util;
import java.io.Serializable;
-import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
-import javassist.util.proxy.MethodHandler;
import javassist.util.proxy.ProxyFactory;
import javassist.util.proxy.ProxyObject;
import javax.webbeans.manager.Bean;
-import javax.webbeans.manager.Context;
import org.jboss.webbeans.ManagerImpl;
@@ -22,35 +19,7 @@
public ClientProxyFactory(ManagerImpl manager) {
this.manager = manager;
}
-
- public class ProxyMethodHandler implements MethodHandler, Serializable {
- private static final long serialVersionUID = -5391564935097267888L;
-
- private ManagerImpl manager;
- private Bean<?> bean;
-
- public ProxyMethodHandler(Bean<?> bean, ManagerImpl manager) {
- this.bean = bean;
- this.manager = manager;
- }
-
- public Object invoke(Object self, Method method, Method proceed, Object[] args)
throws Throwable
- {
- Context context = manager.getContext(bean.getScopeType());
- Object proxiedInstance = context.get(bean, true);
- Method proxiedMethod = proxiedInstance.getClass().getMethod(method.getName(),
method.getParameterTypes());
- return proxiedMethod.invoke(proxiedInstance, args);
- }
-
- }
-
- private Class<?>[] addSerializableInterface(Class<?> clazz) {
- // TODO Doesn't compile Class<?>[] interfaces =
Arrays.copyOf(clazz.getInterfaces(), clazz.getInterfaces().length + 1);
- Class[] interfaces = new Class[0];
- interfaces[interfaces.length] = Serializable.class;
- return interfaces;
- }
-
+
private class TypeInfo {
Class<?>[] interfaces;
Class<?> superclass;
@@ -73,13 +42,13 @@
return typeInfo;
}
- public <T> T createClientProxy(Bean<T> bean) throws
InstantiationException, IllegalAccessException {
+ public <T> T createClientProxy(Bean<T> bean, int beanIndex) throws
InstantiationException, IllegalAccessException {
ProxyFactory proxyFactory = new ProxyFactory();
TypeInfo typeInfo = getTypeInfo(bean.getTypes());
proxyFactory.setInterfaces(typeInfo.interfaces);
proxyFactory.setSuperclass(typeInfo.superclass);
T clientProxy = (T) proxyFactory.createClass().newInstance();
- ProxyMethodHandler proxyMethodHandler = new ProxyMethodHandler(bean, manager);
+ ProxyMethodHandler proxyMethodHandler = new ProxyMethodHandler(bean, beanIndex,
manager);
((ProxyObject)clientProxy).setHandler(proxyMethodHandler);
return clientProxy;
}
Added: ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/util/ProxyMethodHandler.java
===================================================================
--- ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/util/ProxyMethodHandler.java
(rev 0)
+++
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/util/ProxyMethodHandler.java 2008-11-02
20:24:21 UTC (rev 216)
@@ -0,0 +1,41 @@
+package org.jboss.webbeans.util;
+
+import java.io.Serializable;
+import java.lang.reflect.Method;
+
+import javassist.util.proxy.MethodHandler;
+
+import javax.webbeans.manager.Bean;
+import javax.webbeans.manager.Context;
+
+import org.jboss.webbeans.ManagerImpl;
+
+public class ProxyMethodHandler implements MethodHandler, Serializable
+{
+ private static final long serialVersionUID = -5391564935097267888L;
+
+ private transient Bean<?> bean;
+ private int beanIndex;
+ private static ManagerImpl manager;
+
+ public ProxyMethodHandler(Bean<?> bean, int beanIndex, ManagerImpl manager)
+ {
+ this.bean = bean;
+ this.beanIndex = beanIndex;
+ ProxyMethodHandler.manager = manager;
+ }
+
+ @Override
+ public Object invoke(Object self, Method method, Method proceed, Object[] args) throws
Throwable
+ {
+ if (bean == null)
+ {
+ bean = manager.getBean(beanIndex);
+ }
+ Context context = manager.getContext(bean.getScopeType());
+ Object proxiedInstance = context.get(bean, true);
+ Method proxiedMethod = proxiedInstance.getClass().getMethod(method.getName(),
method.getParameterTypes());
+ return proxiedMethod.invoke(proxiedInstance, args);
+ }
+
+}
Modified: ri/trunk/webbeans-ri/src/test/java/org/jboss/webbeans/test/ClientProxyTest.java
===================================================================
---
ri/trunk/webbeans-ri/src/test/java/org/jboss/webbeans/test/ClientProxyTest.java 2008-11-02
18:58:32 UTC (rev 215)
+++
ri/trunk/webbeans-ri/src/test/java/org/jboss/webbeans/test/ClientProxyTest.java 2008-11-02
20:24:21 UTC (rev 216)
@@ -1,53 +1,107 @@
package org.jboss.webbeans.test;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+
import javax.webbeans.UnproxyableDependencyException;
+import javax.webbeans.manager.Bean;
+import org.jboss.webbeans.test.beans.Fox;
+import org.jboss.webbeans.test.beans.Tuna;
+import org.jboss.webbeans.test.util.Util;
import org.testng.annotations.Test;
@SpecVersion("PDR")
-public class ClientProxyTest
+public class ClientProxyTest extends AbstractTest
{
-
- @Test(groups="clientProxy") @SpecAssertion(section={"4.4",
"4.8"})
+ @Test(groups = "clientProxy")
+ @SpecAssertion(section = { "4.4", "4.8" })
public void testClientProxyUsedForNormalScope()
{
- assert false;
+ Bean<Tuna> tunaBean = Util.createSimpleWebBean(Tuna.class, manager);
+ Tuna tuna = manager.getInstance(tunaBean);
+ assert tuna.getClass().getName().indexOf("$$_javassist_") > 0;
}
-
- @Test(groups="clientProxy") @SpecAssertion(section={"4.4",
"4.8"})
+
+ @Test(groups = "clientProxy")
+ @SpecAssertion(section = { "4.4", "4.8" })
public void testClientProxyNotUsedForPseudoScope()
{
- assert false;
+ Bean<Fox> foxBean = Util.createSimpleWebBean(Fox.class, manager);
+ Fox fox = manager.getInstance(foxBean);
+ assert fox.getClass() == Fox.class;
}
-
- @Test(groups="clientProxy") @SpecAssertion(section="4.4")
+
+ @Test(groups = "clientProxy")
+ @SpecAssertion(section = "4.4")
public void testClientProxyIsSerializable()
{
- assert false;
+ Bean<Tuna> tunaBean = Util.createSimpleWebBean(Tuna.class, manager);
+ manager.addBean(tunaBean);
+ Tuna tuna = manager.getInstance(tunaBean);
+ ByteArrayOutputStream bytes = new ByteArrayOutputStream();
+ ObjectOutputStream out = null;
+ ObjectInputStream in = null;
+ try
+ {
+ out = new ObjectOutputStream(bytes);
+ out.writeObject(tuna);
+ out.flush();
+ byte[] data = bytes.toByteArray();
+ in = new ObjectInputStream(new ByteArrayInputStream(data));
+ tuna = (Tuna) in.readObject();
+ assert tuna.getState().equals("tuned");
+ }
+ catch (Exception e)
+ {
+ e.printStackTrace();
+ assert false;
+ }
+ finally
+ {
+ try
+ {
+ if (bytes != null)
+ {
+ bytes.close();
+ }
+ if (out != null)
+ {
+ out.close();
+ }
+ if (in != null)
+ {
+ in.close();
+ }
+ }
+ catch (IOException e)
+ {
+ e.printStackTrace();
+ }
+ }
+ assert true;
}
-
- @Test(groups="clientProxy",
expectedExceptions=UnproxyableDependencyException.class)
@SpecAssertion(section="4.4.1")
+
+ @Test(groups = "clientProxy", expectedExceptions =
UnproxyableDependencyException.class)
+ @SpecAssertion(section = "4.4.1")
public void testInjectionPointWithUnproxyableTypeWhichResolvesToNormalScopedWebBean()
{
assert false;
}
-
- @Test(groups="clientProxy") @SpecAssertion(section="4.4.2")
+
+ @Test(groups = "clientProxy")
+ @SpecAssertion(section = "4.4.2")
public void testClientProxyInvocation()
{
- assert false;
+ Bean<Tuna> tunaBean = Util.createSimpleWebBean(Tuna.class, manager);
+ manager.addBean(tunaBean);
+ Tuna tuna = manager.getInstance(tunaBean);
+ assert tuna.getClass().getName().indexOf("$$_javassist_") > 0;
+ assert tuna.getState().equals("tuned");
}
-
- /*
- @Test(groups="clientProxy") @SpecAssertion(section="4.4")
- public void test
- {
- assert false;
- }
-
- */
-
-
}
Modified: ri/trunk/webbeans-ri/src/test/java/org/jboss/webbeans/test/beans/Tuna.java
===================================================================
--- ri/trunk/webbeans-ri/src/test/java/org/jboss/webbeans/test/beans/Tuna.java 2008-11-02
18:58:32 UTC (rev 215)
+++ ri/trunk/webbeans-ri/src/test/java/org/jboss/webbeans/test/beans/Tuna.java 2008-11-02
20:24:21 UTC (rev 216)
@@ -8,5 +8,8 @@
@RequestScoped
public class Tuna
{
+ public String getState() {
+ return "tuned";
+ }
}