001: /*
002: * HA-JDBC: High-Availability JDBC
003: * Copyright (c) 2004-2007 Paul Ferraro
004: *
005: * This library is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU Lesser General Public License as published by the
007: * Free Software Foundation; either version 2.1 of the License, or (at your
008: * option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful, but WITHOUT
011: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
012: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
013: * for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public License
016: * along with this library; if not, write to the Free Software Foundation,
017: * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: *
019: * Contact: ferraro@users.sourceforge.net
020: */
021: package net.sf.hajdbc.sql;
022:
023: import java.sql.Connection;
024: import java.sql.Driver;
025: import java.sql.DriverManager;
026: import java.sql.SQLException;
027: import java.util.Properties;
028:
029: import javax.management.DynamicMBean;
030: import javax.management.NotCompliantMBeanException;
031: import javax.management.StandardMBean;
032:
033: import net.sf.hajdbc.Messages;
034:
035: /**
036: * @author Paul Ferraro
037: * @version $Revision: 1777 $
038: * @since 1.0
039: */
040: public class DriverDatabase extends AbstractDatabase<Driver> implements
041: InactiveDriverDatabaseMBean {
042: private static final String USER = "user"; //$NON-NLS-1$
043: private static final String PASSWORD = "password"; //$NON-NLS-1$
044:
045: private String url;
046: private Class<? extends Driver> driverClass;
047:
048: /**
049: * @see net.sf.hajdbc.sql.ActiveDriverDatabaseMBean#getUrl()
050: */
051: @Override
052: public String getUrl() {
053: return this .url;
054: }
055:
056: /**
057: * @see net.sf.hajdbc.sql.InactiveDriverDatabaseMBean#setUrl(java.lang.String)
058: */
059: @Override
060: public void setUrl(String url) {
061: this .getDriver(url);
062: this .checkDirty(this .url, url);
063: this .url = url;
064: }
065:
066: /**
067: * @see net.sf.hajdbc.sql.ActiveDriverDatabaseMBean#getDriver()
068: */
069: @Override
070: public String getDriver() {
071: return (this .driverClass != null) ? this .driverClass.getName()
072: : null;
073: }
074:
075: /**
076: * @see net.sf.hajdbc.sql.InactiveDriverDatabaseMBean#setDriver(java.lang.String)
077: */
078: @Override
079: public void setDriver(String driver) {
080: try {
081: Class<? extends Driver> driverClass = null;
082:
083: if ((driver != null) && (driver.length() > 0)) {
084: driverClass = Class.forName(driver).asSubclass(
085: Driver.class);
086: }
087:
088: this .checkDirty(this .driverClass, driverClass);
089: this .driverClass = driverClass;
090: } catch (ClassNotFoundException e) {
091: throw new IllegalArgumentException(e);
092: } catch (ClassCastException e) {
093: throw new IllegalArgumentException(e);
094: }
095: }
096:
097: /**
098: * @see net.sf.hajdbc.Database#connect(java.lang.Object)
099: */
100: @Override
101: public Connection connect(Driver driver) throws SQLException {
102: Properties properties = new Properties(this .getProperties());
103:
104: if (this .user != null) {
105: properties.setProperty(USER, this .user);
106: }
107:
108: if (this .password != null) {
109: properties.setProperty(PASSWORD, this .password);
110: }
111:
112: return driver.connect(this .url, properties);
113: }
114:
115: /**
116: * @see net.sf.hajdbc.Database#createConnectionFactory()
117: */
118: @Override
119: public Driver createConnectionFactory() {
120: return this .getDriver(this .url);
121: }
122:
123: private Driver getDriver(String url) {
124: try {
125: return DriverManager.getDriver(url);
126: } catch (SQLException e) {
127: throw new IllegalArgumentException(Messages.getMessage(
128: Messages.JDBC_URL_REJECTED, url), e);
129: }
130: }
131:
132: /**
133: * @see net.sf.hajdbc.Database#getActiveMBean()
134: */
135: @Override
136: public DynamicMBean getActiveMBean() {
137: try {
138: return new StandardMBean(this ,
139: ActiveDriverDatabaseMBean.class);
140: } catch (NotCompliantMBeanException e) {
141: throw new IllegalStateException(e);
142: }
143: }
144:
145: /**
146: * @see net.sf.hajdbc.Database#getInactiveMBean()
147: */
148: @Override
149: public DynamicMBean getInactiveMBean() {
150: try {
151: return new StandardMBean(this ,
152: InactiveDriverDatabaseMBean.class);
153: } catch (NotCompliantMBeanException e) {
154: throw new IllegalStateException(e);
155: }
156: }
157: }
|