[jboss-cvs] JBossAS SVN: r71371 - in projects/ejb3/trunk: attachments and 17 other directories.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Fri Mar 28 08:08:06 EDT 2008


Author: wolfc
Date: 2008-03-28 08:08:06 -0400 (Fri, 28 Mar 2008)
New Revision: 71371

Added:
   projects/ejb3/trunk/attachments/
   projects/ejb3/trunk/attachments/.classpath
   projects/ejb3/trunk/attachments/.project
   projects/ejb3/trunk/attachments/.settings/
   projects/ejb3/trunk/attachments/.settings/org.eclipse.jdt.core.prefs
   projects/ejb3/trunk/attachments/pom.xml
   projects/ejb3/trunk/attachments/src/
   projects/ejb3/trunk/attachments/src/main/
   projects/ejb3/trunk/attachments/src/main/java/
   projects/ejb3/trunk/attachments/src/main/java/org/
   projects/ejb3/trunk/attachments/src/main/java/org/jboss/
   projects/ejb3/trunk/attachments/src/main/java/org/jboss/ejb3/
   projects/ejb3/trunk/attachments/src/main/java/org/jboss/ejb3/attachments/
   projects/ejb3/trunk/attachments/src/main/java/org/jboss/ejb3/attachments/AttachmentNotFoundException.java
   projects/ejb3/trunk/attachments/src/main/java/org/jboss/ejb3/attachments/ObjectAttachments.java
   projects/ejb3/trunk/attachments/src/test/
   projects/ejb3/trunk/attachments/src/test/java/
   projects/ejb3/trunk/attachments/src/test/java/org/
   projects/ejb3/trunk/attachments/src/test/java/org/jboss/
   projects/ejb3/trunk/attachments/src/test/java/org/jboss/ejb3/
   projects/ejb3/trunk/attachments/src/test/java/org/jboss/ejb3/test/
   projects/ejb3/trunk/attachments/src/test/java/org/jboss/ejb3/test/attachments/
   projects/ejb3/trunk/attachments/src/test/java/org/jboss/ejb3/test/attachments/basic/
   projects/ejb3/trunk/attachments/src/test/java/org/jboss/ejb3/test/attachments/basic/FinalizableObject.java
   projects/ejb3/trunk/attachments/src/test/java/org/jboss/ejb3/test/attachments/basic/unit/
   projects/ejb3/trunk/attachments/src/test/java/org/jboss/ejb3/test/attachments/basic/unit/BasicTestCase.java
Log:
Object attachments implementation


Property changes on: projects/ejb3/trunk/attachments
___________________________________________________________________
Name: svn:ignore
   + eclipse-target
target


Added: projects/ejb3/trunk/attachments/.classpath
===================================================================
--- projects/ejb3/trunk/attachments/.classpath	                        (rev 0)
+++ projects/ejb3/trunk/attachments/.classpath	2008-03-28 12:08:06 UTC (rev 71371)
@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<classpath>
+	<classpathentry kind="src" path="src/main/java"/>
+	<classpathentry kind="src" output="eclipse-target/tests-classes" path="src/test/java"/>
+	<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
+	<classpathentry kind="con" path="org.maven.ide.eclipse.MAVEN2_CLASSPATH_CONTAINER"/>
+	<classpathentry kind="output" path="eclipse-target/classes"/>
+</classpath>

Added: projects/ejb3/trunk/attachments/.project
===================================================================
--- projects/ejb3/trunk/attachments/.project	                        (rev 0)
+++ projects/ejb3/trunk/attachments/.project	2008-03-28 12:08:06 UTC (rev 71371)
@@ -0,0 +1,25 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<projectDescription>
+	<name>jboss-ejb3-attachments</name>
+	<comment>With JBoss EJB 3 Attachments objects can be attached to an
+    object. The attached objects will then be retained until 
+    the attachee is garbage collected.</comment>
+	<projects>
+	</projects>
+	<buildSpec>
+		<buildCommand>
+			<name>org.maven.ide.eclipse.maven2Builder</name>
+			<arguments>
+			</arguments>
+		</buildCommand>
+		<buildCommand>
+			<name>org.eclipse.jdt.core.javabuilder</name>
+			<arguments>
+			</arguments>
+		</buildCommand>
+	</buildSpec>
+	<natures>
+		<nature>org.eclipse.jdt.core.javanature</nature>
+		<nature>org.maven.ide.eclipse.maven2Nature</nature>
+	</natures>
+</projectDescription>

Added: projects/ejb3/trunk/attachments/.settings/org.eclipse.jdt.core.prefs
===================================================================
--- projects/ejb3/trunk/attachments/.settings/org.eclipse.jdt.core.prefs	                        (rev 0)
+++ projects/ejb3/trunk/attachments/.settings/org.eclipse.jdt.core.prefs	2008-03-28 12:08:06 UTC (rev 71371)
@@ -0,0 +1,5 @@
+#Fri Mar 28 09:59:23 CET 2008
+org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5
+eclipse.preferences.version=1
+org.eclipse.jdt.core.compiler.source=1.5
+org.eclipse.jdt.core.compiler.compliance=1.5

Added: projects/ejb3/trunk/attachments/pom.xml
===================================================================
--- projects/ejb3/trunk/attachments/pom.xml	                        (rev 0)
+++ projects/ejb3/trunk/attachments/pom.xml	2008-03-28 12:08:06 UTC (rev 71371)
@@ -0,0 +1,31 @@
+<!-- 
+  vi:ts=2:sw=2:expandtab 
+-->
+<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">
+  <parent>
+    <groupId>org.jboss.ejb3</groupId>
+    <artifactId>jboss-ejb3-build</artifactId>
+    <version>0.13.0-SNAPSHOT</version>
+    <relativePath>../build/pom.xml</relativePath>
+  </parent>
+  <modelVersion>4.0.0</modelVersion>
+  <artifactId>jboss-ejb3-attachments</artifactId>
+  <version>0.1.0-SNAPSHOT</version>
+  <packaging>jar</packaging>
+  <name>JBoss EJB 3.0 Attachments</name>
+  <url>http://www.jboss.org</url>
+  <description>
+    With JBoss EJB 3 Attachments objects can be attached to a master
+    object. The attached objects will then be retained until 
+    the master is garbage collected.
+  </description>
+  
+  <dependencies>
+    <dependency>
+      <groupId>junit</groupId>
+      <artifactId>junit</artifactId>
+      <scope>test</scope>
+    </dependency>
+  </dependencies>
+</project>

Added: projects/ejb3/trunk/attachments/src/main/java/org/jboss/ejb3/attachments/AttachmentNotFoundException.java
===================================================================
--- projects/ejb3/trunk/attachments/src/main/java/org/jboss/ejb3/attachments/AttachmentNotFoundException.java	                        (rev 0)
+++ projects/ejb3/trunk/attachments/src/main/java/org/jboss/ejb3/attachments/AttachmentNotFoundException.java	2008-03-28 12:08:06 UTC (rev 71371)
@@ -0,0 +1,38 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2008, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+  *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.ejb3.attachments;
+
+/**
+ * Thrown if the requested attachment can not be found.
+ * 
+ * @author <a href="mailto:carlo.dewolf at jboss.com">Carlo de Wolf</a>
+ * @version $Revision: $
+ */
+public class AttachmentNotFoundException extends Exception
+{
+   private static final long serialVersionUID = 1L;
+
+   protected AttachmentNotFoundException(Object master, Object identifier, Class<?> expectedType)
+   {
+      super("Unable to find attachment '" + identifier + "' on '" + master + "'");
+   }
+}

Added: projects/ejb3/trunk/attachments/src/main/java/org/jboss/ejb3/attachments/ObjectAttachments.java
===================================================================
--- projects/ejb3/trunk/attachments/src/main/java/org/jboss/ejb3/attachments/ObjectAttachments.java	                        (rev 0)
+++ projects/ejb3/trunk/attachments/src/main/java/org/jboss/ejb3/attachments/ObjectAttachments.java	2008-03-28 12:08:06 UTC (rev 71371)
@@ -0,0 +1,113 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2008, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+  *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.ejb3.attachments;
+
+import java.util.Map;
+import java.util.WeakHashMap;
+import java.util.concurrent.ConcurrentHashMap;
+
+/**
+ * Allow for objects to be attached to another object (called the master). This will
+ * ensure that once the master is garbage collected all attachments are garbage
+ * collected as wel. Note that care must be taken that the attachment in no way
+ * has a strong reference to the master.
+ * 
+ * @author <a href="mailto:carlo.dewolf at jboss.com">Carlo de Wolf</a>
+ * @version $Revision: $
+ */
+public class ObjectAttachments
+{
+   private static WeakHashMap<Object, Map<Object, Object>> globalAttachments = new WeakHashMap<Object, Map<Object,Object>>();
+   
+   /**
+    * For debugging purposes.
+    * 
+    * Once a master is GCed the entry is put on a queue in WeakHashMap. To
+    * really cleanup the entry it must be expunged.
+    */
+   public static void expungeStaleEntries()
+   {
+      // internal knowledge of WeakHashMap
+      globalAttachments.size();
+   }
+   
+   /**
+    * Get a given attachment from an object.
+    * 
+    * @param <T>
+    * @param master         the master object
+    * @param identifier     the identifier of the attachment
+    * @param expectedType   the class of the attachment
+    * @return               the attachment
+    * @throws AttachmentNotFoundException   if no attachment could be found
+    * @throws ClassCastException            if the attachment is of a different class
+    */
+   public static <T> T getAttachment(Object master, Object identifier, Class<T> expectedType) throws AttachmentNotFoundException
+   {
+      if(master == null) throw new NullPointerException("master is null");
+      if(identifier == null) throw new NullPointerException("identifier is null");
+      
+      Map<Object, ?> attachments = getAttachments(master);
+      Object attachment = attachments.get(identifier);
+      if(attachment == null)
+         throw new AttachmentNotFoundException(master, identifier, expectedType);
+      Class<?> attachmentClass = attachment.getClass();
+      if(expectedType.isAssignableFrom(attachmentClass))
+         return expectedType.cast(attachment);
+      throw new ClassCastException("Expected type " + expectedType + " for attachment '" + identifier + "' on object '" + master + "', but was " + attachmentClass);
+   }
+   
+   private static Map<Object, Object> getAttachments(Object master)
+   {
+      Map<Object, Object> attachments = globalAttachments.get(master);
+      if(attachments == null)
+      {
+         synchronized (master)
+         {
+            attachments = globalAttachments.get(master);
+            if(attachments == null)
+            {
+               attachments = new ConcurrentHashMap<Object, Object>();
+               globalAttachments.put(master, attachments);
+            }
+         }
+      }
+      return attachments;
+   }
+   
+   /**
+    * Attach an object to a master object.
+    * 
+    * @param master         the master object
+    * @param identifier     the identifier of the attachment
+    * @param attachment     the attachment
+    */
+   public static void setAttachment(Object master, Object identifier, Object attachment)
+   {
+      if(master == null) throw new NullPointerException("master is null");
+      if(identifier == null) throw new NullPointerException("identifier is null");
+      if(attachment == null) throw new NullPointerException("attachment is null");
+      
+      Map<Object, Object> attachments = getAttachments(master);
+      attachments.put(identifier, attachment);
+   }
+}

Added: projects/ejb3/trunk/attachments/src/test/java/org/jboss/ejb3/test/attachments/basic/FinalizableObject.java
===================================================================
--- projects/ejb3/trunk/attachments/src/test/java/org/jboss/ejb3/test/attachments/basic/FinalizableObject.java	                        (rev 0)
+++ projects/ejb3/trunk/attachments/src/test/java/org/jboss/ejb3/test/attachments/basic/FinalizableObject.java	2008-03-28 12:08:06 UTC (rev 71371)
@@ -0,0 +1,55 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2008, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+  *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.ejb3.test.attachments.basic;
+
+/**
+ * @author <a href="mailto:carlo.dewolf at jboss.com">Carlo de Wolf</a>
+ * @version $Revision: $
+ */
+public class FinalizableObject
+{
+   public static int finalized = 0;
+   
+   private Object state;
+   
+   public FinalizableObject()
+   {
+      this(null);
+   }
+   
+   public FinalizableObject(Object state)
+   {
+      this.state = state;
+   }
+   
+   @Override
+   protected void finalize() throws Throwable
+   {
+      finalized++;
+      super.finalize();
+   }
+   
+   public Object getState()
+   {
+      return state;
+   }
+}

Added: projects/ejb3/trunk/attachments/src/test/java/org/jboss/ejb3/test/attachments/basic/unit/BasicTestCase.java
===================================================================
--- projects/ejb3/trunk/attachments/src/test/java/org/jboss/ejb3/test/attachments/basic/unit/BasicTestCase.java	                        (rev 0)
+++ projects/ejb3/trunk/attachments/src/test/java/org/jboss/ejb3/test/attachments/basic/unit/BasicTestCase.java	2008-03-28 12:08:06 UTC (rev 71371)
@@ -0,0 +1,153 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2008, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+  *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.ejb3.test.attachments.basic.unit;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.fail;
+
+import org.jboss.ejb3.attachments.AttachmentNotFoundException;
+import org.jboss.ejb3.attachments.ObjectAttachments;
+import org.jboss.ejb3.test.attachments.basic.FinalizableObject;
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ * @author <a href="mailto:carlo.dewolf at jboss.com">Carlo de Wolf</a>
+ * @version $Revision: $
+ */
+public class BasicTestCase
+{
+   private static void gc()
+   {
+      gc1();
+      ObjectAttachments.expungeStaleEntries();
+      gc1();
+   }
+   
+   private static void gc1()
+   {
+      for(int i = 0; i < 3; i++)
+      {
+         System.gc();
+         try
+         {
+            Thread.sleep(100);
+         }
+         catch (InterruptedException e)
+         {
+            // ignore
+         }
+         System.runFinalization();
+      }
+   }
+   
+   @Before
+   public void before()
+   {
+      FinalizableObject.finalized = 0;
+   }
+   
+   @Test
+   public void testFinalizeAttachment() throws Exception
+   {
+      Object master = new FinalizableObject();
+      
+      FinalizableObject attachment = new FinalizableObject();
+      
+      Object identifier = "random key";
+      
+      ObjectAttachments.setAttachment(master, identifier, attachment);
+      
+      attachment = null;
+      master = null;
+      
+      gc();
+      
+      assertEquals(2, FinalizableObject.finalized);
+   }
+
+   @Test
+   public void testKeepAttachmentAlive() throws Exception
+   {
+      Object master = new FinalizableObject();
+      
+      FinalizableObject attachment = new FinalizableObject("Hello world");
+      
+      Object identifier = "random key";
+      
+      ObjectAttachments.setAttachment(master, identifier, attachment);
+      attachment = null;
+      
+      gc();
+      
+      FinalizableObject actual = ObjectAttachments.getAttachment(master, identifier, FinalizableObject.class);
+      
+      assertEquals("Hello world", actual.getState());
+      
+      actual = null;
+      master = null;
+      
+      gc();
+      
+      assertEquals(2, FinalizableObject.finalized);
+   }
+
+   @Test
+   public void testNotFound() throws Exception
+   {
+      Object master = new Object();
+      
+      Object identifier = "not found";
+      
+      try
+      {
+         ObjectAttachments.getAttachment(master, identifier, Object.class);
+         fail("Expected AttachmentNotFoundException");
+      }
+      catch(AttachmentNotFoundException e)
+      {
+         // okay
+      }
+   }
+   
+   @Test
+   public void testSubclass() throws Exception
+   {
+      Object master = new Object();
+      
+      Object attachment = new Object();
+      
+      Object identifier = "random key";
+      
+      ObjectAttachments.setAttachment(master, identifier, attachment);
+      
+      try
+      {
+         ObjectAttachments.getAttachment(master, identifier, String.class);
+         fail("Should have gotten a ClassCastException");
+      }
+      catch(ClassCastException e)
+      {
+         // okay
+      }
+   }
+}




More information about the jboss-cvs-commits mailing list