001/* 002 * HA-JDBC: High-Availability JDBC 003 * Copyright (C) 2012 Paul Ferraro 004 * 005 * This program is free software: you can redistribute it and/or modify 006 * it under the terms of the GNU Lesser General Public License as published by 007 * the Free Software Foundation, either version 3 of the License, or 008 * (at your option) any later version. 009 * 010 * This program is distributed in the hope that it will be useful, 011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 013 * GNU Lesser General Public License for more details. 014 * 015 * You should have received a copy of the GNU Lesser General Public License 016 * along with this program. If not, see <http://www.gnu.org/licenses/>. 017 */ 018package net.sf.hajdbc.sql; 019 020import java.sql.Connection; 021import java.sql.Driver; 022import java.sql.DriverManager; 023import java.sql.SQLException; 024import java.util.Map; 025import java.util.Properties; 026 027import javax.xml.bind.annotation.XmlType; 028 029import net.sf.hajdbc.Messages; 030import net.sf.hajdbc.management.Description; 031import net.sf.hajdbc.management.MBean; 032import net.sf.hajdbc.management.ManagedAttribute; 033 034/** 035 * @author Paul Ferraro 036 * @version $Revision: 1948 $ 037 * @since 1.0 038 */ 039@MBean 040@Description("Database accessed via DriverManager") 041@XmlType(name = "database") 042public class DriverDatabase extends AbstractDatabase<Driver> 043{ 044 private static final String USER = "user"; //$NON-NLS-1$ 045 private static final String PASSWORD = "password"; //$NON-NLS-1$ 046 047 /** 048 * {@inheritDoc} 049 * @see net.sf.hajdbc.sql.AbstractDatabase#setLocation(java.lang.String) 050 */ 051 @ManagedAttribute 052 @Description("JDBC url") 053 @Override 054 public void setLocation(String location) 055 { 056 getDriver(location); 057 058 super.setLocation(location); 059 } 060 061 /** 062 * {@inheritDoc} 063 * @see net.sf.hajdbc.Database#connect(java.lang.Object, java.lang.String) 064 */ 065 @Override 066 public Connection connect(Driver driver, String password) throws SQLException 067 { 068 Properties properties = new Properties(); 069 070 for (Map.Entry<String, String> entry: this.getProperties().entrySet()) 071 { 072 properties.setProperty(entry.getKey(), entry.getValue()); 073 } 074 075 if (this.requiresAuthentication()) 076 { 077 properties.setProperty(USER, this.getUser()); 078 if (password != null) 079 { 080 properties.setProperty(PASSWORD, password); 081 } 082 } 083 084 return driver.connect(this.getLocation(), properties); 085 } 086 087 /** 088 * @see net.sf.hajdbc.Database#getConnectionSource() 089 */ 090 @Override 091 public Driver getConnectionSource() 092 { 093 return getDriver(this.getLocation()); 094 } 095 096 public String parseVendor() 097 { 098 String url = this.getLocation(); 099 return url.substring(5, url.indexOf(":", 5)); 100 } 101 102 private static Driver getDriver(String url) 103 { 104 try 105 { 106 return DriverManager.getDriver(url); 107 } 108 catch (SQLException e) 109 { 110 throw new IllegalArgumentException(Messages.JDBC_URL_REJECTED.getMessage(url), e); 111 } 112 } 113}