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.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            @Override
086            public boolean abort() throws LoginException {
087                    return _loginModule.abort();
088            }
089    
090            @Override
091            public boolean commit() throws LoginException {
092                    return _loginModule.commit();
093            }
094    
095            @Override
096            public void initialize(
097                    Subject subject, CallbackHandler callbackHandler,
098                    Map<String, ?> sharedState, Map<String, ?> options) {
099    
100                    _loginModule.initialize(subject, callbackHandler, sharedState, options);
101            }
102    
103            @Override
104            public boolean login() throws LoginException {
105                    return _loginModule.login();
106            }
107    
108            @Override
109            public boolean logout() throws LoginException {
110                    return _loginModule.logout();
111            }
112    
113            private static Log _log = LogFactoryUtil.getLog(PortalLoginModule.class);
114    
115            private LoginModule _loginModule;
116    
117    }