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  
26  
27  
28  /* ---------------------------------------------------- */
29  /** JDBCLoginModule
30   * <p>JAAS LoginModule to retrieve user information from
31   *  a database and authenticate the user.
32   *
33   * <p><h4>Notes</h4>
34   * <p>This version uses plain old JDBC connections NOT
35   * Datasources.
36   *
37   * <p><h4>Usage</h4>
38   * <pre>
39   */
40  /*
41   * </pre>
42   *
43   * @see 
44   * @version 1.0 Tue Apr 15 2003
45   * 
46   */
47  public class JDBCLoginModule extends AbstractDatabaseLoginModule
48  {
49      private String dbDriver;
50      private String dbUrl;
51      private String dbUserName;
52      private String dbPassword;
53  
54      
55  
56    
57  
58      
59      /** 
60       * Get a connection from the DriverManager
61       * @see org.eclipse.jetty.server.server.plus.jaas.spi.AbstractDatabaseLoginModule#getConnection()
62       * @return
63       * @throws Exception
64       */
65      public Connection getConnection ()
66      throws Exception
67      {
68          if (!((dbDriver != null)
69                  &&
70                  (dbUrl != null)))
71              throw new IllegalStateException ("Database connection information not configured");
72          
73          if(Log.isDebugEnabled())Log.debug("Connecting using dbDriver="+dbDriver+"+ dbUserName="+dbUserName+", dbPassword="+dbUrl);
74          
75          return DriverManager.getConnection (dbUrl,
76                  dbUserName,
77                  dbPassword);
78      }
79     
80     
81      
82      /* ------------------------------------------------ */
83      /** Init LoginModule.
84       * Called once by JAAS after new instance created.
85       * @param subject 
86       * @param callbackHandler 
87       * @param sharedState 
88       * @param options 
89       */
90      public void initialize(Subject subject,
91                             CallbackHandler callbackHandler,
92                             Map sharedState,
93                             Map options)
94      {
95          try
96          {
97              super.initialize(subject, callbackHandler, sharedState, options);
98              
99              //get the jdbc  username/password, jdbc url out of the options
100             dbDriver = (String)options.get("dbDriver");
101             dbUrl = (String)options.get("dbUrl");
102             dbUserName = (String)options.get("dbUserName");
103             dbPassword = (String)options.get("dbPassword");
104 
105             if (dbUserName == null)
106                 dbUserName = "";
107 
108             if (dbPassword == null)
109                 dbPassword = "";
110             
111             if (dbDriver != null)
112                 Loader.loadClass(this.getClass(), dbDriver).newInstance();
113         }
114         catch (ClassNotFoundException e)
115         {
116             throw new IllegalStateException (e.toString());
117         }
118         catch (InstantiationException e)
119         {
120             throw new IllegalStateException (e.toString());
121         }
122         catch (IllegalAccessException e)
123         {
124             throw new IllegalStateException (e.toString());
125         }
126     }
127 }