Author: pete.muir(a)jboss.org
Date: 2009-10-14 10:20:50 -0400 (Wed, 14 Oct 2009)
New Revision: 4037
Added:
extensions/trunk/wicket/src/main/java/org/jboss/weld/wicket/util/
extensions/trunk/wicket/src/main/java/org/jboss/weld/wicket/util/NonContextual.java
Removed:
extensions/trunk/wicket/src/main/resources/META-INF/beans.xml
Modified:
extensions/trunk/wicket/src/main/java/org/jboss/weld/wicket/BeanManagerLookup.java
extensions/trunk/wicket/src/main/java/org/jboss/weld/wicket/WeldApplication.java
extensions/trunk/wicket/src/main/java/org/jboss/weld/wicket/WeldComponentInstantiationListener.java
extensions/trunk/wicket/src/main/java/org/jboss/weld/wicket/WeldMetaData.java
Log:
Don't make classes beans, but use NonContextual injection
Modified:
extensions/trunk/wicket/src/main/java/org/jboss/weld/wicket/BeanManagerLookup.java
===================================================================
---
extensions/trunk/wicket/src/main/java/org/jboss/weld/wicket/BeanManagerLookup.java 2009-10-14
14:19:37 UTC (rev 4036)
+++
extensions/trunk/wicket/src/main/java/org/jboss/weld/wicket/BeanManagerLookup.java 2009-10-14
14:20:50 UTC (rev 4037)
@@ -21,7 +21,8 @@
* This is the spec-defined name for the bean manager as registered in JNDI
* See JSR-299 11.3
*/
- private static String beanManagerJndiName = "java:comp/BeanManager";
+ // TODO Change to java:comp/BeanManager when JBoss AS supports it
+ private static String beanManagerJndiName = "java:app/BeanManager";
public static void setBeanManagerJndiName(String beanManagerJndiName)
{
Modified:
extensions/trunk/wicket/src/main/java/org/jboss/weld/wicket/WeldApplication.java
===================================================================
---
extensions/trunk/wicket/src/main/java/org/jboss/weld/wicket/WeldApplication.java 2009-10-14
14:19:37 UTC (rev 4036)
+++
extensions/trunk/wicket/src/main/java/org/jboss/weld/wicket/WeldApplication.java 2009-10-14
14:20:50 UTC (rev 4037)
@@ -1,13 +1,5 @@
package org.jboss.weld.wicket;
-import java.lang.reflect.Type;
-import java.util.Set;
-
-import javax.enterprise.inject.AmbiguousResolutionException;
-import javax.enterprise.inject.UnsatisfiedResolutionException;
-import javax.enterprise.inject.spi.Bean;
-import javax.enterprise.inject.spi.BeanManager;
-
import org.apache.wicket.Request;
import org.apache.wicket.RequestCycle;
import org.apache.wicket.Response;
@@ -15,6 +7,7 @@
import org.apache.wicket.protocol.http.WebRequest;
import org.apache.wicket.protocol.http.WebResponse;
import org.apache.wicket.request.IRequestCycleProcessor;
+import org.jboss.weld.wicket.util.NonContextual;
/**
* A convenience subclass of wicket's WebApplication which adds the hooks
@@ -27,6 +20,7 @@
* Weld, or your subclasses of those classes.
*
* @author cpopetz
+ * @author pmuir
*
* @see WebApplication
* @see WeldWebRequestCycleProcessor
@@ -35,30 +29,15 @@
public abstract class WeldApplication extends WebApplication
{
- private <T> T getInstanceByType(Class<T> beanType)
- {
- BeanManager manager = BeanManagerLookup.getBeanManager();
- Bean<T> bean = (Bean<T>) ensureUniqueBean(beanType,
manager.getBeans(beanType));
- return (T) manager.getReference(bean, beanType,
manager.createCreationalContext(bean));
- }
-
- private static Bean<?> ensureUniqueBean(Type type, Set<Bean<?>>
beans)
- {
- if (beans.size() == 0)
- {
- throw new UnsatisfiedResolutionException("Unable to resolve any Web Beans
of " + type);
- }
- else if (beans.size() > 1)
- {
- throw new AmbiguousResolutionException("More than one bean available for
type " + type);
- }
- return beans.iterator().next();
- }
+ private final NonContextual<WeldComponentInstantiationListener>
weldComponentInstantiationListener;
+ private final NonContextual<WeldWebRequestCycleProcessor>
weldWebRequestCycleProcessor;
/**
*/
public WeldApplication()
{
+ this.weldComponentInstantiationListener = new
NonContextual<WeldComponentInstantiationListener>(BeanManagerLookup.getBeanManager(),
WeldComponentInstantiationListener.class);
+ this.weldWebRequestCycleProcessor = new
NonContextual<WeldWebRequestCycleProcessor>(BeanManagerLookup.getBeanManager(),
WeldWebRequestCycleProcessor.class);
}
/**
@@ -66,10 +45,11 @@
*
* @see WeldComponentInstantiationListener
*/
+ @Override
protected void internalInit()
{
super.internalInit();
-
addComponentInstantiationListener(getInstanceByType(WeldComponentInstantiationListener.class));
+
addComponentInstantiationListener(weldComponentInstantiationListener.newInstance().produce().inject().get());
}
@@ -81,7 +61,7 @@
@Override
protected IRequestCycleProcessor newRequestCycleProcessor()
{
- return getInstanceByType(WeldWebRequestCycleProcessor.class);
+ return weldWebRequestCycleProcessor.newInstance().produce().inject().get();
}
/**
Modified:
extensions/trunk/wicket/src/main/java/org/jboss/weld/wicket/WeldComponentInstantiationListener.java
===================================================================
---
extensions/trunk/wicket/src/main/java/org/jboss/weld/wicket/WeldComponentInstantiationListener.java 2009-10-14
14:19:37 UTC (rev 4036)
+++
extensions/trunk/wicket/src/main/java/org/jboss/weld/wicket/WeldComponentInstantiationListener.java 2009-10-14
14:20:50 UTC (rev 4037)
@@ -5,6 +5,7 @@
import org.apache.wicket.Component;
import org.apache.wicket.application.IComponentInstantiationListener;
+import org.jboss.weld.wicket.util.NonContextual;
/**
* This listener uses the BeanManager to handle injections for all wicket components.
@@ -14,8 +15,9 @@
*/
public class WeldComponentInstantiationListener implements
IComponentInstantiationListener
{
+
@Inject
- BeanManager manager;
+ private BeanManager manager;
public void onInstantiation(Component component)
{
@@ -24,8 +26,8 @@
*/
if (manager != null)
{
- manager.createInjectionTarget(manager.createAnnotatedType((Class)
component.getClass()))
- .inject(component, manager.createCreationalContext(null));
+ // TODO Cache the NonContextual!
+ new NonContextual<Component>(manager,
component.getClass()).existingInstance(component).inject();
}
}
}
\ No newline at end of file
Modified: extensions/trunk/wicket/src/main/java/org/jboss/weld/wicket/WeldMetaData.java
===================================================================
---
extensions/trunk/wicket/src/main/java/org/jboss/weld/wicket/WeldMetaData.java 2009-10-14
14:19:37 UTC (rev 4036)
+++
extensions/trunk/wicket/src/main/java/org/jboss/weld/wicket/WeldMetaData.java 2009-10-14
14:20:50 UTC (rev 4037)
@@ -16,7 +16,7 @@
* This is the key we will use to to store the conversation metadata in the
* wicket page.
*/
- public static final MetaDataKey CID = new MetaDataKey<String>()
+ public static final MetaDataKey<String> CID = new MetaDataKey<String>()
{
private static final long serialVersionUID = -8788010688731927318L;
};
Added:
extensions/trunk/wicket/src/main/java/org/jboss/weld/wicket/util/NonContextual.java
===================================================================
--- extensions/trunk/wicket/src/main/java/org/jboss/weld/wicket/util/NonContextual.java
(rev 0)
+++
extensions/trunk/wicket/src/main/java/org/jboss/weld/wicket/util/NonContextual.java 2009-10-14
14:20:50 UTC (rev 4037)
@@ -0,0 +1,184 @@
+package org.jboss.weld.wicket.util;
+
+import javax.enterprise.context.spi.CreationalContext;
+import javax.enterprise.inject.spi.AnnotatedType;
+import javax.enterprise.inject.spi.BeanManager;
+import javax.enterprise.inject.spi.InjectionTarget;
+
+/**
+ * Perform production, injection, lifecycle callbacks and cleanup on a non-contextual
object
+ *
+ * @author pmuir
+ *
+ * @param <T>
+ */
+public class NonContextual<T>
+{
+
+ // Store the injection target. The CDI spec doesn't require an implementation
+ // to cache it, so we do
+ private final InjectionTarget<? extends T> injectionTarget;
+
+ // Store a reference to the CDI BeanManager
+ private final BeanManager beanManager;
+
+ /**
+ * Create an injector for the given class
+ */
+ public NonContextual(BeanManager manager, Class<? extends T> clazz)
+ {
+ this.beanManager = manager;
+
+ // Generate an "Annotated Type"
+ AnnotatedType<? extends T> type = manager.createAnnotatedType(clazz);
+
+ // Generate the InjectionTarget
+ this.injectionTarget = manager.createInjectionTarget(type);
+ }
+
+ public Instance<T> newInstance()
+ {
+ return new Instance<T>(beanManager, (InjectionTarget<T>)
injectionTarget);
+ }
+
+ public Instance<T> existingInstance(T instance)
+ {
+ return new Instance<T>(beanManager, (InjectionTarget<T>)
injectionTarget, instance);
+ }
+
+ /**
+ * Represents a non-contextual instance
+ *
+ * @author pmuir
+ *
+ * @param <T>
+ */
+ public static class Instance<T>
+ {
+
+ private final CreationalContext<T> ctx;
+ private final InjectionTarget<T> injectionTarget;
+ private T instance;
+ private boolean disposed = false;
+
+ private Instance(BeanManager beanManager, InjectionTarget<T>
injectionTarget)
+ {
+ this.injectionTarget = injectionTarget;
+ this.ctx = beanManager.createCreationalContext(null);
+ }
+
+ private Instance(BeanManager beanManager, InjectionTarget<T> injectionTarget,
T instance)
+ {
+ this.injectionTarget = injectionTarget;
+ this.ctx = beanManager.createCreationalContext(null);
+ this.instance = instance;
+ }
+
+ /**
+ * Get the instance
+ *
+ * @return
+ */
+ public T get()
+ {
+ return instance;
+ }
+
+ /**
+ * Create the instance
+ *
+ * @return
+ */
+ public Instance<T> produce()
+ {
+ if (this.instance != null)
+ {
+ throw new IllegalStateException("Trying to call produce() on already
constructed instance");
+ }
+ if (disposed)
+ {
+ throw new IllegalStateException("Trying to call produce() on an already
disposed instance");
+ }
+ this.instance = injectionTarget.produce(ctx);
+ return this;
+ }
+
+ /**
+ * Inject the instance
+ *
+ * @return
+ */
+ public Instance<T> inject()
+ {
+ if (this.instance == null)
+ {
+ throw new IllegalStateException("Trying to call inject() before
produce() was called");
+ }
+ if (disposed)
+ {
+ throw new IllegalStateException("Trying to call inject() on already
disposed instance");
+ }
+ injectionTarget.inject(instance, ctx);
+ return this;
+ }
+
+ /**
+ * Call the @PostConstruct callback
+ *
+ * @return
+ */
+ public Instance<T> postConstruct()
+ {
+ if (this.instance == null)
+ {
+ throw new IllegalStateException("Trying to call postConstruct() before
produce() was called");
+ }
+ if (disposed)
+ {
+ throw new IllegalStateException("Trying to call preDestroy() on already
disposed instance");
+ }
+ injectionTarget.postConstruct(instance);
+ return this;
+ }
+
+ /**
+ * Call the @PreDestroy callback
+ *
+ * @return
+ */
+ public Instance<T> preDestroy()
+ {
+ if (this.instance == null)
+ {
+ throw new IllegalStateException("Trying to call preDestroy() before
produce() was called");
+ }
+ if (disposed)
+ {
+ throw new IllegalStateException("Trying to call preDestroy() on already
disposed instance");
+ }
+ injectionTarget.preDestroy(instance);
+ return this;
+ }
+
+ /**
+ * Dispose of the instance, doing any necessary cleanup
+ *
+ */
+ public Instance<T> dispose()
+ {
+ if (this.instance == null)
+ {
+ throw new IllegalStateException("Trying to call dispose() before
produce() was called");
+ }
+ if (disposed)
+ {
+ throw new IllegalStateException("Trying to call dispose() on already
disposed instance");
+ }
+ injectionTarget.dispose(instance);
+ ctx.release();
+ return this;
+ }
+
+ }
+
+}
\ No newline at end of file
Property changes on:
extensions/trunk/wicket/src/main/java/org/jboss/weld/wicket/util/NonContextual.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Name: svn:eol-style
+ native
Deleted: extensions/trunk/wicket/src/main/resources/META-INF/beans.xml
===================================================================