1 19 20 package org.netbeans.lib.ddl.impl; 21 22 import java.sql.Connection ; 23 import java.sql.DatabaseMetaData ; 24 import java.sql.SQLException ; 25 import java.io.InputStream ; 26 import java.text.MessageFormat ; 27 import java.util.Iterator ; 28 import java.util.HashMap ; 29 import java.util.Set ; 30 import java.util.Vector ; 31 32 import org.openide.util.NbBundle; 33 34 import org.netbeans.lib.ddl.DatabaseProductNotFoundException; 35 import org.netbeans.lib.ddl.DatabaseSpecification; 36 import org.netbeans.lib.ddl.DatabaseSpecificationFactory; 37 import org.netbeans.lib.ddl.DBConnection; 38 import org.netbeans.lib.ddl.DDLException; 39 import org.netbeans.lib.ddl.DriverSpecificationFactory; 40 41 52 public class SpecificationFactory implements DatabaseSpecificationFactory, DriverSpecificationFactory { 53 54 57 private static final String dbFile = "org/netbeans/lib/ddl/resources/dbspec.plist"; 58 59 62 private static final String drvFile = "org/netbeans/lib/ddl/resources/driverspec.plist"; 63 64 67 private HashMap dbSpecs; 68 69 72 private HashMap drvSpecs; 73 74 76 private boolean debug = false; 77 78 83 public SpecificationFactory () throws DDLException { 84 String fileDB = System.getProperty("db.specifications.file"); 85 String fileDrv = System.getProperty("driver.specifications.file"); 86 SpecificationParser parser; 87 88 try { 89 if (fileDB == null) { 90 ClassLoader cl = getClass().getClassLoader(); 91 InputStream stream = cl.getResourceAsStream(dbFile); 92 if (stream == null) { 93 String message = MessageFormat.format(NbBundle.getBundle("org.netbeans.lib.ddl.resources.Bundle").getString("EXC_UnableToOpenStream"), new String [] {dbFile}); throw new Exception (message); 95 } 96 parser = new SpecificationParser(stream); 97 dbSpecs = parser.getData(); 98 stream.close(); 99 } else { 100 parser = new SpecificationParser(fileDB); 101 dbSpecs = parser.getData(); 102 } 103 } catch (Exception e) { 104 if (fileDB != null) 105 throw new DDLException("unable to read specifications file " + fileDB + ", " + e.getMessage()); 106 else 107 throw new DDLException("unable to read default specifications file, " + e.getMessage()); 108 } 109 110 try { 111 if (fileDrv == null) { 112 ClassLoader cl = getClass().getClassLoader(); 113 InputStream stream = cl.getResourceAsStream(drvFile); 114 if (stream == null) { 115 String message = MessageFormat.format(NbBundle.getBundle("org.netbeans.lib.ddl.resources.Bundle").getString("EXC_UnableToOpenStream"), new String [] {drvFile}); throw new Exception (message); 117 } 118 parser = new SpecificationParser(stream); 119 drvSpecs = parser.getData(); 120 stream.close(); 121 } else { 122 parser = new SpecificationParser(fileDrv); 123 drvSpecs = parser.getData(); 124 } 125 } catch (Exception e) { 126 if (fileDrv != null) 127 throw new DDLException("unable to read specifications file " + fileDrv + ", " + e.getMessage()); 128 else 129 throw new DDLException("unable to read default specifications file, " + e.getMessage()); 130 } 131 } 132 133 137 public Set supportedDatabases() { 138 return dbSpecs.keySet(); 139 } 140 141 144 public boolean isDatabaseSupported(String databaseProductName) { 145 return (dbSpecs.containsKey(databaseProductName)); 146 } 147 148 154 public DatabaseSpecification createSpecification(DBConnection dbcon, Connection jdbccon) throws DatabaseProductNotFoundException, DDLException { 155 String pn = null; 156 try { 157 boolean close = (jdbccon != null ? false : true); 158 Connection con = (jdbccon != null ? jdbccon : dbcon.createJDBCConnection()); 159 DatabaseMetaData dmd = con.getMetaData(); 160 pn = dmd.getDatabaseProductName().trim(); 161 162 DatabaseSpecification spec = createSpecification(dbcon, pn, con); 163 if (close) con.close(); 164 return spec; 165 } catch (SQLException e) { 166 throw new DDLException("unable to connect to server"); 167 } catch (Exception e) { 168 throw new DatabaseProductNotFoundException(pn, "unable to create specification, "+e.getMessage()); 169 } 170 } 171 172 178 public DatabaseSpecification createSpecification(DBConnection connection, String databaseProductName, Connection c) throws DatabaseProductNotFoundException { 179 if (databaseProductName.toUpperCase().startsWith("DB2/")) databaseProductName = "DB2/"; 183 HashMap product = (HashMap ) dbSpecs.get(databaseProductName); 184 185 if (product == null) 186 throw new DatabaseProductNotFoundException(databaseProductName); 187 HashMap specmap = deepUnion(product, (HashMap ) dbSpecs.get("GenericDatabaseSystem"), true); 188 specmap.put("connection", connection); 189 DatabaseSpecification spec = new Specification(specmap, c); 190 specmap.put("dbproduct", databaseProductName); 191 spec.setSpecificationFactory(this); 192 193 return spec; 194 } 195 196 202 public DatabaseSpecification createSpecification(String databaseProductName, Connection c) throws DatabaseProductNotFoundException { 203 if (databaseProductName.toUpperCase().startsWith("DB2/")) databaseProductName = "DB2/"; 207 HashMap product = (HashMap ) dbSpecs.get(databaseProductName); 208 if (product == null) throw new DatabaseProductNotFoundException(databaseProductName); 209 HashMap specmap = deepUnion(product, (HashMap ) dbSpecs.get("GenericDatabaseSystem"), true); 210 specmap.put("dbproduct", databaseProductName); 211 return new Specification(specmap, c); 212 } 213 214 public DatabaseSpecification createSpecification(Connection c) throws DatabaseProductNotFoundException, SQLException { 215 return createSpecification(c, c.getMetaData().getDatabaseProductName().trim()); 216 } 217 218 public DatabaseSpecification createSpecification(Connection c, String databaseProductName) throws DatabaseProductNotFoundException { 219 if (databaseProductName.toUpperCase().startsWith("DB2/")) databaseProductName = "DB2/"; 223 HashMap product = (HashMap ) dbSpecs.get(databaseProductName); 224 if (product == null) throw new DatabaseProductNotFoundException(databaseProductName); 225 HashMap specmap = deepUnion(product, (HashMap ) dbSpecs.get("GenericDatabaseSystem"), true); 226 DatabaseSpecification spec = new Specification(specmap, c); 227 spec.setSpecificationFactory(this); 228 return spec; 229 } 230 231 233 public boolean isDebugMode() { 234 return debug; 235 } 236 237 239 public void setDebugMode(boolean mode) { 240 debug = mode; 241 } 242 243 247 public Set supportedDrivers() { 248 return drvSpecs.keySet(); 249 } 250 251 254 public boolean isDriverSupported(String driverName) { 255 return (drvSpecs.containsKey(driverName)); 256 } 257 258 261 public DriverSpecification createDriverSpecification(String driverName) { 262 HashMap product = (HashMap ) drvSpecs.get(driverName); 263 if (product == null) 264 product = (HashMap ) drvSpecs.get("DefaultDriver"); 265 HashMap specmap = deepUnion(product, (HashMap ) drvSpecs.get("DefaultDriver"), true); 266 DriverSpecification spec = new DriverSpecification(specmap); 267 spec.setDriverSpecificationFactory(this); 268 269 return spec; 270 } 271 272 275 private HashMap deepClone(HashMap map) { 276 HashMap newone = (HashMap )map.clone(); 277 Iterator it = newone.keySet().iterator(); 278 while (it.hasNext()) { 279 Object newkey = it.next(); 280 Object deepobj = null, newobj = newone.get(newkey); 281 if (newobj instanceof HashMap ) 282 deepobj = deepClone((HashMap )newobj); 283 else if (newobj instanceof String ) 284 deepobj = (Object )new String ((String )newobj); 285 else if (newobj instanceof Vector ) 286 deepobj = ((Vector )newobj).clone(); 287 newone.put(newkey, deepobj); 288 } 289 290 return newone; 291 } 292 293 296 private HashMap deepUnion(HashMap base, HashMap additional, boolean deep) { 297 Iterator it = additional.keySet().iterator(); 298 while (it.hasNext()) { 299 Object addkey = it.next(); 300 Object addobj = additional.get(addkey); 301 302 if (addkey.equals("TypeMap")) 304 continue; 305 306 if (base.containsKey(addkey)) { 307 Object baseobj = base.get(addkey); 308 if (deep && (baseobj instanceof HashMap ) && (addobj instanceof HashMap )) { 309 deepUnion((HashMap )baseobj, (HashMap )addobj, deep); 310 } 311 } else { 312 if (addobj instanceof HashMap ) 313 addobj = deepClone((HashMap )addobj); 314 else if (addobj instanceof String ) 315 addobj = (Object )new String ((String )addobj); 316 else if (addobj instanceof Vector ) 317 addobj = ((Vector )addobj).clone(); 318 base.put(addkey, addobj); 319 } 320 } 321 322 return base; 323 } 324 325 } 326 | Popular Tags |