1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
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.StringUtil;
28  
29  import java.util.HashMap;
30  import java.util.Map;
31  
32  import javax.naming.Context;
33  import javax.naming.NamingException;
34  
35  /**
36   * <a href="JNDIUtil.java.html"><b><i>View Source</i></b></a>
37   *
38   * @author Brian Wing Shun Chan
39   *
40   */
41  public class JNDIUtil {
42  
43      public static Object lookup(Context ctx, String location)
44          throws NamingException {
45  
46          return lookup(ctx, location, false);
47      }
48  
49      public static Object lookup(Context ctx, String location, boolean cache)
50          throws NamingException {
51  
52          Object obj = null;
53  
54          if (cache) {
55              obj = _cache.get(location);
56  
57              if (obj == null) {
58                  obj = _lookup(ctx, location);
59  
60                  _cache.put(location, obj);
61              }
62          }
63          else {
64              obj = _lookup(ctx, location);
65          }
66  
67          return obj;
68      }
69  
70      private static Object _lookup(Context ctx, String location)
71          throws NamingException {
72  
73          if (_log.isDebugEnabled()) {
74              _log.debug("Lookup " + location);
75          }
76  
77          Object obj = null;
78  
79          try {
80              obj = ctx.lookup(location);
81          }
82          catch (NamingException n1) {
83  
84              // java:comp/env/ObjectName to ObjectName
85  
86              if (location.indexOf("java:comp/env/") != -1) {
87                  try {
88                      String newLocation = StringUtil.replace(
89                          location, "java:comp/env/", "");
90  
91                      if (_log.isDebugEnabled()) {
92                          _log.debug(n1.getMessage());
93                          _log.debug("Attempt " + newLocation);
94                      }
95  
96                      obj = ctx.lookup(newLocation);
97                  }
98                  catch (NamingException n2) {
99  
100                     // java:comp/env/ObjectName to java:ObjectName
101 
102                     String newLocation = StringUtil.replace(
103                         location, "comp/env/", "");
104 
105                     if (_log.isDebugEnabled()) {
106                         _log.debug(n2.getMessage());
107                         _log.debug("Attempt " + newLocation);
108                     }
109 
110                     obj = ctx.lookup(newLocation);
111                 }
112             }
113 
114             // java:ObjectName to ObjectName
115 
116             else if (location.indexOf("java:") != -1) {
117                 try {
118                     String newLocation = StringUtil.replace(
119                         location, "java:", "");
120 
121                     if (_log.isDebugEnabled()) {
122                         _log.debug(n1.getMessage());
123                         _log.debug("Attempt " + newLocation);
124                     }
125 
126                     obj = ctx.lookup(newLocation);
127                 }
128                 catch (NamingException n2) {
129 
130                     // java:ObjectName to java:comp/env/ObjectName
131 
132                     String newLocation = StringUtil.replace(
133                         location, "java:", "java:comp/env/");
134 
135                     if (_log.isDebugEnabled()) {
136                         _log.debug(n2.getMessage());
137                         _log.debug("Attempt " + newLocation);
138                     }
139 
140                     obj = ctx.lookup(newLocation);
141                 }
142             }
143 
144             // ObjectName to java:ObjectName
145 
146             else if (location.indexOf("java:") == -1) {
147                 try {
148                     String newLocation = "java:" + location;
149 
150                     if (_log.isDebugEnabled()) {
151                         _log.debug(n1.getMessage());
152                         _log.debug("Attempt " + newLocation);
153                     }
154 
155                     obj = ctx.lookup(newLocation);
156                 }
157                 catch (NamingException n2) {
158 
159                     // ObjectName to java:comp/env/ObjectName
160 
161                     String newLocation = "java:comp/env/" + location;
162 
163                     if (_log.isDebugEnabled()) {
164                         _log.debug(n2.getMessage());
165                         _log.debug("Attempt " + newLocation);
166                     }
167 
168                     obj = ctx.lookup(newLocation);
169                 }
170             }
171             else {
172                 throw new NamingException();
173             }
174         }
175 
176         return obj;
177     }
178 
179     private static Log _log = LogFactoryUtil.getLog(JNDIUtil.class);
180 
181     private static Map<String, Object> _cache = new HashMap<String, Object>();
182 
183 }