001: /**
002: * EasyBeans
003: * Copyright (C) 2006 Bull S.A.S.
004: * Contact: easybeans@ow2.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: JNDILookupHelper.java 237 2006-03-20 16:14:01Z benoitf $
023: * --------------------------------------------------------------------------
024: */package org.ow2.easybeans.injection;
025:
026: import java.net.URL;
027:
028: import javax.naming.Context;
029: import javax.naming.InitialContext;
030: import javax.naming.LinkRef;
031: import javax.naming.NamingException;
032: import javax.naming.Reference;
033: import javax.naming.StringRefAddr;
034:
035: import org.ow2.easybeans.naming.url.URLFactory;
036: import org.ow2.util.log.Log;
037: import org.ow2.util.log.LogFactory;
038:
039: /**
040: * Helper class for injecting an object in JNDI (ENC env).
041: * @author Florent Benoit
042: */
043: public final class JNDIBinderHelper {
044:
045: /**
046: * Type of lookup available.
047: */
048: public enum JndiType {
049: JAVA_COMP_ENV
050: }
051:
052: /**
053: * Comp prefix.
054: */
055: private static final String JAVA_COMP = "java:comp/";
056:
057: /**
058: * ENV prefix.
059: */
060: private static final String JAVA_COMP_ENV = JAVA_COMP + "env";
061:
062: /**
063: * Logger.
064: */
065: private static Log logger = LogFactory
066: .getLog(JNDIBinderHelper.class);
067:
068: /**
069: * Utility class, no public constructor.
070: */
071: private JNDIBinderHelper() {
072:
073: }
074:
075: /**
076: * Gets the context for a given name (java: , java:comp, etc).
077: * @param name the name of the context to lookup
078: * @return context found or null.
079: */
080: public static Context getContext(final String name) {
081: InitialContext ictx;
082: try {
083: ictx = new InitialContext();
084: } catch (NamingException e) {
085: logger.error("Cannot instantiate an initial context", e);
086: return null;
087: }
088:
089: Object o = null;
090: try {
091: o = ictx.lookup(name);
092: } catch (NamingException e) {
093: logger.error("Cannot find the JNDI name {0}", name, e);
094: }
095: if (o == null) {
096: logger.error("No object was found for JNDI name {0}", name);
097: }
098: Context ctx = null;
099: if (o instanceof Context) {
100: ctx = (Context) o;
101: } else {
102: logger.error(
103: "Object not instance of context. Object = {0}", o);
104: }
105: return ctx;
106: }
107:
108: /**
109: * Bind a JNDI name object in java:comp/env/.
110: * @param encName the name of the object to bind in enc
111: * @param jndiName the jndi name to link.
112: */
113: public static void bindLinkRefEnvJndiName(final String encName,
114: final String jndiName) {
115: try {
116: getContext(JAVA_COMP_ENV).rebind(encName,
117: new LinkRef(jndiName));
118: } catch (NamingException e) {
119: logger
120: .error(
121: "Cannot do a LinkRef between jndiName {0} with ENC name {1}",
122: jndiName, encName, e);
123: }
124: }
125:
126: /**
127: * Bind a JNDI name object in java:comp/env/.
128: * @param name the name of the object to bind.
129: * @param object the value of the object.
130: */
131: public static void bindEnvJndiName(final String name,
132: final Object object) {
133: try {
134: getContext(JAVA_COMP_ENV).rebind(name, object);
135: } catch (NamingException e) {
136: logger.error("Cannot bind object {0} with name {1}",
137: object, name, e);
138: }
139: }
140:
141: /**
142: * Bind an URL object in java:comp/env.
143: * @param encName the name of the object to bind in enc
144: * @param url the URL to register in the context
145: */
146: public static void bindLinkRefEnvURL(final String encName,
147: final String url) {
148: // Specify the factory to use with the right URL
149: Reference ref = new Reference(URL.class.getName(),
150: URLFactory.class.getName(), null);
151: StringRefAddr refAddr = new StringRefAddr("url", url);
152: ref.add(refAddr);
153: try {
154: getContext(JAVA_COMP_ENV).rebind(encName, ref);
155: } catch (NamingException e) {
156: logger
157: .error(
158: "Cannot bind an URL with name {0} with ENC name {1}",
159: url, encName, e);
160: }
161: }
162:
163: }
|