View Javadoc

1   // ========================================================================
2   // $Id: JDBCLoginModule.java 478 2006-04-23 22:00:17Z gregw $
3   // Copyright 2002-2004 Mort Bay Consulting Pty. Ltd.
4   // ------------------------------------------------------------------------
5   // Licensed under the Apache License, Version 2.0 (the "License");
6   // you may not use this file except in compliance with the License.
7   // You may obtain a copy of the License at 
8   // http://www.apache.org/licenses/LICENSE-2.0
9   // Unless required by applicable law or agreed to in writing, software
10  // distributed under the License is distributed on an "AS IS" BASIS,
11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  // See the License for the specific language governing permissions and
13  // limitations under the License.
14  // ========================================================================
15  
16  package org.mortbay.jetty.plus.jaas.spi;
17  
18  import java.sql.Connection;
19  import java.sql.DriverManager;
20  import java.util.Map;
21  
22  import javax.security.auth.Subject;
23  import javax.security.auth.callback.CallbackHandler;
24  
25  import org.mortbay.log.Log;
26  import org.mortbay.util.Loader;
27  
28  
29  
30  /* ---------------------------------------------------- */
31  /** JDBCLoginModule
32   * <p>JAAS LoginModule to retrieve user information from
33   *  a database and authenticate the user.
34   *
35   * <p><h4>Notes</h4>
36   * <p>This version uses plain old JDBC connections NOT
37   * Datasources.
38   *
39   * <p><h4>Usage</h4>
40   * <pre>
41   */
42  /*
43   * </pre>
44   *
45   * @see 
46   * @version 1.0 Tue Apr 15 2003
47   * @author Jan Bartel (janb)
48   */
49  public class JDBCLoginModule extends AbstractDatabaseLoginModule
50  {
51      private String dbDriver;
52      private String dbUrl;
53      private String dbUserName;
54      private String dbPassword;
55  
56      
57  
58    
59  
60      
61      /** 
62       * Get a connection from the DriverManager
63       * @see org.mortbay.jetty.plus.jaas.spi.AbstractDatabaseLoginModule#getConnection()
64       * @return
65       * @throws Exception
66       */
67      public Connection getConnection ()
68      throws Exception
69      {
70          if (!((dbDriver != null)
71                  &&
72                  (dbUrl != null)))
73              throw new IllegalStateException ("Database connection information not configured");
74          
75          if(Log.isDebugEnabled())Log.debug("Connecting using dbDriver="+dbDriver+"+ dbUserName="+dbUserName+", dbPassword="+dbUrl);
76          
77          return DriverManager.getConnection (dbUrl,
78                  dbUserName,
79                  dbPassword);
80      }
81     
82     
83      
84      /* ------------------------------------------------ */
85      /** Init LoginModule.
86       * Called once by JAAS after new instance created.
87       * @param subject 
88       * @param callbackHandler 
89       * @param sharedState 
90       * @param options 
91       */
92      public void initialize(Subject subject,
93                             CallbackHandler callbackHandler,
94                             Map sharedState,
95                             Map options)
96      {
97          try
98          {
99              super.initialize(subject, callbackHandler, sharedState, options);
100             
101             //get the jdbc  username/password, jdbc url out of the options
102             dbDriver = (String)options.get("dbDriver");
103             dbUrl = (String)options.get("dbUrl");
104             dbUserName = (String)options.get("dbUserName");
105             dbPassword = (String)options.get("dbPassword");
106 
107             if (dbUserName == null)
108                 dbUserName = "";
109 
110             if (dbPassword == null)
111                 dbPassword = "";
112             
113             if (dbDriver != null)
114                 Loader.loadClass(this.getClass(), dbDriver).newInstance();
115         }
116         catch (ClassNotFoundException e)
117         {
118             throw new IllegalStateException (e.toString());
119         }
120         catch (InstantiationException e)
121         {
122             throw new IllegalStateException (e.toString());
123         }
124         catch (IllegalAccessException e)
125         {
126             throw new IllegalStateException (e.toString());
127         }
128     }
129 }