View Javadoc

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