You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tomee.apache.org by rm...@apache.org on 2012/10/09 20:02:00 UTC

svn commit: r1396142 - in /openejb/trunk/openejb/container/openejb-core/src: main/java/org/apache/openejb/assembler/classic/ main/java/org/apache/openejb/util/ test/java/org/apache/openejb/core/mdb/ test/java/org/apache/openejb/ivm/ test/java/org/apach...

Author: rmannibucau
Date: Tue Oct  9 18:01:59 2012
New Revision: 1396142

URL: http://svn.apache.org/viewvc?rev=1396142&view=rev
Log:
TOMEE-456 unbinding global names + (small jms test fix)

Added:
    openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/util/JndiTreeBrowser.java
    openejb/trunk/openejb/container/openejb-core/src/test/java/org/apache/openejb/ivm/
    openejb/trunk/openejb/container/openejb-core/src/test/java/org/apache/openejb/ivm/naming/
    openejb/trunk/openejb/container/openejb-core/src/test/java/org/apache/openejb/ivm/naming/IvmContextTest.java
Modified:
    openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/Assembler.java
    openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/util/Contexts.java
    openejb/trunk/openejb/container/openejb-core/src/test/java/org/apache/openejb/core/mdb/JmsTest.java

Modified: openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/Assembler.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/Assembler.java?rev=1396142&r1=1396141&r2=1396142&view=diff
==============================================================================
--- openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/Assembler.java (original)
+++ openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/Assembler.java Tue Oct  9 18:01:59 2012
@@ -94,6 +94,7 @@ import org.apache.openejb.spi.SecuritySe
 import org.apache.openejb.util.AsmParameterNameLoader;
 import org.apache.openejb.util.Contexts;
 import org.apache.openejb.util.EventHelper;
+import org.apache.openejb.util.JndiTreeBrowser;
 import org.apache.openejb.util.LogCategory;
 import org.apache.openejb.util.Logger;
 import org.apache.openejb.util.Messages;
@@ -894,10 +895,10 @@ public class Assembler extends Assembler
                     continue;
                 }
 
-                // a bit weird but just to be consistent if user doesn't lookup directly the resource
+                // a bit weird but just < ></>o be consistent if user doesn't lookup directly the resource
                 Context lastContext = Contexts.createSubcontexts(containerSystemContext, path);
                 try {
-                    lastContext.bind(path.substring(path.lastIndexOf("/") + 1, path.length()), value.getValue());
+                    lastContext.rebind(path.substring(path.lastIndexOf("/") + 1, path.length()), value.getValue());
                 } catch (NameAlreadyBoundException nabe) {
                     nabe.printStackTrace();
                 }
@@ -1190,6 +1191,7 @@ public class Assembler extends Assembler
 
         SystemInstance.get().fireEvent(new AssemblerBeforeApplicationDestroyed(appInfo));
 
+        final Context globalContext = containerSystem.getJNDIContext();
         final AppContext appContext = containerSystem.getAppContext(appInfo.appId);
 
         if (null == appContext) {
@@ -1204,6 +1206,8 @@ public class Assembler extends Assembler
 
             final Map<String, Object> cb = appContext.getBindings();
 
+            // dumpJndiTree(globalContext, "\n\nJndi Tree Before unbinds:\n===================\n\n");
+
             for (Map.Entry<String, Object> value : cb.entrySet()) {
                 String path = value.getKey();
                 if (path.startsWith("global")) {
@@ -1213,12 +1217,24 @@ public class Assembler extends Assembler
                     continue;
                 }
 
+                unbind(globalContext, path);
+                unbind(globalContext, "openejb/global/" + path.substring("java:".length()));
+                unbind(globalContext, path.substring("java:global".length()));
+            }
+
+            if (globalContext instanceof IvmContext) {
                 try {
-                    containerSystem.getJNDIContext().unbind(path);
-                } catch (NamingException ignored) {
+                    ((IvmContext) globalContext).prune("java:global");
+                } catch (NamingException e) {
                     // no-op
                 }
             }
+
+            unbind(globalContext, "global/" + appInfo.appId);
+            unbind(globalContext, appInfo.appId);
+            unbind(globalContext, "openejb/global/global/" + appInfo.appId);
+
+            // dumpJndiTree(globalContext, "\n\nJndi Tree After unbinds:\n======================\n\n");
         }
 
         EjbResolver globalResolver = new EjbResolver(null, EjbResolver.Scope.GLOBAL);
@@ -1227,8 +1243,6 @@ public class Assembler extends Assembler
         }
         SystemInstance.get().setComponent(EjbResolver.class, globalResolver);
 
-
-        Context globalContext = containerSystem.getJNDIContext();
         UndeployException undeployException = new UndeployException(messages.format("destroyApplication.failed", appInfo.path));
 
         WebAppBuilder webAppBuilder = SystemInstance.get().getComponent(WebAppBuilder.class);
@@ -1392,11 +1406,10 @@ public class Assembler extends Assembler
             }
         }
 
-        final Context ic = containerSystem.getJNDIContext();
         for (String id : appInfo.resourceIds) {
             final String name = OPENEJB_RESOURCE_JNDI_PREFIX + id;
             try {
-                final Object object = ic.lookup(name);
+                final Object object = globalContext.lookup(name);
                 final String clazz;
                 if (object == null) { // should it be possible?
                     clazz = "?";
@@ -1404,7 +1417,7 @@ public class Assembler extends Assembler
                     clazz = object.getClass().getName();
                 }
                 destroyResource(id, clazz, object);
-                ic.unbind(name);
+                globalContext.unbind(name);
             } catch (NamingException e) {
                 logger.warning("can't unbind resource '{0}'", id);
             }
@@ -1421,6 +1434,23 @@ public class Assembler extends Assembler
         logger.debug("destroyApplication.success", appInfo.path);
     }
 
+    private void unbind(final Context context, final String name) {
+        try {
+            context.unbind(name);
+        } catch (NamingException e) {
+            // no-op
+        }
+    }
+
+    private void dumpJndiTree(final Context containerSystemContext, final String message) {
+        System.out.println(message);
+        try {
+            JndiTreeBrowser.log(containerSystemContext, "foo");
+        } catch (NamingException e) {
+            // no-op
+        }
+    }
+
     public ClassLoader createAppClassLoader(AppInfo appInfo) throws OpenEJBException, IOException {
         Set<URL> jars = new HashSet<URL>();
         for (EjbJarInfo info : appInfo.ejbJars) {

Modified: openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/util/Contexts.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/util/Contexts.java?rev=1396142&r1=1396141&r2=1396142&view=diff
==============================================================================
--- openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/util/Contexts.java (original)
+++ openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/util/Contexts.java Tue Oct  9 18:01:59 2012
@@ -25,6 +25,33 @@ public class Contexts {
         // no-op
     }
 
+    public static void unbindLeaf(final Context context, final String path) {
+        if (path == null) {
+            return;
+        }
+
+        final String[] parts = path.split("/");
+        final String name = parts[parts.length - 1];
+        if (parts.length > 1 && parts[0].startsWith("java:")) {
+            parts[0] = parts[0].substring("java:".length());
+        }
+
+        Context lastContext = context;
+        for (int i = 0; i < parts.length - 1; i++) { // browse it this way to do the same than createSubcontexts
+            try {
+                lastContext = (Context) lastContext.lookup(parts[i]);
+            } catch (NamingException e) {
+                return;
+            }
+        }
+
+        try {
+            lastContext.unbind(name);
+        } catch (NamingException e) {
+            // no-op
+        }
+    }
+
     public static Context createSubcontexts(Context context, String key) {
         final String[] parts = key.split("/");
 

Added: openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/util/JndiTreeBrowser.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/util/JndiTreeBrowser.java?rev=1396142&view=auto
==============================================================================
--- openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/util/JndiTreeBrowser.java (added)
+++ openejb/trunk/openejb/container/openejb-core/src/main/java/org/apache/openejb/util/JndiTreeBrowser.java Tue Oct  9 18:01:59 2012
@@ -0,0 +1,98 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.openejb.util;
+
+import javax.naming.Binding;
+import javax.naming.Context;
+import javax.naming.InitialContext;
+import javax.naming.NamingEnumeration;
+import javax.naming.NamingException;
+
+public class JndiTreeBrowser {
+    private static final String ROOT = "";
+
+    private Context context;
+    private String path;
+
+    private JndiTreeBrowser(Context ctx, String name) {
+        path = name;
+        context = ctx;
+    }
+
+    private JndiTreeBrowser(Context ctx) {
+        this(ctx, ROOT);
+    }
+
+    private JndiTreeBrowser(String name) throws NamingException {
+        this(new InitialContext(), name);
+    }
+
+    private JndiTreeBrowser() throws NamingException {
+        this(new InitialContext(), ROOT);
+    }
+
+    private void runOnTree(JndiNodeWorker worker) throws NamingException {
+        NamingEnumeration<Binding> ne = context.listBindings(ROOT);
+        while (ne.hasMoreElements()) {
+            Binding current = ne.next();
+            Object obj = current.getObject();
+            worker.doWork(path, current.getName(), obj);
+            if (obj instanceof Context) {
+                runOnJndiTree((Context) obj, worker,
+                        path + '/' + current.getName());
+            }
+        }
+    }
+
+    private void runOnJndiTree(Context ctx, JndiNodeWorker worker, String prefix)
+            throws NamingException {
+        new JndiTreeBrowser(ctx, prefix).runOnTree(worker);
+    }
+
+    public static void runOnJndiTree(Context ctx, JndiNodeWorker worker) throws NamingException {
+        new JndiTreeBrowser(ctx).runOnTree(worker);
+    }
+
+    public static void log(Context ctx) throws NamingException {
+        new JndiTreeBrowser(ctx).runOnTree(new LogJndiWorker(null));
+    }
+
+    public static void log(final Context ctx, final String foo) throws NamingException {
+        new JndiTreeBrowser(ctx).runOnTree(new LogJndiWorker(foo));
+    }
+
+    private static interface JndiNodeWorker {
+        void doWork(String path, String name, Object obj);
+    }
+
+    private static class LogJndiWorker implements JndiNodeWorker {
+        private final String filter;
+
+        public LogJndiWorker(final String filter) {
+            this.filter = filter;
+        }
+
+        @Override
+        public void doWork(String path, String name, Object obj) {
+            final String complete = path + "/" + name;
+            if (complete.contains(filter)) {
+                System.out.println(complete);
+            }
+        }
+    }
+}
+

Modified: openejb/trunk/openejb/container/openejb-core/src/test/java/org/apache/openejb/core/mdb/JmsTest.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb/container/openejb-core/src/test/java/org/apache/openejb/core/mdb/JmsTest.java?rev=1396142&r1=1396141&r2=1396142&view=diff
==============================================================================
--- openejb/trunk/openejb/container/openejb-core/src/test/java/org/apache/openejb/core/mdb/JmsTest.java (original)
+++ openejb/trunk/openejb/container/openejb-core/src/test/java/org/apache/openejb/core/mdb/JmsTest.java Tue Oct  9 18:01:59 2012
@@ -118,7 +118,7 @@ public class JmsTest extends TestCase {
 
             // create a new temp response queue
             session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
-            final Destination responseQueue = session.createTemporaryQueue();
+            final Destination responseQueue = session.createQueue("queue-tmp");
 
             // Create a request messages
             final ObjectMessage requestMessage = session.createObjectMessage();

Added: openejb/trunk/openejb/container/openejb-core/src/test/java/org/apache/openejb/ivm/naming/IvmContextTest.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb/container/openejb-core/src/test/java/org/apache/openejb/ivm/naming/IvmContextTest.java?rev=1396142&view=auto
==============================================================================
--- openejb/trunk/openejb/container/openejb-core/src/test/java/org/apache/openejb/ivm/naming/IvmContextTest.java (added)
+++ openejb/trunk/openejb/container/openejb-core/src/test/java/org/apache/openejb/ivm/naming/IvmContextTest.java Tue Oct  9 18:01:59 2012
@@ -0,0 +1,81 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.openejb.ivm.naming;
+
+import org.apache.openejb.core.ivm.naming.IvmContext;
+import org.apache.openejb.util.Contexts;
+import org.junit.Test;
+
+import javax.naming.Context;
+import javax.naming.NamingException;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.fail;
+
+public class IvmContextTest {
+    @Test
+    public void unbind() throws NamingException {
+        final IvmContext context = new IvmContext();
+        context.bind("global/foo/Bar", "Bar");
+
+        assertEquals("Bar", context.lookup("global/foo/Bar"));
+
+        context.unbind("global/foo/Bar");
+
+        try {
+            context.lookup("global/foo/Bar");
+            fail();
+        } catch (NamingException ne) {
+            // ok
+        }
+
+        try {
+            final Context subCtx = (Context) context.lookup("global/foo");
+            subCtx.lookup("Bar");
+            fail();
+        } catch (NamingException ne) {
+            // ok
+        }
+    }
+
+    @Test
+    public void unbindWithSubContext() throws NamingException {
+        final IvmContext context = new IvmContext("");
+        context.bind("global/foo/Bar", "Bar");
+
+        Contexts.createSubcontexts(context, "global/foo/Bar").bind("Bar", "Foo");
+
+        assertEquals("Bar", context.lookup("global/foo/Bar"));
+
+        context.unbind("global/foo/Bar");
+
+        try {
+            context.lookup("global/foo/Bar");
+            fail();
+        } catch (NamingException ne) {
+            // ok
+        }
+
+        try {
+            ((Context) ((Context) context.lookup("global")).lookup("foo"))
+                .lookup("Bar");
+            fail();
+        } catch (NamingException ne) {
+            // ok
+        }
+    }
+}