1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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  public class JNDIUtil {
43  
44      public static Object lookup(Context ctx, String location)
45          throws NamingException {
46  
47          if (ServerDetector.isGlassfish() &&
48              location.equals("mail/MailSession")) {
49  
50              location = "java:comp/env/" + location;
51          }
52  
53          return lookup(ctx, location, false);
54      }
55  
56      public static Object lookup(Context ctx, String location, boolean cache)
57          throws NamingException {
58  
59          Object obj = null;
60  
61          if (cache) {
62              obj = _cache.get(location);
63  
64              if (obj == null) {
65                  obj = _lookup(ctx, location);
66  
67                  _cache.put(location, obj);
68              }
69          }
70          else {
71              obj = _lookup(ctx, location);
72          }
73  
74          return obj;
75      }
76  
77      private static Object _lookup(Context ctx, String location)
78          throws NamingException {
79  
80          if (_log.isDebugEnabled()) {
81              _log.debug("Lookup " + location);
82          }
83  
84          Object obj = null;
85  
86          try {
87              obj = ctx.lookup(location);
88          }
89          catch (NamingException n1) {
90  
91              // java:comp/env/ObjectName to ObjectName
92  
93              if (location.indexOf("java:comp/env/") != -1) {
94                  try {
95                      String newLocation = StringUtil.replace(
96                          location, "java:comp/env/", "");
97  
98                      if (_log.isDebugEnabled()) {
99                          _log.debug(n1.getMessage());
100                         _log.debug("Attempt " + newLocation);
101                     }
102 
103                     obj = ctx.lookup(newLocation);
104                 }
105                 catch (NamingException n2) {
106 
107                     // java:comp/env/ObjectName to java:ObjectName
108 
109                     String newLocation = StringUtil.replace(
110                         location, "comp/env/", "");
111 
112                     if (_log.isDebugEnabled()) {
113                         _log.debug(n2.getMessage());
114                         _log.debug("Attempt " + newLocation);
115                     }
116 
117                     obj = ctx.lookup(newLocation);
118                 }
119             }
120 
121             // java:ObjectName to ObjectName
122 
123             else if (location.indexOf("java:") != -1) {
124                 try {
125                     String newLocation = StringUtil.replace(
126                         location, "java:", "");
127 
128                     if (_log.isDebugEnabled()) {
129                         _log.debug(n1.getMessage());
130                         _log.debug("Attempt " + newLocation);
131                     }
132 
133                     obj = ctx.lookup(newLocation);
134                 }
135                 catch (NamingException n2) {
136 
137                     // java:ObjectName to java:comp/env/ObjectName
138 
139                     String newLocation = StringUtil.replace(
140                         location, "java:", "java:comp/env/");
141 
142                     if (_log.isDebugEnabled()) {
143                         _log.debug(n2.getMessage());
144                         _log.debug("Attempt " + newLocation);
145                     }
146 
147                     obj = ctx.lookup(newLocation);
148                 }
149             }
150 
151             // ObjectName to java:ObjectName
152 
153             else if (location.indexOf("java:") == -1) {
154                 try {
155                     String newLocation = "java:" + location;
156 
157                     if (_log.isDebugEnabled()) {
158                         _log.debug(n1.getMessage());
159                         _log.debug("Attempt " + newLocation);
160                     }
161 
162                     obj = ctx.lookup(newLocation);
163                 }
164                 catch (NamingException n2) {
165 
166                     // ObjectName to java:comp/env/ObjectName
167 
168                     String newLocation = "java:comp/env/" + location;
169 
170                     if (_log.isDebugEnabled()) {
171                         _log.debug(n2.getMessage());
172                         _log.debug("Attempt " + newLocation);
173                     }
174 
175                     obj = ctx.lookup(newLocation);
176                 }
177             }
178             else {
179                 throw new NamingException();
180             }
181         }
182 
183         return obj;
184     }
185 
186     private static Log _log = LogFactoryUtil.getLog(JNDIUtil.class);
187 
188     private static Map<String, Object> _cache = new HashMap<String, Object>();
189 
190 }