Save This Page
Home » MySQL-JDBC-5.1.11 » com.mysql.jdbc.jdbc2.optional » [javadoc | source]
    1   /*
    2    Copyright  2002-2007 MySQL AB, 2008 Sun Microsystems
    3   
    4    This program is free software; you can redistribute it and/or modify
    5    it under the terms of version 2 of the GNU General Public License as 
    6    published by the Free Software Foundation.
    7   
    8    There are special exceptions to the terms and conditions of the GPL 
    9    as it is applied to this software. View the full text of the 
   10    exception in file EXCEPTIONS-CONNECTOR-J in the directory of this 
   11    software distribution.
   12   
   13    This program is distributed in the hope that it will be useful,
   14    but WITHOUT ANY WARRANTY; without even the implied warranty of
   15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   16    GNU General Public License for more details.
   17   
   18    You should have received a copy of the GNU General Public License
   19    along with this program; if not, write to the Free Software
   20    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   21   
   22   
   23   
   24    */
   25   package com.mysql.jdbc.jdbc2.optional;
   26   
   27   import java.io.PrintWriter;
   28   import java.io.Serializable;
   29   import java.sql.SQLException;
   30   import java.util.Iterator;
   31   import java.util.Properties;
   32   
   33   import javax.naming.NamingException;
   34   import javax.naming.Reference;
   35   import javax.naming.Referenceable;
   36   import javax.naming.StringRefAddr;
   37   import javax.sql.DataSource;
   38   
   39   import com.mysql.jdbc.ConnectionPropertiesImpl;
   40   import com.mysql.jdbc.NonRegisteringDriver;
   41   
   42   /**
   43    * A JNDI DataSource for a Mysql JDBC connection
   44    * 
   45    * @author Mark Matthews
   46    */
   47   public class MysqlDataSource extends ConnectionPropertiesImpl implements
   48   		DataSource, Referenceable, Serializable  {
   49   	/** The driver to create connections with */
   50   	protected static NonRegisteringDriver mysqlDriver = null;
   51   
   52   	static {
   53   		try {
   54   			mysqlDriver = new NonRegisteringDriver();
   55   		} catch (Exception E) {
   56   			throw new RuntimeException(
   57   					"Can not load Driver class com.mysql.jdbc.Driver");
   58   		}
   59   	}
   60   
   61   	/** Log stream */
   62   	protected PrintWriter logWriter = null;
   63   
   64   	/** Database Name */
   65   	protected String databaseName = null;
   66   
   67   	/** Character Encoding */
   68   	protected String encoding = null;
   69   
   70   	/** Hostname */
   71   	protected String hostName = null;
   72   
   73   	/** Password */
   74   	protected String password = null;
   75   
   76   	/** The profileSql property */
   77   	protected String profileSql = "false";
   78   
   79   	/** The JDBC URL */
   80   	protected String url = null;
   81   
   82   	/** User name */
   83   	protected String user = null;
   84   
   85   	/** Should we construct the URL, or has it been set explicitly */
   86   	protected boolean explicitUrl = false;
   87   
   88   	/** Port number */
   89   	protected int port = 3306;
   90   
   91   	/**
   92   	 * Default no-arg constructor for Serialization
   93   	 */
   94   	public MysqlDataSource() {
   95   	}
   96   
   97   	/**
   98   	 * Creates a new connection using the already configured username and
   99   	 * password.
  100   	 * 
  101   	 * @return a connection to the database
  102   	 * 
  103   	 * @throws SQLException
  104   	 *             if an error occurs
  105   	 */
  106   	public java.sql.Connection getConnection() throws SQLException {
  107   		return getConnection(this.user, this.password);
  108   	}
  109   
  110   	/**
  111   	 * Creates a new connection with the given username and password
  112   	 * 
  113   	 * @param userID
  114   	 *            the user id to connect with
  115   	 * @param password
  116   	 *            the password to connect with
  117   	 * 
  118   	 * @return a connection to the database
  119   	 * 
  120   	 * @throws SQLException
  121   	 *             if an error occurs
  122   	 */
  123   	public java.sql.Connection getConnection(String userID, String pass)
  124   			throws SQLException {
  125   		Properties props = new Properties();
  126   
  127   		if (userID != null) {
  128   			props.setProperty(NonRegisteringDriver.USER_PROPERTY_KEY, userID);
  129   		}
  130   
  131   		if (pass != null) {
  132   			props.setProperty(NonRegisteringDriver.PASSWORD_PROPERTY_KEY, pass);
  133   		}
  134   
  135   		exposeAsProperties(props);
  136   
  137   		return getConnection(props);
  138   	}
  139   
  140   	/**
  141   	 * Sets the database name.
  142   	 * 
  143   	 * @param dbName
  144   	 *            the name of the database
  145   	 */
  146   	public void setDatabaseName(String dbName) {
  147   		this.databaseName = dbName;
  148   	}
  149   
  150   	/**
  151   	 * Gets the name of the database
  152   	 * 
  153   	 * @return the name of the database for this data source
  154   	 */
  155   	public String getDatabaseName() {
  156   		return (this.databaseName != null) ? this.databaseName : "";
  157   	}
  158   
  159   	/**
  160   	 * Sets the log writer for this data source.
  161   	 * 
  162   	 * @see javax.sql.DataSource#setLogWriter(PrintWriter)
  163   	 */
  164   	public void setLogWriter(PrintWriter output) throws SQLException {
  165   		this.logWriter = output;
  166   	}
  167   
  168   	/**
  169   	 * Returns the log writer for this data source
  170   	 * 
  171   	 * @return the log writer for this data source
  172   	 */
  173   	public java.io.PrintWriter getLogWriter() {
  174   		return this.logWriter;
  175   	}
  176   
  177   	/**
  178   	 * DOCUMENT ME!
  179   	 * 
  180   	 * @param seconds
  181   	 *            DOCUMENT ME!
  182   	 * 
  183   	 * @throws SQLException
  184   	 *             DOCUMENT ME!
  185   	 */
  186   	public void setLoginTimeout(int seconds) throws SQLException {
  187   	}
  188   
  189   	/**
  190   	 * Returns the login timeout
  191   	 * 
  192   	 * @return the login timeout
  193   	 */
  194   	public int getLoginTimeout() {
  195   		return 0;
  196   	}
  197   
  198   	/**
  199   	 * Sets the password
  200   	 * 
  201   	 * @param pass
  202   	 *            the password
  203   	 */
  204   	public void setPassword(String pass) {
  205   		this.password = pass;
  206   	}
  207   
  208   	/**
  209   	 * Sets the database port.
  210   	 * 
  211   	 * @param p
  212   	 *            the port
  213   	 */
  214   	public void setPort(int p) {
  215   		this.port = p;
  216   	}
  217   
  218   	/**
  219   	 * Returns the port number
  220   	 * 
  221   	 * @return the port number
  222   	 */
  223   	public int getPort() {
  224   		return this.port;
  225   	}
  226   
  227   	/**
  228   	 * Sets the port number
  229   	 * 
  230   	 * @param p
  231   	 *            the port
  232   	 * 
  233   	 * @see #setPort
  234   	 */
  235   	public void setPortNumber(int p) {
  236   		setPort(p);
  237   	}
  238   
  239   	/**
  240   	 * Returns the port number
  241   	 * 
  242   	 * @return the port number
  243   	 */
  244   	public int getPortNumber() {
  245   		return getPort();
  246   	}
  247   
  248   	/**
  249   	 * DOCUMENT ME!
  250   	 * 
  251   	 * @param ref
  252   	 *            DOCUMENT ME!
  253   	 * 
  254   	 * @throws SQLException
  255   	 *             DOCUMENT ME!
  256   	 */
  257   	public void setPropertiesViaRef(Reference ref) throws SQLException {
  258   		super.initializeFromRef(ref);
  259   	}
  260   
  261   	/**
  262   	 * Required method to support this class as a <CODE>Referenceable</CODE>.
  263   	 * 
  264   	 * @return a Reference to this data source
  265   	 * 
  266   	 * @throws NamingException
  267   	 *             if a JNDI error occurs
  268   	 */
  269   	public Reference getReference() throws NamingException {
  270   		String factoryName = "com.mysql.jdbc.jdbc2.optional.MysqlDataSourceFactory";
  271   		Reference ref = new Reference(getClass().getName(), factoryName, null);
  272   		ref.add(new StringRefAddr(NonRegisteringDriver.USER_PROPERTY_KEY,
  273   				getUser()));
  274   		ref.add(new StringRefAddr(NonRegisteringDriver.PASSWORD_PROPERTY_KEY,
  275   				this.password));
  276   		ref.add(new StringRefAddr("serverName", getServerName()));
  277   		ref.add(new StringRefAddr("port", "" + getPort()));
  278   		ref.add(new StringRefAddr("databaseName", getDatabaseName()));
  279   		ref.add(new StringRefAddr("url", getUrl()));
  280   		ref.add(new StringRefAddr("explicitUrl", String
  281   				.valueOf(this.explicitUrl)));
  282   
  283   		//
  284   		// Now store all of the 'non-standard' properties...
  285   		//
  286   		try {
  287   			storeToRef(ref);
  288   		} catch (SQLException sqlEx) {
  289   			throw new NamingException(sqlEx.getMessage());
  290   		}
  291   
  292   		return ref;
  293   	}
  294   
  295   	/**
  296   	 * Sets the server name.
  297   	 * 
  298   	 * @param serverName
  299   	 *            the server name
  300   	 */
  301   	public void setServerName(String serverName) {
  302   		this.hostName = serverName;
  303   	}
  304   
  305   	/**
  306   	 * Returns the name of the database server
  307   	 * 
  308   	 * @return the name of the database server
  309   	 */
  310   	public String getServerName() {
  311   		return (this.hostName != null) ? this.hostName : "";
  312   	}
  313   
  314   	//
  315   	// I've seen application servers use both formats
  316   	// URL or url (doh)
  317   	//
  318   
  319   	/**
  320   	 * Sets the URL for this connection
  321   	 * 
  322   	 * @param url
  323   	 *            the URL for this connection
  324   	 */
  325   	public void setURL(String url) {
  326   		setUrl(url);
  327   	}
  328   
  329   	/**
  330   	 * Returns the URL for this connection
  331   	 * 
  332   	 * @return the URL for this connection
  333   	 */
  334   	public String getURL() {
  335   		return getUrl();
  336   	}
  337   
  338   	/**
  339   	 * This method is used by the app server to set the url string specified
  340   	 * within the datasource deployment descriptor. It is discovered using
  341   	 * introspection and matches if property name in descriptor is "url".
  342   	 * 
  343   	 * @param url
  344   	 *            url to be used within driver.connect
  345   	 */
  346   	public void setUrl(String url) {
  347   		this.url = url;
  348   		this.explicitUrl = true;
  349   	}
  350   
  351   	/**
  352   	 * Returns the JDBC URL that will be used to create the database connection.
  353   	 * 
  354   	 * @return the URL for this connection
  355   	 */
  356   	public String getUrl() {
  357   		if (!this.explicitUrl) {
  358   			String builtUrl = "jdbc:mysql://";
  359   			builtUrl = builtUrl + getServerName() + ":" + getPort() + "/"
  360   					+ getDatabaseName();
  361   
  362   			return builtUrl;
  363   		}
  364   
  365   		return this.url;
  366   	}
  367   
  368   	/**
  369   	 * Sets the user ID.
  370   	 * 
  371   	 * @param userID
  372   	 *            the User ID
  373   	 */
  374   	public void setUser(String userID) {
  375   		this.user = userID;
  376   	}
  377   
  378   	/**
  379   	 * Returns the configured user for this connection
  380   	 * 
  381   	 * @return the user for this connection
  382   	 */
  383   	public String getUser() {
  384   		return this.user;
  385   	}
  386   
  387   	/**
  388   	 * Creates a connection using the specified properties.
  389   	 * 
  390   	 * @param props
  391   	 *            the properties to connect with
  392   	 * 
  393   	 * @return a connection to the database
  394   	 * 
  395   	 * @throws SQLException
  396   	 *             if an error occurs
  397   	 */
  398   	protected java.sql.Connection getConnection(Properties props)
  399   			throws SQLException {
  400   		String jdbcUrlToUse = null;
  401   
  402   		if (!this.explicitUrl) {
  403   			StringBuffer jdbcUrl = new StringBuffer("jdbc:mysql://");
  404   
  405   			if (this.hostName != null) {
  406   				jdbcUrl.append(this.hostName);
  407   			}
  408   
  409   			jdbcUrl.append(":");
  410   			jdbcUrl.append(this.port);
  411   			jdbcUrl.append("/");
  412   
  413   			if (this.databaseName != null) {
  414   				jdbcUrl.append(this.databaseName);
  415   			}
  416   
  417   			jdbcUrlToUse = jdbcUrl.toString();
  418   		} else {
  419   			jdbcUrlToUse = this.url;
  420   		}
  421   
  422   		//
  423   		// URL should take precedence over properties
  424   		//
  425   		
  426   		Properties urlProps = mysqlDriver.parseURL(jdbcUrlToUse, null);
  427   		urlProps.remove(NonRegisteringDriver.DBNAME_PROPERTY_KEY);
  428   		urlProps.remove(NonRegisteringDriver.HOST_PROPERTY_KEY);
  429   		urlProps.remove(NonRegisteringDriver.PORT_PROPERTY_KEY);
  430   		
  431   		Iterator keys = urlProps.keySet().iterator();
  432   		
  433   		while (keys.hasNext()) {
  434   			String key = (String)keys.next();
  435   			
  436   			props.setProperty(key, urlProps.getProperty(key));
  437   		}
  438   		
  439   		return mysqlDriver.connect(jdbcUrlToUse, props);
  440   	}
  441   //
  442   //	public boolean isWrapperFor(Class<?> iface) throws SQLException {
  443   //		throw SQLError.notImplemented();
  444   //	}
  445   //
  446   //	public <T> T unwrap(Class<T> iface) throws SQLException {
  447   //		throw SQLError.notImplemented();
  448   //	}
  449   }

Save This Page
Home » MySQL-JDBC-5.1.11 » com.mysql.jdbc.jdbc2.optional » [javadoc | source]