001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
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    /**
026     * @author Brian Wing Shun Chan
027     * @author Sandeep Soni
028     */
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            /**
038             * @deprecated As of 6.2.0, replaced by {@link #lookup(Context, String)}
039             */
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                            // java:comp/env/ObjectName to ObjectName
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                                            // java:comp/env/ObjectName to java:ObjectName
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                            // java:ObjectName to ObjectName
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                                            // java:ObjectName to java:comp/env/ObjectName
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                            // ObjectName to java:ObjectName
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                                            // ObjectName to java:comp/env/ObjectName
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    }