Author: nickarls
Date: 2008-10-30 01:32:02 -0400 (Thu, 30 Oct 2008)
New Revision: 208
Modified:
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/ManagerImpl.java
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/util/ClientProxyFactory.java
Log:
Client proxy changes
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-10-29
14:03:54 UTC (rev 207)
+++ ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/ManagerImpl.java 2008-10-30
05:32:02 UTC (rev 208)
@@ -60,16 +60,16 @@
}
}
- private class ProxyPool extends MapWrapper<Bean<?>, Bean<?>>
+ private class ClientProxyCache extends MapWrapper<Bean<?>, Object>
{
- public ProxyPool()
+ public ClientProxyCache()
{
- super(new HashMap<Bean<?>, Bean<?>>());
+ super(new HashMap<Bean<?>, Object>());
}
- public Bean<?> get(Bean<?> key)
+ public <T> T get(Bean<T> key)
{
- return (Bean<?>) super.get(key);
+ return (T) super.get(key);
}
}
@@ -79,7 +79,7 @@
private EventBus eventBus;
private ResolutionManager resolutionManager;
private ContextMap contextMap;
- private ProxyPool proxyPool;
+ private ClientProxyCache clientProxyCache;
// TODO This could be a static method?
private ClientProxyFactory clientProxyFactory;
@@ -94,7 +94,7 @@
this.beans = new HashSet<Bean<?>>();
this.eventBus = new EventBus();
this.resolutionManager = new ResolutionManager(this);
- this.proxyPool = new ProxyPool();
+ this.clientProxyCache = new ClientProxyCache();
clientProxyFactory = new ClientProxyFactory(this);
}
@@ -295,7 +295,7 @@
if (getModelManager().getScopeModel(bean.getScopeType()).isNormal())
{
// TODO What *really* to proxy? The bean? The instance?
- return (T) getClientProxy(bean).create();
+ return (T) getClientProxy(bean);
}
else
{
@@ -391,19 +391,19 @@
return null;
}
- private Bean<?> getClientProxy(Bean<?> bean)
+ private Object getClientProxy(Bean<?> bean)
{
- Bean<?> clientProxy = proxyPool.get(bean);
+ Object clientProxy = clientProxyCache.get(bean);
if (clientProxy == null)
{
try
{
- clientProxy = clientProxyFactory.createProxyClient(bean);
+ clientProxy = clientProxyFactory.createClientProxy(bean);
} catch (Exception e) {
// TODO: What to *really* do here?
- throw new UnproxyableDependencyException("Could not create proxy",
e);
+ throw new UnproxyableDependencyException("Could not create client proxy
for " + bean.getName(), e);
}
- proxyPool.put(bean, clientProxy);
+ clientProxyCache.put(bean, clientProxy);
}
return clientProxy;
}
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-10-29
14:03:54 UTC (rev 207)
+++
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/util/ClientProxyFactory.java 2008-10-30
05:32:02 UTC (rev 208)
@@ -12,7 +12,6 @@
import org.jboss.webbeans.ManagerImpl;
import org.jboss.webbeans.test.beans.Tuna;
-import org.jboss.webbeans.test.mock.MockManagerImpl;
import org.jboss.webbeans.test.util.Util;
public class ClientProxyFactory
@@ -25,33 +24,32 @@
public class ProxyMethodHandler implements MethodHandler {
private ManagerImpl manager;
+ private Bean<?> bean;
- public ProxyMethodHandler(ManagerImpl manager) {
+ public ProxyMethodHandler(Bean<?> bean, ManagerImpl manager) {
+ this.bean = bean;
this.manager = manager;
}
@Override
public Object invoke(Object self, Method method, Method proceed, Object[] args)
throws Throwable
{
- Bean<?> bean = (Bean<?>)self;
- Class<? extends Annotation> beanScope = bean.getScopeType();
- Context context = manager.getContext(beanScope);
- Object instance = context.get(bean, true);
- return proceed.invoke(instance, args);
-// Oneliner ;-)
-// return
proceed.invoke(manager.getContext(((Bean<?>)self).getScopeType()).get(bean, true),
args);
+ 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);
}
-
+
}
- public Bean<?> createProxyClient(Bean<?> bean) throws
InstantiationException, IllegalAccessException {
- ProxyFactory factory = new ProxyFactory();
- factory.setSuperclass(bean.getClass());
- Class<?> proxyClass = factory.createClass();
- Object proxy = proxyClass.newInstance();
- ProxyMethodHandler proxyMethodHandler = new ProxyMethodHandler(manager);
- ((ProxyObject)proxy).setHandler(proxyMethodHandler);
- return (Bean<?>) proxy;
+ public <T> T createClientProxy(Bean<T> bean) throws
InstantiationException, IllegalAccessException {
+ ProxyFactory proxyFactory = new ProxyFactory();
+ // TODO How to get the type T from a bean?
+ proxyFactory.setSuperclass(bean.getTypes().toArray()[0].getClass());
+ T clientProxy = (T) proxyFactory.createClass().newInstance();
+ ProxyMethodHandler proxyMethodHandler = new ProxyMethodHandler(bean, manager);
+ ((ProxyObject)clientProxy).setHandler(proxyMethodHandler);
+ return clientProxy;
}
-
+
}