KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hsqldb > DatabaseURL


1 /* Copyright (c) 2001-2005, The HSQL Development Group
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * Redistributions of source code must retain the above copyright notice, this
8  * list of conditions and the following disclaimer.
9  *
10  * Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  *
14  * Neither the name of the HSQL Development Group nor the names of its
15  * contributors may be used to endorse or promote products derived from this
16  * software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG,
22  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */

30
31
32 package org.hsqldb;
33
34 import java.util.Locale JavaDoc;
35
36 import org.hsqldb.persist.HsqlProperties;
37
38 /*
39  * Parses a connection URL into parts.
40  *
41  * @author fredt@users
42  * @version 1.8.0
43  * @since 1.8.0
44  */

45 public class DatabaseURL {
46
47     static final String JavaDoc S_DOT = ".";
48     public static final String JavaDoc S_MEM = "mem:";
49     public static final String JavaDoc S_FILE = "file:";
50     public static final String JavaDoc S_RES = "res:";
51     public static final String JavaDoc S_ALIAS = "alias:";
52     public static final String JavaDoc S_HSQL = "hsql://";
53     public static final String JavaDoc S_HSQLS = "hsqls://";
54     public static final String JavaDoc S_HTTP = "http://";
55     public static final String JavaDoc S_HTTPS = "https://";
56     public static final String JavaDoc S_URL_PREFIX = "jdbc:hsqldb:";
57
58     /**
59      * Returns true if type represents an in-process connection to a file backed
60      * database.
61      */

62     public static boolean isFileBasedDatabaseType(String JavaDoc url) {
63
64         if (url == S_FILE || url == S_RES) {
65             return true;
66         }
67
68         return false;
69     }
70
71     /**
72      * Returns true if type represents an in-process connection to database.
73      */

74     public static boolean isInProcessDatabaseType(String JavaDoc url) {
75
76         if (url == S_FILE || url == S_RES || url == S_MEM) {
77             return true;
78         }
79
80         return false;
81     }
82
83     /**
84      * Parses the url into components that are returned in a properties
85      * object. <p>
86      *
87      * The following components are isolated: <p>
88      *
89      * <ul>
90      * url: the original url<p>
91      * connection_type: a static string that indicate the protocol. If the
92      * url does not begin with a valid protocol, null is returned by this
93      * method instead of the properties object.<p>
94      * host: name of host in networked modes in lowercase<p>
95      * port: port number in networked mode, or 0 if not present<p>
96      * path: path of the resource on server in networked modes,
97      * / (slash) in all cases apart from
98      * servlet path which is / (slash) plus the name of the servlet<p>
99      * database: database name. For memory, resource and networked modes,
100      * this is returned in lowercase, for file databases the original
101      * case of characters is preserved. Returns empty string if name is not
102      * present in the url.<p>
103      * for each protocol if port number is not in the url<p>
104      * Additional connection properties specified as key/value pairs.
105      * </ul>
106      * @return null returned if the part that should represent the port is not
107      * an integer or the part for database name is empty.
108      * Empty HsqlProperties returned if if url does not begin with valid
109      * protocol and could refer to another JDBC driver.
110      *
111      */

112     public static HsqlProperties parseURL(String JavaDoc url, boolean hasPrefix) {
113
114         String JavaDoc urlImage = url.toLowerCase(Locale.ENGLISH);
115         HsqlProperties props = new HsqlProperties();
116         HsqlProperties extraProps = null;
117         String JavaDoc arguments = null;
118         int pos = 0;
119
120         if (hasPrefix) {
121             if (urlImage.startsWith(S_URL_PREFIX)) {
122                 pos = S_URL_PREFIX.length();
123             } else {
124                 return props;
125             }
126         }
127
128         String JavaDoc type = null;
129         String JavaDoc host;
130         int port = 0;
131         String JavaDoc database;
132         String JavaDoc path;
133         boolean isNetwork = false;
134
135         props.setProperty("url", url);
136
137         int semicolpos = url.indexOf(';', pos);
138
139         if (semicolpos < 0) {
140             semicolpos = url.length();
141         } else {
142             arguments = urlImage.substring(semicolpos + 1, urlImage.length());
143             extraProps = HsqlProperties.delimitedArgPairsToProps(arguments,
144                     "=", ";", null);
145
146             //todo - check if properties have valid names / values
147
props.addProperties(extraProps);
148         }
149
150         if (semicolpos == pos + 1 && urlImage.startsWith(S_DOT, pos)) {
151             type = S_DOT;
152         } else if (urlImage.startsWith(S_MEM, pos)) {
153             type = S_MEM;
154         } else if (urlImage.startsWith(S_FILE, pos)) {
155             type = S_FILE;
156         } else if (urlImage.startsWith(S_RES, pos)) {
157             type = S_RES;
158         } else if (urlImage.startsWith(S_ALIAS, pos)) {
159             type = S_ALIAS;
160         } else if (urlImage.startsWith(S_HSQL, pos)) {
161             type = S_HSQL;
162             port = ServerConstants.SC_DEFAULT_HSQL_SERVER_PORT;
163             isNetwork = true;
164         } else if (urlImage.startsWith(S_HSQLS, pos)) {
165             type = S_HSQLS;
166             port = ServerConstants.SC_DEFAULT_HSQLS_SERVER_PORT;
167             isNetwork = true;
168         } else if (urlImage.startsWith(S_HTTP, pos)) {
169             type = S_HTTP;
170             port = ServerConstants.SC_DEFAULT_HTTP_SERVER_PORT;
171             isNetwork = true;
172         } else if (urlImage.startsWith(S_HTTPS, pos)) {
173             type = S_HTTPS;
174             port = ServerConstants.SC_DEFAULT_HTTPS_SERVER_PORT;
175             isNetwork = true;
176         }
177
178         if (type == null) {
179             type = S_FILE;
180         } else if (type == S_DOT) {
181             type = S_MEM;
182
183             // keep pos
184
} else {
185             pos += type.length();
186         }
187
188         props.setProperty("connection_type", type);
189
190         if (isNetwork) {
191             int slashpos = url.indexOf('/', pos);
192
193             if (slashpos < pos || slashpos > semicolpos) {
194                 slashpos = semicolpos;
195             }
196
197             int colonpos = url.indexOf(':', pos);
198
199             if (colonpos < pos || colonpos > slashpos) {
200                 colonpos = slashpos;
201             } else {
202                 try {
203                     port = Integer.parseInt(url.substring(colonpos + 1,
204                                                           slashpos));
205                 } catch (NumberFormatException JavaDoc e) {
206                     return null;
207                 }
208             }
209
210             host = urlImage.substring(pos, colonpos);
211
212             int secondslashpos = url.lastIndexOf('/', semicolpos);
213
214             if (secondslashpos < pos) {
215                 path = "/";
216                 database = "";
217             } else if (secondslashpos == slashpos) {
218                 path = "/";
219                 database = urlImage.substring(secondslashpos + 1, semicolpos);
220             } else {
221                 path = url.substring(slashpos, secondslashpos);
222                 database = urlImage.substring(secondslashpos + 1, semicolpos);
223             }
224
225             props.setProperty("port", port);
226             props.setProperty("host", host);
227             props.setProperty("path", path);
228
229             if (extraProps != null) {
230                 String JavaDoc filePath = extraProps.getProperty("filepath");
231
232                 if (filePath != null && database.length() != 0) {
233                     database += ";" + filePath;
234                 }
235             }
236         } else {
237             if (type == S_MEM || type == S_RES) {
238                 database = urlImage.substring(pos, semicolpos).toLowerCase();
239
240                 if (type == S_RES) {
241                     if (database.indexOf('/') != 0) {
242                         database = '/' + database;
243                     }
244                 }
245             } else {
246                 database = url.substring(pos, semicolpos);
247             }
248
249             if (database.length() == 0) {
250                 return null;
251             }
252         }
253
254         props.setProperty("database", database);
255
256         return props;
257     }
258 /*
259     public static void main(String[] argv) {
260
261         parseURL(
262             "JDBC:hsqldb:hsql://myhost:1777/mydb;filepath=c:/myfile/database/db",
263             true);
264         parseURL("JDBC:hsqldb:../data/mydb.db", true);
265         parseURL("JDBC:hsqldb:../data/mydb.db;ifexists=true", true);
266         parseURL("JDBC:hsqldb:HSQL://localhost:9000/mydb", true);
267         parseURL(
268             "JDBC:hsqldb:Http://localhost:8080/servlet/org.hsqldb.Servlet/mydb;ifexists=true",
269             true);
270         parseURL("JDBC:hsqldb:Http://localhost/servlet/org.hsqldb.Servlet/",
271                  true);
272         parseURL("JDBC:hsqldb:hsql://myhost", true);
273     }
274 */

275 }
276
Popular Tags