Clone URL (Committers only):
https://cms.apache.org/redirect?new=anonymous;action=diff;uri=http://tomee.apache.org/examples-trunk%2Fsimple-singleton%2FREADME.md
Fabrizio Giordano
Index: trunk/content/examples-trunk/simple-singleton/README.md
===================================================================
--- trunk/content/examples-trunk/simple-singleton/README.md (revision
1585628)
+++ trunk/content/examples-trunk/simple-singleton/README.md (working copy)
@@ -66,59 +66,40 @@
package org.superbiz.registry;
- import org.junit.Assert;
- import org.junit.AfterClass;
- import org.junit.Test;
+import javax.ejb.Lock;
+import javax.ejb.Singleton;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.Map;
- import javax.ejb.embeddable.EJBContainer;
- import javax.naming.Context;
- import java.net.URI;
- import java.util.Collection;
- import java.util.Date;
+import static javax.ejb.LockType.READ;
+import static javax.ejb.LockType.WRITE;
- public class ComponentRegistryTest {
+@Singleton
+@Lock(READ)
+public class ComponentRegistry {
- private final static EJBContainer ejbContainer =
EJBContainer.createEJBContainer();
+ private final Map<Class, Object> components = new HashMap<Class, Object>();
- @Test
- public void oneInstancePerMultipleReferences() throws Exception {
+ public <T> T getComponent(Class<T> type) {
+ return (T) components.get(type);
+ }
- final Context context = ejbContainer.getContext();
+ public Collection<?> getComponents() {
+ return new ArrayList(components.values());
+ }
- // Both references below will point to the exact same instance
- ComponentRegistry one = (ComponentRegistry)
context.lookup("java:global/simple-singleton/ComponentRegistry");
- ComponentRegistry two = (ComponentRegistry)
context.lookup("java:global/simple-singleton/ComponentRegistry");
+ @Lock(WRITE)
+ public <T> T setComponent(Class<T> type, T value) {
+ return (T) components.put(type, value);
+ }
- URI expectedUri = new URI("foo://bar/baz");
- one.setComponent(URI.class, expectedUri);
- URI actualUri = two.getComponent(URI.class);
- Assert.assertSame(expectedUri, actualUri);
-
- two.removeComponent(URI.class);
- URI uri = one.getComponent(URI.class);
- Assert.assertNull(uri);
-
- Date expectedDate = new Date();
- two.setComponent(Date.class, expectedDate);
- Date actualDate = one.getComponent(Date.class);
- Assert.assertSame(expectedDate, actualDate);
-
- one.removeComponent(URI.class);
- uri = two.getComponent(URI.class);
- Assert.assertNull(uri);
-
- Collection<?> collection = one.getComponents();
- Assert.assertTrue("Reference 'one' - ComponentRegistry is
empty",collection.isEmpty());
-
- collection = two.getComponents();
- Assert.assertTrue("Reference 'two' - ComponentRegistry is
empty",collection.isEmpty());
- }
-
- @AfterClass
- public static void closeEjbContainer() {
- ejbContainer.close();
- }
+ @Lock(WRITE)
+ public <T> T removeComponent(Class<T> type) {
+ return (T) components.remove(type);
}
+}
Unless specified explicitly on the bean class or a method, the default `@Lock`
value is `@Lock(WRITE)`. The code above uses the `@Lock(READ)` annotation on
bean class to change the default so that multi-threaded access is granted by
default. We then only need to apply the `@Lock(WRITE)` annotation to the
methods that modify the state of the bean.