1 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 ; 25 import java.net.MalformedURLException ; 26 import java.net.URI ; 27 import java.net.URL ; 28 import java.nio.charset.Charset ; 29 import java.util.Map ; 30 31 37 public class ConnectionParameters { 38 private Map <String , ?> properties; 39 private String url; 40 private String user; 41 private String password; 42 private String schema; 43 private String catalog; 44 private DriverContext context; 45 46 49 protected ConnectionParameters() { 50 } 51 52 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 71 public Map <String , ?> getProperties() { 72 return properties; 73 } 74 75 82 public Object getProperty(String name) { 83 return properties.get(name); 84 } 85 86 91 public String getStringProperty(String name) { 92 Object v = properties.get(name); 93 return v == null ? null : v.toString(); 94 } 95 96 101 public Integer getIntegerProperty(String name, int defaultValue) throws ConfigurationException { 102 return getNumberProperty(name, defaultValue).intValue(); 103 } 104 105 public Integer getIntegerProperty(String name) throws ConfigurationException { 106 Number res = getNumberProperty(name, null); 107 return res == null ? null : res.intValue(); 108 } 109 110 119 public Number getNumberProperty(String name, Number defaultValue) throws ConfigurationException { 120 Object v = properties.get(name); 121 if (v == null) { 122 return defaultValue; 123 } 124 if (v instanceof Number ) { 125 return ((Number ) v); 126 } 127 String s = v.toString().trim(); 128 if (s.length() == 0) { 129 return defaultValue; 130 } 131 132 try { 134 return Long.decode(s); 135 } catch (NumberFormatException e) { 136 throw new ConfigurationException(name + " property must be integer."); 137 } 138 } 139 140 141 144 public boolean getBooleanProperty(String name) throws ConfigurationException { 145 return getBooleanProperty(name, false); 146 } 147 148 156 public boolean getBooleanProperty(String name, boolean defaultValue) throws ConfigurationException { 157 Object a = getProperty(name); 158 if (a == null) { 159 return defaultValue; 160 } 161 if (a instanceof Boolean ) { 162 return (Boolean ) a; 163 } 164 if (a instanceof Number ) { 165 return ((Number ) a).intValue() > 0; 166 } 167 String 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 188 public String getCharsetProperty(String name) throws ConfigurationException { 189 Object cs = getProperty(name); 190 if (cs == null) { 191 return null; 192 } 193 if (cs instanceof Charset ) { 194 return ((Charset ) cs).name(); 195 } 196 String 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 211 public URL getUrlProperty(String name) throws ConfigurationException { 212 Object u = getProperty(name); 213 if (u == null) { 214 return null; 215 } 216 if (u instanceof URL ) { 217 return (URL ) u; 218 } 219 try { 220 if (u instanceof URI ) { 221 URI uri = (URI ) u; 222 return uri.toURL(); 223 } 224 if (u instanceof File ) { 225 File f = (File ) u; 226 return IOUtils.toUrl(f); 227 } 228 } catch (MalformedURLException e) { 229 ExceptionUtils.ignoreThrowable(e); 230 } 232 233 try { 234 String uri = u.toString().trim(); 235 return getContext().resolve(uri); 236 } catch (MalformedURLException e) { 237 throw new ConfigurationException("Specified URL " + u + " is malformed"); 238 } 239 240 } 241 242 248 public String getUrl() { 249 return url; 250 } 251 252 257 public URL 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 e) { 264 throw new ConfigurationException("Specified connection URL " + url + " is malformed"); 265 } 266 } 267 268 273 public String getUser() { 274 return user; 275 } 276 277 282 public String getPassword() { 283 return password; 284 } 285 286 289 public String getSchema() { 290 return schema; 291 } 292 293 296 public String getCatalog() { 297 return catalog; 298 } 299 300 305 public DriverContext getContext() { 306 return context; 307 } 308 309 public String toString() { 310 return "ConnectionParameters{" + "properties=" + properties + ", url='" + url + '\'' + ", user='" + user + '\'' + ", password='" + password + '\'' + ", schema='" + schema + '\'' + ", catalog='" + catalog + '\'' + '}'; 311 } 312 } 313 | Popular Tags |