1 30 31 32 package org.hsqldb; 33 34 import java.util.Locale ; 35 36 import org.hsqldb.persist.HsqlProperties; 37 38 45 public class DatabaseURL { 46 47 static final String S_DOT = "."; 48 public static final String S_MEM = "mem:"; 49 public static final String S_FILE = "file:"; 50 public static final String S_RES = "res:"; 51 public static final String S_ALIAS = "alias:"; 52 public static final String S_HSQL = "hsql://"; 53 public static final String S_HSQLS = "hsqls://"; 54 public static final String S_HTTP = "http://"; 55 public static final String S_HTTPS = "https://"; 56 public static final String S_URL_PREFIX = "jdbc:hsqldb:"; 57 58 62 public static boolean isFileBasedDatabaseType(String url) { 63 64 if (url == S_FILE || url == S_RES) { 65 return true; 66 } 67 68 return false; 69 } 70 71 74 public static boolean isInProcessDatabaseType(String url) { 75 76 if (url == S_FILE || url == S_RES || url == S_MEM) { 77 return true; 78 } 79 80 return false; 81 } 82 83 112 public static HsqlProperties parseURL(String url, boolean hasPrefix) { 113 114 String urlImage = url.toLowerCase(Locale.ENGLISH); 115 HsqlProperties props = new HsqlProperties(); 116 HsqlProperties extraProps = null; 117 String 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 type = null; 129 String host; 130 int port = 0; 131 String database; 132 String 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 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 } 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 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 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 275 } 276 | Popular Tags |