/Users/lyon/j4p/src/classUtils/pack/util/jdbc/JdbcURL.java
|
1 package classUtils.pack.util.jdbc;
2
3 import java.net.InetAddress;
4 import java.util.Properties;
5 import java.util.Set;
6
7 import classUtils.pack.util.SymbolTable;
8 import classUtils.pack.util.SymbolTable.UndefinedSymbolException;
9
10 /**
11 * A base class for template JDBC URL templates.
12 * <p>
13 * This class must be provided with a template at construction (containing symbols are
14 * defined in the <tt>$(<i>symbol</i>);</tt> form), then the corresponding values
15 * must be filled in and a propre JDBC url can be obtained by {@link #getURL() getURL()}.
16 * <p>
17 * The following symbols:
18 * <p>
19 * <table border align=center>
20 * <tr><td><b>subprotocol</b></td><td>the subprotocol, usually identifying a JDBC driver</td></tr>
21 * <tr><td><b>host</b></td><td>the datbase host name</td></tr>
22 * <tr><td><b>port</b></td><td>the datbase port</td></tr>
23 * <tr><td><b>database</b></td><td>the datbase name</td></tr>
24 * <tr><td><b>schema</b></td><td>the schema name</td></tr>
25 * <tr><td><b>user</b></td><td>the username for connecting</td></tr>
26 * <tr><td><b>password</b></td><td>the password for connecting</td></tr>
27 * </table>
28 * <p>
29 * are directly supported. However, arbitrary symbols can be present in the template
30 * and their values defined by {@link #setSymbolValue(java.lang.String, java.lang.String)
31 * setSymbolValue()}.
32 * <p>
33 * All the relative setters are <b>protected</b>. A subclass defines which symbol to
34 * expose publicly, simply by overriding one of the set<i>Symbol</i> methods with
35 * <b>public</b> visibility an whose body just invokes the corresponding method in this class.
36 * @author Cristiano Sadun
37 */
38 public class JdbcURL {
39
40 private SymbolTable symbolTable;
41 private String template;
42 private Properties connectionProperties;
43 private Set symbols;
44
45 private JdbcURL(String template, Properties connectionProperties) {
46 this.template=template;
47 if (!template.startsWith("jdbc:")) throw new IllegalArgumentException("The given template is not a JDBC url");
48 this.symbols=createSymbolsFromTemplate(template);
49 this.connectionProperties=connectionProperties;
50 this.symbolTable=new SymbolTable();
51 }
52
53 /**
54 * Create a jdbc URL object using a given template
55 */
56 protected JdbcURL(String template) {
57 this(template, new Properties());
58 }
59
60 /**
61 * Used to define a symbol in the template.
62 * <p>
63 * The symbol <i>must</i> be used in the template passed at construction.
64 */
65 protected void setSymbolValue(String symbol, String value) {
66 if (! symbols.contains(symbol)) {
67 throw new IllegalStateException("No "+symbol+" defined in the JDBC url template");
68 }
69 symbolTable.defineSymbol(symbol, value);
70 }
71
72 /**
73 * Define the standard "subprotocol" symbol.
74 */
75 protected void setSubprotocol(String subprotocol) {
76 setSymbolValue("subprotocol", subprotocol);
77 }
78
79
80 /**
81 * Define the standard "database" symbol.
82 */
83 protected void setDatabase(String database) {
84 setSymbolValue("database", database);
85 }
86
87 /**
88 * Define the standard "port" symbol.
89 */
90 protected void setPort(String port) {
91 setSymbolValue("port", port);
92 }
93
94 /**
95 * Define the standard "port" symbol.
96 */
97 protected void setPort(int port) {
98 setPort(String.valueOf(port));
99 }
100
101 /**
102 * Define the standard "host" symbol.
103 */
104 protected void setHost(String host) {
105 setSymbolValue("host", host);
106 }
107
108 /**
109 * Define the standard "host" symbol.
110 */
111 protected void setHost(InetAddress host) {
112 setSymbolValue("host", host.getHostAddress());
113 }
114
115 /**
116 * Define the standard "schema" symbol.
117 */
118 protected void setSchema(String schema) {
119 setSymbolValue("schemaw", schema);
120 }
121
122 /**
123 * Define the standard "user" symbol.
124 */
125 protected void setUser(String username) {
126 setSymbolValue("user", username);
127 }
128
129 /**
130 * Define the standard "password" symbol.
131 */
132 protected void setPassword(String password) {
133 setSymbolValue("password", password);
134 }
135
136 /**
137 * Return the URL - substituting values to template symbols.
138 * @return the final JDBC URL
139 * @exception IllegalStateException if a symbol in the template is not defined
140 */
141 public final String getURL() {
142 try {
143 return symbolTable.evaluate(template);
144 } catch(SymbolTable.UndefinedSymbolException e) {
145 throw new IllegalStateException("template symbol "+e.getSymbolName()+" has not been defined");
146 }
147 }
148
149 /**
150 * Method createSymbolsFromTemplate.
151 */
152 private static Set createSymbolsFromTemplate(String template) {
153 SymbolTable st = new SymbolTable();
154 st.setBehaviourOnUndefinedSymbol(st.RETURN_SYMBOL_ON_UNDEFINED_SYMBOL);
155 st.evaluate(template);
156 return st.getUndefinedSymbolsForLastEvaluation();
157 }
158 }
159