1 19 20 21 package org.netbeans.modules.j2ee.samples; 22 23 import java.io.File ; 24 import java.io.FileOutputStream ; 25 import java.io.IOException ; 26 import java.util.Set ; 27 import javax.xml.parsers.DocumentBuilder ; 28 import javax.xml.parsers.DocumentBuilderFactory ; 29 import javax.xml.transform.Result ; 30 import javax.xml.transform.Source ; 31 import javax.xml.transform.Transformer ; 32 import javax.xml.transform.TransformerFactory ; 33 import javax.xml.transform.dom.DOMSource ; 34 import javax.xml.transform.stream.StreamResult ; 35 import javax.xml.xpath.XPath ; 36 import javax.xml.xpath.XPathConstants ; 37 import javax.xml.xpath.XPathFactory ; 38 import org.netbeans.api.db.explorer.DatabaseException; 39 import org.netbeans.modules.derby.api.DerbyDatabases; 40 import org.openide.WizardDescriptor; 41 import org.openide.util.NbBundle; 42 import org.w3c.dom.Document ; 43 import org.w3c.dom.Node ; 44 45 49 public class DatabaseUsingSampleWizardIterator extends JavaEESamplesWizardIterator { 50 private static final String DB_RES_FILE = "CustomerCMP-ejb/setup/derby_netPool.sun-resource"; 52 @Override protected WizardDescriptor.Panel[] createPanels() { 53 return new WizardDescriptor.Panel[] { 54 new JavaEESamplesWizardPanel(true) 55 }; 56 } 57 58 @Override public Set instantiate() throws IOException { 59 String dbName = (String ) wiz.getProperty(WizardProperties.DB_NAME); 60 try { 61 DerbyDatabases.createDatabase(dbName, "app", "app"); 63 } catch (IllegalStateException ex) { 64 throw new RuntimeException (ex); 65 } catch (DatabaseException ex) { 66 throw new RuntimeException (ex); 67 } 68 Set r = super.instantiate(); 69 70 File projectDir = (File ) wiz.getProperty(WizardProperties.PROJ_DIR); 71 File dbResource = new File (projectDir, DB_RES_FILE); 72 updateDBResource(dbResource, dbName); 73 74 return r; 75 } 76 77 public static DatabaseUsingSampleWizardIterator createIterator() { 78 return new DatabaseUsingSampleWizardIterator(); 79 } 80 81 private void updateDBResource(File dbResource, String dbName) throws IOException { 82 String xPathPath = "/resources/jdbc-connection-pool/property[@name='DatabaseName']/@value"; setValueInXMLFile(dbResource, xPathPath, dbName); 84 } 85 86 private static void setValueInXMLFile(File srcFile, String xPathPath, String value) { 87 88 try { 89 DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); 90 Document document = builder.parse(srcFile); 91 92 XPath xPath = XPathFactory.newInstance().newXPath(); 93 Node node = (Node ) xPath.evaluate(xPathPath, document, XPathConstants.NODE); 94 node.setTextContent(value); 95 96 TransformerFactory tranFactory = TransformerFactory.newInstance(); 97 98 Transformer aTransformer = tranFactory.newTransformer(); 99 Source src = new DOMSource (document); 100 FileOutputStream fos = new FileOutputStream (srcFile); 101 Result dest = new StreamResult (fos); 102 aTransformer.transform(src, dest); 103 fos.close(); 104 105 } catch (Exception e) { 106 throw new RuntimeException (e); 107 } 108 } 109 } 110 | Popular Tags |