KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb3 > enc > DeploymentEjbResolver


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.ejb3.enc;
23
24 import org.jboss.ejb3.EJBContainer;
25 import org.jboss.ejb3.Ejb3Deployment;
26 import org.jboss.ejb3.Ejb3Registry;
27 import org.jboss.ejb3.ProxyFactoryHelper;
28 import org.jboss.ejb3.Container;
29 import org.jboss.ejb3.DeploymentScope;
30
31 import javax.naming.NameNotFoundException JavaDoc;
32 import java.util.Iterator JavaDoc;
33
34 /**
35  * Class to resolve EJB containers from ejb-ref or @EJB metadata
36  * This class is overriden for specific behaviors, specifically whether or not
37  * to resolve the EJB internally in a specific deployment or not. There will be one for
38  * an EJB jar deployment and a WAR deployment and any other deployment package that needs to
39  * use @EJB.
40  *
41  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
42  * @version $Revision: 57481 $
43  */

44 public abstract class DeploymentEjbResolver
45 {
46    protected DeploymentScope deploymentScope;
47    protected String JavaDoc errorName;
48
49    protected DeploymentEjbResolver(DeploymentScope deploymentScope, String JavaDoc errorName)
50    {
51       this.deploymentScope = deploymentScope;
52       this.errorName = errorName;
53    }
54
55    protected abstract EJBContainer searchDeploymentInternally(String JavaDoc ejbLink, Class JavaDoc businessIntf);
56
57    public EJBContainer getEjbContainer(String JavaDoc ejbLink, Class JavaDoc businessIntf)
58    {
59       int hashIndex = ejbLink.indexOf('#');
60       if (hashIndex != -1)
61       {
62          if (deploymentScope == null) return null;
63          String JavaDoc relativePath = ejbLink.substring(0, hashIndex);
64          Ejb3Deployment dep = deploymentScope.findRelativeDeployment(relativePath);
65          if (dep == null) return null;
66          String JavaDoc ejbName = ejbLink.substring(hashIndex);
67          return dep.getEjbContainer(ejbName, businessIntf);
68       }
69       // look internally
70
EJBContainer ejb = searchDeploymentInternally(ejbLink, businessIntf);
71       if (ejb != null) return ejb;
72       for (Object JavaDoc obj : Ejb3Registry.getContainers())
73       {
74          EJBContainer container = (EJBContainer) obj;
75          if (container.getEjbName().equals(ejbLink))
76          {
77             return container;
78          }
79       }
80       return null;
81    }
82
83    public String JavaDoc getEjbJndiName(String JavaDoc ejbLink, Class JavaDoc businessIntf)
84    {
85       EJBContainer container = getEjbContainer(ejbLink, businessIntf);
86       if (container == null)
87       {
88          return null;
89       }
90       return ProxyFactoryHelper.getJndiName(container, businessIntf);
91    }
92
93    public EJBContainer getEjbContainer(Ejb3Deployment deployment, Class JavaDoc businessIntf) throws NameNotFoundException JavaDoc
94    {
95       EJBContainer container = null;
96       // search in myself
97
for (Object JavaDoc obj : deployment.getEjbContainers().values())
98       {
99          EJBContainer newContainer = (EJBContainer) obj;
100          if (container == newContainer) continue;
101          if (ProxyFactoryHelper.publishesInterface(newContainer, businessIntf))
102          {
103             if (container != null) throw new NameNotFoundException JavaDoc("duplicated in " + errorName);
104             container = newContainer;
105          }
106       }
107       return container;
108    }
109
110    public EJBContainer getEjbContainer(Class JavaDoc businessIntf) throws NameNotFoundException JavaDoc
111    {
112       EJBContainer rtnContainer = null;
113       // search in deployment first
114
rtnContainer = searchForEjbContainerInternally(businessIntf);
115       if (rtnContainer != null) return rtnContainer;
116       // search in EAR
117
String JavaDoc jarName = null;
118       if (deploymentScope != null)
119       {
120          for (Ejb3Deployment deployment : deploymentScope.getEjbDeployments())
121          {
122             EJBContainer newContainer = getEjbContainer(deployment, businessIntf);
123             if (rtnContainer == newContainer) continue; // don't check self
124
if (rtnContainer != null && newContainer != null)
125             {
126                throw new NameNotFoundException JavaDoc("duplicated in .ear within " + jarName +
127                        " and " + deployment.getDeploymentUnit().getShortName());
128             }
129             if (newContainer != null)
130             {
131                rtnContainer = newContainer;
132                jarName = deployment.getDeploymentUnit().getShortName();
133             }
134          }
135       }
136       if (rtnContainer != null)
137       {
138          return rtnContainer;
139       }
140       // search everywhere
141
Iterator JavaDoc containers = Ejb3Registry.getContainers().iterator();
142       while (containers.hasNext())
143       {
144          Container container = (Container)containers.next();
145          EJBContainer ejbContainer = (EJBContainer) container;
146          if (ejbContainer == rtnContainer) continue;
147          if (ProxyFactoryHelper.publishesInterface(container, businessIntf))
148          {
149             if (rtnContainer != null)
150             {
151                throw new NameNotFoundException JavaDoc("duplicated in " + ejbContainer.getDeployment().getDeploymentUnit().getShortName()
152                        + " and " + jarName);
153             }
154             rtnContainer = ejbContainer;
155             jarName = ejbContainer.getDeployment().getDeploymentUnit().getShortName();
156          }
157       }
158       if (rtnContainer != null) return rtnContainer;
159       throw new NameNotFoundException JavaDoc("not used by any EJBs");
160    }
161
162    protected abstract EJBContainer searchForEjbContainerInternally(Class JavaDoc businessIntf) throws NameNotFoundException JavaDoc;
163
164    public String JavaDoc getEjbJndiName(Class JavaDoc businessIntf) throws NameNotFoundException JavaDoc
165    {
166       EJBContainer container = getEjbContainer(businessIntf);
167       String JavaDoc jndiName = ProxyFactoryHelper.getJndiName(container, businessIntf);
168       if (jndiName == null) throw new NameNotFoundException JavaDoc("not used by any EJBs");
169       return jndiName;
170    }
171 }
172
Popular Tags