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