1 19 20 package org.netbeans.modules.j2ee.sun.ide.j2ee.db; 21 22 import java.net.MalformedURLException ; 23 import java.net.URL ; 24 25 import java.io.File ; 26 import java.io.FileInputStream ; 27 import java.io.FileOutputStream ; 28 import java.io.IOException ; 29 import java.io.InputStream ; 30 import java.io.PrintStream ; 31 import java.io.BufferedInputStream ; 32 import java.io.BufferedOutputStream ; 33 import java.io.BufferedWriter ; 34 import java.io.OutputStreamWriter ; 35 import java.io.Writer ; 36 import java.util.MissingResourceException ; 37 import java.util.Properties ; 38 import java.util.zip.ZipEntry ; 39 import java.util.zip.ZipInputStream ; 40 import org.netbeans.api.db.explorer.ConnectionManager; 41 import org.netbeans.api.db.explorer.DatabaseConnection; 42 import org.netbeans.api.db.explorer.DatabaseException; 43 import org.netbeans.api.db.explorer.JDBCDriver; 44 import org.netbeans.api.db.explorer.JDBCDriverManager ; 45 import org.netbeans.modules.derby.api.DerbyDatabases; 46 import org.netbeans.modules.j2ee.deployment.plugins.api.InstanceProperties; 47 import org.netbeans.modules.j2ee.sun.api.SunURIManager; 48 import org.netbeans.spi.db.explorer.DatabaseRuntime; 49 import org.openide.ErrorManager; 50 import org.openide.filesystems.FileObject; 51 import org.openide.filesystems.FileSystem; 52 import org.openide.filesystems.Repository; 53 import org.openide.util.NbBundle; 54 import org.openide.filesystems.FileUtil; 55 import org.netbeans.modules.j2ee.sun.ide.j2ee.ui.Util; 56 import org.netbeans.modules.derby.spi.support.DerbySupport; 57 import org.netbeans.modules.j2ee.sun.api.ServerLocationManager; 58 import org.openide.filesystems.FileLock; 59 import org.openide.filesystems.FileStateInvalidException; 60 import org.openide.util.Lookup; 61 import org.openide.util.RequestProcessor; 62 66 public class RegisterPointbase implements DatabaseRuntime { 67 68 public static final String DRIVER_DISPLAY_NAME = 69 NbBundle.getMessage(RegisterPointbase.class, "LBL_DriverName"); 71 public static final String DRIVER_NAME = "pointbase"; 73 74 public static final String DRIVER = 75 "com.pointbase.jdbc.jdbcUniversalDriver"; 77 78 public static final String USER_NAME = "pbpublic"; 80 81 public static final String SCHEMA_NAME = "PBPUBLIC"; 83 84 public static final String PASSWORD = "pbpublic"; 86 private static final String RELATIVE_DRIVER_PATH = 87 "/pointbase/lib/pbembedded.jar"; 89 private static RegisterPointbase reg = null; 90 91 92 private static Process process = null; 93 94 private File appServerInstallationDirectory = null; 95 96 97 98 private RegisterPointbase() { 99 } 100 101 public static RegisterPointbase getDefault(){ 102 if (reg==null) { 103 reg= new RegisterPointbase(); 104 } 105 return reg; 106 } 107 private static void copyFile(File file1, File file2) throws IOException { 108 FileInputStream fis = null; 109 BufferedInputStream bis = null; 110 FileOutputStream fos = null; 111 BufferedOutputStream bos = null; 112 try { 113 fis = new FileInputStream (file1); 114 bis = new BufferedInputStream (fis); 115 fos = new FileOutputStream (file2); 116 bos = new BufferedOutputStream (fos); 117 int b; 118 while((b=bis.read())!=-1)bos.write(b); 119 } finally { 120 if (null != bis) { 121 try { 122 bis.close(); 123 } catch (IOException ioe) { 124 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ioe); 125 } 126 } 127 if (null != bos) { 128 try { 129 bos.close(); 130 } catch (IOException ioe) { 131 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ioe); 132 } 133 } 134 if (null != fis) { 135 try { 136 fis.close(); 137 } catch (IOException ioe) { 138 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ioe); 139 } 140 } 141 if (null != fos) { 142 try { 143 fos.close(); 144 } catch (IOException ioe) { 145 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ioe); 146 } 147 } 148 } 149 } 150 private static void unzip(InputStream source, File targetFolder) throws IOException { 151 ZipInputStream zip=new ZipInputStream (source); 153 try { 154 ZipEntry ent; 155 while ((ent = zip.getNextEntry()) != null) { 156 File f = new File (targetFolder, ent.getName()); 157 if (ent.isDirectory()) { 158 f.mkdirs(); 159 } else { 160 f.getParentFile().mkdirs(); 161 FileOutputStream out = new FileOutputStream (f); 162 try { 163 FileUtil.copy(zip, out); 164 } finally { 165 out.close(); 166 } 167 } 168 } 169 } finally { 170 zip.close(); 171 } 172 } 173 private void createLocalInstallation(){ 174 175 String installRoot = appServerInstallationDirectory.getAbsolutePath(); 176 String dest = System.getProperty("netbeans.user"); 177 try { 178 unzip(this.getClass().getClassLoader().getResourceAsStream("org/netbeans/modules/j2ee/sun/ide/j2ee/db/pointbasescripts.zip") , new File (dest)); 179 copyFile(new File (installRoot+"/pointbase/databases/sample.dbn"), new File (dest+"/pointbase/databases/sample.dbn")); 180 copyFile(new File (installRoot+"/pointbase/databases/sun-appserv-samples.dbn"), new File (dest+"/pointbase/databases/sun-appserv-samples.dbn")); 181 try { 182 copyFile(new File (installRoot+"/pointbase/databases/sample$2.wal"), new File (dest+"/pointbase/databases/sample$2.wal")); 183 copyFile(new File (installRoot+"/pointbase/databases/sun-appserv-samples$2.wal"), new File (dest+"/pointbase/databases/sun-appserv-samples$2.wal")); 184 } catch(java.io.FileNotFoundException e){ try { 187 copyFile(new File (installRoot+"/pointbase/databases/sample$1.wal"), new File (dest+"/pointbase/databases/sample$1.wal")); 188 copyFile(new File (installRoot+"/pointbase/databases/sun-appserv-samples$1.wal"), new File (dest+"/pointbase/databases/sun-appserv-samples$1.wal")); 189 } catch(java.io.FileNotFoundException ee){ 190 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ee); 191 } 192 } 193 194 195 BufferedOutputStream bos = new BufferedOutputStream (new FileOutputStream (dest+"/pointbase/tools/serveroption/pbenv.bat")); 196 PrintStream ps = new PrintStream (bos); 197 ps.println("set AS_POINTBASE="+installRoot+"\\pointbase"); 198 ps.println("set AS_POINTBASE_SAMPLESDB="+dest+"\\pointbase"); 199 ps.println("set PB_CONFIGURED_JAVA_HOME="+System.getProperty("java.home")); 200 ps.println("exit /b 0"); 201 ps.close(); 202 BufferedOutputStream bos2 = new BufferedOutputStream (new FileOutputStream (dest+"/pointbase/tools/serveroption/pbenv.conf")); 203 PrintStream ps2 = new PrintStream (bos2); 204 205 ps2.println("AS_POINTBASE="+installRoot+"/pointbase"); 206 ps2.println("AS_POINTBASE_SAMPLESDB="+dest+"/pointbase"); 207 ps2.println("PB_CONFIGURED_JAVA_HOME="+System.getProperty("java.home")); 208 ps2.close(); 209 210 if (File.separator.equals("/")) { String cmd[] ={"chmod","+x",dest+"/pointbase/tools/serveroption/startserver.sh"}; 212 213 Runtime.getRuntime().exec(cmd ); 214 cmd[2]=dest+"/pointbase/tools/serveroption/startconsole.sh"; 215 Runtime.getRuntime().exec(cmd ); 216 cmd[2]=dest+"/pointbase/tools/serveroption/startconsole.sh"; 217 Runtime.getRuntime().exec(cmd ); 218 cmd[2]=dest+"/pointbase/tools/serveroption/stopserver.sh"; 219 Runtime.getRuntime().exec(cmd ); 220 cmd[2]=dest+"/pointbase/tools/serveroption/pbenv.conf"; 221 Runtime.getRuntime().exec(cmd ); 222 } 223 } catch (IOException ex) { 224 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); 225 } 226 } 227 228 public void register(File irf){ 229 if (null != irf && irf.exists()) { 230 String installRoot = irf.getAbsolutePath(); 231 if (installRoot!=null) { 232 233 final FileSystem fs = Repository.getDefault().getDefaultFileSystem(); 234 235 File derbyInstall = new File (irf,"derby"); if (!derbyInstall.exists()){ 237 derbyInstall = new File (irf,"javadb"); } 239 if (ServerLocationManager.isGlassFish(irf)) { 242 registerDerbyLibrary(derbyInstall); 243 } 244 if (derbyInstall.exists()){ 245 final FileObject derb = fs.findResource("Databases/JDBCDrivers/org_apache_derby_jdbc_ClientDriver.xml"); final File installDir = derbyInstall; 247 RequestProcessor.getDefault().post(new ConfigureJavaDBSamples(installDir,derb)); 249 } 250 251 File localInstall = new File (irf,"pointbase"); 253 if (localInstall.exists()){ 254 configureForPointbaseSamples(installRoot, irf, localInstall, fs); 255 } } 257 } 258 } 259 260 static class ConfigureJavaDBSamples implements Runnable { 261 262 private Object derb; 263 264 private File installDir; 265 266 ConfigureJavaDBSamples(File installDir, Object derb) { 267 this.installDir = installDir; 268 this.derb = derb; 269 } 270 271 public void run() { 272 try { 273 if ("".equals(DerbySupport.getLocation())) { 274 DerbySupport.setLocation(installDir.getAbsolutePath()); 275 } else if (null == derb) { 276 DerbySupport.setLocation(installDir.getAbsolutePath()); 279 } 280 if ("".equals(DerbySupport.getSystemHome())) { 281 File dbdir = new File (DerbySupport.getDefaultSystemHome()); 282 if (dbdir.exists()==false){ 283 dbdir.mkdirs(); 284 } 285 DerbySupport.setSystemHome(dbdir.getAbsolutePath()); 286 } 287 288 DerbyDatabases.createSampleDatabase(); 290 } catch (DatabaseException ex) { 291 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); 292 } catch (IOException ex) { 293 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); 294 } 295 } 296 } 297 298 private void configureForPointbaseSamples(final String installRoot, final File irf, final File localInstall, final FileSystem fs) { 299 appServerInstallationDirectory =irf; 300 AddPointBaseMenus.execute(); 301 302 FileObject props = fs.findResource("Databases/JDBCDrivers/com_pointbase_jdbc_jdbcUniversalDriver.xml"); 303 if (props==null) { 304 File dbFile = new File (installRoot+"/pointbase/databases/sample.dbn"); if (dbFile.exists() && (dbFile.canWrite()==false)) { 308 createLocalInstallation(); 310 } 311 try { 312 String driverName = installRoot + RELATIVE_DRIVER_PATH; 313 File f = new File (driverName); 314 if(f.exists()){ 315 File dbDir = new File (localInstall,"databases"); int portVal = getPort(dbDir); 317 318 URL [] urls = new URL [1]; 319 urls[0]= f.toURI().toURL(); 321 JDBCDriver newDriver = JDBCDriver.create(DRIVER_NAME, DRIVER_DISPLAY_NAME, DRIVER,urls); 322 JDBCDriverManager.getDefault().addDriver(newDriver); 323 324 325 File testFile = new File (dbDir,SAMPLE_NAME+DOT_DBN); 326 if (testFile.exists()) { 327 DatabaseConnection dbconn = DatabaseConnection.create(newDriver, 328 LOCALHOST_URL_PREFIX+portVal+SLASH+SAMPLE_NAME, 329 USER_NAME, SCHEMA_NAME, PASSWORD, true); 330 ConnectionManager.getDefault().addConnection(dbconn); 331 } 332 333 testFile = new File (dbDir,SUN_APPSERV_SAMPLES_NAME+DOT_DBN); 334 if (testFile.exists()) { 335 DatabaseConnection dbconn2 = DatabaseConnection.create(newDriver, 336 LOCALHOST_URL_PREFIX+portVal+SLASH+SUN_APPSERV_SAMPLES_NAME, 337 USER_NAME, SCHEMA_NAME, PASSWORD, true); 338 ConnectionManager.getDefault().addConnection(dbconn2); 339 } 340 } 341 } catch (MalformedURLException ex) { 342 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); 343 } catch (IOException ex) { 344 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); 345 } catch (DatabaseException ex) { 346 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); 347 } catch (RuntimeException ex) { 348 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); 349 } 350 } 351 } 352 353 public String getJDBCDriverClass() { 354 return DRIVER; 355 } 356 357 360 public boolean acceptsDatabaseURL(String url){ 361 return url.startsWith(LOCALHOST_URL_PREFIX); 362 } 363 364 public boolean isRegisterable() { 365 boolean retVal = false; 366 if (null == appServerInstallationDirectory) { 367 String instances[] = InstanceProperties.getInstanceList(); 369 if (null != instances) { 370 for (int i = 0; i < instances.length; i++) { 371 int end = instances[i].indexOf(']'); 372 if (instances[i].indexOf(SunURIManager.SUNSERVERSURI) > -1 && 373 end > -1) { 374 File irf = new File (instances[i].substring(1,end)); 375 register(irf); 376 break; 377 } 378 } 379 } 380 } 381 if (null != appServerInstallationDirectory && appServerInstallationDirectory.exists()) { 382 retVal = true; 383 } 384 return retVal; 385 } 386 387 390 public boolean isRunning(){ 391 if (process!=null){ 392 try{ 393 process.exitValue(); 394 process=null; 395 } catch (IllegalThreadStateException e){ 396 } 399 } 400 return (process!=null); 401 402 } 403 404 407 public boolean canStart(){ 408 410 return System.getProperty("com.sun.aas.installRoot") != null; 412 } 413 414 417 public void start(){ 418 start(5000); } 420 424 public File getScriptsLocation(){ 425 File irf = appServerInstallationDirectory; 426 File localInstall = null; 427 if (null != irf && irf.exists()) { 428 String installRoot = irf.getAbsolutePath(); 429 if (installRoot != null) { 430 localInstall = new File (System.getProperty("netbeans.user")+"/pointbase/tools/serveroption"); 431 if (!localInstall.exists()){ 432 localInstall = new File (installRoot+ "/pointbase/tools/serveroption"); 433 } 434 } else { 435 Util.showInformation(NbBundle.getMessage(StartAction.class, "ERR_NotThere")); 436 } 437 } 438 return localInstall; 439 } 440 441 444 public void start(int waitTime){ 445 if (process!=null){ stop(); 447 } 448 String suffix; 449 if (File.separator.equals("\\")) { suffix = ".bat"; } else { 452 suffix = ".sh"; } 454 File loc = getScriptsLocation(); 455 if (loc==null){ 456 return; } 458 String script = null; if (File.separator.equals("\\")) { script = loc.getAbsolutePath() +"\\startserver"; } else { 462 script = loc.getAbsolutePath() +"/startserver"; } 464 try { 465 ExecSupport ee= new ExecSupport(); 466 process= Runtime.getRuntime().exec(script + suffix,null,loc ); 467 ee.displayProcessOutputs(process,NbBundle.getMessage(StartAction.class, "LBL_outputtab"),""); 468 if (waitTime>0){ 469 Thread.sleep(waitTime); } 471 } catch (MissingResourceException ex) { 472 Util.showInformation(ex.getLocalizedMessage()); 473 } catch (IOException ex) { 474 Util.showInformation(ex.getLocalizedMessage()); 475 } catch (InterruptedException ex) { 476 Util.showInformation(ex.getLocalizedMessage()); 477 } catch (RuntimeException ex) { 478 Util.showInformation(ex.getLocalizedMessage()); 479 } 480 } 481 482 483 484 485 488 public void stop(){ 489 BufferedWriter processIn = null; 490 try { 491 if (process!=null) { processIn = new BufferedWriter (new OutputStreamWriter (process.getOutputStream())); 493 processIn.write("q\ny\n"); 494 processIn.flush(); 495 process.destroy(); 496 process=null; 497 } 498 499 } catch (IOException e) { 500 Util.showInformation(e.getMessage()); 501 process=null; 502 } catch (RuntimeException ex) { 503 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL,ex); 504 Util.showInformation(ex.getMessage()); 505 process=null; 506 } finally { 507 if (null != processIn) { 508 try { 509 processIn.close(); 510 } catch (IOException ioe) { 511 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, 512 ioe); 513 } 514 } 515 } 516 } 517 518 private static final String POINTBASE_URL_PREFIX = "jdbc:pointbase:"; private static final String LOCALHOST_URL_PREFIX = 520 POINTBASE_URL_PREFIX + "//localhost:"; private static final String SAMPLE_NAME = "sample"; private static final String SUN_APPSERV_SAMPLES_NAME = 523 "sun-appserv-samples"; private static final String DOT_DBN = ".dbn"; private static final String SLASH = "/"; 527 private int getPort(File databaseDir) throws IOException { 528 File iniFile = new File (databaseDir,"pointbase.ini"); 530 int port = 9092; 532 Properties iniProps = new Properties (); 533 iniProps.load(new FileInputStream (iniFile)); 534 port = Integer.parseInt(iniProps.getProperty("server.port", "9092")); return port; 536 } 537 540 private void registerDerbyLibrary(final File location) { final Repository rep = (Repository) Lookup.getDefault().lookup(Repository.class); 542 final FileObject libsFolder = rep.getDefaultFileSystem().findResource("/org-netbeans-api-project-libraries/Libraries"); if (libsFolder!=null){ 544 try { 545 libsFolder.getFileSystem().runAtomicAction( 546 new DerbyLibraryRegistrar(location,libsFolder)); 547 } catch (FileStateInvalidException ex) { 548 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); 549 } catch (IOException ex) { 550 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex); 551 } 552 } 553 } 554 555 static class DerbyLibraryRegistrar implements FileSystem.AtomicAction { 556 557 private File location; 558 559 private FileObject libsFolder; 560 561 DerbyLibraryRegistrar(File location, FileObject libsFolder) { 562 this.location = location; 563 this.libsFolder = libsFolder; 564 } 565 566 public void run() throws IOException { 567 FileLock ld = null; 568 java.io.OutputStream outStreamd = null; 569 Writer outd = null; 570 OutputStreamWriter osw = null; 571 try { 572 FileObject derbyLib =null; 574 derbyLib = libsFolder.getFileObject("JavaDB" ,"xml"); if (null == derbyLib) { 576 derbyLib = libsFolder.createData("JavaDB" ,"xml"); ld = derbyLib.lock(); 578 outStreamd = derbyLib.getOutputStream(ld); 579 osw = new OutputStreamWriter (outStreamd); 580 outd = new BufferedWriter (osw); 581 outd.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<!DOCTYPE library PUBLIC \"-//NetBeans//DTD Library Declaration 1.0//EN\" \"http://www.netbeans.org/dtds/library-declaration-1_0.dtd\">\n"); outd.write("<library version=\"1.0\">\n<name>JAVADB_DRIVER_LABEL</name>\n"); outd.write("<type>j2se</type>\n"); outd.write("<localizing-bundle>org.netbeans.modules.j2ee.sun.ide.j2ee.db.Bundle</localizing-bundle>\n"); outd.write("<volume>\n<type>classpath</type>\n"); outd.write("<resource>jar:"+new File (location.getAbsolutePath()+"/lib/derby.jar").toURI().toURL()+"!/</resource>\n"); outd.write("<resource>jar:"+new File (location.getAbsolutePath()+"/lib/derbyclient.jar").toURI().toURL()+"!/</resource>\n"); outd.write("<resource>jar:"+new File (location.getAbsolutePath()+"/lib/derbynet.jar").toURI().toURL()+"!/</resource>\n"); 590 591 outd.write("</volume>\n<volume>\n<type>src</type>\n</volume>\n"); outd.write("<volume>\n<type>javadoc</type>\n"); outd.write("</volume>\n</library>"); } 595 } finally { 596 if (null != outd) { 597 try { 598 outd.close(); 599 } catch (IOException ioe) { 600 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ioe); 601 } 602 } 603 if (null != outStreamd) { 604 try { 605 outStreamd.close(); 606 } catch (IOException ioe) { 607 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ioe); 608 } 609 } 610 if (null != ld) { 611 ld.releaseLock(); 612 } 613 } 614 615 } 616 } 617 } 618 619 | Popular Tags |