Author: rmannibucau
Date: Mon Feb 18 17:10:19 2013
New Revision: 1447395

URL: http://svn.apache.org/r1447395
Log:
keeping prefix related to a resource since it can be mandatory while the app is 
not yet deployed + cleaning up the contextual jndi reference when mandatory

Modified:
    
tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/Assembler.java
    
tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/config/AutoConfig.java
    
tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/core/ivm/naming/ContextualJndiReference.java
    
tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/core/ivm/naming/IvmContext.java

Modified: 
tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/Assembler.java
URL: 
http://svn.apache.org/viewvc/tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/Assembler.java?rev=1447395&r1=1447394&r2=1447395&view=diff
==============================================================================
--- 
tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/Assembler.java
 (original)
+++ 
tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/Assembler.java
 Mon Feb 18 17:10:19 2013
@@ -1514,6 +1514,32 @@ public class Assembler extends Assembler
             }
         }
 
+        for (final String id : appInfo.resourceAliases) {
+            final String name = OPENEJB_RESOURCE_JNDI_PREFIX + id;
+            ContextualJndiReference.followReference.set(false);
+            if (globalContext instanceof IvmContext) {
+                IvmContext.class.cast(globalContext).ignoreCache(name);
+            }
+            try {
+                final Object object;
+                try {
+                    object = globalContext.lookup(name);
+                } finally {
+                    ContextualJndiReference.followReference.remove();
+                }
+                if (object instanceof ContextualJndiReference) {
+                    final ContextualJndiReference contextualJndiReference = 
ContextualJndiReference.class.cast(object);
+                    contextualJndiReference.removePrefix(appContext.getId());
+                    if (contextualJndiReference.hasNoMorePrefix()) {
+                        globalContext.unbind(name);
+                    } // else not the last deployed application to use this 
resource so keep it
+                } else {
+                    globalContext.unbind(name);
+                }
+            } catch (NamingException e) {
+                logger.warning("can't unbind resource '{0}'", id);
+            }
+        }
         for (final String id : appInfo.resourceIds) {
             final String name = OPENEJB_RESOURCE_JNDI_PREFIX + id;
             try {
@@ -1530,14 +1556,6 @@ public class Assembler extends Assembler
                 logger.warning("can't unbind resource '{0}'", id);
             }
         }
-        for (final String id : appInfo.resourceAliases) {
-            final String name = OPENEJB_RESOURCE_JNDI_PREFIX + id;
-            try {
-                globalContext.unbind(name);
-            } catch (NamingException e) {
-                logger.warning("can't unbind resource '{0}'", id);
-            }
-        }
 
         containerSystem.removeAppContext(appInfo.appId);
 
@@ -1982,6 +2000,7 @@ public class Assembler extends Assembler
             final String baseJndiName = 
serviceInfo.id.substring(serviceInfo.originAppName.length() + 1);
             serviceInfo.aliases.add(baseJndiName);
             final ContextualJndiReference ref = new 
ContextualJndiReference(baseJndiName);
+            ref.addPrefix(serviceInfo.originAppName);
             bindResource(baseJndiName, ref);
         }
 
@@ -1995,10 +2014,14 @@ public class Assembler extends Assembler
 
     private void bindResource(final String id, final Object service) throws 
OpenEJBException {
         final String name = OPENEJB_RESOURCE_JNDI_PREFIX + id;
+        final Context jndiContext = containerSystem.getJNDIContext();
         Object existing = null;
         try {
             ContextualJndiReference.followReference.set(false);
-            existing = containerSystem.getJNDIContext().lookup(name);
+            if (jndiContext instanceof IvmContext) {
+                IvmContext.class.cast(jndiContext).ignoreCache(name);
+            }
+            existing = jndiContext.lookup(name);
         } catch (final Exception ignored) {
             // no-op
         } finally {
@@ -2015,21 +2038,22 @@ public class Assembler extends Assembler
             } else if (existingIsContextual && !serviceIsExisting) {
                 
ContextualJndiReference.class.cast(existing).setDefaultValue(service);
             } else if (existingIsContextual) { // && serviceIsExisting is 
always true here
+                
ContextualJndiReference.class.cast(existing).addPrefix(ContextualJndiReference.class.cast(service).lastPrefix());
                 return;
             }
         }
 
         try {
             if (rebind) {
-                containerSystem.getJNDIContext().rebind(name, service);
+                jndiContext.rebind(name, service);
             } else {
-                containerSystem.getJNDIContext().bind(name, service);
+                jndiContext.bind(name, service);
             }
         } catch (NameAlreadyBoundException nabe) {
             logger.warning("unbounding resource " + name + " can happen 
because of a redeployment or because of a duplicated id");
             try {
-                containerSystem.getJNDIContext().unbind(name);
-                containerSystem.getJNDIContext().bind(name, service);
+                jndiContext.unbind(name);
+                jndiContext.bind(name, service);
             } catch (NamingException e) {
                 throw new OpenEJBException("Cannot bind resource adapter with 
id " + id, e);
             }

Modified: 
tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/config/AutoConfig.java
URL: 
http://svn.apache.org/viewvc/tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/config/AutoConfig.java?rev=1447395&r1=1447394&r2=1447395&view=diff
==============================================================================
--- 
tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/config/AutoConfig.java
 (original)
+++ 
tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/config/AutoConfig.java
 Mon Feb 18 17:10:19 2013
@@ -947,8 +947,14 @@ public class AutoConfig implements Dynam
 
         Collections.sort(resourceInfos, new 
ConfigurationFactory.ResourceInfoComparator(resourceInfos));
         for (ResourceInfo resourceInfo : resourceInfos) {
+            final int originalSize = resourceInfo.aliases.size();
             final String id = installResource(module.getModuleId(), 
resourceInfo);
-            resourcesMap.remove(resourceInfo).setId(id);
+
+            final Resource resource = resourcesMap.remove(resourceInfo);
+            resource.setId(id);
+            if (resourceInfo.aliases.size() > originalSize) { // an aliases is 
generally added to be able to bind in global jndi tree
+                
resource.getAliases().add(resourceInfo.aliases.get(resourceInfo.aliases.size() 
- 1));
+            }
         }
 
         resourceInfos.clear();

Modified: 
tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/core/ivm/naming/ContextualJndiReference.java
URL: 
http://svn.apache.org/viewvc/tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/core/ivm/naming/ContextualJndiReference.java?rev=1447395&r1=1447394&r2=1447395&view=diff
==============================================================================
--- 
tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/core/ivm/naming/ContextualJndiReference.java
 (original)
+++ 
tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/core/ivm/naming/ContextualJndiReference.java
 Mon Feb 18 17:10:19 2013
@@ -27,6 +27,8 @@ import javax.naming.NameNotFoundExceptio
 import javax.naming.NamingException;
 import java.util.ArrayList;
 import java.util.Collection;
+import java.util.List;
+import java.util.concurrent.CopyOnWriteArrayList;
 
 public class ContextualJndiReference extends IntraVmJndiReference {
     public static final ThreadLocal<Boolean> followReference = new 
ThreadLocal<Boolean>() {
@@ -37,6 +39,7 @@ public class ContextualJndiReference ext
     };
 
     private Object defaultValue;
+    private List<String> prefixes = new CopyOnWriteArrayList<String>();
 
     public ContextualJndiReference(final String jndiName) {
         super(jndiName);
@@ -46,6 +49,29 @@ public class ContextualJndiReference ext
         this.defaultValue = defaultValue;
     }
 
+    public void addPrefix(final String value) {
+        if (value != null) {
+            prefixes.add(value);
+        }
+    }
+
+    public void removePrefix(final String value) {
+        if (value != null) {
+            prefixes.remove(value);
+        }
+    }
+
+    public String lastPrefix() {
+        if (prefixes.isEmpty()) {
+            return null;
+        }
+        return prefixes.get(prefixes.size() - 1);
+    }
+
+    public boolean hasNoMorePrefix() {
+        return prefixes.isEmpty();
+    }
+
     @Override
     public Object getObject() throws NamingException {
         final Boolean rawValue = !followReference.get();
@@ -66,8 +92,8 @@ public class ContextualJndiReference ext
         }
 
         final Collection<Object> values = new ArrayList<Object>();
-        for (final String p : allPrefixes()) {
-            if (prefix != null && !prefix.isEmpty()) {
+        for (final String p : prefixes) {
+            if (p != null && !p.isEmpty()) {
                 try {
                     values.add(lookup(p + '/' + jndiName));
                 } catch (final NamingException e) {
@@ -108,14 +134,6 @@ public class ContextualJndiReference ext
         return null;
     }
 
-    private Collection<String> allPrefixes() {
-        final Collection<String> prefixes = new ArrayList<String>();
-        for (final AppContext appContext : 
SystemInstance.get().getComponent(ContainerSystem.class).getAppContexts()) {
-            prefixes.add(appContext.getId());
-        }
-        return prefixes;
-    }
-
     private Object lookup(final String s) throws NamingException {
         final ContainerSystem containerSystem = 
SystemInstance.get().getComponent(ContainerSystem.class);
         final Context jndiContext = containerSystem.getJNDIContext();

Modified: 
tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/core/ivm/naming/IvmContext.java
URL: 
http://svn.apache.org/viewvc/tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/core/ivm/naming/IvmContext.java?rev=1447395&r1=1447394&r2=1447395&view=diff
==============================================================================
--- 
tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/core/ivm/naming/IvmContext.java
 (original)
+++ 
tomee/tomee/trunk/container/openejb-core/src/main/java/org/apache/openejb/core/ivm/naming/IvmContext.java
 Mon Feb 18 17:10:19 2013
@@ -469,6 +469,10 @@ public class IvmContext implements Conte
         if (readOnly) throw new OperationNotSupportedException();
     }
 
+    public void ignoreCache(final String name) {
+        fastCache.remove(name);
+    }
+
     protected class MyBindingEnumeration extends MyNamingEnumeration {
 
         public MyBindingEnumeration(NameNode parentNode) {


Reply via email to