1   /**
2    * Copyright (c) 2000-2007 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions 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.util.ServerDetector;
26  import com.liferay.portal.kernel.util.Validator;
27  import com.liferay.portal.util.PropsUtil;
28  
29  import java.util.Map;
30  
31  import javax.security.auth.Subject;
32  import javax.security.auth.callback.CallbackHandler;
33  import javax.security.auth.login.LoginException;
34  import javax.security.auth.spi.LoginModule;
35  
36  import org.apache.commons.logging.Log;
37  import org.apache.commons.logging.LogFactory;
38  
39  /**
40   * <a href="PortalLoginModule.java.html"><b><i>View Source</i></b></a>
41   *
42   * @author Brian Wing Shun Chan
43   *
44   */
45  public class PortalLoginModule implements LoginModule {
46  
47      public PortalLoginModule() {
48          String jaasImpl = PropsUtil.get(PropsUtil.PORTAL_JAAS_IMPL);
49  
50          if (Validator.isNotNull(jaasImpl)) {
51              try {
52                  _loginModule = (LoginModule)Class.forName(
53                      jaasImpl).newInstance();
54              }
55              catch (Exception e) {
56                  _log.error(e);
57              }
58          }
59  
60          if (_loginModule == null) {
61  
62              // Check application servers
63  
64              if (ServerDetector.isJBoss()) {
65                  _loginModule =
66                      new com.liferay.portal.security.jaas.ext.jboss.PortalLoginModule();
67              }
68              else if (ServerDetector.isJOnAS()) {
69                  _loginModule =
70                      new com.liferay.portal.security.jaas.ext.jonas.PortalLoginModule();
71              }
72              else if (ServerDetector.isResin()) {
73                  _loginModule =
74                      new com.liferay.portal.security.jaas.ext.resin.PortalLoginModule();
75              }
76              else if (ServerDetector.isWebLogic()) {
77                  _loginModule =
78                      new com.liferay.portal.security.jaas.ext.weblogic.PortalLoginModule();
79              }
80  
81              // Check servlet containers
82  
83              else if (ServerDetector.isJetty()) {
84                  _loginModule =
85                      new com.liferay.portal.security.jaas.ext.jetty.PortalLoginModule();
86              }
87              else if (ServerDetector.isTomcat()) {
88                  _loginModule =
89                      new com.liferay.portal.security.jaas.ext.tomcat.PortalLoginModule();
90              }
91          }
92  
93          if (_log.isDebugEnabled()) {
94              _log.debug(_loginModule.getClass().getName());
95          }
96      }
97  
98      public boolean abort() throws LoginException {
99          return _loginModule.abort();
100     }
101 
102     public boolean commit() throws LoginException {
103         return _loginModule.commit();
104     }
105 
106     public void initialize(
107         Subject subject, CallbackHandler callbackHandler, Map sharedState,
108         Map options) {
109 
110         _loginModule.initialize(subject, callbackHandler, sharedState, options);
111     }
112 
113     public boolean login() throws LoginException {
114         return _loginModule.login();
115     }
116 
117     public boolean logout() throws LoginException {
118         return _loginModule.logout();
119     }
120 
121     private static Log _log = LogFactory.getLog(PortalLoginModule.class);
122 
123     private LoginModule _loginModule;
124 
125 }