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 1970 2007-10-16 11:49:25Z benoitf $
023: * --------------------------------------------------------------------------
024: */package org.ow2.easybeans.injection;
025:
026: import javax.naming.InitialContext;
027: import javax.naming.NamingException;
028:
029: import org.ow2.util.log.Log;
030: import org.ow2.util.log.LogFactory;
031:
032: /**
033: * Helper class for injecting a JNDI Name in the bean.
034: * @author Florent Benoit
035: */
036: public final class JNDILookupHelper {
037:
038: /**
039: * Type of lookup available.
040: */
041: public enum JndiType {
042: REGISTRY, JAVA_COMP, JAVA_COMP_ENV
043: }
044:
045: /**
046: * Comp prefix.
047: */
048: private static final String JAVA_COMP = "java:comp/";
049:
050: /**
051: * ENV prefix.
052: */
053: private static final String JAVA_COMP_ENV = JAVA_COMP + "env/";
054:
055: /**
056: * Logger.
057: */
058: private static Log logger = LogFactory
059: .getLog(JNDILookupHelper.class);
060:
061: /**
062: * Utility class, no public constructor.
063: */
064: private JNDILookupHelper() {
065:
066: }
067:
068: /**
069: * Gets a JNDI name object.
070: * @param name the name of the object to lookup.
071: * @return object found for the given JNDI name, else null.
072: */
073: public static Object getJndiName(final String name) {
074: InitialContext ictx;
075: try {
076: ictx = new InitialContext();
077: } catch (NamingException e) {
078: logger.error("Cannot instantiate an initial context", e);
079: return null;
080: }
081:
082: Object o = null;
083: try {
084: o = ictx.lookup(name);
085: } catch (NamingException e) {
086: logger.error("Cannot find the JNDI name {0}", name, e);
087: }
088: if (o == null) {
089: logger.error("No object was found for JNDI name {0}", name);
090: }
091: return o;
092: }
093:
094: /**
095: * Gets a JNDI name object in java:comp/env/.
096: * @param name the name of the object to lookup.
097: * @return object found for the given JNDI name, else null.
098: */
099: public static Object getEnvJndiName(final String name) {
100: return getJndiName(JAVA_COMP_ENV + name);
101: }
102:
103: /**
104: * Gets a JNDI name object in java:comp/.
105: * @param name the name of the object to lookup.
106: * @return object found for the given JNDI name, else null.
107: */
108: public static Object getCompJndiName(final String name) {
109: return getJndiName(JAVA_COMP + name);
110: }
111:
112: }
|