KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > easybeans > injection > JNDILookupHelper


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: JNDILookupHelper.java 237 2006-03-20 16:14:01Z benoitf $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.easybeans.injection;
27
28 import javax.naming.InitialContext JavaDoc;
29 import javax.naming.NamingException JavaDoc;
30
31 import org.objectweb.easybeans.log.JLog;
32 import org.objectweb.easybeans.log.JLogFactory;
33
34 /**
35  * Helper class for injecting a JNDI Name in the bean.
36  * @author Florent Benoit
37  */

38 public final class JNDILookupHelper {
39
40     /**
41      * Type of lookup available.
42      */

43     public enum JndiType { REGISTRY, JAVA_COMP, JAVA_COMP_ENV}
44
45     /**
46      * Comp prefix.
47      */

48     private static final String JavaDoc JAVA_COMP = "java:comp/";
49
50     /**
51      * ENV prefix.
52      */

53     private static final String JavaDoc JAVA_COMP_ENV = JAVA_COMP + "env/";
54
55     /**
56      * Logger.
57      */

58     private static JLog logger = JLogFactory.getLog(JNDILookupHelper.class);
59
60
61
62     /**
63      * Utility class, no public constructor.
64      */

65     private JNDILookupHelper() {
66
67     }
68
69     /**
70      * Gets a JNDI name object.
71      * @param name the name of the object to lookup.
72      * @return object found for the given JNDI name, else null.
73      */

74     public static Object JavaDoc getJndiName(final String JavaDoc name) {
75         InitialContext JavaDoc ictx;
76         try {
77             ictx = new InitialContext JavaDoc();
78         } catch (NamingException JavaDoc e) {
79             logger.error("Cannot instantiate an initial context", e);
80             return null;
81         }
82
83         Object JavaDoc o = null;
84         try {
85             o = ictx.lookup(name);
86         } catch (NamingException JavaDoc e) {
87             logger.error("Cannot find the JNDI name {0}", name, e);
88         }
89         if (o == null) {
90             logger.error("No object was found for JNDI name {0}", name);
91         }
92         return o;
93     }
94
95     /**
96      * Gets a JNDI name object in java:comp/env/.
97      * @param name the name of the object to lookup.
98      * @return object found for the given JNDI name, else null.
99      */

100     public static Object JavaDoc getEnvJndiName(final String JavaDoc name) {
101         return getJndiName(JAVA_COMP_ENV + name);
102     }
103
104     /**
105      * Gets a JNDI name object in java:comp/.
106      * @param name the name of the object to lookup.
107      * @return object found for the given JNDI name, else null.
108      */

109     public static Object JavaDoc getCompJndiName(final String JavaDoc name) {
110         return getJndiName(JAVA_COMP + name);
111     }
112
113 }
114
Popular Tags