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 public static Object lookup(Context context, String location, boolean cache)
041 throws NamingException {
042
043 return _lookup(context, location);
044 }
045
046 private static Object _lookup(Context context, String location)
047 throws NamingException {
048
049 PortalRuntimePermission.checkGetBeanProperty(JNDIUtil.class);
050
051 if (_log.isDebugEnabled()) {
052 _log.debug("Lookup " + location);
053 }
054
055 Object obj = null;
056
057 try {
058 obj = context.lookup(location);
059 }
060 catch (NamingException ne1) {
061
062
063
064 if (location.contains("java:comp/env/")) {
065 try {
066 String newLocation = StringUtil.replace(
067 location, "java:comp/env/", "");
068
069 if (_log.isDebugEnabled()) {
070 _log.debug(ne1.getMessage());
071 _log.debug("Attempt " + newLocation);
072 }
073
074 obj = context.lookup(newLocation);
075 }
076 catch (NamingException ne2) {
077
078
079
080 String newLocation = StringUtil.replace(
081 location, "comp/env/", "");
082
083 if (_log.isDebugEnabled()) {
084 _log.debug(ne2.getMessage());
085 _log.debug("Attempt " + newLocation);
086 }
087
088 obj = context.lookup(newLocation);
089 }
090 }
091
092
093
094 else if (location.contains("java:")) {
095 try {
096 String newLocation = StringUtil.replace(
097 location, "java:", "");
098
099 if (_log.isDebugEnabled()) {
100 _log.debug(ne1.getMessage());
101 _log.debug("Attempt " + newLocation);
102 }
103
104 obj = context.lookup(newLocation);
105 }
106 catch (NamingException ne2) {
107
108
109
110 String newLocation = StringUtil.replace(
111 location, "java:", "java:comp/env/");
112
113 if (_log.isDebugEnabled()) {
114 _log.debug(ne2.getMessage());
115 _log.debug("Attempt " + newLocation);
116 }
117
118 obj = context.lookup(newLocation);
119 }
120 }
121
122
123
124 else if (!location.contains("java:")) {
125 try {
126 String newLocation = "java:" + location;
127
128 if (_log.isDebugEnabled()) {
129 _log.debug(ne1.getMessage());
130 _log.debug("Attempt " + newLocation);
131 }
132
133 obj = context.lookup(newLocation);
134 }
135 catch (NamingException ne2) {
136
137
138
139 String newLocation = "java:comp/env/" + location;
140
141 if (_log.isDebugEnabled()) {
142 _log.debug(ne2.getMessage());
143 _log.debug("Attempt " + newLocation);
144 }
145
146 obj = context.lookup(newLocation);
147 }
148 }
149 else {
150 throw new NamingException();
151 }
152 }
153
154 return obj;
155 }
156
157 private static Log _log = LogFactoryUtil.getLog(JNDIUtil.class);
158
159 }