KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > scriptella > spi > ConnectionParameters


1 /*
2  * Copyright 2006-2007 The Scriptella Project Team.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package scriptella.spi;
18
19 import scriptella.configuration.ConfigurationException;
20 import scriptella.configuration.ConnectionEl;
21 import scriptella.util.ExceptionUtils;
22 import scriptella.util.IOUtils;
23
24 import java.io.File JavaDoc;
25 import java.net.MalformedURLException JavaDoc;
26 import java.net.URI JavaDoc;
27 import java.net.URL JavaDoc;
28 import java.nio.charset.Charset JavaDoc;
29 import java.util.Map JavaDoc;
30
31 /**
32  * Represents connection parameters.
33  *
34  * @author Fyodor Kupolov
35  * @version 1.0
36  */

37 public class ConnectionParameters {
38     private Map JavaDoc<String JavaDoc, ?> properties;
39     private String JavaDoc url;
40     private String JavaDoc user;
41     private String JavaDoc password;
42     private String JavaDoc schema;
43     private String JavaDoc catalog;
44     private DriverContext context;
45
46     /**
47      * For testing purposes.
48      */

49     protected ConnectionParameters() {
50     }
51
52     /**
53      * Creates connection parameters based on &lt;connection&gt; element..
54      */

55     public ConnectionParameters(ConnectionEl conf, DriverContext context) {
56         this.properties = conf.getProperties();
57         this.url = conf.getUrl();
58         this.user = conf.getUser();
59         this.password = conf.getPassword();
60         this.schema = conf.getSchema();
61         this.catalog = conf.getCatalog();
62
63         this.context = context;
64     }
65
66     /**
67      * This method returns properties for connection specified inside &lt;connection&gt; element
68      *
69      * @return properties map.
70      */

71     public Map JavaDoc<String JavaDoc, ?> getProperties() {
72         return properties;
73     }
74
75     /**
76      * Convenience method which returns property by name.
77      *
78      * @param name property name
79      * @return property value
80      * @see #getProperties()
81      */

82     public Object JavaDoc getProperty(String JavaDoc name) {
83         return properties.get(name);
84     }
85
86     /**
87      * Returns string value of the property.
88      *
89      * @param name property name.
90      */

91     public String JavaDoc getStringProperty(String JavaDoc name) {
92         Object JavaDoc v = properties.get(name);
93         return v == null ? null : v.toString();
94     }
95
96     /**
97      * Returns the value of integer property.
98      *
99      * @see #getNumberProperty(String,Number)
100      */

101     public Integer JavaDoc getIntegerProperty(String JavaDoc name, int defaultValue) throws ConfigurationException {
102         return getNumberProperty(name, defaultValue).intValue();
103     }
104
105     public Integer JavaDoc getIntegerProperty(String JavaDoc name) throws ConfigurationException {
106         Number JavaDoc res = getNumberProperty(name, null);
107         return res == null ? null : res.intValue();
108     }
109
110     /**
111      * Returns numeric value of the property.
112      * <p>Accepts decimal, hexadecimal, and octal numbers if property is String.
113      *
114      * @param name property name.
115      * @param defaultValue default value to use when property omitted.
116      * @return numeric property value.
117      * @see Long#decode(String)
118      */

119     public Number JavaDoc getNumberProperty(String JavaDoc name, Number JavaDoc defaultValue) throws ConfigurationException {
120         Object JavaDoc v = properties.get(name);
121         if (v == null) {
122             return defaultValue;
123         }
124         if (v instanceof Number JavaDoc) {
125             return ((Number JavaDoc) v);
126         }
127         String JavaDoc s = v.toString().trim();
128         if (s.length() == 0) {
129             return defaultValue;
130         }
131
132         //For now we do not support doubles etc.
133
try {
134             return Long.decode(s);
135         } catch (NumberFormatException JavaDoc e) {
136             throw new ConfigurationException(name + " property must be integer.");
137         }
138     }
139
140
141     /**
142      * @see #getBooleanProperty(String,boolean)
143      */

144     public boolean getBooleanProperty(String JavaDoc name) throws ConfigurationException {
145         return getBooleanProperty(name, false);
146     }
147
148     /**
149      * Parses property value as boolean flag.
150      *
151      * @param name property name.
152      * @param defaultValue default value to use if connection has no such property.
153      * @return boolean property value.
154      * @throws ConfigurationException if property has unrecognized value.
155      */

156     public boolean getBooleanProperty(String JavaDoc name, boolean defaultValue) throws ConfigurationException {
157         Object JavaDoc a = getProperty(name);
158         if (a == null) {
159             return defaultValue;
160         }
161         if (a instanceof Boolean JavaDoc) {
162             return (Boolean JavaDoc) a;
163         }
164         if (a instanceof Number JavaDoc) {
165             return ((Number JavaDoc) a).intValue() > 0;
166         }
167         String JavaDoc s = a.toString().trim();
168
169         if ("true".equalsIgnoreCase(s) || "1".equalsIgnoreCase(s) || "on".equalsIgnoreCase(s) || "yes".equalsIgnoreCase(s))
170         {
171             return true;
172         }
173
174         if ("false".equalsIgnoreCase(s) || "0".equalsIgnoreCase(s) || "off".equalsIgnoreCase(s) || "no".equalsIgnoreCase(s))
175         {
176             return false;
177         }
178         throw new ConfigurationException("Unrecognized boolean property value " + a);
179     }
180
181     /**
182      * Parses property value as a charset encoding name.
183      *
184      * @param name property name.
185      * @return value of the property or null if connection has no such property.
186      * @throws ConfigurationException if charset name is unsupported.
187      */

188     public String JavaDoc getCharsetProperty(String JavaDoc name) throws ConfigurationException {
189         Object JavaDoc cs = getProperty(name);
190         if (cs == null) {
191             return null;
192         }
193         if (cs instanceof Charset JavaDoc) {
194             return ((Charset JavaDoc) cs).name();
195         }
196         String JavaDoc enc = cs.toString().trim();
197         if (!Charset.isSupported(enc)) {
198             throw new ConfigurationException("Specified encoding " + enc + " is not supported. Supported encodings are " + Charset.availableCharsets().keySet());
199         }
200         return enc;
201     }
202
203     /**
204      * Parses property value as URL parameter.
205      * <p>The URL is resolved relative to script file location.
206      *
207      * @param name property name
208      * @return value of the property or null if connection has no such property.
209      * @throws ConfigurationException if URL is malformed.
210      */

211     public URL JavaDoc getUrlProperty(String JavaDoc name) throws ConfigurationException {
212         Object JavaDoc u = getProperty(name);
213         if (u == null) {
214             return null;
215         }
216         if (u instanceof URL JavaDoc) {
217             return (URL JavaDoc) u;
218         }
219         try {
220             if (u instanceof URI JavaDoc) {
221                 URI JavaDoc uri = (URI JavaDoc) u;
222                 return uri.toURL();
223             }
224             if (u instanceof File JavaDoc) {
225                 File JavaDoc f = (File JavaDoc) u;
226                 return IOUtils.toUrl(f);
227             }
228         } catch (MalformedURLException JavaDoc e) {
229             ExceptionUtils.ignoreThrowable(e);
230             //If malformed URL try to use the toString resolving
231
}
232
233         try {
234             String JavaDoc uri = u.toString().trim();
235             return getContext().resolve(uri);
236         } catch (MalformedURLException JavaDoc e) {
237             throw new ConfigurationException("Specified URL " + u + " is malformed");
238         }
239
240     }
241
242     /**
243      * Returns connection URL.
244      * <p>URL format is arbitrary and specified by Service Provider.
245      *
246      * @return connection URL
247      */

248     public String JavaDoc getUrl() {
249         return url;
250     }
251
252     /**
253      * Returns the url property resolved relative to a script location.
254      *
255      * @throws ConfigurationException if connection URL is malformed or null.
256      */

257     public URL JavaDoc getResolvedUrl() throws ConfigurationException {
258         if (url == null) {
259             throw new ConfigurationException("URL connection property is requred");
260         }
261         try {
262             return getContext().resolve(url);
263         } catch (MalformedURLException JavaDoc e) {
264             throw new ConfigurationException("Specified connection URL " + url + " is malformed");
265         }
266     }
267
268     /**
269      * Optional user name for connection.
270      *
271      * @return user name
272      */

273     public String JavaDoc getUser() {
274         return user;
275     }
276
277     /**
278      * Optional user password for connection.
279      *
280      * @return user password
281      */

282     public String JavaDoc getPassword() {
283         return password;
284     }
285
286     /**
287      * @return optional schema name
288      */

289     public String JavaDoc getSchema() {
290         return schema;
291     }
292
293     /**
294      * @return optional catalog name
295      */

296     public String JavaDoc getCatalog() {
297         return catalog;
298     }
299
300     /**
301      * Get a drivers context.
302      *
303      * @return script context.
304      */

305     public DriverContext getContext() {
306         return context;
307     }
308
309     public String JavaDoc toString() {
310         return "ConnectionParameters{" + "properties=" + properties + ", url='" + url + '\'' + ", user='" + user + '\'' + ", password='" + password + '\'' + ", schema='" + schema + '\'' + ", catalog='" + catalog + '\'' + '}';
311     }
312 }
313
Popular Tags