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 org.eclipse.jetty.server.server.plus.jaas.spi.JDBCLoginModule
31   *
32   */
33  public class DataSourceLoginModule extends AbstractDatabaseLoginModule
34  {
35  
36      private String dbJNDIName;
37      private DataSource dataSource;
38      
39      /* ------------------------------------------------ */
40      /** Init LoginModule.
41       * Called once by JAAS after new instance created.
42       * @param subject 
43       * @param callbackHandler 
44       * @param sharedState 
45       * @param options 
46       */
47      public void initialize(Subject subject,
48                             CallbackHandler callbackHandler,
49                             Map sharedState,
50                             Map options)
51      {
52          try
53          {
54              super.initialize(subject, callbackHandler, sharedState, options);
55              
56              //get the datasource jndi name
57              dbJNDIName = (String)options.get("dbJNDIName");
58              
59              InitialContext ic = new InitialContext();
60              dataSource = (DataSource)ic.lookup("java:comp/env/"+dbJNDIName);
61          }
62          catch (NamingException e)
63          {
64              throw new IllegalStateException (e.toString());
65          }
66      }
67  
68  
69      /** 
70       * Get a connection from the DataSource
71       * @see org.eclipse.jetty.server.server.plus.jaas.spi.AbstractDatabaseLoginModule#getConnection()
72       * @return
73       * @throws Exception
74       */
75      public Connection getConnection ()
76      throws Exception
77      {
78          return dataSource.getConnection();
79      }
80  
81  
82      
83    
84  
85  }