1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.security.jaas;
16  
17  import com.liferay.portal.kernel.log.Log;
18  import com.liferay.portal.kernel.log.LogFactoryUtil;
19  import com.liferay.portal.kernel.util.ServerDetector;
20  import com.liferay.portal.kernel.util.Validator;
21  import com.liferay.portal.util.PropsValues;
22  
23  import java.util.Map;
24  
25  import javax.security.auth.Subject;
26  import javax.security.auth.callback.CallbackHandler;
27  import javax.security.auth.login.LoginException;
28  import javax.security.auth.spi.LoginModule;
29  
30  /**
31   * <a href="PortalLoginModule.java.html"><b><i>View Source</i></b></a>
32   *
33   * @author Brian Wing Shun Chan
34   */
35  public class PortalLoginModule implements LoginModule {
36  
37      public PortalLoginModule() {
38          if (Validator.isNotNull(PropsValues.PORTAL_JAAS_IMPL)) {
39              try {
40                  _loginModule = (LoginModule)Class.forName(
41                      PropsValues.PORTAL_JAAS_IMPL).newInstance();
42              }
43              catch (Exception e) {
44                  _log.error(e);
45              }
46          }
47  
48          if (_loginModule == null) {
49  
50              // Check application servers
51  
52              if (ServerDetector.isJBoss()) {
53                  _loginModule =
54                      new com.liferay.portal.security.jaas.ext.jboss.PortalLoginModule();
55              }
56              else if (ServerDetector.isJOnAS()) {
57                  _loginModule =
58                      new com.liferay.portal.security.jaas.ext.jonas.PortalLoginModule();
59              }
60              else if (ServerDetector.isResin()) {
61                  _loginModule =
62                      new com.liferay.portal.security.jaas.ext.resin.PortalLoginModule();
63              }
64              else if (ServerDetector.isWebLogic()) {
65                  _loginModule =
66                      new com.liferay.portal.security.jaas.ext.weblogic.PortalLoginModule();
67              }
68  
69              // Check servlet containers
70  
71              else if (ServerDetector.isJetty()) {
72                  _loginModule =
73                      new com.liferay.portal.security.jaas.ext.jetty.PortalLoginModule();
74              }
75              else if (ServerDetector.isTomcat()) {
76                  _loginModule =
77                      new com.liferay.portal.security.jaas.ext.tomcat.PortalLoginModule();
78              }
79          }
80  
81          if (_log.isDebugEnabled()) {
82              _log.debug(_loginModule.getClass().getName());
83          }
84      }
85  
86      public boolean abort() throws LoginException {
87          return _loginModule.abort();
88      }
89  
90      public boolean commit() throws LoginException {
91          return _loginModule.commit();
92      }
93  
94      public void initialize(
95          Subject subject, CallbackHandler callbackHandler,
96          Map<String, ?> sharedState, Map<String, ?> options) {
97  
98          _loginModule.initialize(subject, callbackHandler, sharedState, options);
99      }
100 
101     public boolean login() throws LoginException {
102         return _loginModule.login();
103     }
104 
105     public boolean logout() throws LoginException {
106         return _loginModule.logout();
107     }
108 
109     private static Log _log = LogFactoryUtil.getLog(PortalLoginModule.class);
110 
111     private LoginModule _loginModule;
112 
113 }