View Javadoc

1   // ========================================================================
2   // Copyright (c) 2002-2009 Mort Bay Consulting Pty. Ltd.
3   // ------------------------------------------------------------------------
4   // All rights reserved. This program and the accompanying materials
5   // are made available under the terms of the Eclipse Public License v1.0
6   // and Apache License v2.0 which accompanies this distribution.
7   // The Eclipse Public License is available at 
8   // http://www.eclipse.org/legal/epl-v10.html
9   // The Apache License v2.0 is available at
10  // http://www.opensource.org/licenses/apache2.0.php
11  // You may elect to redistribute this code under either of these licenses. 
12  // ========================================================================
13  
14  package org.eclipse.jetty.plus.jaas.spi;
15  
16  import java.sql.Connection;
17  import java.sql.DriverManager;
18  import java.util.Map;
19  
20  import javax.security.auth.Subject;
21  import javax.security.auth.callback.CallbackHandler;
22  
23  import org.eclipse.jetty.util.Loader;
24  import org.eclipse.jetty.util.log.Log;
25  import org.eclipse.jetty.util.log.Logger;
26  
27  
28  
29  /* ---------------------------------------------------- */
30  /** JDBCLoginModule
31   * <p>JAAS LoginModule to retrieve user information from
32   *  a database and authenticate the user.
33   *
34   * <p><h4>Notes</h4>
35   * <p>This version uses plain old JDBC connections NOT
36   * Datasources.
37   *
38   * <p><h4>Usage</h4>
39   * <pre>
40   * </pre>
41   *
42   * @version 1.0 Tue Apr 15 2003
43   */
44  public class JDBCLoginModule extends AbstractDatabaseLoginModule
45  {
46      private static final Logger LOG = Log.getLogger(JDBCLoginModule.class);
47  
48      private String dbDriver;
49      private String dbUrl;
50      private String dbUserName;
51      private String dbPassword;
52  
53      
54      /** 
55       * Get a connection from the DriverManager
56       * @see AbstractDatabaseLoginModule#getConnection()
57       * @return the connection for this datasource
58       * @throws Exception
59       */
60      public Connection getConnection ()
61      throws Exception
62      {
63          if (!((dbDriver != null)
64                  &&
65                  (dbUrl != null)))
66              throw new IllegalStateException ("Database connection information not configured");
67          
68          if(LOG.isDebugEnabled())LOG.debug("Connecting using dbDriver="+dbDriver+"+ dbUserName="+dbUserName+", dbPassword="+dbUrl);
69          
70          return DriverManager.getConnection (dbUrl,
71                  dbUserName,
72                  dbPassword);
73      }
74     
75     
76      
77      /* ------------------------------------------------ */
78      /** Init LoginModule.
79       * Called once by JAAS after new instance created.
80       * @param subject 
81       * @param callbackHandler 
82       * @param sharedState 
83       * @param options 
84       */
85      public void initialize(Subject subject,
86                             CallbackHandler callbackHandler,
87                             Map<String,?> sharedState,
88                             Map<String,?> options)
89      {
90          try
91          {
92              super.initialize(subject, callbackHandler, sharedState, options);
93              
94              //get the jdbc  username/password, jdbc url out of the options
95              dbDriver = (String)options.get("dbDriver");
96              dbUrl = (String)options.get("dbUrl");
97              dbUserName = (String)options.get("dbUserName");
98              dbPassword = (String)options.get("dbPassword");
99  
100             if (dbUserName == null)
101                 dbUserName = "";
102 
103             if (dbPassword == null)
104                 dbPassword = "";
105             
106             if (dbDriver != null)
107                 Loader.loadClass(this.getClass(), dbDriver).newInstance();
108         }
109         catch (ClassNotFoundException e)
110         {
111             throw new IllegalStateException (e.toString());
112         }
113         catch (InstantiationException e)
114         {
115             throw new IllegalStateException (e.toString());
116         }
117         catch (IllegalAccessException e)
118         {
119             throw new IllegalStateException (e.toString());
120         }
121     }
122 }