KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb > EjbUtil50


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.ejb;
23
24 import java.net.MalformedURLException JavaDoc;
25 import java.net.URL JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.StringTokenizer JavaDoc;
28
29 import javax.management.MBeanServer JavaDoc;
30
31 import org.jboss.deployers.spi.deployer.DeploymentUnit;
32 import org.jboss.deployers.spi.structure.DeploymentContext;
33 import org.jboss.deployment.MainDeployerMBean;
34 import org.jboss.logging.Logger;
35 import org.jboss.metadata.ApplicationMetaData;
36 import org.jboss.metadata.BeanMetaData;
37 import org.jboss.metadata.MessageDestinationMetaData;
38 import org.jboss.metadata.WebMetaData;
39 import org.jboss.util.Strings;
40
41 /** Utility methods for resolving ejb-ref and ejb-local-ref within the
42  * scope of a deployment.
43  *
44  * @author <a HREF="mailto:criege@riege.com">Christian Riege</a>
45  * @author Scott.Stark@jboss.org
46  * @author Thomas.Diesler@jboss.org
47  *
48  * @version $Revision: 58099 $
49  */

50 public final class EjbUtil50
51 {
52    private static final Logger log = Logger.getLogger(EjbUtil50.class);
53
54    /**
55     * Resolves an &lt;ejb-link&gt; target for an &lt;ejb-ref&gt; entry and
56     * returns the name of the target in the JNDI tree.
57     *
58     * @param unit DeploymentUnit
59     * @param link Content of the &lt;ejb-link&gt; entry.
60     *
61     * @return The JNDI Entry of the target bean; <code>null</code> if
62     * no appropriate target could be found.
63     */

64    public static String JavaDoc findEjbLink(MBeanServer JavaDoc server, DeploymentUnit unit, String JavaDoc link)
65    {
66       return resolveLink(server, unit, link, false);
67    }
68
69    /**
70     * Resolves an &lt;ejb-link&gt; target for an &lt;ejb-local-ref&gt; entry
71     * and returns the name of the target in the JNDI tree.
72     *
73     * @param unit DeploymentUnit
74     * @param link Content of the &lt;ejb-link&gt; entry.
75     *
76     * @return The JNDI Entry of the target bean; <code>null</code> if
77     * no appropriate target could be found.
78     */

79    public static String JavaDoc findLocalEjbLink(MBeanServer JavaDoc server, DeploymentUnit unit, String JavaDoc link)
80    {
81       return resolveLink(server, unit, link, true);
82    }
83
84    /**
85     * Resolves a &lt;message-destination&gt; target for a &lt;message-destination-link&gt;
86     * entry and returns the name of the target in the JNDI tree.
87     *
88     * @param di DeploymentUnit
89     * @param link Content of the &lt;message-driven-link&gt; entry.
90     *
91     * @return The JNDI Entry of the target; <code>null</code> if
92     * no appropriate target could be found.
93     */

94    public static MessageDestinationMetaData findMessageDestination(MBeanServer JavaDoc server, DeploymentUnit di, String JavaDoc link)
95    {
96       return resolveMessageDestination(server, di, link);
97    }
98
99    private static String JavaDoc resolveLink(MBeanServer JavaDoc server, DeploymentUnit di, String JavaDoc link, boolean isLocal)
100    {
101       if (link == null)
102       {
103          return null;
104       }
105
106       if (log.isTraceEnabled())
107       {
108          log.trace("resolveLink( {" + di + "}, {" + link + "}, {" + isLocal + "}");
109       }
110
111       if (di == null)
112       {
113          // We should throw an IllegalArgumentException here probably?
114
return null;
115       }
116
117       if (link.indexOf('#') != -1)
118       {
119          // <ejb-link> is specified in the form path/file.jar#Bean
120
return resolveRelativeLink(server, di, link, isLocal);
121       }
122       else
123       {
124          // <ejb-link> contains a Bean Name, scan the DeploymentUnit tree
125
DeploymentUnit top = di;
126          while (top.getDeploymentContext().getParent() != null)
127          {
128             top = top.getDeploymentContext().getParent().getDeploymentUnit();
129          }
130
131          return resolveAbsoluteLink(top, link, isLocal);
132       }
133    }
134
135    private static String JavaDoc resolveRelativeLink(MBeanServer JavaDoc server, DeploymentUnit unit, String JavaDoc link, boolean isLocal)
136    {
137
138       String JavaDoc path = link.substring(0, link.indexOf('#'));
139       String JavaDoc ejbName = link.substring(link.indexOf('#') + 1);
140       String JavaDoc us = unit.getName();
141
142       // Remove the trailing slash for unpacked deployments
143
if (us.charAt(us.length() - 1) == '/')
144          us = us.substring(0, us.length() - 1);
145
146       String JavaDoc ourPath = us.substring(0, us.lastIndexOf('/'));
147
148       if (log.isTraceEnabled())
149       {
150          log.trace("Resolving relative link: " + link);
151          log.trace("Looking for: '" + link + "', we're located at: '" + ourPath + "'");
152       }
153
154       for (StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(path, "/"); st.hasMoreTokens();)
155       {
156          String JavaDoc s = st.nextToken();
157          if (s.equals(".."))
158          {
159             ourPath = ourPath.substring(0, ourPath.lastIndexOf('/'));
160          }
161          else
162          {
163             ourPath += "/" + s;
164          }
165       }
166
167       URL JavaDoc target = null;
168
169       try
170       {
171          target = Strings.toURL(ourPath);
172       }
173       catch (MalformedURLException JavaDoc mue)
174       {
175          log.warn("Can't construct URL for: " + ourPath);
176          return null;
177       }
178
179       DeploymentUnit targetUnit = null;
180       try
181       {
182          targetUnit = (DeploymentUnit)server.invoke(MainDeployerMBean.OBJECT_NAME, "getDeployment", new Object JavaDoc[] { target }, new String JavaDoc[] { URL JavaDoc.class.getName() });
183       }
184       catch (Exception JavaDoc e)
185       {
186          log.warn("Got Exception when looking for DeploymentUnit: " + e);
187          return null;
188       }
189
190       if (targetUnit == null)
191       {
192          log.warn("Can't locate DeploymentUnit for target: " + target);
193          return null;
194       }
195
196       if (log.isTraceEnabled())
197       {
198          log.trace("Found appropriate DeploymentUnit: " + targetUnit);
199       }
200
201       String JavaDoc linkTarget = null;
202       if (targetUnit.getAttachment(ApplicationMetaData.class) != null)
203       {
204          ApplicationMetaData appMD = targetUnit.getAttachment(ApplicationMetaData.class);
205          BeanMetaData beanMD = appMD.getBeanByEjbName(ejbName);
206
207          if (beanMD != null)
208          {
209             linkTarget = getJndiName(beanMD, isLocal);
210          }
211          else
212          {
213             log.warn("No Bean named '" + ejbName + "' found in '" + path + "'!");
214          }
215       }
216       else
217       {
218          log.warn("DeploymentUnit " + targetUnit + " is not an EJB .jar " + "file!");
219       }
220
221       return linkTarget;
222    }
223
224    private static String JavaDoc resolveAbsoluteLink(DeploymentUnit unit, String JavaDoc link, boolean isLocal)
225    {
226       if (log.isTraceEnabled())
227       {
228          log.trace("Resolving absolute link, di: " + unit);
229       }
230
231       String JavaDoc ejbName = null;
232
233       // Search current DeploymentUnit
234
if (unit.getAttachment(ApplicationMetaData.class) != null)
235       {
236          ApplicationMetaData appMD = unit.getAttachment(ApplicationMetaData.class);
237          BeanMetaData beanMD = appMD.getBeanByEjbName(link);
238          if (beanMD != null)
239          {
240             ejbName = getJndiName(beanMD, isLocal);
241             if (log.isTraceEnabled())
242             {
243                log.trace("Found Bean: " + beanMD + ", resolves to: " + ejbName);
244             }
245
246             return ejbName;
247          }
248          else if (log.isTraceEnabled())
249          {
250             // Dump the ejb module ejbNames
251
log.trace("No match for ejb-link: " + link + ", module names:");
252             Iterator JavaDoc iter = appMD.getEnterpriseBeans();
253             while (iter.hasNext())
254             {
255                beanMD = (BeanMetaData)iter.next();
256                String JavaDoc beanEjbName = getJndiName(beanMD, isLocal);
257                log.trace("... ejbName: " + beanEjbName);
258             }
259          }
260       }
261
262       // Search each subcontext
263
Iterator JavaDoc it = unit.getDeploymentContext().getChildren().iterator();
264       while (it.hasNext() && ejbName == null)
265       {
266          DeploymentUnit child = ((DeploymentContext)it.next()).getDeploymentUnit();
267          ejbName = resolveAbsoluteLink(child, link, isLocal);
268       }
269
270       return ejbName;
271    }
272
273    private static String JavaDoc getJndiName(BeanMetaData beanMD, boolean isLocal)
274    {
275       String JavaDoc jndiName = null;
276       if (isLocal)
277       {
278          // Validate that there is a local home associated with this bean
279
String JavaDoc localHome = beanMD.getLocalHome();
280          if (localHome != null)
281             jndiName = beanMD.getLocalJndiName();
282          else
283          {
284             log.warn("LocalHome jndi name requested for: '" + beanMD.getEjbName() + "' but there is no LocalHome class");
285          }
286       }
287       else
288       {
289          jndiName = beanMD.getJndiName();
290       }
291       return jndiName;
292    }
293
294    private static MessageDestinationMetaData resolveMessageDestination(MBeanServer JavaDoc server, DeploymentUnit di, String JavaDoc link)
295    {
296       if (link == null)
297          return null;
298
299       if (log.isTraceEnabled())
300          log.trace("resolveLink( {" + di + "}, {" + link + "})");
301
302       if (di == null)
303          // We should throw an IllegalArgumentException here probably?
304
return null;
305
306       if (link.indexOf('#') != -1)
307          // link is specified in the form path/file.jar#Bean
308
return resolveRelativeMessageDestination(server, di, link);
309       else
310       {
311          // link contains a Bean Name, scan the DeploymentUnit tree
312
DeploymentUnit top = di;
313          while (top.getDeploymentContext().getParent() != null)
314             top = top.getDeploymentContext().getParent().getDeploymentUnit();
315
316          return resolveAbsoluteMessageDestination(top, link);
317       }
318    }
319
320    private static MessageDestinationMetaData resolveRelativeMessageDestination(MBeanServer JavaDoc server, DeploymentUnit unit, String JavaDoc link)
321    {
322       String JavaDoc path = link.substring(0, link.indexOf('#'));
323       String JavaDoc destinationName = link.substring(link.indexOf('#') + 1);
324       String JavaDoc us = unit.getName();
325
326       // Remove the trailing slash for unpacked deployments
327
if (us.charAt(us.length() - 1) == '/')
328          us = us.substring(0, us.length() - 1);
329
330       String JavaDoc ourPath = us.substring(0, us.lastIndexOf('/'));
331
332       if (log.isTraceEnabled())
333       {
334          log.trace("Resolving relative message-destination-link: " + link);
335          log.trace("Looking for: '" + link + "', we're located at: '" + ourPath + "'");
336       }
337
338       for (StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(path, "/"); st.hasMoreTokens();)
339       {
340          String JavaDoc s = st.nextToken();
341          if (s.equals(".."))
342             ourPath = ourPath.substring(0, ourPath.lastIndexOf('/'));
343          else ourPath += "/" + s;
344       }
345
346       URL JavaDoc target = null;
347       try
348       {
349          target = Strings.toURL(ourPath);
350       }
351       catch (MalformedURLException JavaDoc mue)
352       {
353          log.warn("Can't construct URL for: " + ourPath);
354          return null;
355       }
356
357       DeploymentUnit targetUnit = null;
358       try
359       {
360          targetUnit = (DeploymentUnit)server.invoke(MainDeployerMBean.OBJECT_NAME, "getDeployment", new Object JavaDoc[] { target }, new String JavaDoc[] { URL JavaDoc.class.getName() });
361       }
362       catch (Exception JavaDoc e)
363       {
364          log.warn("Got Exception when looking for DeploymentUnit: " + e);
365          return null;
366       }
367
368       if (targetUnit == null)
369       {
370          log.warn("Can't locate DeploymentUnit for target: " + target);
371          return null;
372       }
373
374       if (log.isTraceEnabled())
375          log.trace("Found appropriate DeploymentUnit: " + targetUnit);
376
377       if (targetUnit.getAttachment(ApplicationMetaData.class) != null)
378       {
379          ApplicationMetaData appMD = targetUnit.getAttachment(ApplicationMetaData.class);
380          return appMD.getMessageDestination(destinationName);
381       }
382       if (targetUnit.getAttachment(WebMetaData.class) != null)
383       {
384          WebMetaData webMD = targetUnit.getAttachment(WebMetaData.class);
385          return webMD.getMessageDestination(destinationName);
386       }
387       else
388       {
389          log.warn("DeploymentUnit " + targetUnit + " is not an EJB .jar " + "file!");
390          return null;
391       }
392    }
393
394    private static MessageDestinationMetaData resolveAbsoluteMessageDestination(DeploymentUnit unit, String JavaDoc link)
395    {
396       if (log.isTraceEnabled())
397          log.trace("Resolving absolute link, di: " + unit);
398
399       // Search current DeploymentUnit
400
if (unit.getAttachment(ApplicationMetaData.class) != null)
401       {
402          ApplicationMetaData appMD = unit.getAttachment(ApplicationMetaData.class);
403          MessageDestinationMetaData mdMD = appMD.getMessageDestination(link);
404          if (mdMD != null)
405             return mdMD;
406       }
407       if (unit.getAttachment(WebMetaData.class) != null)
408       {
409          WebMetaData webMD = unit.getAttachment(WebMetaData.class);
410          return webMD.getMessageDestination(link);
411       }
412
413       // Search each subcontext
414
Iterator JavaDoc it = unit.getDeploymentContext().getChildren().iterator();
415       while (it.hasNext())
416       {
417          DeploymentUnit child = ((DeploymentContext)it.next()).getDeploymentUnit();
418          MessageDestinationMetaData mdMD = resolveAbsoluteMessageDestination(child, link);
419          if (mdMD != null)
420             return mdMD;
421       }
422
423       // Not found
424
return null;
425    }
426 }
Popular Tags