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