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.security.pacl.jndi;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.util.InstanceFactory;
020    import com.liferay.portal.security.lang.DoPrivilegedFactory;
021    import com.liferay.portal.security.pacl.PACLPolicy;
022    import com.liferay.portal.security.pacl.PACLUtil;
023    
024    import java.util.Hashtable;
025    
026    import javax.naming.Context;
027    import javax.naming.NamingException;
028    import javax.naming.spi.InitialContextFactory;
029    import javax.naming.spi.InitialContextFactoryBuilder;
030    
031    /**
032     * @author Brian Wing Shun Chan
033     */
034    public class PACLInitialContextFactory implements InitialContextFactory {
035    
036            public PACLInitialContextFactory(
037                    InitialContextFactoryBuilder initialContextFactoryBuilder,
038                    Hashtable<?, ?> environment) {
039    
040                    _initialContextFactoryBuilder = initialContextFactoryBuilder;
041    
042                    if (environment != null) {
043                            _environment = new Hashtable<Object, Object>(environment);
044                    }
045            }
046    
047            public Context getInitialContext(Hashtable<?, ?> environment)
048                    throws NamingException {
049    
050                    try {
051                            return doGetInitialContext(environment);
052                    }
053                    catch (NamingException ne) {
054                            throw ne;
055                    }
056                    catch (Exception e) {
057                            NamingException ne = new NamingException();
058    
059                            ne.initCause(e);
060    
061                            throw ne;
062                    }
063            }
064    
065            protected Context doGetInitialContext(Hashtable<?, ?> environment)
066                    throws Exception {
067    
068                    InitialContextFactory initialContextFactory = null;
069    
070                    if (_initialContextFactoryBuilder != null) {
071                            if (_log.isDebugEnabled()) {
072                                    _log.debug(
073                                            "Use " + _initialContextFactoryBuilder.getClass() +
074                                                    " to instantiate initial context factory");
075                            }
076    
077                            initialContextFactory =
078                                    _initialContextFactoryBuilder.createInitialContextFactory(
079                                            environment);
080                    }
081                    else {
082                            if (environment == null) {
083                                    environment = _environment;
084                            }
085    
086                            String initialContextFactoryClassName = null;
087    
088                            if (environment != null) {
089                                    initialContextFactoryClassName = (String)environment.get(
090                                            Context.INITIAL_CONTEXT_FACTORY);
091    
092                                    if (_log.isDebugEnabled()) {
093                                            _log.debug(
094                                                    "Environment initial context factory " +
095                                                            initialContextFactoryClassName);
096                                    }
097                            }
098    
099                            if (initialContextFactoryClassName == null) {
100                                    initialContextFactoryClassName = System.getProperty(
101                                            Context.INITIAL_CONTEXT_FACTORY);
102    
103                                    if (_log.isDebugEnabled()) {
104                                            _log.debug(
105                                                    "System initial context factory " +
106                                                            initialContextFactoryClassName);
107                                    }
108                            }
109    
110                            if (_log.isDebugEnabled()) {
111                                    _log.debug("Instantiating " + initialContextFactoryClassName);
112                            }
113    
114                            initialContextFactory =
115                                    (InitialContextFactory)InstanceFactory.newInstance(
116                                            initialContextFactoryClassName);
117                    }
118    
119                    Context context = initialContextFactory.getInitialContext(environment);
120    
121                    context = new SchemeAwareContextWrapper(context);
122    
123                    PACLPolicy paclPolicy = PACLUtil.getPACLPolicy();
124    
125                    if (paclPolicy == null) {
126                            return context;
127                    }
128    
129                    context = DoPrivilegedFactory.wrap(context);
130                    paclPolicy = DoPrivilegedFactory.wrap(paclPolicy);
131    
132                    return new PACLContext(context, paclPolicy);
133            }
134    
135            private Hashtable<?, ?> _environment;
136            private InitialContextFactoryBuilder _initialContextFactoryBuilder;
137    
138            // This must not be static because of LPS-33404
139    
140            private Log _log = LogFactoryUtil.getLog(
141                    PACLInitialContextFactory.class.getName());
142    
143    }