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