1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.security.jaas;
24  
25  import com.liferay.portal.kernel.log.Log;
26  import com.liferay.portal.kernel.log.LogFactoryUtil;
27  import com.liferay.portal.kernel.util.ServerDetector;
28  import com.liferay.portal.kernel.util.Validator;
29  import com.liferay.portal.util.PropsValues;
30  
31  import java.util.Map;
32  
33  import javax.security.auth.Subject;
34  import javax.security.auth.callback.CallbackHandler;
35  import javax.security.auth.login.LoginException;
36  import javax.security.auth.spi.LoginModule;
37  
38  /**
39   * <a href="PortalLoginModule.java.html"><b><i>View Source</i></b></a>
40   *
41   * @author Brian Wing Shun Chan
42   */
43  public class PortalLoginModule implements LoginModule {
44  
45      public PortalLoginModule() {
46          if (Validator.isNotNull(PropsValues.PORTAL_JAAS_IMPL)) {
47              try {
48                  _loginModule = (LoginModule)Class.forName(
49                      PropsValues.PORTAL_JAAS_IMPL).newInstance();
50              }
51              catch (Exception e) {
52                  _log.error(e);
53              }
54          }
55  
56          if (_loginModule == null) {
57  
58              // Check application servers
59  
60              if (ServerDetector.isJBoss()) {
61                  _loginModule =
62                      new com.liferay.portal.security.jaas.ext.jboss.PortalLoginModule();
63              }
64              else if (ServerDetector.isJOnAS()) {
65                  _loginModule =
66                      new com.liferay.portal.security.jaas.ext.jonas.PortalLoginModule();
67              }
68              else if (ServerDetector.isResin()) {
69                  _loginModule =
70                      new com.liferay.portal.security.jaas.ext.resin.PortalLoginModule();
71              }
72              else if (ServerDetector.isWebLogic()) {
73                  _loginModule =
74                      new com.liferay.portal.security.jaas.ext.weblogic.PortalLoginModule();
75              }
76  
77              // Check servlet containers
78  
79              else if (ServerDetector.isJetty()) {
80                  _loginModule =
81                      new com.liferay.portal.security.jaas.ext.jetty.PortalLoginModule();
82              }
83              else if (ServerDetector.isTomcat()) {
84                  _loginModule =
85                      new com.liferay.portal.security.jaas.ext.tomcat.PortalLoginModule();
86              }
87          }
88  
89          if (_log.isDebugEnabled()) {
90              _log.debug(_loginModule.getClass().getName());
91          }
92      }
93  
94      public boolean abort() throws LoginException {
95          return _loginModule.abort();
96      }
97  
98      public boolean commit() throws LoginException {
99          return _loginModule.commit();
100     }
101 
102     public void initialize(
103         Subject subject, CallbackHandler callbackHandler,
104         Map<String, ?> sharedState, Map<String, ?> options) {
105 
106         _loginModule.initialize(subject, callbackHandler, sharedState, options);
107     }
108 
109     public boolean login() throws LoginException {
110         return _loginModule.login();
111     }
112 
113     public boolean logout() throws LoginException {
114         return _loginModule.logout();
115     }
116 
117     private static Log _log = LogFactoryUtil.getLog(PortalLoginModule.class);
118 
119     private LoginModule _loginModule;
120 
121 }