Author: dblevins
Date: Wed Jul 2 17:06:36 2008
New Revision: 673551
URL: http://svn.apache.org/viewvc?rev=673551&view=rev
Log:
OPENEJB-850: Example: Singleton bean with bean vs container concurrency
Added:
openejb/trunk/openejb3/examples/simple-singleton/
- copied from r672456, openejb/trunk/openejb3/examples/simple-stateless/
openejb/trunk/openejb3/examples/simple-singleton/src/main/java/org/superbiz/registry/
openejb/trunk/openejb3/examples/simple-singleton/src/main/java/org/superbiz/registry/ComponentRegistry.java
openejb/trunk/openejb3/examples/simple-singleton/src/main/java/org/superbiz/registry/ComponentRegistryBean.java
openejb/trunk/openejb3/examples/simple-singleton/src/main/java/org/superbiz/registry/PropertyRegistry.java
openejb/trunk/openejb3/examples/simple-singleton/src/main/java/org/superbiz/registry/PropertyRegistryBean.java
openejb/trunk/openejb3/examples/simple-singleton/src/test/java/org/superbiz/registry/
openejb/trunk/openejb3/examples/simple-singleton/src/test/java/org/superbiz/registry/ComponentRegistryBeanTest.java
openejb/trunk/openejb3/examples/simple-singleton/src/test/java/org/superbiz/registry/PropertiesRegistryBeanTest.java
Removed:
openejb/trunk/openejb3/examples/simple-singleton/src/main/java/org/superbiz/calculator/
openejb/trunk/openejb3/examples/simple-singleton/src/test/java/org/superbiz/calculator/
Modified:
openejb/trunk/openejb3/examples/pom.xml
openejb/trunk/openejb3/examples/simple-singleton/pom.xml
Modified: openejb/trunk/openejb3/examples/pom.xml
URL:
http://svn.apache.org/viewvc/openejb/trunk/openejb3/examples/pom.xml?rev=673551&r1=673550&r2=673551&view=diff
==============================================================================
--- openejb/trunk/openejb3/examples/pom.xml (original)
+++ openejb/trunk/openejb3/examples/pom.xml Wed Jul 2 17:06:36 2008
@@ -32,6 +32,7 @@
<modules>
<module>simple-stateful</module>
<module>simple-stateless</module>
+ <module>simple-singleton</module>
<module>simple-mdb</module>
<module>simple-cmp2</module>
<module>simple-webservice</module>
Modified: openejb/trunk/openejb3/examples/simple-singleton/pom.xml
URL:
http://svn.apache.org/viewvc/openejb/trunk/openejb3/examples/simple-singleton/pom.xml?rev=673551&r1=672456&r2=673551&view=diff
==============================================================================
--- openejb/trunk/openejb3/examples/simple-singleton/pom.xml (original)
+++ openejb/trunk/openejb3/examples/simple-singleton/pom.xml Wed Jul 2
17:06:36 2008
@@ -22,10 +22,10 @@
<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-stateless</artifactId>
+ <artifactId>simple-singleton</artifactId>
<packaging>jar</packaging>
<version>1.1-SNAPSHOT</version>
- <name>OpenEJB :: Examples :: Simple Stateless Pojo</name>
+ <name>OpenEJB :: Examples :: Simple Singleton</name>
<build>
<defaultGoal>install</defaultGoal>
<plugins>
@@ -53,6 +53,12 @@
<scope>provided</scope>
</dependency>
<dependency>
+ <groupId>org.apache.openejb</groupId>
+ <artifactId>ejb31-api-experimental</artifactId>
+ <version>3.1-SNAPSHOT</version>
+ <scope>provided</scope>
+ </dependency>
+ <dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.1</version>
Added:
openejb/trunk/openejb3/examples/simple-singleton/src/main/java/org/superbiz/registry/ComponentRegistry.java
URL:
http://svn.apache.org/viewvc/openejb/trunk/openejb3/examples/simple-singleton/src/main/java/org/superbiz/registry/ComponentRegistry.java?rev=673551&view=auto
==============================================================================
---
openejb/trunk/openejb3/examples/simple-singleton/src/main/java/org/superbiz/registry/ComponentRegistry.java
(added)
+++
openejb/trunk/openejb3/examples/simple-singleton/src/main/java/org/superbiz/registry/ComponentRegistry.java
Wed Jul 2 17:06:36 2008
@@ -0,0 +1,30 @@
+/**
+ * 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.registry;
+
+/**
+ * @version $Revision$ $Date$
+ */
+public interface ComponentRegistry {
+
+ public <T> T getComponent(Class<T> type);
+
+ public <T> T setComponent(Class<T> type, T value);
+
+ public <T> T removeComponent(Class<T> type);
+
+}
Added:
openejb/trunk/openejb3/examples/simple-singleton/src/main/java/org/superbiz/registry/ComponentRegistryBean.java
URL:
http://svn.apache.org/viewvc/openejb/trunk/openejb3/examples/simple-singleton/src/main/java/org/superbiz/registry/ComponentRegistryBean.java?rev=673551&view=auto
==============================================================================
---
openejb/trunk/openejb3/examples/simple-singleton/src/main/java/org/superbiz/registry/ComponentRegistryBean.java
(added)
+++
openejb/trunk/openejb3/examples/simple-singleton/src/main/java/org/superbiz/registry/ComponentRegistryBean.java
Wed Jul 2 17:06:36 2008
@@ -0,0 +1,43 @@
+/**
+ * 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.registry;
+
+import static javax.ejb.LockType.READ;
+import javax.ejb.Lock;
+import javax.ejb.Singleton;
+import java.util.HashMap;
+import java.util.Map;
+
[EMAIL PROTECTED]
+public class ComponentRegistryBean implements ComponentRegistry {
+
+ private final Map<Class, Object> components = new HashMap<Class, Object>();
+
+ @Lock(READ)
+ public <T> T getComponent(Class<T> type) {
+ return (T) components.get(type);
+ }
+
+ public <T> T setComponent(Class<T> type, T value) {
+ return (T) components.put(type, value);
+ }
+
+ public <T> T removeComponent(Class<T> type) {
+ return (T) components.remove(type);
+ }
+
+}
Added:
openejb/trunk/openejb3/examples/simple-singleton/src/main/java/org/superbiz/registry/PropertyRegistry.java
URL:
http://svn.apache.org/viewvc/openejb/trunk/openejb3/examples/simple-singleton/src/main/java/org/superbiz/registry/PropertyRegistry.java?rev=673551&view=auto
==============================================================================
---
openejb/trunk/openejb3/examples/simple-singleton/src/main/java/org/superbiz/registry/PropertyRegistry.java
(added)
+++
openejb/trunk/openejb3/examples/simple-singleton/src/main/java/org/superbiz/registry/PropertyRegistry.java
Wed Jul 2 17:06:36 2008
@@ -0,0 +1,27 @@
+/**
+ * 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.registry;
+
+public interface PropertyRegistry {
+
+ public String getProperty(String key);
+
+ public String setProperty(String key, String value);
+
+ public String removeProperty(String key);
+
+}
Added:
openejb/trunk/openejb3/examples/simple-singleton/src/main/java/org/superbiz/registry/PropertyRegistryBean.java
URL:
http://svn.apache.org/viewvc/openejb/trunk/openejb3/examples/simple-singleton/src/main/java/org/superbiz/registry/PropertyRegistryBean.java?rev=673551&view=auto
==============================================================================
---
openejb/trunk/openejb3/examples/simple-singleton/src/main/java/org/superbiz/registry/PropertyRegistryBean.java
(added)
+++
openejb/trunk/openejb3/examples/simple-singleton/src/main/java/org/superbiz/registry/PropertyRegistryBean.java
Wed Jul 2 17:06:36 2008
@@ -0,0 +1,42 @@
+/**
+ * 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.registry;
+
+import static javax.ejb.ConcurrencyManagementType.BEAN;
+import javax.ejb.Singleton;
+import javax.ejb.ConcurrencyManagement;
+import java.util.Properties;
+
[EMAIL PROTECTED]
[EMAIL PROTECTED](BEAN)
+public class PropertyRegistryBean implements PropertyRegistry {
+
+ private final Properties properties = new Properties();
+
+ public String getProperty(String key) {
+ return properties.getProperty(key);
+ }
+
+ public String setProperty(String key, String value) {
+ return (String) properties.setProperty(key, value);
+ }
+
+ public String removeProperty(String key) {
+ return (String) properties.remove(key);
+ }
+
+}
Added:
openejb/trunk/openejb3/examples/simple-singleton/src/test/java/org/superbiz/registry/ComponentRegistryBeanTest.java
URL:
http://svn.apache.org/viewvc/openejb/trunk/openejb3/examples/simple-singleton/src/test/java/org/superbiz/registry/ComponentRegistryBeanTest.java?rev=673551&view=auto
==============================================================================
---
openejb/trunk/openejb3/examples/simple-singleton/src/test/java/org/superbiz/registry/ComponentRegistryBeanTest.java
(added)
+++
openejb/trunk/openejb3/examples/simple-singleton/src/test/java/org/superbiz/registry/ComponentRegistryBeanTest.java
Wed Jul 2 17:06:36 2008
@@ -0,0 +1,56 @@
+/**
+ * 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.registry;
+
+import junit.framework.TestCase;
+
+import javax.naming.Context;
+import javax.naming.InitialContext;
+import java.util.Properties;
+import java.util.Date;
+import java.net.URI;
+
+public class ComponentRegistryBeanTest extends TestCase {
+
+ public void test() throws Exception {
+ Properties props = new Properties();
+ props.setProperty(Context.INITIAL_CONTEXT_FACTORY,
"org.apache.openejb.client.LocalInitialContextFactory");
+
+ InitialContext context = new InitialContext(props);
+
+ ComponentRegistry refOne = (ComponentRegistry)
context.lookup("ComponentRegistryBeanLocal");
+
+ ComponentRegistry refTwo = (ComponentRegistry)
context.lookup("ComponentRegistryBeanLocal");
+
+
+ refOne.setComponent(URI.class, new URI("foo://bar/baz"));
+
+ URI uri = refTwo.getComponent(URI.class);
+
+ assertEquals(uri, new URI("foo://bar/baz"));
+
+
+ Date now = new Date();
+
+ refTwo.setComponent(Date.class, now);
+
+ Date date = refOne.getComponent(Date.class);
+
+ assertEquals(now, date);
+
+ }
+}
Added:
openejb/trunk/openejb3/examples/simple-singleton/src/test/java/org/superbiz/registry/PropertiesRegistryBeanTest.java
URL:
http://svn.apache.org/viewvc/openejb/trunk/openejb3/examples/simple-singleton/src/test/java/org/superbiz/registry/PropertiesRegistryBeanTest.java?rev=673551&view=auto
==============================================================================
---
openejb/trunk/openejb3/examples/simple-singleton/src/test/java/org/superbiz/registry/PropertiesRegistryBeanTest.java
(added)
+++
openejb/trunk/openejb3/examples/simple-singleton/src/test/java/org/superbiz/registry/PropertiesRegistryBeanTest.java
Wed Jul 2 17:06:36 2008
@@ -0,0 +1,52 @@
+/**
+ * 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.registry;
+
+import junit.framework.TestCase;
+
+import javax.naming.Context;
+import javax.naming.InitialContext;
+import java.util.Properties;
+
+public class PropertiesRegistryBeanTest extends TestCase {
+
+ public void test() throws Exception {
+ Properties props = new Properties();
+ props.setProperty(Context.INITIAL_CONTEXT_FACTORY,
"org.apache.openejb.client.LocalInitialContextFactory");
+
+ InitialContext context = new InitialContext(props);
+
+ PropertyRegistry refOne = (PropertyRegistry)
context.lookup("PropertyRegistryBeanLocal");
+
+ PropertyRegistry refTwo = (PropertyRegistry)
context.lookup("PropertyRegistryBeanLocal");
+
+
+ refOne.setProperty("url", "http://superbiz.org");
+
+ String url = refTwo.getProperty("url");
+
+ assertEquals("http://superbiz.org", url);
+
+
+ refTwo.setProperty("version", "1.0.5");
+
+ String version = refOne.getProperty("version");
+
+ assertEquals("1.0.5", version);
+
+ }
+}