001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.security.jaas;
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.kernel.util.ServerDetector;
021    import com.liferay.portal.kernel.util.Validator;
022    import com.liferay.portal.util.PropsValues;
023    
024    import java.util.Map;
025    
026    import javax.security.auth.Subject;
027    import javax.security.auth.callback.CallbackHandler;
028    import javax.security.auth.login.LoginException;
029    import javax.security.auth.spi.LoginModule;
030    
031    /**
032     * @author Brian Wing Shun Chan
033     */
034    public class PortalLoginModule implements LoginModule {
035    
036            public PortalLoginModule() {
037                    if (Validator.isNotNull(PropsValues.PORTAL_JAAS_IMPL)) {
038                            try {
039                                    _loginModule = (LoginModule)InstanceFactory.newInstance(
040                                            PropsValues.PORTAL_JAAS_IMPL);
041                            }
042                            catch (Exception e) {
043                                    _log.error(e, e);
044                            }
045                    }
046    
047                    if (_loginModule == null) {
048                            if (ServerDetector.isJBoss()) {
049                                    _loginModule =
050                                            new com.liferay.portal.security.jaas.ext.jboss.
051                                                    PortalLoginModule();
052                            }
053                            else if (ServerDetector.isJetty()) {
054                                    _loginModule =
055                                            new com.liferay.portal.security.jaas.ext.jetty.
056                                                    PortalLoginModule();
057                            }
058                            else if (ServerDetector.isJOnAS()) {
059                                    _loginModule =
060                                            new com.liferay.portal.security.jaas.ext.jonas.
061                                                    PortalLoginModule();
062                            }
063                            else if (ServerDetector.isResin()) {
064                                    _loginModule =
065                                            new com.liferay.portal.security.jaas.ext.resin.
066                                                    PortalLoginModule();
067                            }
068                            else if (ServerDetector.isTomcat()) {
069                                    _loginModule =
070                                            new com.liferay.portal.security.jaas.ext.tomcat.
071                                                    PortalLoginModule();
072                            }
073                            else if (ServerDetector.isWebLogic()) {
074                                    _loginModule =
075                                            new com.liferay.portal.security.jaas.ext.weblogic.
076                                                    PortalLoginModule();
077                            }
078                    }
079    
080                    if (_log.isDebugEnabled()) {
081                            _log.debug(_loginModule.getClass().getName());
082                    }
083            }
084    
085            public boolean abort() throws LoginException {
086                    return _loginModule.abort();
087            }
088    
089            public boolean commit() throws LoginException {
090                    return _loginModule.commit();
091            }
092    
093            public void initialize(
094                    Subject subject, CallbackHandler callbackHandler,
095                    Map<String, ?> sharedState, Map<String, ?> options) {
096    
097                    _loginModule.initialize(subject, callbackHandler, sharedState, options);
098            }
099    
100            public boolean login() throws LoginException {
101                    return _loginModule.login();
102            }
103    
104            public boolean logout() throws LoginException {
105                    return _loginModule.logout();
106            }
107    
108            private static Log _log = LogFactoryUtil.getLog(PortalLoginModule.class);
109    
110            private LoginModule _loginModule;
111    
112    }