View Javadoc

1   // ========================================================================
2   // Copyright (c) 2004-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  import java.sql.Connection;
16  import java.util.Map;
17  
18  import javax.naming.InitialContext;
19  import javax.naming.NamingException;
20  import javax.security.auth.Subject;
21  import javax.security.auth.callback.CallbackHandler;
22  import javax.sql.DataSource;
23  
24  /**
25   * DataSourceLoginModule
26   *
27   * A LoginModule that uses a DataSource to retrieve user authentication
28   * and authorisation information.
29   * 
30   * @see JDBCLoginModule
31   */
32  public class DataSourceLoginModule extends AbstractDatabaseLoginModule
33  {
34  
35      private String dbJNDIName;
36      private DataSource dataSource;
37      
38      /* ------------------------------------------------ */
39      /** Init LoginModule.
40       * Called once by JAAS after new instance created.
41       * @param subject 
42       * @param callbackHandler 
43       * @param sharedState 
44       * @param options 
45       */
46      public void initialize(Subject subject,
47                             CallbackHandler callbackHandler,
48                             Map sharedState,
49                             Map options)
50      {
51          try
52          {
53              super.initialize(subject, callbackHandler, sharedState, options);
54              
55              //get the datasource jndi name
56              dbJNDIName = (String)options.get("dbJNDIName");
57              
58              InitialContext ic = new InitialContext();
59              dataSource = (DataSource)ic.lookup("java:comp/env/"+dbJNDIName);
60          }
61          catch (NamingException e)
62          {
63              throw new IllegalStateException (e.toString());
64          }
65      }
66  
67  
68      /** 
69       * Get a connection from the DataSource
70       * @see AbstractDatabaseLoginModule#getConnection()
71       * @return the connection for the datasource 
72       * @throws Exception
73       */
74      public Connection getConnection ()
75      throws Exception
76      {
77          return dataSource.getConnection();
78      }
79  
80  
81      
82    
83  
84  }