001    /**
002     * Copyright (c) 2000-2010 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.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.ServerDetector;
020    import com.liferay.portal.kernel.util.Validator;
021    import com.liferay.portal.util.PropsValues;
022    
023    import java.util.Map;
024    
025    import javax.security.auth.Subject;
026    import javax.security.auth.callback.CallbackHandler;
027    import javax.security.auth.login.LoginException;
028    import javax.security.auth.spi.LoginModule;
029    
030    /**
031     * @author Brian Wing Shun Chan
032     */
033    public class PortalLoginModule implements LoginModule {
034    
035            public PortalLoginModule() {
036                    if (Validator.isNotNull(PropsValues.PORTAL_JAAS_IMPL)) {
037                            try {
038                                    _loginModule = (LoginModule)Class.forName(
039                                            PropsValues.PORTAL_JAAS_IMPL).newInstance();
040                            }
041                            catch (Exception e) {
042                                    _log.error(e);
043                            }
044                    }
045    
046                    if (_loginModule == null) {
047    
048                            // Check application servers
049    
050                            if (ServerDetector.isJBoss()) {
051                                    _loginModule =
052                                            new com.liferay.portal.security.jaas.ext.jboss.PortalLoginModule();
053                            }
054                            else if (ServerDetector.isJOnAS()) {
055                                    _loginModule =
056                                            new com.liferay.portal.security.jaas.ext.jonas.PortalLoginModule();
057                            }
058                            else if (ServerDetector.isResin()) {
059                                    _loginModule =
060                                            new com.liferay.portal.security.jaas.ext.resin.PortalLoginModule();
061                            }
062                            else if (ServerDetector.isWebLogic()) {
063                                    _loginModule =
064                                            new com.liferay.portal.security.jaas.ext.weblogic.PortalLoginModule();
065                            }
066    
067                            // Check servlet containers
068    
069                            else if (ServerDetector.isJetty()) {
070                                    _loginModule =
071                                            new com.liferay.portal.security.jaas.ext.jetty.PortalLoginModule();
072                            }
073                            else if (ServerDetector.isTomcat()) {
074                                    _loginModule =
075                                            new com.liferay.portal.security.jaas.ext.tomcat.PortalLoginModule();
076                            }
077                    }
078    
079                    if (_log.isDebugEnabled()) {
080                            _log.debug(_loginModule.getClass().getName());
081                    }
082            }
083    
084            public boolean abort() throws LoginException {
085                    return _loginModule.abort();
086            }
087    
088            public boolean commit() throws LoginException {
089                    return _loginModule.commit();
090            }
091    
092            public void initialize(
093                    Subject subject, CallbackHandler callbackHandler,
094                    Map<String, ?> sharedState, Map<String, ?> options) {
095    
096                    _loginModule.initialize(subject, callbackHandler, sharedState, options);
097            }
098    
099            public boolean login() throws LoginException {
100                    return _loginModule.login();
101            }
102    
103            public boolean logout() throws LoginException {
104                    return _loginModule.logout();
105            }
106    
107            private static Log _log = LogFactoryUtil.getLog(PortalLoginModule.class);
108    
109            private LoginModule _loginModule;
110    
111    }