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