Author: dblevins
Date: Tue Nov  6 09:13:16 2007
New Revision: 592489

URL: http://svn.apache.org/viewvc?rev=592489&view=rev
Log:
OPENEJB-253: Per EJB or EJB Interface JNDI Name declaration

Added this format to the openejb-jar.xml :
<jndi name="foo" interface="org.acme.Foo"/>

Or more generally...
<jndi name="foo" interface="Remote"/>

Or more generally still...
<jndi name="foo"/>

The 'name' attribute can still use templates if it likes, such as:
<jndi name="ejb/{interfaceClass.simpleName}" interface="org.acme.Foo"/>


Added:
    
openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/JndiNameInfo.java
    
openejb/trunk/openejb3/container/openejb-jee/src/main/java/org/apache/openejb/jee/oejb3/Jndi.java
Modified:
    
openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/EnterpriseBeanInfo.java
    
openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/JndiBuilder.java
    
openejb/trunk/openejb3/container/openejb-jee/src/main/java/org/apache/openejb/jee/oejb3/EjbDeployment.java

Modified: 
openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/EnterpriseBeanInfo.java
URL: 
http://svn.apache.org/viewvc/openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/EnterpriseBeanInfo.java?rev=592489&r1=592488&r2=592489&view=diff
==============================================================================
--- 
openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/EnterpriseBeanInfo.java
 (original)
+++ 
openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/EnterpriseBeanInfo.java
 Tue Nov  6 09:13:16 2007
@@ -16,8 +16,8 @@
  */
 package org.apache.openejb.assembler.classic;
 
-import java.util.List;
 import java.util.ArrayList;
+import java.util.List;
 
 public abstract class EnterpriseBeanInfo extends InfoObject {
 
@@ -61,6 +61,8 @@
 
     public String containerId;
     public String serviceEndpoint;
+
+    public List<JndiNameInfo> jndiNamess = new ArrayList<JndiNameInfo>();
 
     public List<String> jndiNames = new ArrayList<String>();
 }

Modified: 
openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/JndiBuilder.java
URL: 
http://svn.apache.org/viewvc/openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/JndiBuilder.java?rev=592489&r1=592488&r2=592489&view=diff
==============================================================================
--- 
openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/JndiBuilder.java
 (original)
+++ 
openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/JndiBuilder.java
 Tue Nov  6 09:13:16 2007
@@ -69,7 +69,12 @@
 
         for (EnterpriseBeanInfo beanInfo : ejbJar.enterpriseBeans) {
             DeploymentInfo deploymentInfo = 
deployments.get(beanInfo.ejbDeploymentId);
-            bind(ejbJar, deploymentInfo, beanInfo, strategy);
+            strategy.begin(deploymentInfo);
+            try {
+                bind(ejbJar, deploymentInfo, beanInfo, strategy);
+            } finally {
+                strategy.end();
+            }
         }
     }
 
@@ -159,13 +164,21 @@
 
         }
 
-        public String getName(DeploymentInfo deploymentInfo, Class interfce, 
Interface type);
+        public void begin(DeploymentInfo deploymentInfo);
+        public String getName(Class interfce, Interface type);
+        public void end();
     }
 
     // TODO: put these into the classpath and get them with xbean-finder
     public static class TemplatedStrategy implements JndiNameStrategy {
         private static final String JNDINAME_FORMAT = 
"openejb.jndiname.format";
         private org.codehaus.swizzle.stream.StringTemplate template;
+        private HashMap<String, EnterpriseBeanInfo> beanInfos;
+
+        // Set in begin()
+        private DeploymentInfo deploymentInfo;
+        // Set in begin()
+        private Map<String, StringTemplate> templates;
 
         public TemplatedStrategy(EjbJarInfo ejbJarInfo, Map<String, 
DeploymentInfo> deployments) {
             String format = SystemInstance.get().getProperty(JNDINAME_FORMAT, 
"{deploymentId}{interfaceType.annotationName}");
@@ -174,9 +187,36 @@
             logger.debug("Using " + JNDINAME_FORMAT + " '" + format + "'");
 
             this.template = new StringTemplate(format);
+
+            beanInfos = new HashMap<String, EnterpriseBeanInfo>();
+            for (EnterpriseBeanInfo beanInfo : ejbJarInfo.enterpriseBeans) {
+                beanInfos.put(beanInfo.ejbDeploymentId, beanInfo);
+            }
+        }
+
+        public void begin(DeploymentInfo deploymentInfo) {
+            this.deploymentInfo = deploymentInfo;
+            EnterpriseBeanInfo beanInfo = 
beanInfos.get(deploymentInfo.getDeploymentID());
+
+            templates = new HashMap<String, StringTemplate>();
+            templates.put("", template);
+
+            for (JndiNameInfo nameInfo : beanInfo.jndiNamess) {
+                String intrface = nameInfo.intrface;
+                if (intrface == null) intrface = "";
+                templates.put(intrface, new StringTemplate(nameInfo.name));
+            }
+            beanInfo.jndiNames.clear();
+            beanInfo.jndiNamess.clear();
+        }
+
+        public void end() {
         }
 
-        public String getName(DeploymentInfo deploymentInfo, Class interfce, 
Interface type) {
+        public String getName(Class interfce, Interface type) {
+            StringTemplate template = templates.get(interfce.getName());
+            if (template == null) template = 
templates.get(type.getAnnotationName());
+            if (template == null) template = templates.get("");
 
             Map<String,String> contextData = new HashMap<String,String>();
             contextData.put("moduleId", deploymentInfo.getModuleID());
@@ -194,12 +234,21 @@
             contextData.put("interfaceClass", interfce.getName());
             contextData.put("interfaceClass.simpleName", 
interfce.getSimpleName());
             contextData.put("interfaceClass.packageName", 
packageName(interfce));
-            return this.template.apply(contextData);
+            return template.apply(contextData);
         }
     }
 
     public static class LegacyAddedSuffixStrategy implements JndiNameStrategy {
-        public String getName(DeploymentInfo deploymentInfo, Class interfce, 
Interface type) {
+        private DeploymentInfo deploymentInfo;
+
+        public void begin(DeploymentInfo deploymentInfo) {
+            this.deploymentInfo = deploymentInfo;
+        }
+
+        public void end() {
+        }
+
+        public String getName(Class interfce, Interface type) {
             String id = deploymentInfo.getDeploymentID() + "";
             if (id.charAt(0) == '/') {
                 id = id.substring(1);
@@ -225,17 +274,18 @@
         Bindings bindings = new Bindings();
         deployment.set(Bindings.class, bindings);
 
+
         Object id = deployment.getDeploymentID();
         try {
             Class homeInterface = deployment.getHomeInterface();
             if (homeInterface != null) {
 
-                String name = "openejb/ejb/" + strategy.getName(deployment, 
deploymentInfo.getHomeInterface(), JndiNameStrategy.Interface.REMOTE_HOME);
+                String name = "openejb/ejb/" + strategy.getName(homeInterface, 
JndiNameStrategy.Interface.REMOTE_HOME);
                 ObjectReference ref = new 
ObjectReference(deployment.getEJBHome());
-                bind(name, ref, bindings, beanInfo);
+                bind(name, ref, bindings, beanInfo, homeInterface);
 
                 name = "openejb/Deployment/" + deployment.getDeploymentID() + 
"/" + deployment.getRemoteInterface().getName();
-                bind(name, ref, bindings, beanInfo);
+                bind(name, ref, bindings, beanInfo, homeInterface);
             }
         } catch (NamingException e) {
             throw new RuntimeException("Unable to bind home interface for 
deployment " + id, e);
@@ -245,12 +295,12 @@
             Class localHomeInterface = deployment.getLocalHomeInterface();
             if (localHomeInterface != null) {
 
-                String name = "openejb/ejb/" + strategy.getName(deployment, 
deploymentInfo.getLocalHomeInterface(), JndiNameStrategy.Interface.LOCAL_HOME);
+                String name = "openejb/ejb/" + 
strategy.getName(deploymentInfo.getLocalHomeInterface(), 
JndiNameStrategy.Interface.LOCAL_HOME);
                 ObjectReference ref = new 
ObjectReference(deployment.getEJBLocalHome());
-                bind(name, ref, bindings, beanInfo);
+                bind(name, ref, bindings, beanInfo, localHomeInterface);
 
                 name = "openejb/Deployment/" + deployment.getDeploymentID() + 
"/" + deployment.getLocalInterface().getName();
-                bind(name, ref, bindings, beanInfo);
+                bind(name, ref, bindings, beanInfo, localHomeInterface);
             }
         } catch (NamingException e) {
             throw new RuntimeException("Unable to bind local interface for 
deployment " + id, e);
@@ -267,10 +317,10 @@
                 BusinessLocalReference ref = new BusinessLocalReference(home);
 
                 String internalName = "openejb/Deployment/" + 
deployment.getDeploymentID() + "/" + interfce.getName();
-                bind(internalName, ref, bindings, beanInfo);
+                bind(internalName, ref, bindings, beanInfo, interfce);
 
-                String externalName = "openejb/ejb/" + 
strategy.getName(deployment, interfce, 
JndiNameStrategy.Interface.BUSINESS_LOCAL);
-                bind(externalName, ref, bindings, beanInfo);
+                String externalName = "openejb/ejb/" + 
strategy.getName(interfce, JndiNameStrategy.Interface.BUSINESS_LOCAL);
+                bind(externalName, ref, bindings, beanInfo, interfce);
             }
         } catch (NamingException e) {
             throw new RuntimeException("Unable to bind business local 
interface for deployment " + id, e);
@@ -288,10 +338,10 @@
                 BusinessRemoteReference ref = new 
BusinessRemoteReference(home);
 
                 String internalName = "openejb/Deployment/" + 
deployment.getDeploymentID() + "/" + interfce.getName();
-                bind(internalName, ref, bindings, beanInfo);
+                bind(internalName, ref, bindings, beanInfo, interfce);
 
-                String externalName = "openejb/ejb/" + 
strategy.getName(deployment, interfce, 
JndiNameStrategy.Interface.BUSINESS_REMOTE);
-                bind(externalName, ref, bindings, beanInfo);
+                String externalName = "openejb/ejb/" + 
strategy.getName(interfce, JndiNameStrategy.Interface.BUSINESS_REMOTE);
+                bind(externalName, ref, bindings, beanInfo, interfce);
             }
         } catch (NamingException e) {
             throw new RuntimeException("Unable to bind business remote 
deployment in jndi.", e);
@@ -305,7 +355,7 @@
                 String jndiName = "java:openejb/Resource/" + destinationId;
                 Reference reference = new IntraVmJndiReference(jndiName);
 
-                bind(name, reference, bindings, beanInfo);
+                bind(name, reference, bindings, beanInfo, 
MessageListener.class);
             }
         } catch (NamingException e) {
             throw new RuntimeException("Unable to bind mdb destination in 
jndi.", e);
@@ -313,21 +363,29 @@
     }
 
 
-    private void bind(String name, Reference ref, Bindings bindings, 
EnterpriseBeanInfo beanInfo) throws NamingException {
+    private void bind(String name, Reference ref, Bindings bindings, 
EnterpriseBeanInfo beanInfo, Class intrface) throws NamingException {
 
         if (name.startsWith("openejb/ejb/")) {
 
             String externalName = name.replaceFirst("openejb/ejb/", "");
 
-            if (beanInfo.jndiNames.contains(externalName)){
+            if (bindings.contains(name)){
                 logger.debug("Duplicate: Jndi(name=" + externalName +")");
                 return;
             }
 
             try {
                 context.bind(name, ref);
+
                 bindings.add(name);
+
                 beanInfo.jndiNames.add(externalName);
+
+                JndiNameInfo nameInfo = new JndiNameInfo();
+                nameInfo.intrface = intrface.getName();
+                nameInfo.name = externalName;
+                beanInfo.jndiNamess.add(nameInfo);
+
                 logger.info("Jndi(name=" + externalName +") --> 
Ejb(deployment-id="+beanInfo.ejbDeploymentId+")");
             } catch (NameAlreadyBoundException e) {
                 DeploymentInfo deployment = findNameOwner(name);
@@ -378,6 +436,10 @@
 
         public boolean add(String o) {
             return bindings.add(o);
+        }
+
+        public boolean contains(String o) {
+            return bindings.contains(o);
         }
     }
 

Added: 
openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/JndiNameInfo.java
URL: 
http://svn.apache.org/viewvc/openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/JndiNameInfo.java?rev=592489&view=auto
==============================================================================
--- 
openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/JndiNameInfo.java
 (added)
+++ 
openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/assembler/classic/JndiNameInfo.java
 Tue Nov  6 09:13:16 2007
@@ -0,0 +1,25 @@
+/**
+ * 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.assembler.classic;
+
+/**
+ * @version $Rev$ $Date$
+ */
+public class JndiNameInfo extends InfoObject {
+    public String name;
+    public String intrface;
+}

Modified: 
openejb/trunk/openejb3/container/openejb-jee/src/main/java/org/apache/openejb/jee/oejb3/EjbDeployment.java
URL: 
http://svn.apache.org/viewvc/openejb/trunk/openejb3/container/openejb-jee/src/main/java/org/apache/openejb/jee/oejb3/EjbDeployment.java?rev=592489&r1=592488&r2=592489&view=diff
==============================================================================
--- 
openejb/trunk/openejb3/container/openejb-jee/src/main/java/org/apache/openejb/jee/oejb3/EjbDeployment.java
 (original)
+++ 
openejb/trunk/openejb3/container/openejb-jee/src/main/java/org/apache/openejb/jee/oejb3/EjbDeployment.java
 Tue Nov  6 09:13:16 2007
@@ -32,10 +32,13 @@
 import java.util.Iterator;
 
 @XmlAccessorType(XmlAccessType.FIELD)
[EMAIL PROTECTED](propOrder = {"ejbLink", "resourceLink", "query"})
[EMAIL PROTECTED](propOrder = {"jndi","ejbLink", "resourceLink", "query"})
 @XmlRootElement(name = "ejb-deployment")
 public class EjbDeployment {
 
+    @XmlElement(name = "jndi", required = true)
+    protected List<Jndi> jndi;
+
     @XmlElement(name = "ejb-link", required = true)
     protected List<EjbLink> ejbLink;
 
@@ -70,6 +73,13 @@
             ejbLink = new ArrayList<EjbLink>();
         }
         return this.ejbLink;
+    }
+
+    public List<Jndi> getJndi() {
+        if (jndi == null){
+            jndi = new ArrayList<Jndi>();
+        }
+        return jndi;
     }
 
     public List<ResourceLink> getResourceLink() {

Added: 
openejb/trunk/openejb3/container/openejb-jee/src/main/java/org/apache/openejb/jee/oejb3/Jndi.java
URL: 
http://svn.apache.org/viewvc/openejb/trunk/openejb3/container/openejb-jee/src/main/java/org/apache/openejb/jee/oejb3/Jndi.java?rev=592489&view=auto
==============================================================================
--- 
openejb/trunk/openejb3/container/openejb-jee/src/main/java/org/apache/openejb/jee/oejb3/Jndi.java
 (added)
+++ 
openejb/trunk/openejb3/container/openejb-jee/src/main/java/org/apache/openejb/jee/oejb3/Jndi.java
 Tue Nov  6 09:13:16 2007
@@ -0,0 +1,60 @@
+/**
+ * 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.jee.oejb3;
+
+import javax.xml.bind.annotation.XmlAccessorType;
+import javax.xml.bind.annotation.XmlAccessType;
+import javax.xml.bind.annotation.XmlType;
+import javax.xml.bind.annotation.XmlRootElement;
+import javax.xml.bind.annotation.XmlAttribute;
+
[EMAIL PROTECTED](XmlAccessType.FIELD)
[EMAIL PROTECTED](name = "")
[EMAIL PROTECTED](name = "jndi")
+public class Jndi {
+
+    @XmlAttribute(name = "name")
+    protected String name;
+
+    @XmlAttribute(name = "interface")
+    protected String intrface;
+
+    public Jndi() {
+    }
+
+    public Jndi(String ejbRefName, String deployentId) {
+        this.intrface = ejbRefName;
+        this.name = deployentId;
+    }
+
+    public String getName() {
+        return name;
+    }
+
+    public void setName(String value) {
+        this.name = value;
+    }
+
+    public String getInterface() {
+        return intrface;
+    }
+
+    public void setInterface(String value) {
+        this.intrface = value;
+    }
+
+}


Reply via email to