tomee-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dblev...@apache.org
Subject svn commit: r600036 - in /openejb/trunk/openejb3/server: openejb-client/src/main/java/org/apache/openejb/client/ openejb-ejbd/src/test/java/org/apache/openejb/
Date Sat, 01 Dec 2007 02:12:36 GMT
Author: dblevins
Date: Fri Nov 30 18:12:34 2007
New Revision: 600036

URL: http://svn.apache.org/viewvc?rev=600036&view=rev
Log:
Implemented listBindings for remote clients.  Did it via a special Binding subclass that fetches
the associated object as you iterate and call getObject()

Modified:
    openejb/trunk/openejb3/server/openejb-client/src/main/java/org/apache/openejb/client/JNDIContext.java
    openejb/trunk/openejb3/server/openejb-client/src/main/java/org/apache/openejb/client/NameClassPairEnumeration.java
    openejb/trunk/openejb3/server/openejb-ejbd/src/test/java/org/apache/openejb/JndiTest.java

Modified: openejb/trunk/openejb3/server/openejb-client/src/main/java/org/apache/openejb/client/JNDIContext.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb3/server/openejb-client/src/main/java/org/apache/openejb/client/JNDIContext.java?rev=600036&r1=600035&r2=600036&view=diff
==============================================================================
--- openejb/trunk/openejb3/server/openejb-client/src/main/java/org/apache/openejb/client/JNDIContext.java
(original)
+++ openejb/trunk/openejb3/server/openejb-client/src/main/java/org/apache/openejb/client/JNDIContext.java
Fri Nov 30 18:12:34 2007
@@ -19,22 +19,14 @@
 import org.omg.CORBA.ORB;
 
 import java.io.Serializable;
-import java.io.Externalizable;
-import java.io.ObjectOutput;
-import java.io.IOException;
-import java.io.ObjectInput;
 import java.net.URI;
 import java.net.URISyntaxException;
 import java.net.ConnectException;
 import java.rmi.RemoteException;
 import java.util.Hashtable;
 import java.util.Properties;
-import java.util.Enumeration;
-import java.util.Vector;
 import java.util.ArrayList;
 import java.util.List;
-import java.util.Iterator;
-import java.util.Collections;
 import java.lang.reflect.Constructor;
 import javax.naming.AuthenticationException;
 import javax.naming.ConfigurationException;
@@ -48,6 +40,7 @@
 import javax.naming.OperationNotSupportedException;
 import javax.naming.ServiceUnavailableException;
 import javax.naming.NameClassPair;
+import javax.naming.Binding;
 import javax.naming.spi.InitialContextFactory;
 import javax.sql.DataSource;
 
@@ -332,7 +325,7 @@
         return lookup(name.toString());
     }
 
-    public NamingEnumeration list(String name) throws NamingException {
+    public NamingEnumeration<NameClassPair> list(String name) throws NamingException
{
         if (name == null) throw new InvalidNameException("The name cannot be null");
         else if (name.startsWith("java:")) name = name.replaceFirst("^java:", "");
         else if (!name.startsWith("/")) name = tail + name;
@@ -378,15 +371,53 @@
 
     }
 
-    public NamingEnumeration list(Name name) throws NamingException {
+    public NamingEnumeration<NameClassPair> list(Name name) throws NamingException
{
         return list(name.toString());
     }
 
-    public NamingEnumeration listBindings(String name) throws NamingException {
-        throw new OperationNotSupportedException("Use list(String) instead");
+    public NamingEnumeration<Binding> listBindings(String name) throws NamingException
{
+        Object o = lookup(name);
+        if (o instanceof Context) {
+            Context context = (Context) o;
+            NamingEnumeration<NameClassPair> enumeration = context.list("");
+            List<NameClassPair> bindings = new ArrayList<NameClassPair>();
+
+            while (enumeration.hasMoreElements()) {
+                NameClassPair pair = enumeration.nextElement();
+                bindings.add(new LazyBinding(pair.getName(), pair.getClassName(), context));
+            }
+
+            return new NameClassPairEnumeration(bindings);
+
+        } else {
+            return null;
+        }
+
+    }
+
+    private static class LazyBinding extends Binding {
+        private RuntimeException failed;
+        private Context context;
+
+        public LazyBinding(String name, String className, Context context) {
+            super(name, className, null);
+            this.context = context;
+        }
+
+        public synchronized Object getObject() {
+            if (super.getObject() == null){
+                if (failed != null) throw failed;
+                try {
+                    super.setObject(context.lookup(getName()));
+                } catch (NamingException e) {
+                    throw failed = new RuntimeException("Failed to lazily fetch the binding
'"+getName()+"'", e);
+                }
+            }
+            return super.getObject();
+        }
     }
 
-    public NamingEnumeration listBindings(Name name) throws NamingException {
+    public NamingEnumeration<Binding> listBindings(Name name) throws NamingException{
         return listBindings(name.toString());
     }
 

Modified: openejb/trunk/openejb3/server/openejb-client/src/main/java/org/apache/openejb/client/NameClassPairEnumeration.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb3/server/openejb-client/src/main/java/org/apache/openejb/client/NameClassPairEnumeration.java?rev=600036&r1=600035&r2=600036&view=diff
==============================================================================
--- openejb/trunk/openejb3/server/openejb-client/src/main/java/org/apache/openejb/client/NameClassPairEnumeration.java
(original)
+++ openejb/trunk/openejb3/server/openejb-client/src/main/java/org/apache/openejb/client/NameClassPairEnumeration.java
Fri Nov 30 18:12:34 2007
@@ -18,6 +18,7 @@
 
 import javax.naming.NameClassPair;
 import javax.naming.NamingEnumeration;
+import javax.naming.NamingException;
 import java.io.Externalizable;
 import java.io.ObjectOutput;
 import java.io.IOException;
@@ -30,7 +31,7 @@
 /**
  * The product of a javax.naming.Context.list() method
  */
-public class NameClassPairEnumeration implements NamingEnumeration<NameClassPair>,
Externalizable {
+public class NameClassPairEnumeration<T extends NameClassPair> implements NamingEnumeration<T>,
Externalizable {
 
     private List<NameClassPair> list;
     private Iterator<NameClassPair> iterator;
@@ -57,11 +58,11 @@
         return hasMore();
     }
 
-    public NameClassPair next() {
-        return iterator.next();
+    public T next() {
+        return (T) iterator.next();
     }
 
-    public NameClassPair nextElement() {
+    public T nextElement() {
         return next();
     }
 
@@ -86,6 +87,7 @@
         for (; size > 0; size--) {
             String name = (String) in.readObject();
             String className = (String) in.readObject();
+
             list.add(new NameClassPair(name, className));
         }
 

Modified: openejb/trunk/openejb3/server/openejb-ejbd/src/test/java/org/apache/openejb/JndiTest.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb3/server/openejb-ejbd/src/test/java/org/apache/openejb/JndiTest.java?rev=600036&r1=600035&r2=600036&view=diff
==============================================================================
--- openejb/trunk/openejb3/server/openejb-ejbd/src/test/java/org/apache/openejb/JndiTest.java
(original)
+++ openejb/trunk/openejb3/server/openejb-ejbd/src/test/java/org/apache/openejb/JndiTest.java
Fri Nov 30 18:12:34 2007
@@ -31,6 +31,7 @@
 import javax.naming.NamingException;
 import javax.naming.NameClassPair;
 import javax.naming.NamingEnumeration;
+import javax.naming.Binding;
 import javax.ejb.Remote;
 import java.util.Properties;
 import java.util.Collections;
@@ -83,6 +84,9 @@
             assertNameClassPair(context.list(""));
             assertNameClassPair(context.list("ejb"));
 
+            assertBindings(context.listBindings(""));
+            assertBindings(context.listBindings("ejb"));
+
         } finally {
             serviceDaemon.stop();
             OpenEJB.destroy();
@@ -104,6 +108,31 @@
         assertTrue("PeachRemote", map.containsKey("PeachRemote"));
         assertTrue("PearRemote", map.containsKey("PearRemote"));
         assertTrue("PlumRemote", map.containsKey("PlumRemote"));
+    }
+
+    private void assertBindings(NamingEnumeration<Binding> namingEnumeration) {
+        assertNotNull("namingEnumeration", namingEnumeration);
+
+        Map<String, Object> map = new HashMap<String, Object>();
+        while (namingEnumeration.hasMoreElements()) {
+            Binding pair = namingEnumeration.nextElement();
+            map.put(pair.getName(), pair.getObject());
+        }
+
+        assertTrue("OrangeRemote", map.containsKey("OrangeRemote"));
+        assertTrue("OrangeRemote is FruitRemote", map.get("OrangeRemote") instanceof FruitRemote);
+
+        assertTrue("AppleRemote", map.containsKey("AppleRemote"));
+        assertTrue("AppleRemote is FruitRemote", map.get("AppleRemote") instanceof FruitRemote);
+
+        assertTrue("PeachRemote", map.containsKey("PeachRemote"));
+        assertTrue("PeachRemote is FruitRemote", map.get("PeachRemote") instanceof FruitRemote);
+
+        assertTrue("PearRemote", map.containsKey("PearRemote"));
+        assertTrue("PearRemote is FruitRemote", map.get("PearRemote") instanceof FruitRemote);
+
+        assertTrue("PlumRemote", map.containsKey("PlumRemote"));
+        assertTrue("PlumRemote is FruitRemote", map.get("PlumRemote") instanceof FruitRemote);
     }
 
     @Remote



Mime
View raw message