001
014
015 package com.liferay.portal.kernel.jndi;
016
017 import com.liferay.portal.kernel.log.Log;
018 import com.liferay.portal.kernel.log.LogFactoryUtil;
019 import com.liferay.portal.kernel.util.StringUtil;
020
021 import java.util.HashMap;
022 import java.util.Map;
023
024 import javax.naming.Context;
025 import javax.naming.NamingException;
026
027
031 public class JNDIUtil {
032
033 public static Object lookup(Context ctx, String location)
034 throws NamingException {
035
036 return lookup(ctx, location, false);
037 }
038
039 public static Object lookup(Context ctx, String location, boolean cache)
040 throws NamingException {
041
042 Object obj = null;
043
044 if (cache) {
045 obj = _cache.get(location);
046
047 if (obj == null) {
048 obj = _lookup(ctx, location);
049
050 _cache.put(location, obj);
051 }
052 }
053 else {
054 obj = _lookup(ctx, location);
055 }
056
057 return obj;
058 }
059
060 private static Object _lookup(Context ctx, String location)
061 throws NamingException {
062
063 if (_log.isDebugEnabled()) {
064 _log.debug("Lookup " + location);
065 }
066
067 Object obj = null;
068
069 try {
070 obj = ctx.lookup(location);
071 }
072 catch (NamingException n1) {
073
074
075
076 if (location.indexOf("java:comp/env/") != -1) {
077 try {
078 String newLocation = StringUtil.replace(
079 location, "java:comp/env/", "");
080
081 if (_log.isDebugEnabled()) {
082 _log.debug(n1.getMessage());
083 _log.debug("Attempt " + newLocation);
084 }
085
086 obj = ctx.lookup(newLocation);
087 }
088 catch (NamingException n2) {
089
090
091
092 String newLocation = StringUtil.replace(
093 location, "comp/env/", "");
094
095 if (_log.isDebugEnabled()) {
096 _log.debug(n2.getMessage());
097 _log.debug("Attempt " + newLocation);
098 }
099
100 obj = ctx.lookup(newLocation);
101 }
102 }
103
104
105
106 else if (location.indexOf("java:") != -1) {
107 try {
108 String newLocation = StringUtil.replace(
109 location, "java:", "");
110
111 if (_log.isDebugEnabled()) {
112 _log.debug(n1.getMessage());
113 _log.debug("Attempt " + newLocation);
114 }
115
116 obj = ctx.lookup(newLocation);
117 }
118 catch (NamingException n2) {
119
120
121
122 String newLocation = StringUtil.replace(
123 location, "java:", "java:comp/env/");
124
125 if (_log.isDebugEnabled()) {
126 _log.debug(n2.getMessage());
127 _log.debug("Attempt " + newLocation);
128 }
129
130 obj = ctx.lookup(newLocation);
131 }
132 }
133
134
135
136 else if (location.indexOf("java:") == -1) {
137 try {
138 String newLocation = "java:" + location;
139
140 if (_log.isDebugEnabled()) {
141 _log.debug(n1.getMessage());
142 _log.debug("Attempt " + newLocation);
143 }
144
145 obj = ctx.lookup(newLocation);
146 }
147 catch (NamingException n2) {
148
149
150
151 String newLocation = "java:comp/env/" + location;
152
153 if (_log.isDebugEnabled()) {
154 _log.debug(n2.getMessage());
155 _log.debug("Attempt " + newLocation);
156 }
157
158 obj = ctx.lookup(newLocation);
159 }
160 }
161 else {
162 throw new NamingException();
163 }
164 }
165
166 return obj;
167 }
168
169 private static Log _log = LogFactoryUtil.getLog(JNDIUtil.class);
170
171 private static Map<String, Object> _cache = new HashMap<String, Object>();
172
173 }