1   package org.mortbay.jetty.plus.jaas.spi;
2   import java.sql.Connection;
3   import java.util.Map;
4   
5   import javax.naming.InitialContext;
6   import javax.naming.NamingException;
7   import javax.security.auth.Subject;
8   import javax.security.auth.callback.CallbackHandler;
9   import javax.sql.DataSource;
10  // ========================================================================
11  // $Id: DataSourceLoginModule.java 701 2006-07-18 13:37:30Z janb $
12  // Copyright 1999-2004 Mort Bay Consulting Pty. Ltd.
13  // ------------------------------------------------------------------------
14  // Licensed under the Apache License, Version 2.0 (the "License");
15  // you may not use this file except in compliance with the License.
16  // You may obtain a copy of the License at 
17  // http://www.apache.org/licenses/LICENSE-2.0
18  // Unless required by applicable law or agreed to in writing, software
19  // distributed under the License is distributed on an "AS IS" BASIS,
20  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21  // See the License for the specific language governing permissions and
22  // limitations under the License.
23  // ========================================================================
24  
25  /**
26   * DataSourceLoginModule
27   *
28   * A LoginModule that uses a DataSource to retrieve user authentication
29   * and authorisation information.
30   * 
31   * @see org.mortbay.jetty.plus.jaas.spi.JDBCLoginModule
32   *
33   */
34  public class DataSourceLoginModule extends AbstractDatabaseLoginModule
35  {
36  
37      private String dbJNDIName;
38      private DataSource dataSource;
39      
40      /* ------------------------------------------------ */
41      /** Init LoginModule.
42       * Called once by JAAS after new instance created.
43       * @param subject 
44       * @param callbackHandler 
45       * @param sharedState 
46       * @param options 
47       */
48      public void initialize(Subject subject,
49                             CallbackHandler callbackHandler,
50                             Map sharedState,
51                             Map options)
52      {
53          try
54          {
55              super.initialize(subject, callbackHandler, sharedState, options);
56              
57              //get the datasource jndi name
58              dbJNDIName = (String)options.get("dbJNDIName");
59              
60              InitialContext ic = new InitialContext();
61              dataSource = (DataSource)ic.lookup("java:comp/env/"+dbJNDIName);
62          }
63          catch (NamingException e)
64          {
65              throw new IllegalStateException (e.toString());
66          }
67      }
68  
69  
70      /** 
71       * Get a connection from the DataSource
72       * @see org.mortbay.jetty.plus.jaas.spi.AbstractDatabaseLoginModule#getConnection()
73       * @return
74       * @throws Exception
75       */
76      public Connection getConnection ()
77      throws Exception
78      {
79          return dataSource.getConnection();
80      }
81  
82  
83      
84    
85  
86  }