OSDN Git Service

Upgrade to 4.2.0
[stew/Stew4.git] / src / net / argius / stew / Connector.java
1 package net.argius.stew;
2
3 import java.sql.*;
4 import java.util.*;
5
6 /**
7  * This class provides functions to manage database connections in this application. 
8  */
9 public final class Connector {
10
11     private static final Logger log = Logger.getLogger(Connector.class);
12
13     private final String id;
14     private final Properties props;
15     private final Password password;
16
17     private transient Driver driver;
18
19     /**
20      * A constructor.
21      * @param id
22      * @param props
23      */
24     public Connector(String id, Properties props) {
25         assert id != null;
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));
28         }
29         Properties p = new Properties();
30         p.putAll(props);
31         Password password = createPasswordInstance(props.getProperty("password.class"));
32         password.setTransformedString(props.getProperty("password"));
33         this.id = id;
34         this.props = p;
35         this.password = password;
36     }
37
38     private static Password createPasswordInstance(String className) {
39         if (className != null) {
40             try {
41                 return (Password)DynamicLoader.newInstance(className);
42             } catch (Exception ex) {
43                 log.warn(ex);
44             }
45         }
46         return new PlainTextPassword();
47     }
48
49     /**
50      * A constructor (for copying).
51      * @param id
52      * @param src
53      */
54     public Connector(String id, Connector src) {
55         this(id, (Properties)src.props.clone());
56     }
57
58     /**
59      * Returns the ID.
60      * @return
61      */
62     public String getId() {
63         return id;
64     }
65
66     /**
67      * Returns the name.
68      * @return
69      */
70     public String getName() {
71         return props.getProperty("name");
72     }
73
74     /**
75      * Returns the classpath.
76      * @return
77      */
78     public String getClasspath() {
79         return props.getProperty("classpath", "");
80     }
81
82     /**
83      * Returns the JDBC driver (class name).
84      * @return
85      */
86     public String getDriver() {
87         final String driver = props.getProperty("driver");
88         log.debug("driver=[%s]", driver);
89         return driver;
90     }
91
92     /**
93      * Returns the URL.
94      * @return
95      */
96     public String getUrl() {
97         return props.getProperty("url");
98     }
99
100     /**
101      * Returns the user.
102      * @return
103      */
104     public String getUser() {
105         return props.getProperty("user");
106     }
107
108     /**
109      * Returns the Password object.
110      * @return
111      */
112     public Password getPassword() {
113         return password;
114     }
115
116     /**
117      * Returns whether the connection is read-only or not.
118      * @return
119      */
120     public boolean isReadOnly() {
121         String s = props.getProperty("readonly");
122         return Boolean.valueOf(s).booleanValue();
123     }
124
125     /**
126      * Returns whether the connection uses auto-rollback or not.
127      * @return
128      */
129     public boolean usesAutoRollback() {
130         String s = props.getProperty("rollback");
131         return Boolean.valueOf(s).booleanValue();
132     }
133
134     /**
135      * Converts this to Properties.
136      * @return
137      */
138     public Properties toProperties() {
139         return (Properties)props.clone();
140     }
141
142     /**
143      * Attempts to establish a connection.
144      * @return
145      * @throws SQLException
146      */
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");
152             }
153             log.debug(driver);
154         }
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());
160         }
161         log.info("driver.connect start");
162         Connection conn = driver.connect(getUrl(), p);
163         log.info("driver.connect end");
164         if (conn == null) {
165             throw new IllegalStateException("driver returned null");
166         }
167         return conn;
168     }
169
170     @Override
171     public int hashCode() {
172         final int prime = 31;
173         int result = 1;
174         result = prime * result + ((props == null) ? 0 : props.hashCode());
175         return result;
176     }
177
178     @Override
179     public boolean equals(Object obj) {
180         if (this == obj) {
181             return true;
182         }
183         if (!(obj instanceof Connector)) {
184             return false;
185         }
186         Connector other = (Connector)obj;
187         return props.equals(other.props);
188     }
189
190     @Override
191     public String toString() {
192         return "Connector:" + id;
193     }
194
195 }