View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2013 Mort Bay Consulting Pty. Ltd.
4   //  ------------------------------------------------------------------------
5   //  All rights reserved. This program and the accompanying materials
6   //  are made available under the terms of the Eclipse Public License v1.0
7   //  and Apache License v2.0 which accompanies this distribution.
8   //
9   //      The Eclipse Public License is available at
10  //      http://www.eclipse.org/legal/epl-v10.html
11  //
12  //      The Apache License v2.0 is available at
13  //      http://www.opensource.org/licenses/apache2.0.php
14  //
15  //  You may elect to redistribute this code under either of these licenses.
16  //  ========================================================================
17  //
18  
19  package org.eclipse.jetty.plus.jaas.spi;
20  import java.sql.Connection;
21  import java.util.Map;
22  
23  import javax.naming.InitialContext;
24  import javax.naming.NamingException;
25  import javax.security.auth.Subject;
26  import javax.security.auth.callback.CallbackHandler;
27  import javax.sql.DataSource;
28  
29  /**
30   * DataSourceLoginModule
31   *
32   * A LoginModule that uses a DataSource to retrieve user authentication
33   * and authorisation information.
34   * 
35   * @see JDBCLoginModule
36   */
37  public class DataSourceLoginModule extends AbstractDatabaseLoginModule
38  {
39  
40      private String dbJNDIName;
41      private DataSource dataSource;
42      
43      /* ------------------------------------------------ */
44      /** Init LoginModule.
45       * Called once by JAAS after new instance created.
46       * @param subject 
47       * @param callbackHandler 
48       * @param sharedState 
49       * @param options 
50       */
51      public void initialize(Subject subject,
52                             CallbackHandler callbackHandler,
53                             Map<String,?> sharedState,
54                             Map<String,?> options)
55      {
56          try
57          {
58              super.initialize(subject, callbackHandler, sharedState, options);
59              
60              //get the datasource jndi name
61              dbJNDIName = (String)options.get("dbJNDIName");
62              
63              InitialContext ic = new InitialContext();
64              dataSource = (DataSource)ic.lookup("java:comp/env/"+dbJNDIName);
65          }
66          catch (NamingException e)
67          {
68              throw new IllegalStateException (e.toString());
69          }
70      }
71  
72  
73      /** 
74       * Get a connection from the DataSource
75       * @see AbstractDatabaseLoginModule#getConnection()
76       * @return the connection for the datasource 
77       * @throws Exception
78       */
79      public Connection getConnection ()
80      throws Exception
81      {
82          return dataSource.getConnection();
83      }
84  
85  
86      
87    
88  
89  }