1 package net.argius.stew;
7 * This class provides functions to manage database connections in this application.
9 public final class Connector {
11 private static final Logger log = Logger.getLogger(Connector.class);
13 private final String id;
14 private final Properties props;
15 private final Password password;
17 private transient Driver driver;
24 public Connector(String id, Properties props) {
26 if (!id.matches("[A-Za-z0-9]+")) { // XXX move to new public method isValid
27 throw new IllegalArgumentException(ResourceManager.Default.get("e.id-can-only-contain-alphanum", id));
29 Properties p = new Properties();
31 Password password = createPasswordInstance(props.getProperty("password.class"));
32 password.setTransformedString(props.getProperty("password"));
35 this.password = password;
38 private static Password createPasswordInstance(String className) {
39 if (className != null) {
41 return (Password)DynamicLoader.newInstance(className);
42 } catch (Exception ex) {
46 return new PlainTextPassword();
50 * A constructor (for copying).
54 public Connector(String id, Connector src) {
55 this(id, (Properties)src.props.clone());
62 public String getId() {
70 public String getName() {
71 return props.getProperty("name");
75 * Returns the classpath.
78 public String getClasspath() {
79 return props.getProperty("classpath", "");
83 * Returns the JDBC driver (class name).
86 public String getDriver() {
87 final String driver = props.getProperty("driver");
88 log.debug("driver=[%s]", driver);
96 public String getUrl() {
97 return props.getProperty("url");
104 public String getUser() {
105 return props.getProperty("user");
109 * Returns the Password object.
112 public Password getPassword() {
117 * Returns whether the connection is read-only or not.
120 public boolean isReadOnly() {
121 String s = props.getProperty("readonly");
122 return Boolean.valueOf(s).booleanValue();
126 * Returns whether the connection uses auto-rollback or not.
129 public boolean usesAutoRollback() {
130 String s = props.getProperty("rollback");
131 return Boolean.valueOf(s).booleanValue();
135 * Converts this to Properties.
138 public Properties toProperties() {
139 return (Properties)props.clone();
143 * Attempts to establish a connection.
145 * @throws SQLException
147 public Connection getConnection() throws SQLException {
148 if (driver == null) {
149 driver = ConnectorDriverManager.getDriver(getUrl(), getDriver(), getClasspath());
150 if (driver == null) {
151 throw new SQLException("failed to load driver");
155 Properties p = new Properties();
156 p.setProperty("user", getUser());
157 p.setProperty("password", getPassword().getRawString());
158 if (!driver.acceptsURL(getUrl())) {
159 throw new SQLException("invalid url: " + getUrl());
161 log.info("driver.connect start");
162 Connection conn = driver.connect(getUrl(), p);
163 log.info("driver.connect end");
165 throw new IllegalStateException("driver returned null");
171 public int hashCode() {
172 final int prime = 31;
174 result = prime * result + ((props == null) ? 0 : props.hashCode());
179 public boolean equals(Object obj) {
183 if (!(obj instanceof Connector)) {
186 Connector other = (Connector)obj;
187 return props.equals(other.props);
191 public String toString() {
192 return "Connector:" + id;