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