Author: tveronezi
Date: Sat Jul 21 12:52:15 2012
New Revision: 1364076

URL: http://svn.apache.org/viewvc?rev=1364076&view=rev
Log:
https://issues.apache.org/jira/browse/OPENEJB-352

Added:
    openejb/trunk/openejb/examples/simple-stateful-callbacks/
    openejb/trunk/openejb/examples/simple-stateful-callbacks/README.md
    openejb/trunk/openejb/examples/simple-stateful-callbacks/build.xml
    openejb/trunk/openejb/examples/simple-stateful-callbacks/pom.xml
    openejb/trunk/openejb/examples/simple-stateful-callbacks/src/
    openejb/trunk/openejb/examples/simple-stateful-callbacks/src/main/
    openejb/trunk/openejb/examples/simple-stateful-callbacks/src/main/java/
    openejb/trunk/openejb/examples/simple-stateful-callbacks/src/main/java/org/
    
openejb/trunk/openejb/examples/simple-stateful-callbacks/src/main/java/org/superbiz/
    
openejb/trunk/openejb/examples/simple-stateful-callbacks/src/main/java/org/superbiz/counter/
    
openejb/trunk/openejb/examples/simple-stateful-callbacks/src/main/java/org/superbiz/counter/CallbackCounter.java
    
openejb/trunk/openejb/examples/simple-stateful-callbacks/src/main/java/org/superbiz/counter/ExecutionChannel.java
    
openejb/trunk/openejb/examples/simple-stateful-callbacks/src/main/java/org/superbiz/counter/ExecutionObserver.java
    openejb/trunk/openejb/examples/simple-stateful-callbacks/src/test/
    openejb/trunk/openejb/examples/simple-stateful-callbacks/src/test/java/
    openejb/trunk/openejb/examples/simple-stateful-callbacks/src/test/java/org/
    
openejb/trunk/openejb/examples/simple-stateful-callbacks/src/test/java/org/superbiz/
    
openejb/trunk/openejb/examples/simple-stateful-callbacks/src/test/java/org/superbiz/counter/
    
openejb/trunk/openejb/examples/simple-stateful-callbacks/src/test/java/org/superbiz/counter/CounterCallbacksTest.java

Added: openejb/trunk/openejb/examples/simple-stateful-callbacks/README.md
URL: 
http://svn.apache.org/viewvc/openejb/trunk/openejb/examples/simple-stateful-callbacks/README.md?rev=1364076&view=auto
==============================================================================
--- openejb/trunk/openejb/examples/simple-stateful-callbacks/README.md (added)
+++ openejb/trunk/openejb/examples/simple-stateful-callbacks/README.md Sat Jul 
21 12:52:15 2012
@@ -0,0 +1,292 @@
+Title: Simple Stateful with callback methods
+
+This example shows how to create a stateful session bean that uses the 
@PrePassivate, @PostActivate, @PostConstruct, @PreDestroy and @AroundInvoke 
annotations.
+
+## CallbackCounter
+
+    package org.superbiz.counter;
+
+    import javax.annotation.PostConstruct;
+    import javax.annotation.PreDestroy;
+    import javax.ejb.PostActivate;
+    import javax.ejb.PrePassivate;
+    import javax.ejb.Stateful;
+    import javax.ejb.StatefulTimeout;
+    import javax.interceptor.AroundInvoke;
+    import javax.interceptor.InvocationContext;
+    import java.io.Serializable;
+    import java.util.concurrent.TimeUnit;
+
+    @Stateful
+    @StatefulTimeout(value = 1, unit = TimeUnit.SECONDS)
+    public class CallbackCounter implements Serializable {
+
+        private int count = 0;
+
+        @PrePassivate
+        public void prePassivate() {
+            ExecutionChannel.getInstance().notifyObservers("prePassivate");
+        }
+
+        @PostActivate
+        public void postActivate() {
+            ExecutionChannel.getInstance().notifyObservers("postActivate");
+        }
+
+        @PostConstruct
+        public void postConstruct() {
+            ExecutionChannel.getInstance().notifyObservers("postConstruct");
+        }
+
+        @PreDestroy
+        public void preDestroy() {
+            ExecutionChannel.getInstance().notifyObservers("preDestroy");
+        }
+
+        @AroundInvoke
+        public Object intercept(InvocationContext ctx) throws Exception {
+            
ExecutionChannel.getInstance().notifyObservers(ctx.getMethod().getName());
+            return ctx.proceed();
+        }
+
+        public int count() {
+            return count;
+        }
+
+        public int increment() {
+            return ++count;
+        }
+
+        public int reset() {
+            return (count = 0);
+        }
+    }
+
+## ExecutionChannel
+
+    package org.superbiz.counter;
+
+    import java.util.ArrayList;
+    import java.util.List;
+
+    public class ExecutionChannel {
+        private static final ExecutionChannel INSTANCE = new 
ExecutionChannel();
+
+        private final List<ExecutionObserver> observers = new 
ArrayList<ExecutionObserver>();
+
+        public static ExecutionChannel getInstance() {
+            return INSTANCE;
+        }
+
+        public void addObserver(ExecutionObserver observer) {
+            this.observers.add(observer);
+        }
+
+        public void notifyObservers(Object value) {
+            for (ExecutionObserver observer : this.observers) {
+                observer.onExecution(value);
+            }
+        }
+    }
+
+## ExecutionObserver
+
+    package org.superbiz.counter;
+
+    public interface ExecutionObserver {
+
+        void onExecution(Object value);
+
+    }
+
+## CounterCallbacksTest
+
+    package org.superbiz.counter;
+
+    import junit.framework.Assert;
+    import org.junit.Test;
+
+    import javax.ejb.embeddable.EJBContainer;
+    import javax.naming.Context;
+    import javax.naming.InitialContext;
+    import javax.naming.NamingException;
+    import java.util.*;
+
+    public class CounterCallbacksTest implements ExecutionObserver {
+        private static List<Object> received = new ArrayList<Object>();
+
+        public Context getContext() throws NamingException {
+            final Properties p = new Properties();
+            p.put(Context.INITIAL_CONTEXT_FACTORY, 
"org.apache.openejb.core.LocalInitialContextFactory");
+            return new InitialContext(p);
+
+        }
+
+        @Test
+        public void test() throws Exception {
+            final Map<String, Object> p = new HashMap<String, Object>();
+            p.put("MySTATEFUL", "new://Container?type=STATEFUL");
+            p.put("MySTATEFUL.Capacity", "2"); //How many instances of 
Stateful beans can our server hold in memory?
+            p.put("MySTATEFUL.Frequency", "1"); //Interval in seconds between 
checks
+            p.put("MySTATEFUL.BulkPassivate", "0"); //No bulkPassivate - just 
passivate entities whenever it is needed
+            final EJBContainer container = EJBContainer.createEJBContainer(p);
+
+            //this is going to track the execution
+            ExecutionChannel.getInstance().addObserver(this);
+
+            {
+                final Context context = getContext();
+
+                CallbackCounter counterA = (CallbackCounter) 
context.lookup("java:global/simple-stateful-callbacks/CallbackCounter");
+                Assert.assertNotNull(counterA);
+                Assert.assertEquals("postConstruct", received.remove(0));
+
+                Assert.assertEquals(0, counterA.count());
+                Assert.assertEquals("count", received.remove(0));
+
+                Assert.assertEquals(1, counterA.increment());
+                Assert.assertEquals("increment", received.remove(0));
+
+                Assert.assertEquals(0, counterA.reset());
+                Assert.assertEquals("reset", received.remove(0));
+
+                Assert.assertEquals(1, counterA.increment());
+                Assert.assertEquals("increment", received.remove(0));
+
+                System.out.println("Waiting 2 seconds...");
+                Thread.sleep(2000);
+
+                Assert.assertEquals("preDestroy", received.remove(0));
+
+                try {
+                    counterA.increment();
+                    Assert.fail("The ejb is not supposed to be there.");
+                } catch (javax.ejb.NoSuchEJBException e) {
+                    //excepted
+                }
+
+                context.close();
+            }
+
+            {
+                final Context context = getContext();
+
+                CallbackCounter counterA = (CallbackCounter) 
context.lookup("java:global/simple-stateful-callbacks/CallbackCounter");
+                Assert.assertEquals("postConstruct", received.remove(0));
+
+                Assert.assertEquals(1, counterA.increment());
+                Assert.assertEquals("increment", received.remove(0));
+
+                ((CallbackCounter) 
context.lookup("java:global/simple-stateful-callbacks/CallbackCounter")).count();
+                Assert.assertEquals("postConstruct", received.remove(0));
+                Assert.assertEquals("count", received.remove(0));
+
+                ((CallbackCounter) 
context.lookup("java:global/simple-stateful-callbacks/CallbackCounter")).count();
+                Assert.assertEquals("postConstruct", received.remove(0));
+                Assert.assertEquals("count", received.remove(0));
+
+                System.out.println("Waiting 2 seconds...");
+                Thread.sleep(2000);
+                Assert.assertEquals("prePassivate", received.remove(0));
+
+                context.close();
+            }
+            container.close();
+
+            Assert.assertEquals("preDestroy", received.remove(0));
+            Assert.assertEquals("preDestroy", received.remove(0));
+
+            Assert.assertTrue(received.toString(), received.isEmpty());
+        }
+
+        @Override
+        public void onExecution(Object value) {
+            System.out.println("Test step -> " + value);
+            received.add(value);
+        }
+    }
+
+# Running
+
+    -------------------------------------------------------
+     T E S T S
+    -------------------------------------------------------
+    Running org.superbiz.counter.CounterCallbacksTest
+    INFO - 
********************************************************************************
+    INFO - OpenEJB http://openejb.apache.org/
+    INFO - Startup: Sat Jul 21 08:18:28 EDT 2012
+    INFO - Copyright 1999-2012 (C) Apache OpenEJB Project, All Rights Reserved.
+    INFO - Version: 4.1.0-SNAPSHOT
+    INFO - Build date: 20120721
+    INFO - Build time: 04:06
+    INFO - 
********************************************************************************
+    INFO - openejb.home = 
/home/boto/dev/ws/openejb_trunk/openejb/examples/simple-stateful-callbacks
+    INFO - openejb.base = 
/home/boto/dev/ws/openejb_trunk/openejb/examples/simple-stateful-callbacks
+    INFO - Created new singletonService 
org.apache.openejb.cdi.ThreadSingletonServiceImpl@527736bd
+    INFO - Succeeded in installing singleton service
+    INFO - Using 'javax.ejb.embeddable.EJBContainer=true'
+    INFO - Cannot find the configuration file [conf/openejb.xml].  Will 
attempt to create one for the beans deployed.
+    INFO - Configuring Service(id=Default Security Service, 
type=SecurityService, provider-id=Default Security Service)
+    INFO - Configuring Service(id=Default Transaction Manager, 
type=TransactionManager, provider-id=Default Transaction Manager)
+    INFO - Configuring Service(id=MySTATEFUL, type=Container, 
provider-id=Default Stateful Container)
+    INFO - Creating TransactionManager(id=Default Transaction Manager)
+    INFO - Creating SecurityService(id=Default Security Service)
+    INFO - Creating Container(id=MySTATEFUL)
+    INFO - Using directory /tmp for stateful session passivation
+    INFO - Beginning load: 
/home/boto/dev/ws/openejb_trunk/openejb/examples/simple-stateful-callbacks/target/classes
+    INFO - Configuring enterprise application: 
/home/boto/dev/ws/openejb_trunk/openejb/examples/simple-stateful-callbacks
+    INFO - Auto-deploying ejb CallbackCounter: 
EjbDeployment(deployment-id=CallbackCounter)
+    INFO - Configuring Service(id=Default Managed Container, type=Container, 
provider-id=Default Managed Container)
+    INFO - Auto-creating a container for bean 
org.superbiz.counter.CounterCallbacksTest: Container(type=MANAGED, id=Default 
Managed Container)
+    INFO - Creating Container(id=Default Managed Container)
+    INFO - Using directory /tmp for stateful session passivation
+    INFO - Enterprise application 
"/home/boto/dev/ws/openejb_trunk/openejb/examples/simple-stateful-callbacks" 
loaded.
+    INFO - Assembling app: 
/home/boto/dev/ws/openejb_trunk/openejb/examples/simple-stateful-callbacks
+    INFO - 
Jndi(name="java:global/simple-stateful-callbacks/CallbackCounter!org.superbiz.counter.CallbackCounter")
+    INFO - Jndi(name="java:global/simple-stateful-callbacks/CallbackCounter")
+    INFO - Existing thread singleton service in SystemInstance() 
org.apache.openejb.cdi.ThreadSingletonServiceImpl@527736bd
+    INFO - OpenWebBeans Container is starting...
+    INFO - Adding OpenWebBeansPlugin : [CdiPlugin]
+    INFO - All injection points are validated successfully.
+    INFO - OpenWebBeans Container has started, it took 225 ms.
+    INFO - Created Ejb(deployment-id=CallbackCounter, 
ejb-name=CallbackCounter, container=MySTATEFUL)
+    INFO - Started Ejb(deployment-id=CallbackCounter, 
ejb-name=CallbackCounter, container=MySTATEFUL)
+    INFO - Deployed 
Application(path=/home/boto/dev/ws/openejb_trunk/openejb/examples/simple-stateful-callbacks)
+    Test step -> postConstruct
+    Test step -> count
+    Test step -> increment
+    Test step -> reset
+    Test step -> increment
+    Waiting 2 seconds...
+    Test step -> preDestroy
+    INFO - Removing the timed-out stateful session bean instance 
583c10bfdbd326ba:57f94a9b:138a9798adf:-8000
+    INFO - Activation failed: file not found 
/tmp/583c10bfdbd326ba=57f94a9b=138a9798adf=-8000
+    Test step -> postConstruct
+    Test step -> increment
+    Test step -> postConstruct
+    Test step -> count
+    Test step -> postConstruct
+    Test step -> count
+    Waiting 2 seconds...
+    Test step -> prePassivate
+    INFO - Passivating to file /tmp/583c10bfdbd326ba=57f94a9b=138a9798adf=-7fff
+    Test step -> preDestroy
+    INFO - Removing the timed-out stateful session bean instance 
583c10bfdbd326ba:57f94a9b:138a9798adf:-7ffe
+    Test step -> preDestroy
+    INFO - Removing the timed-out stateful session bean instance 
583c10bfdbd326ba:57f94a9b:138a9798adf:-7ffd
+    INFO - Undeploying app: 
/home/boto/dev/ws/openejb_trunk/openejb/examples/simple-stateful-callbacks
+    Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 7.487 sec
+
+    Results :
+
+    Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
+
+    [INFO] 
------------------------------------------------------------------------
+    [INFO] BUILD SUCCESS
+    [INFO] 
------------------------------------------------------------------------
+    [INFO] Total time: 15.803s
+    [INFO] Finished at: Sat Jul 21 08:18:35 EDT 2012
+    [INFO] Final Memory: 11M/247M
+    [INFO] 
------------------------------------------------------------------------
+
+

Added: openejb/trunk/openejb/examples/simple-stateful-callbacks/build.xml
URL: 
http://svn.apache.org/viewvc/openejb/trunk/openejb/examples/simple-stateful-callbacks/build.xml?rev=1364076&view=auto
==============================================================================
--- openejb/trunk/openejb/examples/simple-stateful-callbacks/build.xml (added)
+++ openejb/trunk/openejb/examples/simple-stateful-callbacks/build.xml Sat Jul 
21 12:52:15 2012
@@ -0,0 +1,119 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+
+    Licensed to the Apache Software Foundation (ASF) under one or more
+    contributor license agreements.  See the NOTICE file distributed with
+    this work for additional information regarding copyright ownership.
+    The ASF licenses this file to You under the Apache License, Version 2.0
+    (the "License"); you may not use this file except in compliance with
+    the License.  You may obtain a copy of the License at
+
+       http://www.apache.org/licenses/LICENSE-2.0
+
+    Unless required by applicable law or agreed to in writing, software
+    distributed under the License is distributed on an "AS IS" BASIS,
+    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+    See the License for the specific language governing permissions and
+    limitations under the License.
+-->
+
+<!-- $Rev: 1237516 $ $Date: 2012-01-29 19:48:17 -0500 (Sun, 29 Jan 2012) $ -->
+
+<project name="MyProject" default="dist" basedir="." 
xmlns:artifact="antlib:org.apache.maven.artifact.ant">
+
+  <!-- ===============================================================
+
+  HOW TO RUN
+
+    Download 
http://archive.apache.org/dist/maven/binaries/maven-ant-tasks-2.0.9.jar
+    Then execute ant as follows:
+
+    ant -lib maven-ant-tasks-2.0.9.jar
+
+  NOTE
+
+    You do NOT need maven-ant-tasks-2.0.9.jar to use OpenEJB for embedded EJB
+    testing with Ant.  It is simply used in this example to make the build.xml
+    a bit simpler.  As long as OpenEJB and it's required libraries are in the
+    <junit> classpath, the tests will run with OpenEJB embedded.
+
+  ================================================================= -->
+
+  <artifact:remoteRepository id="apache.snapshot.repository" 
url="http://repository.apache.org/snapshots/"/>
+  <artifact:remoteRepository id="m2.repository" 
url="http://repo1.maven.org/maven2/"/>
+
+  <!-- Build Classpath -->
+  <artifact:dependencies pathId="classpath.main">
+    <dependency groupId="org.apache.openejb" artifactId="javaee-api-embedded" 
version="6.0-3"/>
+  </artifact:dependencies>
+
+  <!-- Test Build Classpath -->
+  <artifact:dependencies pathId="classpath.test.build">
+    <dependency groupId="junit" artifactId="junit" version="4.3.1"/>
+  </artifact:dependencies>
+
+  <!-- Test Run Classpath -->
+  <artifact:dependencies pathId="classpath.test.run">
+    <remoteRepository refid="apache.snapshot.repository"/>
+    <remoteRepository refid="m2.repository"/>
+
+    <dependency groupId="org.apache.openejb" artifactId="openejb-core" 
version="4.0.0-beta-1"/>
+    <dependency groupId="junit" artifactId="junit" version="4.3.1"/>
+  </artifact:dependencies>
+
+  <!-- Properties -->
+
+  <property name="src.main.java" location="src/main/java"/>
+  <property name="src.main.resources" location="src/main/resources"/>
+  <property name="src.test.java" location="src/test/java"/>
+  <property name="build.main" location="target/classes"/>
+  <property name="build.test" location="target/test-classes"/>
+  <property name="test.reports" location="target/test-reports"/>
+  <property name="dist" location="target"/>
+
+
+  <target name="init">
+    <mkdir dir="${build.main}"/>
+    <mkdir dir="${build.test}"/>
+    <mkdir dir="${test.reports}"/>
+  </target>
+
+  <target name="compile" depends="init">
+
+    <javac srcdir="${src.main.java}" destdir="${build.main}">
+      <classpath refid="classpath.main"/>
+    </javac>
+    <copy todir="${build.main}">
+      <fileset dir="${src.main.resources}"/>
+    </copy>
+
+    <javac srcdir="${src.test.java}" destdir="${build.test}">
+      <classpath location="${build.main}"/>
+      <classpath refid="classpath.main"/>
+      <classpath refid="classpath.test.build"/>
+    </javac>
+  </target>
+
+  <target name="test" depends="compile">
+    <junit fork="yes" printsummary="yes">
+      <classpath location="${build.main}"/>
+      <classpath location="${build.test}"/>
+      <classpath refid="classpath.main"/>
+      <classpath refid="classpath.test.build"/>
+      <classpath refid="classpath.test.run"/>
+
+      <formatter type="plain"/>
+
+      <batchtest fork="yes" todir="${test.reports}">
+        <fileset dir="${src.test.java}">
+          <include name="**/*Test.java"/>
+        </fileset>
+      </batchtest>
+    </junit>
+  </target>
+
+  <target name="dist" depends="test">
+    <jar jarfile="${dist}/myproject-1.0.jar" basedir="${build.main}"/>
+  </target>
+
+</project>

Added: openejb/trunk/openejb/examples/simple-stateful-callbacks/pom.xml
URL: 
http://svn.apache.org/viewvc/openejb/trunk/openejb/examples/simple-stateful-callbacks/pom.xml?rev=1364076&view=auto
==============================================================================
--- openejb/trunk/openejb/examples/simple-stateful-callbacks/pom.xml (added)
+++ openejb/trunk/openejb/examples/simple-stateful-callbacks/pom.xml Sat Jul 21 
12:52:15 2012
@@ -0,0 +1,92 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+
+    Licensed to the Apache Software Foundation (ASF) under one or more
+    contributor license agreements.  See the NOTICE file distributed with
+    this work for additional information regarding copyright ownership.
+    The ASF licenses this file to You under the Apache License, Version 2.0
+    (the "License"); you may not use this file except in compliance with
+    the License.  You may obtain a copy of the License at
+
+       http://www.apache.org/licenses/LICENSE-2.0
+
+    Unless required by applicable law or agreed to in writing, software
+    distributed under the License is distributed on an "AS IS" BASIS,
+    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+    See the License for the specific language governing permissions and
+    limitations under the License.
+-->
+
+<!-- $Rev: 1346534 $ $Date: 2012-06-05 14:57:54 -0400 (Tue, 05 Jun 2012) $ -->
+
+<project xmlns="http://maven.apache.org/POM/4.0.0"; 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"; 
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
http://maven.apache.org/maven-v4_0_0.xsd";>
+  <modelVersion>4.0.0</modelVersion>
+  <groupId>org.superbiz</groupId>
+  <artifactId>simple-stateful-callbacks</artifactId>
+  <packaging>jar</packaging>
+  <version>1.1-SNAPSHOT</version>
+  <name>OpenEJB :: Examples :: Simple Stateful Pojo Callbacks</name>
+  <properties>
+    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+  </properties>
+  <build>
+    <defaultGoal>install</defaultGoal>
+    <plugins>
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-compiler-plugin</artifactId>
+        <version>2.4</version>
+        <configuration>
+          <source>1.6</source>
+          <target>1.6</target>
+        </configuration>
+      </plugin>
+    </plugins>
+  </build>
+  <repositories>
+    <repository>
+      <id>apache-m2-snapshot</id>
+      <name>Apache Snapshot Repository</name>
+      <url>http://repository.apache.org/snapshots</url>
+    </repository>
+  </repositories>
+  <dependencies>
+    <dependency>
+      <groupId>org.apache.openejb</groupId>
+      <artifactId>javaee-api</artifactId>
+      <version>6.0-4</version>
+      <scope>provided</scope>
+    </dependency>
+    <dependency>
+      <groupId>junit</groupId>
+      <artifactId>junit</artifactId>
+      <version>4.10</version>
+      <scope>test</scope>
+    </dependency>
+    <!--
+    The <scope>test</scope> guarantees that non of your runtime
+    code is dependent on any OpenEJB classes.
+    -->
+    <dependency>
+      <groupId>org.apache.openejb</groupId>
+      <artifactId>openejb-core</artifactId>
+      <version>4.1.0-SNAPSHOT</version>
+      <scope>test</scope>
+    </dependency>
+  </dependencies>
+  <!--
+  This section allows you to configure where to publish libraries for sharing.
+  It is not required and may be deleted.  For more information see:
+  http://maven.apache.org/plugins/maven-deploy-plugin/
+  -->
+  <distributionManagement>
+    <repository>
+      <id>localhost</id>
+      <url>file://${basedir}/target/repo/</url>
+    </repository>
+    <snapshotRepository>
+      <id>localhost</id>
+      <url>file://${basedir}/target/snapshot-repo/</url>
+    </snapshotRepository>
+  </distributionManagement>
+</project>

Added: 
openejb/trunk/openejb/examples/simple-stateful-callbacks/src/main/java/org/superbiz/counter/CallbackCounter.java
URL: 
http://svn.apache.org/viewvc/openejb/trunk/openejb/examples/simple-stateful-callbacks/src/main/java/org/superbiz/counter/CallbackCounter.java?rev=1364076&view=auto
==============================================================================
--- 
openejb/trunk/openejb/examples/simple-stateful-callbacks/src/main/java/org/superbiz/counter/CallbackCounter.java
 (added)
+++ 
openejb/trunk/openejb/examples/simple-stateful-callbacks/src/main/java/org/superbiz/counter/CallbackCounter.java
 Sat Jul 21 12:52:15 2012
@@ -0,0 +1,73 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.superbiz.counter;
+
+import javax.annotation.PostConstruct;
+import javax.annotation.PreDestroy;
+import javax.ejb.PostActivate;
+import javax.ejb.PrePassivate;
+import javax.ejb.Stateful;
+import javax.ejb.StatefulTimeout;
+import javax.interceptor.AroundInvoke;
+import javax.interceptor.InvocationContext;
+import java.io.Serializable;
+import java.util.concurrent.TimeUnit;
+
+@Stateful
+@StatefulTimeout(value = 1, unit = TimeUnit.SECONDS)
+public class CallbackCounter implements Serializable {
+
+    private int count = 0;
+
+    @PrePassivate
+    public void prePassivate() {
+        ExecutionChannel.getInstance().notifyObservers("prePassivate");
+    }
+
+    @PostActivate
+    public void postActivate() {
+        ExecutionChannel.getInstance().notifyObservers("postActivate");
+    }
+
+    @PostConstruct
+    public void postConstruct() {
+        ExecutionChannel.getInstance().notifyObservers("postConstruct");
+    }
+
+    @PreDestroy
+    public void preDestroy() {
+        ExecutionChannel.getInstance().notifyObservers("preDestroy");
+    }
+
+    @AroundInvoke
+    public Object intercept(InvocationContext ctx) throws Exception {
+        
ExecutionChannel.getInstance().notifyObservers(ctx.getMethod().getName());
+        return ctx.proceed();
+    }
+
+    public int count() {
+        return count;
+    }
+
+    public int increment() {
+        return ++count;
+    }
+
+    public int reset() {
+        return (count = 0);
+    }
+}

Added: 
openejb/trunk/openejb/examples/simple-stateful-callbacks/src/main/java/org/superbiz/counter/ExecutionChannel.java
URL: 
http://svn.apache.org/viewvc/openejb/trunk/openejb/examples/simple-stateful-callbacks/src/main/java/org/superbiz/counter/ExecutionChannel.java?rev=1364076&view=auto
==============================================================================
--- 
openejb/trunk/openejb/examples/simple-stateful-callbacks/src/main/java/org/superbiz/counter/ExecutionChannel.java
 (added)
+++ 
openejb/trunk/openejb/examples/simple-stateful-callbacks/src/main/java/org/superbiz/counter/ExecutionChannel.java
 Sat Jul 21 12:52:15 2012
@@ -0,0 +1,40 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.superbiz.counter;
+
+import java.util.ArrayList;
+import java.util.List;
+
+public class ExecutionChannel {
+    private static final ExecutionChannel INSTANCE = new ExecutionChannel();
+
+    private final List<ExecutionObserver> observers = new 
ArrayList<ExecutionObserver>();
+
+    public static ExecutionChannel getInstance() {
+        return INSTANCE;
+    }
+
+    public void addObserver(ExecutionObserver observer) {
+        this.observers.add(observer);
+    }
+
+    public void notifyObservers(Object value) {
+        for (ExecutionObserver observer : this.observers) {
+            observer.onExecution(value);
+        }
+    }
+}

Added: 
openejb/trunk/openejb/examples/simple-stateful-callbacks/src/main/java/org/superbiz/counter/ExecutionObserver.java
URL: 
http://svn.apache.org/viewvc/openejb/trunk/openejb/examples/simple-stateful-callbacks/src/main/java/org/superbiz/counter/ExecutionObserver.java?rev=1364076&view=auto
==============================================================================
--- 
openejb/trunk/openejb/examples/simple-stateful-callbacks/src/main/java/org/superbiz/counter/ExecutionObserver.java
 (added)
+++ 
openejb/trunk/openejb/examples/simple-stateful-callbacks/src/main/java/org/superbiz/counter/ExecutionObserver.java
 Sat Jul 21 12:52:15 2012
@@ -0,0 +1,23 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.superbiz.counter;
+
+public interface ExecutionObserver {
+
+    void onExecution(Object value);
+
+}

Added: 
openejb/trunk/openejb/examples/simple-stateful-callbacks/src/test/java/org/superbiz/counter/CounterCallbacksTest.java
URL: 
http://svn.apache.org/viewvc/openejb/trunk/openejb/examples/simple-stateful-callbacks/src/test/java/org/superbiz/counter/CounterCallbacksTest.java?rev=1364076&view=auto
==============================================================================
--- 
openejb/trunk/openejb/examples/simple-stateful-callbacks/src/test/java/org/superbiz/counter/CounterCallbacksTest.java
 (added)
+++ 
openejb/trunk/openejb/examples/simple-stateful-callbacks/src/test/java/org/superbiz/counter/CounterCallbacksTest.java
 Sat Jul 21 12:52:15 2012
@@ -0,0 +1,120 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.superbiz.counter;
+
+import junit.framework.Assert;
+import org.junit.Test;
+
+import javax.ejb.embeddable.EJBContainer;
+import javax.naming.Context;
+import javax.naming.InitialContext;
+import javax.naming.NamingException;
+import java.util.*;
+
+public class CounterCallbacksTest implements ExecutionObserver {
+    private static List<Object> received = new ArrayList<Object>();
+
+    public Context getContext() throws NamingException {
+        final Properties p = new Properties();
+        p.put(Context.INITIAL_CONTEXT_FACTORY, 
"org.apache.openejb.core.LocalInitialContextFactory");
+        return new InitialContext(p);
+
+    }
+
+    @Test
+    public void test() throws Exception {
+        final Map<String, Object> p = new HashMap<String, Object>();
+        p.put("MySTATEFUL", "new://Container?type=STATEFUL");
+        p.put("MySTATEFUL.Capacity", "2"); //How many instances of Stateful 
beans can our server hold in memory?
+        p.put("MySTATEFUL.Frequency", "1"); //Interval in seconds between 
checks
+        p.put("MySTATEFUL.BulkPassivate", "0"); //No bulkPassivate - just 
passivate entities whenever it is needed
+        final EJBContainer container = EJBContainer.createEJBContainer(p);
+
+        //this is going to track the execution
+        ExecutionChannel.getInstance().addObserver(this);
+
+        {
+            final Context context = getContext();
+
+            CallbackCounter counterA = (CallbackCounter) 
context.lookup("java:global/simple-stateful-callbacks/CallbackCounter");
+            Assert.assertNotNull(counterA);
+            Assert.assertEquals("postConstruct", received.remove(0));
+
+            Assert.assertEquals(0, counterA.count());
+            Assert.assertEquals("count", received.remove(0));
+
+            Assert.assertEquals(1, counterA.increment());
+            Assert.assertEquals("increment", received.remove(0));
+
+            Assert.assertEquals(0, counterA.reset());
+            Assert.assertEquals("reset", received.remove(0));
+
+            Assert.assertEquals(1, counterA.increment());
+            Assert.assertEquals("increment", received.remove(0));
+
+            System.out.println("Waiting 2 seconds...");
+            Thread.sleep(2000);
+
+            Assert.assertEquals("preDestroy", received.remove(0));
+
+            try {
+                counterA.increment();
+                Assert.fail("The ejb is not supposed to be there.");
+            } catch (javax.ejb.NoSuchEJBException e) {
+                //excepted
+            }
+
+            context.close();
+        }
+
+        {
+            final Context context = getContext();
+
+            CallbackCounter counterA = (CallbackCounter) 
context.lookup("java:global/simple-stateful-callbacks/CallbackCounter");
+            Assert.assertEquals("postConstruct", received.remove(0));
+
+            Assert.assertEquals(1, counterA.increment());
+            Assert.assertEquals("increment", received.remove(0));
+
+            ((CallbackCounter) 
context.lookup("java:global/simple-stateful-callbacks/CallbackCounter")).count();
+            Assert.assertEquals("postConstruct", received.remove(0));
+            Assert.assertEquals("count", received.remove(0));
+
+            ((CallbackCounter) 
context.lookup("java:global/simple-stateful-callbacks/CallbackCounter")).count();
+            Assert.assertEquals("postConstruct", received.remove(0));
+            Assert.assertEquals("count", received.remove(0));
+
+            System.out.println("Waiting 2 seconds...");
+            Thread.sleep(2000);
+            Assert.assertEquals("prePassivate", received.remove(0));
+
+            context.close();
+        }
+        container.close();
+
+        Assert.assertEquals("preDestroy", received.remove(0));
+        Assert.assertEquals("preDestroy", received.remove(0));
+
+        Assert.assertTrue(received.toString(), received.isEmpty());
+    }
+
+    @Override
+    public void onExecution(Object value) {
+        System.out.println("Test step -> " + value);
+        received.add(value);
+    }
+}


Reply via email to