KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > samples > DatabaseUsingSampleWizardIterator


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
21 package org.netbeans.modules.j2ee.samples;
22
23 import java.io.File JavaDoc;
24 import java.io.FileOutputStream JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.util.Set JavaDoc;
27 import javax.xml.parsers.DocumentBuilder JavaDoc;
28 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
29 import javax.xml.transform.Result JavaDoc;
30 import javax.xml.transform.Source JavaDoc;
31 import javax.xml.transform.Transformer JavaDoc;
32 import javax.xml.transform.TransformerFactory JavaDoc;
33 import javax.xml.transform.dom.DOMSource JavaDoc;
34 import javax.xml.transform.stream.StreamResult JavaDoc;
35 import javax.xml.xpath.XPath JavaDoc;
36 import javax.xml.xpath.XPathConstants JavaDoc;
37 import javax.xml.xpath.XPathFactory JavaDoc;
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 JavaDoc;
43 import org.w3c.dom.Node JavaDoc;
44
45 /**
46  *
47  * @author Tomasz.Slota@Sun.COM
48  */

49 public class DatabaseUsingSampleWizardIterator extends JavaEESamplesWizardIterator {
50     private static final String JavaDoc DB_RES_FILE = "CustomerCMP-ejb/setup/derby_netPool.sun-resource"; //NOI18N
51

52     @Override JavaDoc protected WizardDescriptor.Panel[] createPanels() {
53         return new WizardDescriptor.Panel[] {
54             new JavaEESamplesWizardPanel(true)
55         };
56     }
57     
58     @Override JavaDoc public Set JavaDoc/*<FileObject>*/ instantiate() throws IOException JavaDoc{
59         String JavaDoc dbName = (String JavaDoc) wiz.getProperty(WizardProperties.DB_NAME);
60         try {
61             DerbyDatabases.createDatabase(dbName, "app", "app"); //NOI18N
62

63         } catch (IllegalStateException JavaDoc ex) {
64             throw new RuntimeException JavaDoc(ex);
65         } catch (DatabaseException ex) {
66             throw new RuntimeException JavaDoc(ex);
67         }
68         Set JavaDoc r = super.instantiate();
69         
70         File JavaDoc projectDir = (File JavaDoc) wiz.getProperty(WizardProperties.PROJ_DIR);
71         File JavaDoc dbResource = new File JavaDoc(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 JavaDoc dbResource, String JavaDoc dbName) throws IOException JavaDoc {
82         String JavaDoc xPathPath = "/resources/jdbc-connection-pool/property[@name='DatabaseName']/@value"; //NOI18N
83
setValueInXMLFile(dbResource, xPathPath, dbName);
84     }
85
86     private static void setValueInXMLFile(File JavaDoc srcFile, String JavaDoc xPathPath, String JavaDoc value) {
87         
88         try {
89             DocumentBuilder JavaDoc builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
90             Document JavaDoc document = builder.parse(srcFile);
91             
92             XPath JavaDoc xPath = XPathFactory.newInstance().newXPath();
93             Node JavaDoc node = (Node JavaDoc) xPath.evaluate(xPathPath, document, XPathConstants.NODE);
94             node.setTextContent(value);
95             
96             TransformerFactory JavaDoc tranFactory = TransformerFactory.newInstance();
97             
98             Transformer JavaDoc aTransformer = tranFactory.newTransformer();
99             Source JavaDoc src = new DOMSource JavaDoc(document);
100             FileOutputStream JavaDoc fos = new FileOutputStream JavaDoc(srcFile);
101             Result JavaDoc dest = new StreamResult JavaDoc(fos);
102             aTransformer.transform(src, dest);
103             fos.close();
104             
105         } catch (Exception JavaDoc e) {
106             throw new RuntimeException JavaDoc(e);
107         }
108     }
109 }
110
Popular Tags