Author: shane.bryzak(a)jboss.com
Date: 2008-07-03 23:27:53 -0400 (Thu, 03 Jul 2008)
New Revision: 44
Added:
ri/trunk/webbeans-impl/src/main/java/org/jboss/webbeans/BasicContext.java
Log:
Context implementation
Added: ri/trunk/webbeans-impl/src/main/java/org/jboss/webbeans/BasicContext.java
===================================================================
--- ri/trunk/webbeans-impl/src/main/java/org/jboss/webbeans/BasicContext.java
(rev 0)
+++ ri/trunk/webbeans-impl/src/main/java/org/jboss/webbeans/BasicContext.java 2008-07-04
03:27:53 UTC (rev 44)
@@ -0,0 +1,86 @@
+package org.jboss.webbeans;
+
+import java.lang.annotation.Annotation;
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.webbeans.ComponentInstance;
+import javax.webbeans.Container;
+import javax.webbeans.Context;
+
+/**
+ * Basic implementation of javax.webbeans.Context, backed by a HashMap
+ *
+ * @author Shane Bryzak
+ *
+ */
+public class BasicContext implements Context
+{
+ private Map<ComponentInstance<?>, Object> values;
+ private Class<Annotation> scopeType;
+
+ public BasicContext(Class<Annotation> scopeType)
+ {
+ this.scopeType = scopeType;
+ values = new HashMap<ComponentInstance<?>,Object>();
+ }
+
+ @SuppressWarnings("unchecked")
+ public <T> T get(Container container, ComponentInstance<T> component,
boolean create)
+ {
+ T instance = (T) values.get(component);
+ if (instance != null)
+ {
+ return instance;
+ }
+
+ if (!create)
+ {
+ return null;
+ }
+
+ // TODO should component creation be synchronized?
+
+ instance = component.create(container);
+
+ values.put(component, instance);
+ return instance;
+ }
+
+ public Class<Annotation> getScopeType()
+ {
+ return scopeType;
+ }
+
+ @SuppressWarnings("unchecked")
+ public <T> void remove(Container container, ComponentInstance<T>
component)
+ {
+ T instance = (T) values.get(component);
+
+ if (instance != null)
+ {
+ values.remove(component);
+ component.destroy(container, instance);
+ }
+ else
+ {
+ // TODO is this the correct exception to throw? See section 9.1 of spec
+ throw new RuntimeException("Component " + component.getName() + "
cannot be removed as it " +
+ "does not exist in [" + scopeType + "] context.");
+ }
+ }
+
+ @SuppressWarnings("unchecked")
+ public void destroy(Container container)
+ {
+ // TODO this method isn't declared by the interface, but is implied by section
9.1.2 of the spec
+
+ for (ComponentInstance c : values.keySet())
+ {
+ c.destroy(container, values.get(c));
+ }
+
+ values.clear();
+ }
+
+}
Show replies by date