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