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 */
021package net.sf.hajdbc.sql;
022
023import java.sql.Connection;
024import java.sql.Driver;
025import java.sql.DriverManager;
026import java.sql.SQLException;
027import java.util.Properties;
028
029import javax.management.DynamicMBean;
030import javax.management.NotCompliantMBeanException;
031import javax.management.StandardMBean;
032
033import net.sf.hajdbc.Messages;
034
035/**
036 * @author  Paul Ferraro
037 * @version $Revision: 1777 $
038 * @since   1.0
039 */
040public class DriverDatabase extends AbstractDatabase<Driver> implements InactiveDriverDatabaseMBean
041{
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        {
054                return this.url;
055        }
056        
057        /**
058         * @see net.sf.hajdbc.sql.InactiveDriverDatabaseMBean#setUrl(java.lang.String)
059         */
060        @Override
061        public void setUrl(String url)
062        {
063                this.getDriver(url);
064                this.checkDirty(this.url, url);
065                this.url = url;
066        }
067        
068        /**
069         * @see net.sf.hajdbc.sql.ActiveDriverDatabaseMBean#getDriver()
070         */
071        @Override
072        public String getDriver()
073        {
074                return (this.driverClass != null) ? this.driverClass.getName() : null;
075        }
076        
077        /**
078         * @see net.sf.hajdbc.sql.InactiveDriverDatabaseMBean#setDriver(java.lang.String)
079         */
080        @Override
081        public void setDriver(String driver)
082        {
083                try
084                {
085                        Class<? extends Driver> driverClass = null;
086                        
087                        if ((driver != null) && (driver.length() > 0))
088                        {
089                                driverClass = Class.forName(driver).asSubclass(Driver.class);
090                        }
091                        
092                        this.checkDirty(this.driverClass, driverClass);
093                        this.driverClass = driverClass;
094                }
095                catch (ClassNotFoundException e)
096                {
097                        throw new IllegalArgumentException(e);
098                }
099                catch (ClassCastException e)
100                {
101                        throw new IllegalArgumentException(e);
102                }
103        }
104        
105        /**
106         * @see net.sf.hajdbc.Database#connect(java.lang.Object)
107         */
108        @Override
109        public Connection connect(Driver driver) throws SQLException
110        {
111                Properties properties = new Properties(this.getProperties());
112
113                String user = this.getUser();
114                
115                if (user != null)
116                {
117                        properties.setProperty(USER, user);
118                }
119
120                String password = this.getPassword();
121                
122                if (password != null)
123                {
124                        properties.setProperty(PASSWORD, password);
125                }
126                
127                return driver.connect(this.url, properties);
128        }
129
130        /**
131         * @see net.sf.hajdbc.Database#createConnectionFactory()
132         */
133        @Override
134        public Driver createConnectionFactory()
135        {
136                return this.getDriver(this.url);
137        }
138
139        private Driver getDriver(String url)
140        {
141                try
142                {
143                        return DriverManager.getDriver(url);
144                }
145                catch (SQLException e)
146                {
147                        throw new IllegalArgumentException(Messages.getMessage(Messages.JDBC_URL_REJECTED, url), e);
148                }
149        }
150
151        /**
152         * @see net.sf.hajdbc.Database#getActiveMBean()
153         */
154        @Override
155        public DynamicMBean getActiveMBean()
156        {
157                try
158                {
159                        return new StandardMBean(this, ActiveDriverDatabaseMBean.class);
160                }
161                catch (NotCompliantMBeanException e)
162                {
163                        throw new IllegalStateException(e);
164                }
165        }
166
167        /**
168         * @see net.sf.hajdbc.Database#getInactiveMBean()
169         */
170        @Override
171        public DynamicMBean getInactiveMBean()
172        {
173                try
174                {
175                        return new StandardMBean(this, InactiveDriverDatabaseMBean.class);
176                }
177                catch (NotCompliantMBeanException e)
178                {
179                        throw new IllegalStateException(e);
180                }
181        }
182}