1 19 20 package org.netbeans.modules.derby.api; 21 22 import java.io.File ; 23 import java.io.IOException ; 24 import java.net.MalformedURLException ; 25 import java.net.URL ; 26 import java.sql.Connection ; 27 import java.sql.Driver ; 28 import java.sql.PreparedStatement ; 29 import java.sql.SQLException ; 30 import java.util.Properties ; 31 import org.netbeans.api.db.explorer.ConnectionManager; 32 import org.netbeans.api.db.explorer.DatabaseConnection; 33 import org.netbeans.api.db.explorer.DatabaseException; 34 import org.netbeans.api.db.explorer.JDBCDriver; 35 import org.netbeans.api.db.explorer.JDBCDriverManager; 36 import org.netbeans.modules.derby.DbURLClassLoader; 37 import org.netbeans.modules.derby.DerbyOptions; 38 import org.netbeans.modules.derby.Util; 39 import org.netbeans.modules.derby.RegisterDerby; 40 import org.netbeans.modules.derby.spi.support.DerbySupport; 41 import org.openide.filesystems.FileObject; 42 import org.openide.filesystems.FileUtil; 43 import org.openide.modules.InstalledFileLocator; 44 45 51 public final class DerbyDatabases { 52 53 private DerbyDatabases() { 54 } 55 56 62 public static boolean isDerbyRegistered() { 63 return DerbySupport.getLocation().length() > 0 && DerbySupport.getSystemHome().length() > 0; } 65 66 71 public static File getSystemHome() { 72 String systemHome = DerbyOptions.getDefault().getSystemHome(); 73 if (systemHome.length() >= 0) { 74 return new File (systemHome); 75 } 76 return null; 77 } 78 79 86 public static boolean databaseExists(String databaseName) { 87 if (databaseName == null) { 88 throw new NullPointerException ("The databaseName parameter cannot be null"); } 90 if ("".equals(databaseName)) { return false; 93 } 94 95 String systemHome = DerbySupport.getSystemHome(); 96 if (systemHome.length() <= 0) { return false; 98 } 99 File databaseFile = new File (systemHome, databaseName); 100 return databaseFile.exists(); 101 } 102 103 114 public static String getFirstFreeDatabaseName(String baseDatabaseName) { 115 if (baseDatabaseName == null) { 116 throw new NullPointerException ("The baseDatabaseName parameter cannot be null"); } 118 119 String systemHome = DerbySupport.getSystemHome(); 120 if (systemHome.length() <= 0) { return baseDatabaseName; 122 } 123 File databaseFile = new File (systemHome, baseDatabaseName); 124 if (!databaseFile.exists()) { 125 return baseDatabaseName; 126 } 127 128 int i = 1; 129 while (i <= Integer.MAX_VALUE) { 130 String databaseName = baseDatabaseName + String.valueOf(i); 131 databaseFile = new File (systemHome, databaseName); 132 if (!databaseFile.exists()) { 133 return databaseName; 134 } 135 i++; 136 } 137 return null; 138 } 139 140 149 public static int getFirstIllegalCharacter(String databaseName) { 150 if (databaseName == null) { 151 throw new NullPointerException ("The databaseName parameter cannot be null"); } 153 154 for (int i = 0; i < databaseName.length(); i++) { 155 char ch = databaseName.charAt(i); 156 if (ch == '/') { 157 return (int)ch; 158 } 159 if (ch == File.separatorChar) { 160 return (int)ch; 161 } 162 } 163 164 return -1; 165 } 166 167 191 public static DatabaseConnection createDatabase(String databaseName, String user, String password) throws DatabaseException, IOException , IllegalStateException { 192 if (databaseName == null) { 193 throw new NullPointerException ("The databaseName parameter cannot be null"); } 195 196 ensureSystemHome(); 197 RegisterDerby.getDefault().ensureStarted(); 198 199 Driver driver = loadDerbyNetDriver(); 200 Properties props = new Properties (); 201 boolean setupAuthentication = (user != null && user.length() >= 0); 202 203 try { 204 String url = "jdbc:derby://localhost:" + RegisterDerby.getDefault().getPort() + "/" + databaseName; String urlForCreation = url + ";create=true"; Connection connection = driver.connect(urlForCreation, props); 207 208 209 try { 210 if (setupAuthentication) { 211 setupDatabaseAuthentication(connection, user, password); 212 } 213 } finally { 214 connection.close(); 215 } 216 217 if (setupAuthentication) { 218 try { 221 connection = driver.connect(url + ";shutdown=true", props); } catch (SQLException e) { 223 } 225 } 226 } catch (SQLException sqle) { 227 DatabaseException dbe = new DatabaseException(sqle.getMessage()); 228 dbe.initCause(sqle); 229 throw dbe; 230 } 231 232 return registerDatabase(databaseName, user, 233 setupAuthentication ? user.toUpperCase() : "APP", setupAuthentication ? password : null, setupAuthentication); 235 } 236 237 256 public static DatabaseConnection createSampleDatabase() throws DatabaseException, IOException , IllegalStateException { 257 extractSampleDatabase("sample"); return registerDatabase("sample", "app", "APP", "app", true); } 260 261 281 public static DatabaseConnection createSampleDatabase(String databaseName) throws DatabaseException, IOException { 282 if (databaseName == null) { 283 throw new NullPointerException ("The databaseName parameter cannot be null"); } 285 286 extractSampleDatabase(databaseName); 287 return registerDatabase(databaseName, "app", "APP", "app", true); } 289 290 296 static void extractSampleDatabase(String databaseName) throws IOException { 297 File systemHomeFile = ensureSystemHome(); 298 File sourceFO = InstalledFileLocator.getDefault().locate("modules/ext/derbysampledb.zip", null, false); FileObject systemHomeFO = FileUtil.toFileObject(systemHomeFile); 300 FileObject sampleFO = systemHomeFO.getFileObject(databaseName); 301 if (sampleFO == null) { 302 sampleFO = systemHomeFO.createFolder(databaseName); 303 Util.extractZip(sourceFO, sampleFO); 304 } 305 } 306 307 310 private static File ensureSystemHome() throws IOException { 311 String systemHome = DerbySupport.getSystemHome(); 312 boolean noSystemHome = false; 313 if (systemHome.length() <= 0) { noSystemHome = true; 315 systemHome = DerbySupport.getDefaultSystemHome(); 316 } 317 File systemHomeFile = new File (systemHome); 318 if (!systemHomeFile.exists()){ 319 if (!systemHomeFile.mkdirs()) { 320 throw new IOException ("Could not create the derby.system.home directory"); } 322 } 323 if (noSystemHome) { 324 DerbySupport.setSystemHome(systemHome); 325 } 326 return systemHomeFile; 327 } 328 329 333 private static DatabaseConnection registerDatabase(String databaseName, String user, String schema, String password, boolean rememberPassword) throws DatabaseException { 334 JDBCDriver drivers[] = JDBCDriverManager.getDefault().getDrivers(DerbyOptions.DRIVER_CLASS_NET); 335 if (drivers.length == 0) { 336 throw new IllegalStateException ("The " + DerbyOptions.DRIVER_DISP_NAME_NET + " driver was not found"); } 338 DatabaseConnection dbconn = DatabaseConnection.create(drivers[0], "jdbc:derby://localhost:" + RegisterDerby.getDefault().getPort() + "/" + databaseName, user, schema, password, rememberPassword); ConnectionManager.getDefault().addConnection(dbconn); 340 return dbconn; 341 } 342 343 347 private static void setupDatabaseAuthentication(Connection conn, String user, String password) throws SQLException { 348 PreparedStatement stmt = conn.prepareStatement("{call SYSCS_UTIL.SYSCS_SET_DATABASE_PROPERTY(?, ?)}"); try { 350 stmt.setString(1, "derby.connection.requireAuthentication"); stmt.setString(2, "true"); stmt.execute(); 353 354 stmt.clearParameters(); 355 stmt.setString(1, "derby.authentication.provider"); stmt.setString(2, "BUILTIN"); stmt.execute(); 358 359 stmt.clearParameters(); 360 stmt.setString(1, "derby.user." + user); stmt.setString(2, password); stmt.execute(); 363 } finally { 364 stmt.close(); 365 } 366 } 367 368 371 private static Driver loadDerbyNetDriver() throws DatabaseException, IllegalStateException { 372 Exception exception = null; 373 try { 374 File derbyClient = Util.getDerbyFile("lib/derbyclient.jar"); if (derbyClient == null || !derbyClient.exists()) { 376 throw new IllegalStateException ("The " + DerbyOptions.DRIVER_DISP_NAME_NET + " driver was not found"); } 378 URL [] driverURLs = new URL [] { derbyClient.toURI().toURL() }; DbURLClassLoader l = new DbURLClassLoader(driverURLs); 380 Class c = Class.forName(DerbyOptions.DRIVER_CLASS_NET, true, l); 381 return (Driver )c.newInstance(); 382 } catch (MalformedURLException e) { 383 exception = e; 384 } catch (IllegalAccessException e) { 385 exception = e; 386 } catch (ClassNotFoundException e) { 387 exception = e; 388 } catch (InstantiationException e) { 389 exception = e; 390 } 391 if (exception != null) { 392 DatabaseException dbe = new DatabaseException(exception.getMessage()); 393 dbe.initCause(exception); 394 throw dbe; 395 } 396 return null; 398 } 399 } 400 | Popular Tags |