KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > easybeans > deployment > resolver > JNDIResolver


1 /**
2  * EasyBeans
3  * Copyright (C) 2006 Bull S.A.S.
4  * Contact: easybeans@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: JNDIResolver.java 242 2006-03-22 10:32:44Z benoitf $
23  * --------------------------------------------------------------------------
24  */

25 package org.objectweb.easybeans.deployment.resolver;
26
27 import java.util.HashMap JavaDoc;
28 import java.util.Map JavaDoc;
29
30 import org.objectweb.easybeans.container.JContainer3;
31 import org.objectweb.easybeans.deployment.Deployment;
32 import org.objectweb.easybeans.deployment.annotations.analyzer.AnnotationDeploymentAnalyzer;
33 import org.objectweb.easybeans.deployment.annotations.impl.JCommonBean;
34 import org.objectweb.easybeans.deployment.annotations.impl.JLocal;
35 import org.objectweb.easybeans.deployment.annotations.impl.JRemote;
36 import org.objectweb.easybeans.deployment.annotations.metadata.ClassAnnotationMetadata;
37 import org.objectweb.easybeans.deployment.annotations.metadata.EjbJarAnnotationMetadata;
38 import org.objectweb.easybeans.log.JLog;
39 import org.objectweb.easybeans.log.JLogFactory;
40
41 /**
42  * Classes used to resolve JNDI Name for a given deployment.
43  * @author Florent Benoit
44  */

45 public class JNDIResolver {
46
47     /**
48      * Logger.
49      */

50     private JLog logger = JLogFactory.getLog(JNDIResolver.class);
51
52     /**
53      * Name.
54      */

55     public static final String JavaDoc NAME = "jndi.resolver";
56
57     /**
58      * Map with key = interface name, value = jndi name.
59      */

60     private Map JavaDoc<String JavaDoc, String JavaDoc> interfaces = null;
61
62     /**
63      * Map with key = bean name, value = &lt;interface name, jndi name&gt;.
64      */

65     private Map JavaDoc<String JavaDoc, Map JavaDoc<String JavaDoc, String JavaDoc>> beans = null;
66
67     /**
68      * Constructor.
69      */

70     public JNDIResolver() {
71         interfaces = new HashMap JavaDoc<String JavaDoc, String JavaDoc>();
72         beans = new HashMap JavaDoc<String JavaDoc, Map JavaDoc<String JavaDoc, String JavaDoc>>();
73     }
74
75     /**
76      * Build a new resolver with the given deployment.
77      * @param deployment given deployment object to use.
78      */

79     public JNDIResolver(final Deployment deployment) {
80         this();
81         addDeployment(deployment);
82     }
83
84     /**
85      * Add a given deployment object to this resolver.
86      * @param deployment to add to the resolver.
87      */

88     public void addDeployment(final Deployment deployment) {
89         // Get Analyzer
90
AnnotationDeploymentAnalyzer analyzer = deployment.getAnnotationDeploymentAnalyzer();
91
92         // Get metadata for jar file analyzed
93
EjbJarAnnotationMetadata ejbJarAnnotationMetadata = analyzer.getEjbJarAnnotationMetadata();
94         addEjbJarAnnotationMetadata(ejbJarAnnotationMetadata);
95     }
96
97     /**
98      * Adds the given metadata to the resolver.
99      * @param ejbJarAnnotationMetadata the metadata for a given jar file
100      */

101     public void addEjbJarAnnotationMetadata(final EjbJarAnnotationMetadata ejbJarAnnotationMetadata) {
102         // For each bean, get the interfaces and add the jndiName mapping.
103
for (ClassAnnotationMetadata classAnnotationMetadata : ejbJarAnnotationMetadata
104                 .getClassAnnotationMetadataCollection()) {
105             if (classAnnotationMetadata.isBean()) {
106
107                 // Also, go through the EJB annotations (using ejb name)
108
JCommonBean jCommonBean = classAnnotationMetadata.getJCommonBean();
109                 String JavaDoc mappedName = null;
110                 String JavaDoc beanName = null;
111                 if (jCommonBean != null) {
112                     // jndi name ?
113
mappedName = jCommonBean.getMappedName();
114                     // bean name ?
115
beanName = jCommonBean.getName();
116
117                 }
118
119                 // SessionBusinessInterfaceFinder.resolve(classAnnotationMetadata);
120
JLocal localItfs = classAnnotationMetadata.getLocalInterfaces();
121                 JRemote remoteItfs = classAnnotationMetadata.getRemoteInterfaces();
122                 if (localItfs != null) {
123                     for (String JavaDoc itf : localItfs.getInterfaces()) {
124                         addInterface(itf, classAnnotationMetadata.getClassName(), "Local", mappedName, beanName);
125                     }
126                 }
127                 if (remoteItfs != null) {
128                     for (String JavaDoc itf : remoteItfs.getInterfaces()) {
129                         addInterface(itf, classAnnotationMetadata.getClassName(), "Remote", mappedName, beanName);
130                     }
131                 }
132             }
133         }
134     }
135
136     /**
137      * Add the jndi name for a given interface and a given bean name (may be
138      * null).
139      * @param itf the interface of the bean
140      * @param beanClassName the class of the bean
141      * @param mode local/remote
142      * @param mappedName the mappedName (could be used as JNDI name)
143      * @param beanName the name of the bean
144      */

145     private void addInterface(final String JavaDoc itf, final String JavaDoc beanClassName, final String JavaDoc mode, final String JavaDoc mappedName,
146             final String JavaDoc beanName) {
147         String JavaDoc jndiName = JContainer3.jndiNameEncode(beanClassName, itf, mode);
148         if (mappedName != null) {
149             jndiName = mappedName;
150         }
151         String JavaDoc itfName = itf.replace("/", ".");
152         interfaces.put(itfName, jndiName);
153
154         if (beanName != null && !beanName.equals("")) {
155             Map JavaDoc<String JavaDoc, String JavaDoc> tmpBeanMap = beans.get(beanName);
156             if (tmpBeanMap == null) {
157                 tmpBeanMap = new HashMap JavaDoc<String JavaDoc, String JavaDoc>();
158                 beans.put(beanName, tmpBeanMap);
159             }
160             tmpBeanMap.put(itfName, jndiName);
161             beans.put(beanName, tmpBeanMap);
162             logger.debug("Adding jndi name {0} for beanName {1} with itf {2} and className {3}", jndiName, beanName,
163                     itfName, beanClassName);
164         } else {
165             logger.debug("Adding jndi name {0} with itf {1} and className {2}", jndiName, itfName, beanClassName);
166         }
167     }
168
169     /**
170      * Gets jndi name for a given interface and a bean name.
171      * @param itf the name of the interface.
172      * @param beanName the name of the bean.
173      * @return the jndi name or null if not found.
174      */

175     public String JavaDoc getJndiNameInterface(final String JavaDoc itf, final String JavaDoc beanName) {
176         if (beanName != null && !beanName.equals("")) {
177             Map JavaDoc<String JavaDoc, String JavaDoc> lst = beans.get(beanName);
178             if (lst != null) {
179                 return lst.get(itf);
180             }
181             throw new IllegalStateException JavaDoc("No bean with name '" + beanName + "' was found.");
182
183         }
184         return interfaces.get(itf);
185     }
186
187     /**
188      * @return string representation of this object
189      */

190     @Override JavaDoc
191     public String JavaDoc toString() {
192         StringBuilder JavaDoc sb = new StringBuilder JavaDoc("JNDIResolver[");
193         sb.append("Interfaces =[");
194         sb.append(interfaces);
195         sb.append("], Beans =");
196         sb.append(beans);
197         sb.append("]]");
198
199         return sb.toString();
200     }
201 }
202
Popular Tags