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