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