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