Title: [2529] trunk/openejb3/server/openejb-persistence/src/test/java/org/openejb/persistence: Make the JNDI more correct

Diff

Modified: trunk/openejb3/server/openejb-persistence/src/main/java/org/openejb/persistence/PersistenceDeployer.java (2528 => 2529)

--- trunk/openejb3/server/openejb-persistence/src/main/java/org/openejb/persistence/PersistenceDeployer.java	2006-03-05 23:00:10 UTC (rev 2528)
+++ trunk/openejb3/server/openejb-persistence/src/main/java/org/openejb/persistence/PersistenceDeployer.java	2006-03-06 21:19:52 UTC (rev 2529)
@@ -25,7 +25,10 @@
 import java.util.Map;
 import java.util.Properties;
 
+import javax.naming.CompositeName;
+import javax.naming.Context;
 import javax.naming.InitialContext;
+import javax.naming.NameNotFoundException;
 import javax.naming.NamingException;
 import javax.persistence.EntityManagerFactory;
 import javax.persistence.spi.PersistenceProvider;
@@ -47,7 +50,7 @@
 import org.xml.sax.helpers.XMLFilterImpl;
 
 public class PersistenceDeployer {
-    
+
     public static final String PERSISTENCE_SCHEMA = "http://java.sun.com/xml/ns/persistence";
 
     public static final String PROVIDER_PROP = "javax.persistence.provider";
@@ -57,9 +60,9 @@
     public static final String JTADATASOURCE_PROP = "javax.persistence.jtaDataSource";
 
     public static final String NON_JTADATASOURCE_PROP = "javax.persistence.nonJtaDataSource";
-    
-    public static final String FACTORY_JNDI_ROOT = "java://openejb/PersistenceFactories";
 
+    public static final String FACTORY_JNDI_ROOT = "java:openejb/PersistenceFactories";
+
     private Properties jndiProperties = null;
 
     private InitialContext initialContext = null;
@@ -96,8 +99,8 @@
         nonJtaDataSourceEnv = System.getProperty(NON_JTADATASOURCE_PROP);
     }
 
-    public void loadPersistence(ClassLoader cl,
-            URL url) throws PersistenceDeployerException {
+    public void loadPersistence(ClassLoader cl, URL url)
+            throws PersistenceDeployerException {
 
         try {
 
@@ -202,10 +205,11 @@
                         .newInstance();
                 EntityManagerFactory emf = persistenceProvider
                         .createContainerManagerFactory(unitInfo);
-                
-                //Store EntityManagerFactory in the JNDI
-                initialContext.bind(FACTORY_JNDI_ROOT + "/" + unitInfo.getPersistenceUnitName(), emf);
-                
+
+                // Store EntityManagerFactory in the JNDI
+                bind(FACTORY_JNDI_ROOT + "/"
+                        + unitInfo.getPersistenceUnitName(), emf);
+
             }
         } catch (Exception e) {
             throw new PersistenceDeployerException(e);
@@ -243,7 +247,7 @@
                     persistenceDescriptor));
 
             return (org.openejb.persistence.Persistence) u.unmarshal(source);
-            
+
         } finally {
             if (persistenceDescriptor != null)
                 persistenceDescriptor.close();
@@ -270,6 +274,30 @@
         return factoryList;
     }
 
+    private void bind(String name, Object obj) throws NamingException {
+        if (name.startsWith("java:"))
+            name = name.substring(5);
+        
+        CompositeName composite = new CompositeName(name);
+        Context ctx = initialContext;
+        if (composite.size() > 1) {
+            for (int i = 0; i < composite.size() - 1; i++) {
+                try {
+                    Object ctxObj = ctx.lookup(composite.get(i));
+                    if (!(ctxObj instanceof Context)) {
+                        throw new NamingException("Invalid JNDI path.");
+                    }
+                    ctx = (Context) ctxObj;
+                } catch (NameNotFoundException e) {
+                    //Name was not found, so add a new subcontext
+                    ctx = ctx.createSubcontext(composite.get(i));
+                }
+            }
+        }
+        
+        ctx.bind(composite.get(composite.size() -1 ), obj);
+    }
+
     // Inject the proper namespace
     class PersistenceFilter extends XMLFilterImpl {
 

Modified: trunk/openejb3/server/openejb-persistence/src/test/java/org/openejb/persistence/JNDIContext.java (2528 => 2529)

--- trunk/openejb3/server/openejb-persistence/src/test/java/org/openejb/persistence/JNDIContext.java	2006-03-05 23:00:10 UTC (rev 2528)
+++ trunk/openejb3/server/openejb-persistence/src/test/java/org/openejb/persistence/JNDIContext.java	2006-03-06 21:19:52 UTC (rev 2529)
@@ -19,34 +19,81 @@
 import java.util.Hashtable;
 
 import javax.naming.Binding;
+import javax.naming.CompositeName;
 import javax.naming.Context;
 import javax.naming.Name;
+import javax.naming.NameAlreadyBoundException;
 import javax.naming.NameClassPair;
+import javax.naming.NameNotFoundException;
 import javax.naming.NameParser;
 import javax.naming.NamingEnumeration;
 import javax.naming.NamingException;
 
 public class JNDIContext implements Context {
+
+    private Hashtable<String, Object> db = new Hashtable<String, Object>();
+
+    private int level = 0;
+
+    protected JNDIContext() {
+    }
+
+    private JNDIContext(int nextLevel) {
+        level = nextLevel;
+    }
     
-    private static Hashtable<String, Object> db = new Hashtable<String,Object>();
-
+    public void clear(){
+       db.clear(); 
+    }
+    
     public Object lookup(Name arg0) throws NamingException {
         return null;
     }
 
-    public Object lookup(String arg0) throws NamingException {
-        Object value = db.get(arg0);
+    public Object lookup(String name) throws NamingException {
+        if (name.startsWith("java:"))
+            name = name.substring(5);
+
+        CompositeName composite = new CompositeName(name);
+
+        String segment = composite.get(0);
+        Object value = db.get(segment);
+        if (value instanceof JNDIContext) {
+            if (composite.size() > 1)
+                return ((JNDIContext) value).lookup(composite.getSuffix(1)
+                        .toString());
+        }
+
         if (value == null)
-            throw new NamingException(arg0 + " not found.");
-        
+            throw new NameNotFoundException();
+
         return value;
     }
 
     public void bind(Name arg0, Object arg1) throws NamingException {
     }
 
-    public void bind(String arg0, Object arg1) throws NamingException {
-        db.put(arg0, arg1);
+    public void bind(String name, Object arg1) throws NamingException {
+        if (name.startsWith("java:"))
+            name = name.substring(5);
+
+        if (name.equals(""))
+            throw new NamingException(name + "is not valid.");
+
+        CompositeName composite = new CompositeName(name);
+
+        String segment = composite.get(0);
+        Object value = db.get(segment);
+        if (value instanceof JNDIContext) {
+            if (composite.size() > 1)
+                ((JNDIContext) value).bind(composite.getSuffix(1).toString(),
+                        arg1);
+        }
+
+        if (value != null)
+            throw new NameAlreadyBoundException();
+
+        db.put(name, arg1);
     }
 
     public void rebind(Name arg0, Object arg1) throws NamingException {
@@ -59,8 +106,28 @@
     public void unbind(Name arg0) throws NamingException {
     }
 
-    public void unbind(String arg0) throws NamingException {
-        db.remove(arg0);
+    public void unbind(String name) throws NamingException {
+        if (name.startsWith("java:"))
+            name = name.substring(5);
+
+        if (name.equals(""))
+            throw new NamingException(name + "is not valid.");
+
+        CompositeName composite = new CompositeName(name);
+        String segment = composite.get(0);
+        Object value = db.get(segment);
+        if (value == null)
+            throw new NameNotFoundException();
+
+        if (value instanceof JNDIContext) {
+            if (composite.size() > 1){
+                ((JNDIContext) value).unbind(composite.getSuffix(1).toString());
+                return;
+            }else
+                throw new NameNotFoundException("Cannot unbind a subcontext.");
+        }
+
+        db.remove(segment);
     }
 
     public void rename(Name arg0, Name arg1) throws NamingException {
@@ -92,15 +159,31 @@
     public void destroySubcontext(Name arg0) throws NamingException {
     }
 
-    public void destroySubcontext(String arg0) throws NamingException {
+    public void destroySubcontext(String name) throws NamingException {
+        if (name.startsWith("java:"))
+            name = name.substring(5);
+        Object value = db.get(name);
+        if (value instanceof Context)
+            db.remove(name);
+        else
+            throw new NameNotFoundException();
     }
 
     public Context createSubcontext(Name arg0) throws NamingException {
         return null;
     }
 
-    public Context createSubcontext(String arg0) throws NamingException {
-        return null;
+    public Context createSubcontext(String name) throws NamingException {
+        if (name.startsWith("java:"))
+            name = name.substring(5);
+
+        Object value = db.get(name);
+        if (value != null)
+            throw new NameAlreadyBoundException();
+
+        Context ctx = new JNDIContext(level + 1);
+        db.put(name, ctx);
+        return ctx;
     }
 
     public Object lookupLink(Name arg0) throws NamingException {

Modified: trunk/openejb3/server/openejb-persistence/src/test/java/org/openejb/persistence/PersistenceTest.java (2528 => 2529)

--- trunk/openejb3/server/openejb-persistence/src/test/java/org/openejb/persistence/PersistenceTest.java	2006-03-05 23:00:10 UTC (rev 2528)
+++ trunk/openejb3/server/openejb-persistence/src/test/java/org/openejb/persistence/PersistenceTest.java	2006-03-06 21:19:52 UTC (rev 2529)
@@ -21,8 +21,10 @@
 import java.util.List;
 import java.util.Properties;
 
+import javax.naming.CompositeName;
 import javax.naming.Context;
 import javax.naming.InitialContext;
+import javax.naming.NamingException;
 import javax.persistence.EntityManager;
 import javax.persistence.EntityManagerFactory;
 import javax.persistence.spi.PersistenceUnitInfo;
@@ -34,7 +36,7 @@
 
     private static final String DATASOURCE_NAME = "jdbc/MyDataSource";
 
-    private DataSource ds = new FakeDataSource(); 
+    private DataSource ds = new FakeDataSource();
 
     private Context ctx = null;
 
@@ -58,7 +60,7 @@
 
             assertEntityManagerFactory(emf);
         } finally {
-            ctx.unbind(PersistenceDeployer.FACTORY_JNDI_ROOT + "/TestUnit");
+            cleanupJNDI(PersistenceDeployer.FACTORY_JNDI_ROOT + "/TestUnit");
         }
     }
 
@@ -76,7 +78,7 @@
 
             assertEntityManagerFactory(emf);
         } finally {
-            ctx.unbind(PersistenceDeployer.FACTORY_JNDI_ROOT + "/TestUnit");
+            cleanupJNDI(PersistenceDeployer.FACTORY_JNDI_ROOT + "/TestUnit");
         }
     }
 
@@ -108,6 +110,25 @@
 
     }
 
+    private void cleanupJNDI(String jndi) throws Exception {
+        CompositeName composite = new CompositeName(jndi);
+        for (int i = composite.size(); i > 0; i--) {
+            try {
+                Object value = ctx.lookup(composite.getPrefix(i).toString());
+                if (value instanceof Context) {
+                    Object parent = ctx;
+                    if (i > 1)
+                        parent = ctx.lookup(composite.getPrefix(i - 1)
+                                .toString());
+                    ((Context) parent).destroySubcontext(composite.get(i - 1));
+                } else
+                    ctx.unbind(composite.getPrefix(i).toString());
+            } catch (NamingException e) {
+                // Just ignore it
+            }
+        }
+    }
+
     @Override
     protected void setUp() throws Exception {
         super.setUp();
@@ -121,16 +142,17 @@
 
         ctx = new InitialContext(env);
 
-        ctx.bind(DATASOURCE_NAME, ds);
-
+        ctx.createSubcontext("jdbc").bind("MyDataSource", ds);
     }
 
     @Override
     protected void tearDown() throws Exception {
         super.tearDown();
 
+        cleanupJNDI("jdbc/MyDataSource");
         if (previousFactory != null)
-            System.setProperty(Context.INITIAL_CONTEXT_FACTORY,
+            System
+                    .setProperty(Context.INITIAL_CONTEXT_FACTORY,
                             previousFactory);
     }
 

Reply via email to