001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.util;
022:
023: import com.liferay.portal.kernel.util.StringUtil;
024:
025: import java.util.HashMap;
026: import java.util.Map;
027:
028: import javax.naming.Context;
029: import javax.naming.NamingException;
030:
031: import org.apache.commons.logging.Log;
032: import org.apache.commons.logging.LogFactory;
033:
034: /**
035: * <a href="JNDIUtil.java.html"><b><i>View Source</i></b></a>
036: *
037: * @author Brian Wing Shun Chan
038: *
039: */
040: public class JNDIUtil {
041:
042: public static Object lookup(Context ctx, String location)
043: throws NamingException {
044:
045: return lookup(ctx, location, false);
046: }
047:
048: public static Object lookup(Context ctx, String location,
049: boolean cache) throws NamingException {
050:
051: Object obj = null;
052:
053: if (cache) {
054: obj = _cache.get(location);
055:
056: if (obj == null) {
057: obj = _lookup(ctx, location);
058:
059: _cache.put(location, obj);
060: }
061: } else {
062: obj = _lookup(ctx, location);
063: }
064:
065: return obj;
066: }
067:
068: private static Object _lookup(Context ctx, String location)
069: throws NamingException {
070:
071: if (_log.isDebugEnabled()) {
072: _log.debug("Lookup " + location);
073: }
074:
075: Object obj = null;
076:
077: try {
078: obj = ctx.lookup(location);
079: } catch (NamingException n1) {
080:
081: // java:comp/env/ObjectName to ObjectName
082:
083: if (location.indexOf("java:comp/env/") != -1) {
084: try {
085: String newLocation = StringUtil.replace(location,
086: "java:comp/env/", "");
087:
088: if (_log.isDebugEnabled()) {
089: _log.debug(n1.getMessage());
090: _log.debug("Attempt " + newLocation);
091: }
092:
093: obj = ctx.lookup(newLocation);
094: } catch (NamingException n2) {
095:
096: // java:comp/env/ObjectName to java:ObjectName
097:
098: String newLocation = StringUtil.replace(location,
099: "comp/env/", "");
100:
101: if (_log.isDebugEnabled()) {
102: _log.debug(n2.getMessage());
103: _log.debug("Attempt " + newLocation);
104: }
105:
106: obj = ctx.lookup(newLocation);
107: }
108: }
109:
110: // java:ObjectName to ObjectName
111:
112: else if (location.indexOf("java:") != -1) {
113: try {
114: String newLocation = StringUtil.replace(location,
115: "java:", "");
116:
117: if (_log.isDebugEnabled()) {
118: _log.debug(n1.getMessage());
119: _log.debug("Attempt " + newLocation);
120: }
121:
122: obj = ctx.lookup(newLocation);
123: } catch (NamingException n2) {
124:
125: // java:ObjectName to java:comp/env/ObjectName
126:
127: String newLocation = StringUtil.replace(location,
128: "java:", "java:comp/env/");
129:
130: if (_log.isDebugEnabled()) {
131: _log.debug(n2.getMessage());
132: _log.debug("Attempt " + newLocation);
133: }
134:
135: obj = ctx.lookup(newLocation);
136: }
137: }
138:
139: // ObjectName to java:ObjectName
140:
141: else if (location.indexOf("java:") == -1) {
142: try {
143: String newLocation = "java:" + location;
144:
145: if (_log.isDebugEnabled()) {
146: _log.debug(n1.getMessage());
147: _log.debug("Attempt " + newLocation);
148: }
149:
150: obj = ctx.lookup(newLocation);
151: } catch (NamingException n2) {
152:
153: // ObjectName to java:comp/env/ObjectName
154:
155: String newLocation = "java:comp/env/" + location;
156:
157: if (_log.isDebugEnabled()) {
158: _log.debug(n2.getMessage());
159: _log.debug("Attempt " + newLocation);
160: }
161:
162: obj = ctx.lookup(newLocation);
163: }
164: } else {
165: throw new NamingException();
166: }
167: }
168:
169: return obj;
170: }
171:
172: private static Log _log = LogFactory.getLog(JNDIUtil.class);
173:
174: private static Map _cache = new HashMap();
175:
176: }
|