1
22
23 package com.liferay.portal.kernel.jndi;
24
25 import com.liferay.portal.kernel.log.Log;
26 import com.liferay.portal.kernel.log.LogFactoryUtil;
27 import com.liferay.portal.kernel.util.StringUtil;
28
29 import java.util.HashMap;
30 import java.util.Map;
31
32 import javax.naming.Context;
33 import javax.naming.NamingException;
34
35
42 public class JNDIUtil {
43
44 public static Object lookup(Context ctx, String location)
45 throws NamingException {
46
47 return lookup(ctx, location, false);
48 }
49
50 public static Object lookup(Context ctx, String location, boolean cache)
51 throws NamingException {
52
53 Object obj = null;
54
55 if (cache) {
56 obj = _cache.get(location);
57
58 if (obj == null) {
59 obj = _lookup(ctx, location);
60
61 _cache.put(location, obj);
62 }
63 }
64 else {
65 obj = _lookup(ctx, location);
66 }
67
68 return obj;
69 }
70
71 private static Object _lookup(Context ctx, String location)
72 throws NamingException {
73
74 if (_log.isDebugEnabled()) {
75 _log.debug("Lookup " + location);
76 }
77
78 Object obj = null;
79
80 try {
81 obj = ctx.lookup(location);
82 }
83 catch (NamingException n1) {
84
85
87 if (location.indexOf("java:comp/env/") != -1) {
88 try {
89 String newLocation = StringUtil.replace(
90 location, "java:comp/env/", "");
91
92 if (_log.isDebugEnabled()) {
93 _log.debug(n1.getMessage());
94 _log.debug("Attempt " + newLocation);
95 }
96
97 obj = ctx.lookup(newLocation);
98 }
99 catch (NamingException n2) {
100
101
103 String newLocation = StringUtil.replace(
104 location, "comp/env/", "");
105
106 if (_log.isDebugEnabled()) {
107 _log.debug(n2.getMessage());
108 _log.debug("Attempt " + newLocation);
109 }
110
111 obj = ctx.lookup(newLocation);
112 }
113 }
114
115
117 else if (location.indexOf("java:") != -1) {
118 try {
119 String newLocation = StringUtil.replace(
120 location, "java:", "");
121
122 if (_log.isDebugEnabled()) {
123 _log.debug(n1.getMessage());
124 _log.debug("Attempt " + newLocation);
125 }
126
127 obj = ctx.lookup(newLocation);
128 }
129 catch (NamingException n2) {
130
131
133 String newLocation = StringUtil.replace(
134 location, "java:", "java:comp/env/");
135
136 if (_log.isDebugEnabled()) {
137 _log.debug(n2.getMessage());
138 _log.debug("Attempt " + newLocation);
139 }
140
141 obj = ctx.lookup(newLocation);
142 }
143 }
144
145
147 else if (location.indexOf("java:") == -1) {
148 try {
149 String newLocation = "java:" + location;
150
151 if (_log.isDebugEnabled()) {
152 _log.debug(n1.getMessage());
153 _log.debug("Attempt " + newLocation);
154 }
155
156 obj = ctx.lookup(newLocation);
157 }
158 catch (NamingException n2) {
159
160
162 String newLocation = "java:comp/env/" + location;
163
164 if (_log.isDebugEnabled()) {
165 _log.debug(n2.getMessage());
166 _log.debug("Attempt " + newLocation);
167 }
168
169 obj = ctx.lookup(newLocation);
170 }
171 }
172 else {
173 throw new NamingException();
174 }
175 }
176
177 return obj;
178 }
179
180 private static Log _log = LogFactoryUtil.getLog(JNDIUtil.class);
181
182 private static Map<String, Object> _cache = new HashMap<String, Object>();
183
184 }