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