tuscany-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jmar...@apache.org
Subject svn commit: r399943 - in /incubator/tuscany/sandbox/jboynes/sca/core2/src: main/java/org/apache/tuscany/core/context/event/ main/java/org/apache/tuscany/core/context/scope/ test/java/org/apache/tuscany/core/context/scope/
Date Fri, 05 May 2006 04:00:18 GMT
Author: jmarino
Date: Thu May  4 21:00:17 2006
New Revision: 399943

URL: http://svn.apache.org/viewcvs?rev=399943&view=rev
Log:
scope refactors and testcases

Added:
    incubator/tuscany/sandbox/jboynes/sca/core2/src/test/java/org/apache/tuscany/core/context/scope/BasicHttpSessionScopeTestCase.java
    incubator/tuscany/sandbox/jboynes/sca/core2/src/test/java/org/apache/tuscany/core/context/scope/BasicRequestScopeTestCase.java
Modified:
    incubator/tuscany/sandbox/jboynes/sca/core2/src/main/java/org/apache/tuscany/core/context/event/AbstractRequestEvent.java
    incubator/tuscany/sandbox/jboynes/sca/core2/src/main/java/org/apache/tuscany/core/context/event/RequestEnd.java
    incubator/tuscany/sandbox/jboynes/sca/core2/src/main/java/org/apache/tuscany/core/context/event/RequestEvent.java
    incubator/tuscany/sandbox/jboynes/sca/core2/src/main/java/org/apache/tuscany/core/context/event/RequestStart.java
    incubator/tuscany/sandbox/jboynes/sca/core2/src/main/java/org/apache/tuscany/core/context/scope/CompositeScopeContext.java
    incubator/tuscany/sandbox/jboynes/sca/core2/src/main/java/org/apache/tuscany/core/context/scope/HttpSessionScopeContext.java
    incubator/tuscany/sandbox/jboynes/sca/core2/src/main/java/org/apache/tuscany/core/context/scope/RequestScopeContext.java

Modified: incubator/tuscany/sandbox/jboynes/sca/core2/src/main/java/org/apache/tuscany/core/context/event/AbstractRequestEvent.java
URL: http://svn.apache.org/viewcvs/incubator/tuscany/sandbox/jboynes/sca/core2/src/main/java/org/apache/tuscany/core/context/event/AbstractRequestEvent.java?rev=399943&r1=399942&r2=399943&view=diff
==============================================================================
--- incubator/tuscany/sandbox/jboynes/sca/core2/src/main/java/org/apache/tuscany/core/context/event/AbstractRequestEvent.java
(original)
+++ incubator/tuscany/sandbox/jboynes/sca/core2/src/main/java/org/apache/tuscany/core/context/event/AbstractRequestEvent.java
Thu May  4 21:00:17 2006
@@ -20,21 +20,13 @@
  */
 public abstract class AbstractRequestEvent extends AbstractEvent implements RequestEvent
{
 
-    private Object id;
-
     /**
      * Creates a new event
      * @param source the source of the event
-     * @param id the id of the request associated with the event
      */
-     public AbstractRequestEvent(Object source, Object id) {
+     public AbstractRequestEvent(Object source) {
          super(source);
-         assert (id !=null): "Request id was null";
-         this.id = id;
      }
 
-    public Object getId(){
-        return id;
-    }
 
 }

Modified: incubator/tuscany/sandbox/jboynes/sca/core2/src/main/java/org/apache/tuscany/core/context/event/RequestEnd.java
URL: http://svn.apache.org/viewcvs/incubator/tuscany/sandbox/jboynes/sca/core2/src/main/java/org/apache/tuscany/core/context/event/RequestEnd.java?rev=399943&r1=399942&r2=399943&view=diff
==============================================================================
--- incubator/tuscany/sandbox/jboynes/sca/core2/src/main/java/org/apache/tuscany/core/context/event/RequestEnd.java
(original)
+++ incubator/tuscany/sandbox/jboynes/sca/core2/src/main/java/org/apache/tuscany/core/context/event/RequestEnd.java
Thu May  4 21:00:17 2006
@@ -23,10 +23,9 @@
     /**
      * Creates a new event
      * @param source the source of the event
-     * @param id the id of the completed/ended request
      */
-     public RequestEnd(Object source, Object id) {
-         super(source,id);
+     public RequestEnd(Object source) {
+         super(source);
      }
 
 

Modified: incubator/tuscany/sandbox/jboynes/sca/core2/src/main/java/org/apache/tuscany/core/context/event/RequestEvent.java
URL: http://svn.apache.org/viewcvs/incubator/tuscany/sandbox/jboynes/sca/core2/src/main/java/org/apache/tuscany/core/context/event/RequestEvent.java?rev=399943&r1=399942&r2=399943&view=diff
==============================================================================
--- incubator/tuscany/sandbox/jboynes/sca/core2/src/main/java/org/apache/tuscany/core/context/event/RequestEvent.java
(original)
+++ incubator/tuscany/sandbox/jboynes/sca/core2/src/main/java/org/apache/tuscany/core/context/event/RequestEvent.java
Thu May  4 21:00:17 2006
@@ -19,9 +19,4 @@
  */
 public interface RequestEvent {
 
-    /**
-     * Returns the id of the request the event is associated with
-     */
-      public Object getId();
-
 }

Modified: incubator/tuscany/sandbox/jboynes/sca/core2/src/main/java/org/apache/tuscany/core/context/event/RequestStart.java
URL: http://svn.apache.org/viewcvs/incubator/tuscany/sandbox/jboynes/sca/core2/src/main/java/org/apache/tuscany/core/context/event/RequestStart.java?rev=399943&r1=399942&r2=399943&view=diff
==============================================================================
--- incubator/tuscany/sandbox/jboynes/sca/core2/src/main/java/org/apache/tuscany/core/context/event/RequestStart.java
(original)
+++ incubator/tuscany/sandbox/jboynes/sca/core2/src/main/java/org/apache/tuscany/core/context/event/RequestStart.java
Thu May  4 21:00:17 2006
@@ -23,10 +23,9 @@
     /**
      * Creates a new event
      * @param source the source of the event
-     * @param id the id of the request being started
      */
-     public RequestStart(Object source, Object id) {
-         super(source,id);
+     public RequestStart(Object source) {
+         super(source);
      }
 
 }

Modified: incubator/tuscany/sandbox/jboynes/sca/core2/src/main/java/org/apache/tuscany/core/context/scope/CompositeScopeContext.java
URL: http://svn.apache.org/viewcvs/incubator/tuscany/sandbox/jboynes/sca/core2/src/main/java/org/apache/tuscany/core/context/scope/CompositeScopeContext.java?rev=399943&r1=399942&r2=399943&view=diff
==============================================================================
--- incubator/tuscany/sandbox/jboynes/sca/core2/src/main/java/org/apache/tuscany/core/context/scope/CompositeScopeContext.java
(original)
+++ incubator/tuscany/sandbox/jboynes/sca/core2/src/main/java/org/apache/tuscany/core/context/scope/CompositeScopeContext.java
Thu May  4 21:00:17 2006
@@ -57,6 +57,7 @@
     }
 
     public void stop() throws ScopeRuntimeException {
+        lifecycleState = STOPPED;
     }
 
     public void onEvent(Event event) {

Modified: incubator/tuscany/sandbox/jboynes/sca/core2/src/main/java/org/apache/tuscany/core/context/scope/HttpSessionScopeContext.java
URL: http://svn.apache.org/viewcvs/incubator/tuscany/sandbox/jboynes/sca/core2/src/main/java/org/apache/tuscany/core/context/scope/HttpSessionScopeContext.java?rev=399943&r1=399942&r2=399943&view=diff
==============================================================================
--- incubator/tuscany/sandbox/jboynes/sca/core2/src/main/java/org/apache/tuscany/core/context/scope/HttpSessionScopeContext.java
(original)
+++ incubator/tuscany/sandbox/jboynes/sca/core2/src/main/java/org/apache/tuscany/core/context/scope/HttpSessionScopeContext.java
Thu May  4 21:00:17 2006
@@ -33,7 +33,7 @@
     }
 
     public Scope getScope() {
-        return Scope.REQUEST;
+        return Scope.SESSION;
     }
 
     public void onEvent(Event event) {
@@ -60,6 +60,7 @@
         if (lifecycleState != UNINITIALIZED) {
             throw new IllegalStateException("Scope must be in UNINITIALIZED state [" + lifecycleState
+ "]");
         }
+        lifecycleState = RUNNING;
     }
 
     public synchronized void stop() {
@@ -75,10 +76,11 @@
 
     public InstanceContext getInstanceContext(AtomicContext context) throws TargetException
{
         Map<Object, InstanceContext> contextMap = contexts.get(context);
-        InstanceContext ctx = contextMap.get(workContext.getIdentifier(HTTP_IDENTIFIER));
+        Object key = workContext.getIdentifier(HTTP_IDENTIFIER);
+        InstanceContext ctx = contextMap.get(key);
         if (ctx == null) {
             ctx = context.createInstance();
-            contextMap.put(context, ctx);
+            contextMap.put(key, ctx);
         }
         return ctx;
     }
@@ -86,6 +88,9 @@
     private void shutdownInstances(Object key) {
         List<InstanceContext> destroyQueue = destroyQueues.get(key);
         if (destroyQueue != null) {
+            for (Map<Object, InstanceContext> map : contexts.values()) {
+                map.remove(key);
+            }
             for (InstanceContext ctx : destroyQueue) {
                 ctx.stop();
             }

Modified: incubator/tuscany/sandbox/jboynes/sca/core2/src/main/java/org/apache/tuscany/core/context/scope/RequestScopeContext.java
URL: http://svn.apache.org/viewcvs/incubator/tuscany/sandbox/jboynes/sca/core2/src/main/java/org/apache/tuscany/core/context/scope/RequestScopeContext.java?rev=399943&r1=399942&r2=399943&view=diff
==============================================================================
--- incubator/tuscany/sandbox/jboynes/sca/core2/src/main/java/org/apache/tuscany/core/context/scope/RequestScopeContext.java
(original)
+++ incubator/tuscany/sandbox/jboynes/sca/core2/src/main/java/org/apache/tuscany/core/context/scope/RequestScopeContext.java
Thu May  4 21:00:17 2006
@@ -22,7 +22,7 @@
 public class RequestScopeContext extends AbstractScopeContext<AtomicContext> {
 
     private final Map<AtomicContext, Map<Thread, InstanceContext>> contexts;
-    private final Map<Thread,List<InstanceContext>> destroyQueues;
+    private final Map<Thread, List<InstanceContext>> destroyQueues;
 
     public RequestScopeContext(WorkContext workContext) {
         super("Request Scope", workContext);
@@ -38,14 +38,14 @@
         checkInit();
         if (event instanceof RequestEnd) {
             checkInit();
-            shutdownInstances();
+            shutdownInstances(Thread.currentThread());
         } else if (event instanceof InstanceCreated) {
             checkInit();
             InstanceContext context = ((InstanceCreated) event).getContext();
             List<InstanceContext> destroyQueue = destroyQueues.get(Thread.currentThread());
-            if (destroyQueue == null){
+            if (destroyQueue == null) {
                 destroyQueue = new ArrayList<InstanceContext>();
-                destroyQueues.put(Thread.currentThread(),destroyQueue);
+                destroyQueues.put(Thread.currentThread(), destroyQueue);
             }
             synchronized (destroyQueue) {
                 destroyQueue.add(context);
@@ -57,6 +57,7 @@
         if (lifecycleState != UNINITIALIZED) {
             throw new IllegalStateException("Scope must be in UNINITIALIZED state [" + lifecycleState
+ "]");
         }
+        lifecycleState = RUNNING;
     }
 
     public synchronized void stop() {
@@ -79,11 +80,17 @@
         return ctx;
     }
 
-    private void shutdownInstances() {
-        List<InstanceContext> destroyQueue = destroyQueues.get(Thread.currentThread());
-        synchronized (destroyQueue) {
-            for (InstanceContext ctx : destroyQueue) {
-                ctx.stop();
+    private void shutdownInstances(Thread key) {
+        List<InstanceContext> destroyQueue = destroyQueues.remove(key);
+        if (destroyQueue != null && destroyQueue.size() > 0) {
+            synchronized (destroyQueue) {
+                Thread thread = Thread.currentThread();
+                for (Map<Thread, InstanceContext> map : contexts.values()) {
+                    map.remove(thread);
+                }
+                for (InstanceContext ctx : destroyQueue) {
+                    ctx.stop();
+                }
             }
         }
     }

Added: incubator/tuscany/sandbox/jboynes/sca/core2/src/test/java/org/apache/tuscany/core/context/scope/BasicHttpSessionScopeTestCase.java
URL: http://svn.apache.org/viewcvs/incubator/tuscany/sandbox/jboynes/sca/core2/src/test/java/org/apache/tuscany/core/context/scope/BasicHttpSessionScopeTestCase.java?rev=399943&view=auto
==============================================================================
--- incubator/tuscany/sandbox/jboynes/sca/core2/src/test/java/org/apache/tuscany/core/context/scope/BasicHttpSessionScopeTestCase.java
(added)
+++ incubator/tuscany/sandbox/jboynes/sca/core2/src/test/java/org/apache/tuscany/core/context/scope/BasicHttpSessionScopeTestCase.java
Thu May  4 21:00:17 2006
@@ -0,0 +1,88 @@
+package org.apache.tuscany.core.context.scope;
+
+import junit.framework.TestCase;
+import org.apache.tuscany.common.ObjectFactory;
+import org.apache.tuscany.core.context.WorkContextImpl;
+import org.apache.tuscany.core.context.event.HttpSessionEnd;
+import org.apache.tuscany.core.injection.EventInvoker;
+import org.apache.tuscany.core.injection.MethodEventInvoker;
+import org.apache.tuscany.core.injection.PojoObjectFactory;
+import org.apache.tuscany.core.mock.component.SessionScopeInitDestroyComponent;
+import org.apache.tuscany.core.mock.context.MockCompositeContext;
+import org.apache.tuscany.core.system.context.SystemAtomicContext;
+import org.apache.tuscany.spi.context.CompositeContext;
+import org.apache.tuscany.spi.context.WorkContext;
+
+/**
+ * @version $$Rev$$ $$Date$$
+ */
+public class BasicHttpSessionScopeTestCase extends TestCase {
+
+    private EventInvoker<Object> initInvoker;
+    private EventInvoker<Object> destroyInvoker;
+    private ObjectFactory<?> factory;
+
+    public void testLifecycleManagement() throws Exception {
+        WorkContext workContext = new WorkContextImpl();
+        CompositeContext currentModule = new MockCompositeContext();
+        HttpSessionScopeContext scopeContext = new HttpSessionScopeContext(workContext);
+        scopeContext.start();
+        SystemAtomicContext atomicContext = createContext();
+        atomicContext.setScopeContext(scopeContext);
+        // start the request
+        workContext.setCurrentModule(currentModule);
+        Object session = new Object();
+        workContext.setIdentifier(HttpSessionScopeContext.HTTP_IDENTIFIER, session);
+        SessionScopeInitDestroyComponent o1 = (SessionScopeInitDestroyComponent) scopeContext.getInstance(atomicContext);
+        assertTrue(o1.isInitialized());
+        assertFalse(o1.isDestroyed());
+        SessionScopeInitDestroyComponent o2 = (SessionScopeInitDestroyComponent) scopeContext.getInstance(atomicContext);
+        assertSame(o1, o2);
+        scopeContext.onEvent(new HttpSessionEnd(this, session));
+        assertTrue(o1.isDestroyed());
+        scopeContext.stop();
+    }
+
+    public void testSessionIsolation() throws Exception {
+        WorkContext workContext = new WorkContextImpl();
+        CompositeContext currentModule = new MockCompositeContext();
+        HttpSessionScopeContext scopeContext = new HttpSessionScopeContext(workContext);
+        scopeContext.start();
+
+        SystemAtomicContext atomicContext = createContext();
+        atomicContext.setScopeContext(scopeContext);
+
+        workContext.setCurrentModule(currentModule);
+        Object session1 = new Object();
+        workContext.setIdentifier(HttpSessionScopeContext.HTTP_IDENTIFIER, session1);
+        SessionScopeInitDestroyComponent o1 = (SessionScopeInitDestroyComponent) scopeContext.getInstance(atomicContext);
+        assertTrue(o1.isInitialized());
+
+        Object session2 = new Object();
+        workContext.setIdentifier(HttpSessionScopeContext.HTTP_IDENTIFIER, session2);
+        SessionScopeInitDestroyComponent o2 = (SessionScopeInitDestroyComponent) scopeContext.getInstance(atomicContext);
+        assertNotSame(o1, o2);
+
+        scopeContext.onEvent(new HttpSessionEnd(this, session1));
+        assertTrue(o1.isDestroyed());
+        assertFalse(o2.isDestroyed());
+        scopeContext.onEvent(new HttpSessionEnd(this, session2));
+        assertTrue(o2.isDestroyed());
+        scopeContext.stop();
+    }
+
+    protected void setUp() throws Exception {
+        super.setUp();
+        factory = new PojoObjectFactory<SessionScopeInitDestroyComponent>(SessionScopeInitDestroyComponent.class.getConstructor((Class[])
null), null, null);
+        initInvoker = new MethodEventInvoker<Object>(SessionScopeInitDestroyComponent.class.getMethod("init",
(Class[]) null));
+        destroyInvoker = new MethodEventInvoker<Object>(SessionScopeInitDestroyComponent.class.getMethod("destroy",
(Class[]) null));
+    }
+
+    protected void tearDown() throws Exception {
+        super.tearDown();
+    }
+
+    private SystemAtomicContext createContext() {
+        return new SystemAtomicContext("foo", factory, false, initInvoker, destroyInvoker);
+    }
+}

Added: incubator/tuscany/sandbox/jboynes/sca/core2/src/test/java/org/apache/tuscany/core/context/scope/BasicRequestScopeTestCase.java
URL: http://svn.apache.org/viewcvs/incubator/tuscany/sandbox/jboynes/sca/core2/src/test/java/org/apache/tuscany/core/context/scope/BasicRequestScopeTestCase.java?rev=399943&view=auto
==============================================================================
--- incubator/tuscany/sandbox/jboynes/sca/core2/src/test/java/org/apache/tuscany/core/context/scope/BasicRequestScopeTestCase.java
(added)
+++ incubator/tuscany/sandbox/jboynes/sca/core2/src/test/java/org/apache/tuscany/core/context/scope/BasicRequestScopeTestCase.java
Thu May  4 21:00:17 2006
@@ -0,0 +1,80 @@
+package org.apache.tuscany.core.context.scope;
+
+import junit.framework.TestCase;
+import org.apache.tuscany.common.ObjectFactory;
+import org.apache.tuscany.core.context.WorkContextImpl;
+import org.apache.tuscany.core.context.event.RequestEnd;
+import org.apache.tuscany.core.injection.EventInvoker;
+import org.apache.tuscany.core.injection.MethodEventInvoker;
+import org.apache.tuscany.core.injection.PojoObjectFactory;
+import org.apache.tuscany.core.mock.component.RequestScopeInitDestroyComponent;
+import org.apache.tuscany.core.mock.context.MockCompositeContext;
+import org.apache.tuscany.core.system.context.SystemAtomicContext;
+import org.apache.tuscany.spi.context.CompositeContext;
+import org.apache.tuscany.spi.context.WorkContext;
+
+/**
+ * @version $$Rev$$ $$Date$$
+ */
+public class BasicRequestScopeTestCase extends TestCase {
+
+    private EventInvoker<Object> initInvoker;
+    private EventInvoker<Object> destroyInvoker;
+    private ObjectFactory<?> factory;
+
+    public void testLifecycleManagement() throws Exception {
+        WorkContext workContext = new WorkContextImpl();
+        CompositeContext currentModule = new MockCompositeContext();
+        RequestScopeContext scopeContext = new RequestScopeContext(workContext);
+        scopeContext.start();
+        SystemAtomicContext atomicContext = createContext();
+        atomicContext.setScopeContext(scopeContext);
+        // start the request
+        workContext.setCurrentModule(currentModule);
+        RequestScopeInitDestroyComponent o1 = (RequestScopeInitDestroyComponent) scopeContext.getInstance(atomicContext);
+        assertTrue(o1.isInitialized());
+        assertFalse(o1.isDestroyed());
+        RequestScopeInitDestroyComponent o2 = (RequestScopeInitDestroyComponent) scopeContext.getInstance(atomicContext);
+        assertSame(o1, o2);
+        scopeContext.onEvent(new RequestEnd(this));
+        assertTrue(o1.isDestroyed());
+        scopeContext.stop();
+    }
+
+    public void testRequestIsolation() throws Exception {
+        WorkContext workContext = new WorkContextImpl();
+        CompositeContext currentModule = new MockCompositeContext();
+        RequestScopeContext scopeContext = new RequestScopeContext(workContext);
+        scopeContext.start();
+
+        SystemAtomicContext atomicContext = createContext();
+        atomicContext.setScopeContext(scopeContext);
+
+        workContext.setCurrentModule(currentModule);
+        RequestScopeInitDestroyComponent o1 = (RequestScopeInitDestroyComponent) scopeContext.getInstance(atomicContext);
+        assertTrue(o1.isInitialized());
+        scopeContext.onEvent(new RequestEnd(this));
+        assertTrue(o1.isDestroyed());
+
+        RequestScopeInitDestroyComponent o2 = (RequestScopeInitDestroyComponent) scopeContext.getInstance(atomicContext);
+        assertNotSame(o1, o2);
+        scopeContext.onEvent(new RequestEnd(this));
+        assertTrue(o2.isDestroyed());
+        scopeContext.stop();
+    }
+
+    protected void setUp() throws Exception {
+        super.setUp();
+        factory = new PojoObjectFactory<RequestScopeInitDestroyComponent>(RequestScopeInitDestroyComponent.class.getConstructor((Class[])
null), null, null);
+        initInvoker = new MethodEventInvoker<Object>(RequestScopeInitDestroyComponent.class.getMethod("init",
(Class[]) null));
+        destroyInvoker = new MethodEventInvoker<Object>(RequestScopeInitDestroyComponent.class.getMethod("destroy",
(Class[]) null));
+    }
+
+    protected void tearDown() throws Exception {
+        super.tearDown();
+    }
+
+    private SystemAtomicContext createContext() {
+        return new SystemAtomicContext("foo", factory, false, initInvoker, destroyInvoker);
+    }
+}



Mime
View raw message