JDBCLoginService.java
上传用户:zhengdagz
上传日期:2014-03-06
资源大小:1956k
文件大小:7k
源码类别:

xml/soap/webservice

开发平台:

Java

  1. /*
  2.  * $Id: JDBCLoginService.java,v 1.4 2005/10/10 18:02:51 rbair Exp $
  3.  *
  4.  * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
  5.  * Santa Clara, California 95054, U.S.A. All rights reserved.
  6.  *
  7.  * This library is free software; you can redistribute it and/or
  8.  * modify it under the terms of the GNU Lesser General Public
  9.  * License as published by the Free Software Foundation; either
  10.  * version 2.1 of the License, or (at your option) any later version.
  11.  * 
  12.  * This library is distributed in the hope that it will be useful,
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15.  * Lesser General Public License for more details.
  16.  * 
  17.  * You should have received a copy of the GNU Lesser General Public
  18.  * License along with this library; if not, write to the Free Software
  19.  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  20.  */
  21. package org.jdesktop.swingx.auth;
  22. import java.io.IOException;
  23. import java.sql.Connection;
  24. import java.sql.DriverManager;
  25. import java.util.Properties;
  26. import javax.naming.InitialContext;
  27. /**
  28.  * A login service for connecting to SQL based databases via JDBC
  29.  *
  30.  * @author rbair
  31.  */
  32. public class JDBCLoginService extends LoginService {
  33.     /**
  34.      * The connection to the database
  35.      */
  36.     private Connection conn;
  37.     /**
  38.      * If used, defines the JNDI context from which to get a connection to
  39.      * the data base
  40.      */
  41.     private String jndiContext;
  42.     /**
  43.      * When using the DriverManager to connect to the database, this specifies
  44.      * any additional properties to use when connecting.
  45.      */
  46.     private Properties properties;
  47.     
  48.     /**
  49.      * Create a new JDBCLoginService and initializes it to connect to a
  50.      * database using the given params.
  51.      * @param driver
  52.      * @param url
  53.      */
  54.     public JDBCLoginService(String driver, String url) {
  55.         super(url);
  56.         try {
  57.             Class.forName(driver);
  58.         } catch (Exception e) {
  59.             System.err.println("WARN: The driver passed to the " +
  60.                     "JDBCLoginService constructor could not be loaded. " +
  61.                     "This may be due to the driver not being on the classpath");
  62.             e.printStackTrace();
  63.         }
  64.         this.setUrl(url);
  65.     }
  66.     
  67.     /**
  68.      * Create a new JDBCLoginService and initializes it to connect to a
  69.      * database using the given params.
  70.      * @param driver
  71.      * @param url
  72.      * @param props
  73.      */
  74.     public JDBCLoginService(String driver, String url, Properties props) {
  75.         super(url);
  76.         try {
  77.             Class.forName(driver);
  78.         } catch (Exception e) {
  79.             System.err.println("WARN: The driver passed to the " +
  80.                     "JDBCLoginService constructor could not be loaded. " +
  81.                     "This may be due to the driver not being on the classpath");
  82.             e.printStackTrace();
  83.         }
  84.         this.setUrl(url);
  85.         this.setProperties(props);
  86.     }
  87.     
  88.     /**
  89.      * Create a new JDBCLoginService and initializes it to connect to a
  90.      * database using the given params.
  91.      * @param jndiContext
  92.      */
  93.     public JDBCLoginService(String jndiContext) {
  94.         super(jndiContext);
  95.         this.jndiContext = jndiContext;
  96.     }
  97.     
  98.     /**
  99.      * @return the JDBC connection url
  100.      */
  101.     public String getUrl() {
  102.         return getServer();
  103.     }
  104.     /**
  105.      * @param url set the JDBC connection url
  106.      */
  107.     public void setUrl(String url) {
  108.         setServer(url);
  109.     }
  110.     /**
  111.      * @return JDBC connection properties
  112.      */
  113.     public Properties getProperties() {
  114.         return properties;
  115.     }
  116.     /**
  117.      * @param properties miscellaneous JDBC properties to use when connecting
  118.      *        to the database via the JDBC driver
  119.      */
  120.     public void setProperties(Properties properties) {
  121.         this.properties = properties;
  122.     }
  123.     
  124.     public Connection getConnection() {
  125.         return conn;
  126.     }
  127.     
  128.     /**
  129.      * Attempts to get a JDBC Connection from a JNDI javax.sql.DataSource, using
  130.      * that connection for interacting with the database.
  131.      * @throws Exception
  132.      */
  133.     private void connectByJNDI(String userName, char[] password) throws Exception {
  134.         InitialContext ctx = new InitialContext();
  135.         javax.sql.DataSource ds = (javax.sql.DataSource)ctx.lookup(jndiContext);
  136.         conn = ds.getConnection(userName, new String(password));
  137.         conn.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);
  138.     }
  139.     
  140.     /**
  141.      * Attempts to get a JDBC Connection from a DriverManager. If properties
  142.      * is not null, it tries to connect with those properties. If that fails,
  143.      * it then attempts to connect with a user name and password. If that fails,
  144.      * it attempts to connect without any credentials at all.
  145.      * <p>
  146.      * If, on the other hand, properties is null, it first attempts to connect
  147.      * with a username and password. Failing that, it tries to connect without
  148.      * any credentials at all.
  149.      * @throws Exception
  150.      */
  151.     private void connectByDriverManager(String userName, char[] password) throws Exception {
  152.         if (getProperties() != null) {
  153.             try {
  154.                 conn = DriverManager.getConnection(getUrl(), getProperties());
  155.                 conn.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);
  156.             } catch (Exception e) {
  157.                 try {
  158.                     conn = DriverManager.getConnection(getUrl(), userName, new String(password));
  159.                     conn.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);
  160.                 } catch (Exception ex) {
  161.                     conn = DriverManager.getConnection(getUrl());
  162.                     conn.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);
  163.                 }
  164.             }
  165.         } else {
  166.             try {
  167.                 conn = DriverManager.getConnection(getUrl(), userName, new String(password));
  168.             } catch (Exception e) {
  169.                 e.printStackTrace();
  170.                 //try to connect without using the userName and password
  171.                 conn = DriverManager.getConnection(getUrl());
  172.             }
  173.         }
  174.     }
  175.     /**
  176.      * @param name user name
  177.      * @param password user password
  178.      * @param server Must be either a valid JDBC URL for the type of JDBC driver you are using,
  179.      * or must be a valid JNDIContext from which to get the database connection
  180.      */
  181.     public boolean authenticate(String name, char[] password, String server) throws IOException {
  182.         //try to form a connection. If it works, conn will not be null
  183.         //if the jndiContext is not null, then try to get the DataSource to use
  184.         //from jndi
  185.         if (jndiContext != null) {
  186.             try {
  187.                 connectByJNDI(name, password);
  188.             } catch (Exception e) {
  189.                 try {
  190.                     connectByDriverManager(name, password);
  191.                 } catch (Exception ex) {
  192.                     ex.printStackTrace();
  193.                     //login failed
  194.                     return false;
  195.                 }
  196.             }
  197.         } else {
  198.             try {
  199.                 connectByDriverManager(name, password);
  200.             } catch (Exception ex) {
  201.                 ex.printStackTrace();
  202.                 return false;
  203.             }
  204.         }
  205.         return true;
  206.     }
  207. }