001
014
015 package com.liferay.portal.kernel.jndi;
016
017 import com.liferay.portal.kernel.log.Log;
018 import com.liferay.portal.kernel.log.LogFactoryUtil;
019 import com.liferay.portal.kernel.security.pacl.permission.PortalRuntimePermission;
020 import com.liferay.portal.kernel.util.StringUtil;
021
022 import javax.naming.Context;
023 import javax.naming.NamingException;
024
025
029 public class JNDIUtil {
030
031 public static Object lookup(Context context, String location)
032 throws NamingException {
033
034 return _lookup(context, location);
035 }
036
037
040 @Deprecated
041 public static Object lookup(Context context, String location, boolean cache)
042 throws NamingException {
043
044 return _lookup(context, location);
045 }
046
047 private static Object _lookup(Context context, String location)
048 throws NamingException {
049
050 PortalRuntimePermission.checkGetBeanProperty(JNDIUtil.class);
051
052 if (_log.isDebugEnabled()) {
053 _log.debug("Lookup " + location);
054 }
055
056 Object obj = null;
057
058 try {
059 obj = context.lookup(location);
060 }
061 catch (NamingException ne1) {
062
063
064
065 if (location.contains("java:comp/env/")) {
066 try {
067 String newLocation = StringUtil.replace(
068 location, "java:comp/env/", "");
069
070 if (_log.isDebugEnabled()) {
071 _log.debug(ne1.getMessage());
072 _log.debug("Attempt " + newLocation);
073 }
074
075 obj = context.lookup(newLocation);
076 }
077 catch (NamingException ne2) {
078
079
080
081 String newLocation = StringUtil.replace(
082 location, "comp/env/", "");
083
084 if (_log.isDebugEnabled()) {
085 _log.debug(ne2.getMessage());
086 _log.debug("Attempt " + newLocation);
087 }
088
089 obj = context.lookup(newLocation);
090 }
091 }
092
093
094
095 else if (location.contains("java:")) {
096 try {
097 String newLocation = StringUtil.replace(
098 location, "java:", "");
099
100 if (_log.isDebugEnabled()) {
101 _log.debug(ne1.getMessage());
102 _log.debug("Attempt " + newLocation);
103 }
104
105 obj = context.lookup(newLocation);
106 }
107 catch (NamingException ne2) {
108
109
110
111 String newLocation = StringUtil.replace(
112 location, "java:", "java:comp/env/");
113
114 if (_log.isDebugEnabled()) {
115 _log.debug(ne2.getMessage());
116 _log.debug("Attempt " + newLocation);
117 }
118
119 obj = context.lookup(newLocation);
120 }
121 }
122
123
124
125 else if (!location.contains("java:")) {
126 try {
127 String newLocation = "java:" + location;
128
129 if (_log.isDebugEnabled()) {
130 _log.debug(ne1.getMessage());
131 _log.debug("Attempt " + newLocation);
132 }
133
134 obj = context.lookup(newLocation);
135 }
136 catch (NamingException ne2) {
137
138
139
140 String newLocation = "java:comp/env/" + location;
141
142 if (_log.isDebugEnabled()) {
143 _log.debug(ne2.getMessage());
144 _log.debug("Attempt " + newLocation);
145 }
146
147 obj = context.lookup(newLocation);
148 }
149 }
150 else {
151 throw new NamingException();
152 }
153 }
154
155 return obj;
156 }
157
158 private static final Log _log = LogFactoryUtil.getLog(JNDIUtil.class);
159
160 }