KickJava   Java API By Example, From Geeks To Geeks.

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


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.Context JavaDoc;
29 import javax.naming.InitialContext JavaDoc;
30 import javax.naming.LinkRef JavaDoc;
31 import javax.naming.NamingException JavaDoc;
32
33 import org.objectweb.easybeans.log.JLog;
34 import org.objectweb.easybeans.log.JLogFactory;
35
36 /**
37  * Helper class for injecting an object in JNDI (ENC env).
38  * @author Florent Benoit
39  */

40 public final class JNDIBinderHelper {
41
42     /**
43      * Type of lookup available.
44      */

45     public enum JndiType {JAVA_COMP_ENV}
46
47     /**
48      * Comp prefix.
49      */

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

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

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

67     private JNDIBinderHelper() {
68
69     }
70
71     /**
72      * Gets the context for a given name (java: , java:comp, etc).
73      * @param name the name of the context to lookup
74      * @return context found or null.
75      */

76     public static Context JavaDoc getContext(final String JavaDoc name) {
77         InitialContext JavaDoc ictx;
78         try {
79             ictx = new InitialContext JavaDoc();
80         } catch (NamingException JavaDoc e) {
81             logger.error("Cannot instantiate an initial context", e);
82             return null;
83         }
84
85         Object JavaDoc o = null;
86         try {
87             o = ictx.lookup(name);
88         } catch (NamingException JavaDoc e) {
89             logger.error("Cannot find the JNDI name {0}", name, e);
90         }
91         if (o == null) {
92             logger.error("No object was found for JNDI name {0}", name);
93         }
94         Context JavaDoc ctx = null;
95         if (o instanceof Context JavaDoc) {
96             ctx = (Context JavaDoc) o;
97         } else {
98             logger.error("Object not instance of context. Object = {0}", o);
99         }
100         return ctx;
101     }
102
103     /**
104      * Bind a JNDI name object in java:comp/env/.
105      * @param encName the name of the object to bind in enc
106      * @param jndiName the jndi name to link.
107      */

108     public static void bindLinkRefEnvJndiName(final String JavaDoc encName, final String JavaDoc jndiName) {
109         try {
110             getContext(JAVA_COMP_ENV).rebind(encName, new LinkRef JavaDoc(jndiName));
111         } catch (NamingException JavaDoc e) {
112             logger.error("Cannot do a LinkRef between jndiName {0} with ENC name {1}", jndiName, encName, e);
113         }
114     }
115
116     /**
117      * Bind a JNDI name object in java:comp/env/.
118      * @param name the name of the object to bind.
119      * @param object the value of the object.
120      */

121     public static void bindEnvJndiName(final String JavaDoc name, final Object JavaDoc object) {
122         try {
123             getContext(JAVA_COMP_ENV).rebind(name, object);
124         } catch (NamingException JavaDoc e) {
125             logger.error("Cannot bind object {0} with name {1}", object, name, e);
126         }
127     }
128
129
130 }
131
Popular Tags