KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > sun > ide > j2ee > db > RegisterPointbase


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.j2ee.sun.ide.j2ee.db;
21
22 import java.net.MalformedURLException JavaDoc;
23 import java.net.URL JavaDoc;
24
25 import java.io.File JavaDoc;
26 import java.io.FileInputStream JavaDoc;
27 import java.io.FileOutputStream JavaDoc;
28 import java.io.IOException JavaDoc;
29 import java.io.InputStream JavaDoc;
30 import java.io.PrintStream JavaDoc;
31 import java.io.BufferedInputStream JavaDoc;
32 import java.io.BufferedOutputStream JavaDoc;
33 import java.io.BufferedWriter JavaDoc;
34 import java.io.OutputStreamWriter JavaDoc;
35 import java.io.Writer JavaDoc;
36 import java.util.MissingResourceException JavaDoc;
37 import java.util.Properties JavaDoc;
38 import java.util.zip.ZipEntry JavaDoc;
39 import java.util.zip.ZipInputStream JavaDoc;
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 /**
63  *
64  * @author ludo
65  */

66 public class RegisterPointbase implements DatabaseRuntime {
67     /** The name of the Pointbase driver to create the connection to sample database */
68     public static final String JavaDoc DRIVER_DISPLAY_NAME =
69             NbBundle.getMessage(RegisterPointbase.class, "LBL_DriverName"); //NOI18N
70

71     public static final String JavaDoc DRIVER_NAME = "pointbase"; // NOI18N
72

73     /** The driver to create the connection to sample database */
74     public static final String JavaDoc DRIVER =
75             "com.pointbase.jdbc.jdbcUniversalDriver"; //NOI18N
76

77     /** The user name to create the connection to sample database */
78     public static final String JavaDoc USER_NAME = "pbpublic"; //NOI18N
79

80     /** The schema name to create the connection to sample database */
81     public static final String JavaDoc SCHEMA_NAME = "PBPUBLIC"; //NOI18N
82

83     /** The password to create the connection to sample database */
84     public static final String JavaDoc PASSWORD = "pbpublic"; //NOI18N
85

86     private static final String JavaDoc RELATIVE_DRIVER_PATH =
87             "/pointbase/lib/pbembedded.jar"; //NOI18N
88

89     private static RegisterPointbase reg = null;
90     
91     /** pointbase server process */
92     private static Process JavaDoc process = null;
93     
94     private File JavaDoc appServerInstallationDirectory = null;
95     
96     
97     /** Creates a new instance of RegisterPointbase */
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 JavaDoc file1, File JavaDoc file2) throws IOException JavaDoc {
108         FileInputStream JavaDoc fis = null;
109         BufferedInputStream JavaDoc bis = null;
110         FileOutputStream JavaDoc fos = null;
111         BufferedOutputStream JavaDoc bos = null;
112         try {
113             fis = new FileInputStream JavaDoc(file1);
114             bis = new BufferedInputStream JavaDoc(fis);
115             fos = new FileOutputStream JavaDoc(file2);
116             bos = new BufferedOutputStream JavaDoc(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 JavaDoc ioe) {
124                     ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ioe);
125                 }
126             }
127             if (null != bos) {
128                 try {
129                     bos.close();
130                 } catch (IOException JavaDoc ioe) {
131                     ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ioe);
132                 }
133             }
134             if (null != fis) {
135                 try {
136                     fis.close();
137                 } catch (IOException JavaDoc ioe) {
138                     ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ioe);
139                 }
140             }
141             if (null != fos) {
142                 try {
143                     fos.close();
144                 } catch (IOException JavaDoc ioe) {
145                     ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ioe);
146                 }
147             }
148         }
149     }
150     private static void unzip(InputStream JavaDoc source, File JavaDoc targetFolder) throws IOException JavaDoc {
151         //installation
152
ZipInputStream JavaDoc zip=new ZipInputStream JavaDoc(source);
153         try {
154             ZipEntry JavaDoc ent;
155             while ((ent = zip.getNextEntry()) != null) {
156                 File JavaDoc f = new File JavaDoc(targetFolder, ent.getName());
157                 if (ent.isDirectory()) {
158                     f.mkdirs();
159                 } else {
160                     f.getParentFile().mkdirs();
161                     FileOutputStream JavaDoc out = new FileOutputStream JavaDoc(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 JavaDoc installRoot = appServerInstallationDirectory.getAbsolutePath();
176         String JavaDoc 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 JavaDoc(dest));
179             copyFile(new File JavaDoc(installRoot+"/pointbase/databases/sample.dbn"), new File JavaDoc(dest+"/pointbase/databases/sample.dbn"));
180             copyFile(new File JavaDoc(installRoot+"/pointbase/databases/sun-appserv-samples.dbn"), new File JavaDoc(dest+"/pointbase/databases/sun-appserv-samples.dbn"));
181             try {
182                 copyFile(new File JavaDoc(installRoot+"/pointbase/databases/sample$2.wal"), new File JavaDoc(dest+"/pointbase/databases/sample$2.wal"));
183                 copyFile(new File JavaDoc(installRoot+"/pointbase/databases/sun-appserv-samples$2.wal"), new File JavaDoc(dest+"/pointbase/databases/sun-appserv-samples$2.wal"));
184             } catch(java.io.FileNotFoundException JavaDoc e){// UR 1 is different than UR2 there see bug 6309618
185
//continue the logic.
186
try {
187                     copyFile(new File JavaDoc(installRoot+"/pointbase/databases/sample$1.wal"), new File JavaDoc(dest+"/pointbase/databases/sample$1.wal"));
188                     copyFile(new File JavaDoc(installRoot+"/pointbase/databases/sun-appserv-samples$1.wal"), new File JavaDoc(dest+"/pointbase/databases/sun-appserv-samples$1.wal"));
189                 } catch(java.io.FileNotFoundException JavaDoc ee){
190                     ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ee);
191                 }
192             }
193             
194             
195             BufferedOutputStream JavaDoc bos = new BufferedOutputStream JavaDoc(new FileOutputStream JavaDoc(dest+"/pointbase/tools/serveroption/pbenv.bat"));
196             PrintStream JavaDoc ps = new PrintStream JavaDoc(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 JavaDoc bos2 = new BufferedOutputStream JavaDoc(new FileOutputStream JavaDoc(dest+"/pointbase/tools/serveroption/pbenv.conf"));
203             PrintStream JavaDoc ps2 = new PrintStream JavaDoc(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("/")) { // NOI18N now change with the execute flag on Unix!!!
211
String JavaDoc 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 JavaDoc ex) {
224             ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex);
225         }
226     }
227     
228     public void register(File JavaDoc irf){
229         if (null != irf && irf.exists()) {
230             String JavaDoc installRoot = irf.getAbsolutePath();
231             if (installRoot!=null) {
232                 
233                 final FileSystem fs = Repository.getDefault().getDefaultFileSystem();
234                 
235                 File JavaDoc derbyInstall = new File JavaDoc(irf,"derby");//NOI18N
236
if (!derbyInstall.exists()){
237                     derbyInstall = new File JavaDoc(irf,"javadb");//NOI18N for latest Glassfish
238
}
239                 // help support JPA use in J2SE projects, where the user will
240
// need this library... [hence GlassFish only]
241
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"); //NOI18N
246
final File JavaDoc installDir = derbyInstall;
247                     // create sample db if things are not initialized correctly
248
RequestProcessor.getDefault().post(new ConfigureJavaDBSamples(installDir,derb));
249                 }
250                                 
251                 File JavaDoc localInstall = new File JavaDoc(irf,"pointbase"); //NOI18N
252

253                 if (localInstall.exists()){
254                     configureForPointbaseSamples(installRoot, irf, localInstall, fs);
255                 } // stop here
256
}
257         }
258     }
259     
260     static class ConfigureJavaDBSamples implements Runnable JavaDoc {
261         
262         private Object JavaDoc derb;
263         
264         private File JavaDoc installDir;
265         
266         ConfigureJavaDBSamples(File JavaDoc installDir, Object JavaDoc 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                     // The user has an incorrect value in here
277
// fix it quietly.
278
DerbySupport.setLocation(installDir.getAbsolutePath());
279                 }
280                 if ("".equals(DerbySupport.getSystemHome())) {
281                     File JavaDoc dbdir = new File JavaDoc(DerbySupport.getDefaultSystemHome());
282                     if (dbdir.exists()==false){
283                         dbdir.mkdirs();
284                     }
285                     DerbySupport.setSystemHome(dbdir.getAbsolutePath());
286                 }
287                 
288                 //now register the sample db
289
DerbyDatabases.createSampleDatabase();
290             } catch (DatabaseException ex) {
291                 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex);
292             } catch (IOException JavaDoc ex) {
293                 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex);
294             }
295         }
296     }
297
298     private void configureForPointbaseSamples(final String JavaDoc installRoot, final File JavaDoc irf, final File JavaDoc 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             // Go to the conf dir
305
File JavaDoc dbFile = new File JavaDoc(installRoot+"/pointbase/databases/sample.dbn"); //NOI18N
306
// if it is writable
307
if (dbFile.exists() && (dbFile.canWrite()==false)) {
308                 //no write access to the dbs. so we copy them in a location where the ide can RW them
309
createLocalInstallation();
310             }
311             try {
312                 String JavaDoc driverName = installRoot + RELATIVE_DRIVER_PATH;
313                 File JavaDoc f = new File JavaDoc(driverName);
314                 if(f.exists()){
315                     File JavaDoc dbDir = new File JavaDoc(localInstall,"databases"); //NOI18N
316
int portVal = getPort(dbDir);
317                     
318                     URL JavaDoc[] urls = new URL JavaDoc[1];
319                     urls[0]= f.toURI().toURL(); //NOI18N
320

321                     JDBCDriver newDriver = JDBCDriver.create(DRIVER_NAME, DRIVER_DISPLAY_NAME, DRIVER,urls);
322                     JDBCDriverManager.getDefault().addDriver(newDriver);
323                     
324                     
325                     File JavaDoc testFile = new File JavaDoc(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 JavaDoc(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 JavaDoc ex) {
342                 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex);
343             } catch (IOException JavaDoc ex) {
344                 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex);
345             } catch (DatabaseException ex) {
346                 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex);
347             } catch (RuntimeException JavaDoc ex) {
348                 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex);
349             }
350         }
351     }
352     
353     public String JavaDoc getJDBCDriverClass() {
354         return DRIVER;
355     }
356     
357     /**
358      * Whether this runtime accepts this connection string.
359      */

360     public boolean acceptsDatabaseURL(String JavaDoc url){
361         return url.startsWith(LOCALHOST_URL_PREFIX);
362     }
363     
364     public boolean isRegisterable() {
365         boolean retVal = false;
366         if (null == appServerInstallationDirectory) {
367             //System.out.println("PDB is null;");
368
String JavaDoc 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 JavaDoc irf = new File JavaDoc(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     /**
388      * Is database server up and running.
389      */

390     public boolean isRunning(){
391         if (process!=null){
392             try{
393                 process.exitValue();
394                 process=null;
395             } catch (IllegalThreadStateException JavaDoc e){
396                 //not exited yet...it's ok
397
// TODO -- need a better method to do this test?
398
}
399         }
400         return (process!=null);
401         
402     }
403     
404     /**
405      * Can the database be started from inside the IDE?
406      */

407     public boolean canStart(){
408         //System.out.println("can start!!!");
409

410         // only can start if already installed
411
return System.getProperty("com.sun.aas.installRoot") != null;
412     }
413     
414     /**
415      * Start the database server.
416      */

417     public void start(){
418         start(5000);//wait 5 seconds
419
}
420     /* can return null
421      * of the location of the pointbase scripts to be used
422      *
423      **/

424     public File JavaDoc getScriptsLocation(){
425         File JavaDoc irf = appServerInstallationDirectory;
426         File JavaDoc localInstall = null;
427         if (null != irf && irf.exists()) {
428             String JavaDoc installRoot = irf.getAbsolutePath();
429             if (installRoot != null) {
430                 localInstall = new File JavaDoc(System.getProperty("netbeans.user")+"/pointbase/tools/serveroption");
431                 if (!localInstall.exists()){
432                     localInstall = new File JavaDoc(installRoot+ "/pointbase/tools/serveroption");
433                 }
434             } else {
435                 Util.showInformation(NbBundle.getMessage(StartAction.class, "ERR_NotThere"));
436             }
437         }
438         return localInstall;
439     }
440     
441     /**
442      * Start the database server, and wait some time (in milliseconds) to make sure the server is active.
443      */

444     public void start(int waitTime){
445         if (process!=null){// seems to be already running?
446
stop();
447         }
448         String JavaDoc suffix;
449         if (File.separator.equals("\\")) { // NOI18N
450
suffix = ".bat"; // NOI18N
451
} else {
452             suffix = ".sh"; // NOI18N
453
}
454         File JavaDoc loc = getScriptsLocation();
455         if (loc==null){
456             return;//nothing to start...
457
}
458         String JavaDoc script = null; //loc.getAbsolutePath() +"/startserver";/// NOI18N
459
if (File.separator.equals("\\")) { // NOI18N
460
script = loc.getAbsolutePath() +"\\startserver"; // NOI18N
461
} else {
462             script = loc.getAbsolutePath() +"/startserver"; // NOI18N
463
}
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);// to make sure the server is up and running
470
}
471         } catch (MissingResourceException JavaDoc ex) {
472             Util.showInformation(ex.getLocalizedMessage());
473         } catch (IOException JavaDoc ex) {
474             Util.showInformation(ex.getLocalizedMessage());
475         } catch (InterruptedException JavaDoc ex) {
476             Util.showInformation(ex.getLocalizedMessage());
477         } catch (RuntimeException JavaDoc ex) {
478             Util.showInformation(ex.getLocalizedMessage());
479         }
480     }
481     
482     
483     
484     
485     /**
486      * Stop the database server.
487      */

488     public void stop(){
489         BufferedWriter JavaDoc processIn = null;
490         try {
491             if (process!=null) {//something to stop...
492
processIn = new BufferedWriter JavaDoc(new OutputStreamWriter JavaDoc(process.getOutputStream()));
493                 processIn.write("q\ny\n");
494                 processIn.flush();
495                 process.destroy();
496                 process=null;
497             }
498             
499         } catch (IOException JavaDoc e) {
500             Util.showInformation(e.getMessage());
501             process=null;
502         } catch (RuntimeException JavaDoc 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 JavaDoc ioe) {
511                     ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL,
512                             ioe);
513                 }
514             }
515         }
516     }
517     
518     private static final String JavaDoc POINTBASE_URL_PREFIX = "jdbc:pointbase:"; //NOI18N
519
private static final String JavaDoc LOCALHOST_URL_PREFIX =
520             POINTBASE_URL_PREFIX + "//localhost:"; //NOI18N
521
private static final String JavaDoc SAMPLE_NAME = "sample"; //NOI18N
522
private static final String JavaDoc SUN_APPSERV_SAMPLES_NAME =
523             "sun-appserv-samples"; //NOI18N
524
private static final String JavaDoc DOT_DBN = ".dbn"; //NOI18N
525
private static final String JavaDoc SLASH = "/"; //NOI18N
526

527     private int getPort(File JavaDoc databaseDir) throws IOException JavaDoc {
528         File JavaDoc iniFile = new File JavaDoc(databaseDir,"pointbase.ini"); //NOI18N
529

530         // get the port info
531
int port = 9092;
532         Properties JavaDoc iniProps = new Properties JavaDoc();
533         iniProps.load(new FileInputStream JavaDoc(iniFile));
534         port = Integer.parseInt(iniProps.getProperty("server.port", "9092")); //NOI18N
535
return port;
536     }
537     /* register the derby driver to the library manager of netbeans
538      *
539      **/

540     private void registerDerbyLibrary(final File JavaDoc location) { // , final String name){
541
final Repository rep = (Repository) Lookup.getDefault().lookup(Repository.class);
542         final FileObject libsFolder = rep.getDefaultFileSystem().findResource("/org-netbeans-api-project-libraries/Libraries"); //NOI18N
543
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 JavaDoc ex) {
550                 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex);
551             }
552         }
553     }
554     
555     static class DerbyLibraryRegistrar implements FileSystem.AtomicAction {
556         
557         private File JavaDoc location;
558         
559         private FileObject libsFolder;
560         
561         DerbyLibraryRegistrar(File JavaDoc location, FileObject libsFolder) {
562             this.location = location;
563             this.libsFolder = libsFolder;
564         }
565         
566         public void run() throws IOException JavaDoc {
567             FileLock ld = null;
568             java.io.OutputStream JavaDoc outStreamd = null;
569             Writer JavaDoc outd = null;
570             OutputStreamWriter JavaDoc osw = null;
571             try {
572                 // the derby lib driver:
573
FileObject derbyLib =null;
574                 derbyLib = libsFolder.getFileObject("JavaDB" ,"xml");//NOI18N
575
if (null == derbyLib) {
576                     derbyLib = libsFolder.createData("JavaDB" ,"xml");//NOI18N
577
ld = derbyLib.lock();
578                     outStreamd = derbyLib.getOutputStream(ld);
579                     osw = new OutputStreamWriter JavaDoc(outStreamd);
580                     outd = new BufferedWriter JavaDoc(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");//NOI18N
582
outd.write("<library version=\"1.0\">\n<name>JAVADB_DRIVER_LABEL</name>\n");//NOI18N
583
outd.write("<type>j2se</type>\n");//NOI18N
584
outd.write("<localizing-bundle>org.netbeans.modules.j2ee.sun.ide.j2ee.db.Bundle</localizing-bundle>\n");//NOI18N
585
outd.write("<volume>\n<type>classpath</type>\n"); //NOI18N
586
outd.write("<resource>jar:"+new File JavaDoc(location.getAbsolutePath()+"/lib/derby.jar").toURI().toURL()+"!/</resource>\n"); //NOI18N
587
outd.write("<resource>jar:"+new File JavaDoc(location.getAbsolutePath()+"/lib/derbyclient.jar").toURI().toURL()+"!/</resource>\n"); //NOI18N
588
outd.write("<resource>jar:"+new File JavaDoc(location.getAbsolutePath()+"/lib/derbynet.jar").toURI().toURL()+"!/</resource>\n"); //NOI18N
589

590                     
591                     outd.write("</volume>\n<volume>\n<type>src</type>\n</volume>\n"); //NOI18N
592
outd.write("<volume>\n<type>javadoc</type>\n"); //NOI18N
593
outd.write("</volume>\n</library>"); //NOI18N
594
}
595             } finally {
596                 if (null != outd) {
597                     try {
598                         outd.close();
599                     } catch (IOException JavaDoc ioe) {
600                         ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ioe);
601                     }
602                 }
603                 if (null != outStreamd) {
604                     try {
605                         outStreamd.close();
606                     } catch (IOException JavaDoc 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