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            /**
038             * @deprecated As of 6.2.0, replaced by {@link #lookup(Context, String)}
039             */
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                            // java:comp/env/ObjectName to ObjectName
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                                            // java:comp/env/ObjectName to java:ObjectName
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                            // java:ObjectName to ObjectName
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                                            // java:ObjectName to java:comp/env/ObjectName
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                            // ObjectName to java:ObjectName
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                                            // ObjectName to java:comp/env/ObjectName
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    }