1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
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  /**
28   * <a href="JNDIUtil.java.html"><b><i>View Source</i></b></a>
29   *
30   * @author Brian Wing Shun Chan
31   * @author Sandeep Soni
32   */
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              // java:comp/env/ObjectName to ObjectName
77  
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                      // java:comp/env/ObjectName to java:ObjectName
93  
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             // java:ObjectName to ObjectName
107 
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                     // java:ObjectName to java:comp/env/ObjectName
123 
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             // ObjectName to java:ObjectName
137 
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                     // ObjectName to java:comp/env/ObjectName
152 
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 }