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