Author: nickarls
Date: 2008-10-29 03:24:34 -0400 (Wed, 29 Oct 2008)
New Revision: 203
Added:
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/ProxyData.java
Modified:
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/ManagerImpl.java
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/util/ClientProxyUtil.java
Log:
more client proxy theories
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-28
23:07:14 UTC (rev 202)
+++ ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/ManagerImpl.java 2008-10-29
07:24:34 UTC (rev 203)
@@ -42,7 +42,6 @@
{
private class ContextMap extends MapWrapper<Class<? extends Annotation>,
List<Context>>
{
-
public ContextMap()
{
super(new HashMap<Class<? extends Annotation>,
List<Context>>());
@@ -52,25 +51,21 @@
{
return (List<Context>) super.get(key);
}
-
}
- private class ProxyPool extends MapWrapper<Bean<?>, ClientProxy<?>>
+ private class ProxyPool extends MapWrapper<Bean<?>, Bean<?>>
{
-
public ProxyPool()
{
- super(new HashMap<Bean<?>, ClientProxy<?>>());
+ super(new HashMap<Bean<?>, Bean<?>>());
}
- public ClientProxy<?> get(Bean<?> key)
+ public Bean<?> get(Bean<?> key)
{
- return (ClientProxy<?>) super.get(key);
+ return (Bean<?>) super.get(key);
}
+ }
- }
-
-
private List<Class<? extends Annotation>> enabledDeploymentTypes;
private ModelManager modelManager;
private EjbManager ejbLookupManager;
@@ -111,14 +106,14 @@
}
}
}
-
- protected void initContexts(Context ... contexts)
+
+ protected void initContexts(Context... contexts)
{
this.contextMap = new ContextMap();
if (contexts == null)
{
-
- this.dependentContext = new DependentContext();
+
+ this.dependentContext = new DependentContext();
addContext(dependentContext);
addContext(new RequestContext());
addContext(new SessionContext());
@@ -236,20 +231,22 @@
public <T> Manager addObserver(Observer<T> observer, TypeLiteral<T>
eventType, Annotation... bindings)
{
- // TODO Using the eventType TypeLiteral<T>, the Class<T> object must be
retrieved
- this.eventBus.addObserver(observer,
(Class<T>)Reflections.getActualTypeArguements(eventType.getClass())[0], bindings);
+ // TODO Using the eventType TypeLiteral<T>, the Class<T> object must
be
+ // retrieved
+ this.eventBus.addObserver(observer, (Class<T>)
Reflections.getActualTypeArguements(eventType.getClass())[0], bindings);
return this;
}
public void fireEvent(Object event, Annotation... bindings)
{
- // Check the event object for template parameters which are not allowed by the
spec.
+ // Check the event object for template parameters which are not allowed by
+ // the spec.
if (Reflections.isParameterizedType(event.getClass()))
{
- throw new IllegalArgumentException("Event type " +
event.getClass().getName() +
- " is not allowed because it is a generic");
+ throw new IllegalArgumentException("Event type " +
event.getClass().getName() + " is not allowed because it is a generic");
}
- // Get the observers for this event. Although resolveObservers is parameterized,
this
+ // Get the observers for this event. Although resolveObservers is
+ // parameterized, this
// method is not, so we have to use Observer<Object> for observers.
Set<Observer<Object>> observers = this.resolveObservers(event,
bindings);
this.eventBus.notifyObservers(observers, event);
@@ -290,7 +287,7 @@
dependentContext.setActive(true);
if (getModelManager().getScopeModel(bean.getScopeType()).isNormal())
{
- return (T) getClientProxy(bean).getInstance();
+ return (T) getClientProxy(bean);
}
else
{
@@ -363,8 +360,9 @@
public <T> Manager removeObserver(Observer<T> observer,
TypeLiteral<T> eventType, Annotation... bindings)
{
- // TODO The Class<T> for the event type must be retrieved from the
TypeLiteral<T> instance
- this.eventBus.removeObserver(observer,
(Class<T>)Reflections.getActualTypeArguements(eventType.getClass())[0], bindings);
+ // TODO The Class<T> for the event type must be retrieved from the
+ // TypeLiteral<T> instance
+ this.eventBus.removeObserver(observer, (Class<T>)
Reflections.getActualTypeArguements(eventType.getClass())[0], bindings);
return this;
}
@@ -384,17 +382,21 @@
// TODO Auto-generated method stub
return null;
}
-
- private ClientProxy<?> getClientProxy(Bean<?> bean)
+
+ private Bean<?> getClientProxy(Bean<?> bean)
{
- ClientProxy<?> clientProxy = proxyPool.get(bean);
+ Bean<?> clientProxy = proxyPool.get(bean);
if (clientProxy == null)
{
- clientProxy = new ClientProxy(bean, this);
+ try
+ {
+ clientProxy = ClientProxyUtil.createProxy(new ProxyData(bean, this));
+ } catch (Exception e) {
+ throw new UnproxyableDependencyException("Could not create proxy",
e);
+ }
proxyPool.put(bean, clientProxy);
}
return clientProxy;
}
-
}
Copied: ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/ProxyData.java (from rev
201, ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/ClientProxy.java)
===================================================================
--- ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/ProxyData.java
(rev 0)
+++ ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/ProxyData.java 2008-10-29
07:24:34 UTC (rev 203)
@@ -0,0 +1,27 @@
+package org.jboss.webbeans;
+
+import javax.webbeans.manager.Bean;
+import javax.webbeans.manager.Manager;
+
+public class ProxyData
+{
+ private Bean<?> bean;
+ private Manager manager;
+
+ public ProxyData(Bean<?> bean, Manager manager)
+ {
+ this.bean = bean;
+ this.manager = manager;
+ }
+
+ public Bean<?> getBean()
+ {
+ return bean;
+ }
+
+ public Manager getManager()
+ {
+ return manager;
+ }
+
+}
Property changes on: ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/ProxyData.java
___________________________________________________________________
Name: svn:mergeinfo
+
Modified: ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/util/ClientProxyUtil.java
===================================================================
---
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/util/ClientProxyUtil.java 2008-10-28
23:07:14 UTC (rev 202)
+++
ri/trunk/webbeans-ri/src/main/java/org/jboss/webbeans/util/ClientProxyUtil.java 2008-10-29
07:24:34 UTC (rev 203)
@@ -1,6 +1,19 @@
package org.jboss.webbeans.util;
+import java.lang.annotation.Annotation;
+import java.lang.reflect.Method;
+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 javax.webbeans.manager.Manager;
+
+import org.jboss.webbeans.ProxyData;
+
+
public class ClientProxyUtil
{
@@ -30,4 +43,27 @@
}
}
+ private static MethodHandler methodHandler = new MethodHandler()
+ {
+ public Object invoke(Object self, Method m, Method proceed, Object[] args) throws
Throwable
+ {
+ ProxyData proxyData = (ProxyData)self;
+ Manager manager = proxyData.getManager();
+ Class<? extends Annotation> beanScope =
proxyData.getBean().getScopeType();
+ Context context = manager.getContext(beanScope);
+ Object instance = context.get(proxyData.getBean(), true);
+ return proceed.invoke(instance, args);
+ }
+ };
+
+ public static Bean<?> createProxy(ProxyData clientProxy) throws
InstantiationException, IllegalAccessException
+ {
+ ProxyFactory proxyFactory = new ProxyFactory();
+ proxyFactory.setSuperclass(clientProxy.getClass());
+ Class<?> proxyClass = proxyFactory.createClass();
+ Bean<?> proxy = (Bean<?>) proxyClass.newInstance();
+ ((ProxyObject)proxy).setHandler(methodHandler);
+ return proxy;
+ }
+
}