1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.kernel.jndi;
21  
22  import com.liferay.portal.kernel.log.Log;
23  import com.liferay.portal.kernel.log.LogFactoryUtil;
24  import com.liferay.portal.kernel.util.ServerDetector;
25  import com.liferay.portal.kernel.util.StringUtil;
26  
27  import java.util.HashMap;
28  import java.util.Map;
29  
30  import javax.naming.Context;
31  import javax.naming.NamingException;
32  
33  /**
34   * <a href="JNDIUtil.java.html"><b><i>View Source</i></b></a>
35   *
36   * @author Brian Wing Shun Chan
37   * @author Sandeep Soni
38   *
39   */
40  public class JNDIUtil {
41  
42      public static Object lookup(Context ctx, String location)
43          throws NamingException {
44  
45          if (ServerDetector.isGlassfish() &&
46              location.equals("mail/MailSession")){
47  
48              location = "java:comp/env/" + location;
49          }
50  
51          return lookup(ctx, location, false);
52      }
53  
54      public static Object lookup(Context ctx, String location, boolean cache)
55          throws NamingException {
56  
57          Object obj = null;
58  
59          if (cache) {
60              obj = _cache.get(location);
61  
62              if (obj == null) {
63                  obj = _lookup(ctx, location);
64  
65                  _cache.put(location, obj);
66              }
67          }
68          else {
69              obj = _lookup(ctx, location);
70          }
71  
72          return obj;
73      }
74  
75      private static Object _lookup(Context ctx, String location)
76          throws NamingException {
77  
78          if (_log.isDebugEnabled()) {
79              _log.debug("Lookup " + location);
80          }
81  
82          Object obj = null;
83  
84          try {
85              obj = ctx.lookup(location);
86          }
87          catch (NamingException n1) {
88  
89              // java:comp/env/ObjectName to ObjectName
90  
91              if (location.indexOf("java:comp/env/") != -1) {
92                  try {
93                      String newLocation = StringUtil.replace(
94                          location, "java:comp/env/", "");
95  
96                      if (_log.isDebugEnabled()) {
97                          _log.debug(n1.getMessage());
98                          _log.debug("Attempt " + newLocation);
99                      }
100 
101                     obj = ctx.lookup(newLocation);
102                 }
103                 catch (NamingException n2) {
104 
105                     // java:comp/env/ObjectName to java:ObjectName
106 
107                     String newLocation = StringUtil.replace(
108                         location, "comp/env/", "");
109 
110                     if (_log.isDebugEnabled()) {
111                         _log.debug(n2.getMessage());
112                         _log.debug("Attempt " + newLocation);
113                     }
114 
115                     obj = ctx.lookup(newLocation);
116                 }
117             }
118 
119             // java:ObjectName to ObjectName
120 
121             else if (location.indexOf("java:") != -1) {
122                 try {
123                     String newLocation = StringUtil.replace(
124                         location, "java:", "");
125 
126                     if (_log.isDebugEnabled()) {
127                         _log.debug(n1.getMessage());
128                         _log.debug("Attempt " + newLocation);
129                     }
130 
131                     obj = ctx.lookup(newLocation);
132                 }
133                 catch (NamingException n2) {
134 
135                     // java:ObjectName to java:comp/env/ObjectName
136 
137                     String newLocation = StringUtil.replace(
138                         location, "java:", "java:comp/env/");
139 
140                     if (_log.isDebugEnabled()) {
141                         _log.debug(n2.getMessage());
142                         _log.debug("Attempt " + newLocation);
143                     }
144 
145                     obj = ctx.lookup(newLocation);
146                 }
147             }
148 
149             // ObjectName to java:ObjectName
150 
151             else if (location.indexOf("java:") == -1) {
152                 try {
153                     String newLocation = "java:" + location;
154 
155                     if (_log.isDebugEnabled()) {
156                         _log.debug(n1.getMessage());
157                         _log.debug("Attempt " + newLocation);
158                     }
159 
160                     obj = ctx.lookup(newLocation);
161                 }
162                 catch (NamingException n2) {
163 
164                     // ObjectName to java:comp/env/ObjectName
165 
166                     String newLocation = "java:comp/env/" + location;
167 
168                     if (_log.isDebugEnabled()) {
169                         _log.debug(n2.getMessage());
170                         _log.debug("Attempt " + newLocation);
171                     }
172 
173                     obj = ctx.lookup(newLocation);
174                 }
175             }
176             else {
177                 throw new NamingException();
178             }
179         }
180 
181         return obj;
182     }
183 
184     private static Log _log = LogFactoryUtil.getLog(JNDIUtil.class);
185 
186     private static Map<String, Object> _cache = new HashMap<String, Object>();
187 
188 }