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