You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@beehive.apache.org by cr...@apache.org on 2006/05/25 02:16:40 UTC
svn commit: r409270 - in /beehive/trunk/netui:
src/pageflow/org/apache/beehive/netui/pageflow/requeststate/NameService.java
test/src/junitTests/org/apache/beehive/netui/test/nameservice/NameServiceTest.java
Author: crogers
Date: Wed May 24 17:16:40 2006
New Revision: 409270
URL: http://svn.apache.org/viewvc?rev=409270&view=rev
Log:
Additional change for http://issues.apache.org/jira/browse/BEEHIVE-1112 - to make NameSevice implement Serializable. Implemented readObject() so that when the NameService is de-serialized, the _nameMap will be no longer be null. I've also updated the NameService junit test to do a serlialize/de-serialize.
tests: NetUI BVT (winXP pass)
Modified:
beehive/trunk/netui/src/pageflow/org/apache/beehive/netui/pageflow/requeststate/NameService.java
beehive/trunk/netui/test/src/junitTests/org/apache/beehive/netui/test/nameservice/NameServiceTest.java
Modified: beehive/trunk/netui/src/pageflow/org/apache/beehive/netui/pageflow/requeststate/NameService.java
URL: http://svn.apache.org/viewvc/beehive/trunk/netui/src/pageflow/org/apache/beehive/netui/pageflow/requeststate/NameService.java?rev=409270&r1=409269&r2=409270&view=diff
==============================================================================
--- beehive/trunk/netui/src/pageflow/org/apache/beehive/netui/pageflow/requeststate/NameService.java (original)
+++ beehive/trunk/netui/src/pageflow/org/apache/beehive/netui/pageflow/requeststate/NameService.java Wed May 24 17:16:40 2006
@@ -18,8 +18,14 @@
package org.apache.beehive.netui.pageflow.requeststate;
import javax.servlet.http.HttpSession;
+import java.io.IOException;
+import java.io.ObjectInputStream;
import java.lang.ref.WeakReference;
-import java.util.*;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.Set;
/**
* This class implements a service that will name and track objects which implement the
@@ -40,7 +46,7 @@
// static value for situation where this is not stored in the session.
private static NameService _nameService;
- final private transient HashMap/*<String,WeakReference>*/ _nameMap = new HashMap();
+ private transient HashMap/*<String,WeakReference>*/ _nameMap = new HashMap();
private int _nextValue;
private ArrayList _listeners;
@@ -87,6 +93,22 @@
}
/**
+ * Deserialize an instance of this class.
+ * @param in ObjectInputStream to deserialize from.
+ * @throws IOException
+ * @throws ClassNotFoundException
+ */
+ private synchronized void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
+ in.defaultReadObject();
+
+ // the transient _nameMap is null after an instance of
+ // NameService has been serialized.
+ if (_nameMap == null) {
+ _nameMap = new HashMap();
+ }
+ }
+
+ /**
* This method will add a <code>NamingObjectListener</code> to the set of listeners for the NamingObject event.
* @param nol The <code>NamingObjectListener</code> to add as a listener. This must not be null.
* @throws IllegalArgumentException when nol is null.
@@ -183,6 +205,9 @@
if (name == null)
throw new IllegalStateException("name must not be null");
+ if (_nameMap == null)
+ return null;
+
TrackingObject to = (TrackingObject) _nameMap.get(name);
// The object wasn't found
@@ -214,6 +239,9 @@
if (name == null)
throw new IllegalStateException("name must not be null");
+ if (_nameMap == null)
+ return null;
+
TrackingObject to = (TrackingObject) _nameMap.get(name);
// The object wasn't found
@@ -240,6 +268,9 @@
*/
private void reclaimSpace()
{
+ if (_nameMap == null)
+ return;
+
if (_nameMap.size() > 0 && _nameMap.size() % _reclaimPoint == 0) {
synchronized (_nameMap) {
Set s = _nameMap.entrySet();
Modified: beehive/trunk/netui/test/src/junitTests/org/apache/beehive/netui/test/nameservice/NameServiceTest.java
URL: http://svn.apache.org/viewvc/beehive/trunk/netui/test/src/junitTests/org/apache/beehive/netui/test/nameservice/NameServiceTest.java?rev=409270&r1=409269&r2=409270&view=diff
==============================================================================
--- beehive/trunk/netui/test/src/junitTests/org/apache/beehive/netui/test/nameservice/NameServiceTest.java (original)
+++ beehive/trunk/netui/test/src/junitTests/org/apache/beehive/netui/test/nameservice/NameServiceTest.java Wed May 24 17:16:40 2006
@@ -17,6 +17,11 @@
*/
package org.apache.beehive.netui.test.nameservice;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
@@ -66,7 +71,6 @@
assertEquals(no1,nameable);
}
-
public void testNameServiceErrors()
{
NameService ns = NameService.staticInstance();
@@ -104,5 +108,86 @@
}
assertFalse("Didn't fail the get when we passed a null string",error);
+ }
+
+ public void testNameServiceSerialization()
+ {
+ NameService ns = NameService.staticInstance();
+ assertNotNull("The NameService was returned as null", ns);
+
+ // create a nameable object and the name it.
+ NameableObject no1 = new NameableObject();
+ ns.nameObject("Nameable", no1);
+ ns.put(no1);
+
+ // get the object back and verify that it was the same object
+ INameable nameable = ns.get(no1.getObjectName());
+ assertEquals(no1, nameable);
+
+ //System.out.println("Try to serialize NS");
+ byte[] ser = serializeIt(ns);
+ assertNotNull("The NameService was not serialized correctly", ser);
+ //System.out.println("Try to deserialize NS");
+ NameService rehydratedNS = null;
+ try {
+ rehydratedNS = (NameService) deserializeIt(ser);
+ assertNotNull("The NameService was not deserialized correctly",
+ rehydratedNS);
+ }
+ catch (ClassNotFoundException cnfe) {
+ fail("Test failed with a ClassNotFoundException: "
+ + cnfe.getMessage());
+ }
+ INameable nameable2 = rehydratedNS.get(no1.getObjectName());
+ assertTrue(nameable2 == null);
+
+ rehydratedNS.put(no1);
+ nameable2 = rehydratedNS.get(no1.getObjectName());
+ assertEquals(no1, nameable2);
+ }
+
+ private static byte[] serializeIt(Object obj)
+ {
+ byte[] rval = null;
+ ObjectOutputStream oos = null;
+
+ try {
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ oos = new ObjectOutputStream(baos);
+ oos.writeObject(obj);
+ oos.flush();
+ rval = baos.toByteArray();
+ }
+ catch (IOException e) {
+ fail("Test failed to serialize the object: " + e.getMessage());
+ //System.out.println(e);
+ }
+ finally {
+ try { oos.close(); } catch (IOException e) { };
+ }
+
+ return rval;
+ }
+
+ private static Object deserializeIt(byte[] arr)
+ throws ClassNotFoundException
+ {
+ Object rval = null;
+ ObjectInputStream is = null;
+
+ try {
+ ByteArrayInputStream bais = new ByteArrayInputStream(arr);
+ is = new ObjectInputStream(bais);
+ rval = is.readObject();
+ }
+ catch (IOException e) {
+ fail("Test failed to de-serialize the object: " + e.getMessage());
+ //System.out.println(e);
+ }
+ finally {
+ try { is.close(); } catch (IOException e) { };
+ }
+
+ return rval;
}
}