001: /*
002: * The contents of this file are subject to the terms of the Common Development
003: * and Distribution License (the License). You may not use this file except in
004: * compliance with the License.
005: *
006: * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
007: * or http://www.netbeans.org/cddl.txt.
008: *
009: * When distributing Covered Code, include this CDDL Header Notice in each file
010: * and include the License file at http://www.netbeans.org/cddl.txt.
011: * If applicable, add the following below the CDDL Header, with the fields
012: * enclosed by brackets [] replaced by your own identifying information:
013: * "Portions Copyrighted [year] [name of copyright owner]"
014: *
015: * The Original Software is NetBeans. The Initial Developer of the Original
016: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
017: * Microsystems, Inc. All Rights Reserved.
018: */
019:
020: /*
021: * Sun Public License Notice
022: *
023: * The contents of this file are subject to the Sun Public License
024: * Version 1.0 (the "License"). You may not use this file except in
025: * compliance with the License. A copy of the License is available at
026: * http://www.sun.com/
027: *
028: * The Original Code is NetBeans. The Initial Developer of the Original
029: * Code is Sun Microsystems, Inc. Portions Copyright 1997-2005 Sun
030: * Microsystems, Inc. All Rights Reserved.
031: */
032:
033: package org.netbeans.modules.sql.project.anttasks;
034:
035: // IMPORTANT! You need to compile this class against ant.jar. So add the
036: // JAR ide5/ant/lib/ant.jar from your IDE installation directory (or any
037: // other version of Ant you wish to use) to your classpath. Or if
038: // writing your own build target, use e.g.:
039: // <classpath>
040: // <pathelement location="${ant.home}/lib/ant.jar"/>
041: // </classpath>
042:
043: import java.io.File;
044: import java.util.List;
045: import java.util.ArrayList;
046: import java.util.HashMap;
047: import java.util.Map;
048: import java.io.FileFilter;
049:
050: import org.apache.tools.ant.Task;
051: import org.apache.tools.ant.BuildException;
052:
053: import java.sql.Connection;
054:
055: import org.netbeans.api.db.explorer.ConnectionManager;
056: import org.netbeans.api.db.explorer.DatabaseConnection;
057: import org.xml.sax.SAXException;
058: import org.w3c.dom.Document;
059: import org.w3c.dom.Element;
060: import org.w3c.dom.NodeList;
061: import org.w3c.dom.Node;
062: import org.netbeans.modules.sql.project.dbmodel.DBMetaData;
063:
064: import javax.xml.parsers.DocumentBuilderFactory;
065: import javax.xml.parsers.ParserConfigurationException;
066: import javax.swing.JFrame;
067: import javax.swing.JOptionPane;
068: import javax.swing.JFrame;
069: import javax.wsdl.Definition;
070: import java.io.BufferedReader;
071: import java.io.FileReader;
072: import java.io.FileNotFoundException;
073: import java.io.IOException;
074:
075: /**
076: * @author Administrator
077: */
078: public class GenerateWSDL extends Task {
079:
080: private String mSrcDirectoryLocation;
081: private String mBuildDirectoryLocation;
082: private String dbURL = "";
083: private String jndi_name = "";
084: private static final String CONNECTION_FILE = "connectivityInfo.xml";
085: private String engineFileName = null;
086: private JFrame frame;
087: DatabaseConnection dbConn = null;
088: Connection conn = null;
089:
090: /**
091: * @return Returns the srcDirectoryLocation.
092: */
093: public String getSrcDirectoryLocation() {
094: return mSrcDirectoryLocation;
095: }
096:
097: /**
098: * @param srcDirectoryLocation The srcDirectoryLocation to set.
099: */
100: public void setSrcDirectoryLocation(String srcDirectoryLocation) {
101: mSrcDirectoryLocation = srcDirectoryLocation;
102: }
103:
104: /**
105: * @return Returns the srcDirectoryLocation.
106: */
107: public String getBuildDirectoryLocation() {
108: return mBuildDirectoryLocation;
109: }
110:
111: /**
112: * @param buildDirectoryLocation The buildDirectoryLocation to set.
113: */
114: public void setBuildDirectoryLocation(String buildDirectoryLocation) {
115: mBuildDirectoryLocation = buildDirectoryLocation;
116: engineFileName = "sqlse_engine.xml";
117: }
118:
119: public static List getFilesRecursively(File dir, FileFilter filter) {
120: List ret = new ArrayList();
121: if (!dir.isDirectory()) {
122: return ret;
123: }
124: File[] fileNdirs = dir.listFiles(filter);
125: for (int i = 0, I = fileNdirs.length; i < I; i++) {
126: if (fileNdirs[i].isDirectory()) {
127: ret.addAll(getFilesRecursively(fileNdirs[i], filter));
128: } else {
129: ret.add(fileNdirs[i]);
130: }
131: }
132: return ret;
133: }
134:
135: public static List getFilesRecursively(File dir, String[] extensions) {
136: FileFilter filter = null;
137: if (extensions[0].equals(".sql")) {
138: filter = new ExtensionFilter(extensions);
139: }
140: return getFilesRecursively(dir, filter);
141: }
142:
143: public void execute() throws BuildException {
144: File srcDir = new File(mSrcDirectoryLocation);
145: File bldDir = new File(mBuildDirectoryLocation + "/META-INF");
146:
147: if (!srcDir.exists()) {
148: throw new BuildException("Directory "
149: + mSrcDirectoryLocation + " does not exit.");
150: }
151: if (!bldDir.exists()) {
152: throw new BuildException("Directory "
153: + mBuildDirectoryLocation
154: + " does not exit. Please generate the WSDL");
155: }
156: String srcDirPath = srcDir.getAbsolutePath();
157: String bldDirPath = bldDir.getAbsolutePath();
158:
159: if (srcDir.isDirectory()) {
160: String extensions[] = new String[1];
161: extensions[0] = ".wsdl";
162: FileFilter filter = new ExtensionFilter(extensions);
163: File[] fileNdirs = srcDir.listFiles(filter);
164: if (fileNdirs.length <= 0) {
165: JOptionPane
166: .showMessageDialog(
167: frame,
168: "Please Generate the WSDL using \"Generate WSDL...\" option, before building the application.",
169: "Warning", JOptionPane.WARNING_MESSAGE);
170: }
171: }
172: if (bldDir.isDirectory()) {
173: String extensions[] = new String[1];
174: extensions[0] = ".xml";
175: FileFilter filter = new ExtensionFilter(extensions);
176: File[] fileNdirs = bldDir.listFiles(filter);
177: if (fileNdirs.length <= 0) {
178: JOptionPane
179: .showMessageDialog(
180: frame,
181: "JBI.xml is not generated, please generate the WSDL.",
182: "Warning", JOptionPane.WARNING_MESSAGE);
183: }
184: }
185: }
186:
187: public void execute1() throws BuildException {
188: Map wsdlMap = new HashMap();
189: File srcDir = new File(mSrcDirectoryLocation);
190: if (!srcDir.exists()) {
191: throw new BuildException("Directory "
192: + mSrcDirectoryLocation + " does not exit.");
193: }
194: try {
195: String baseDir = this .getProject().getProperty("basedir");
196: String projectName = baseDir.substring(baseDir
197: .lastIndexOf(File.separator) + 1, baseDir.length());
198: SQLEngineFileGenerator engineFileGen = new SQLEngineFileGenerator(
199: mBuildDirectoryLocation + "/" + engineFileName,
200: projectName);
201: String srcDirPath = srcDir.getAbsolutePath();
202: String[] ext = new String[] { ".sql" };
203: List sqlFiles = getFilesRecursively(srcDir, ext);
204: readConnectionInfo(CONNECTION_FILE);
205: getDatabaseConnection();
206: if (conn == null) {
207: throw new BuildException(
208: "Unable to retrieve any database connections with the url "
209: + dbURL
210: + "\n Please associate a Connection for the sql file in netbeans runtime tab or if it exists, connect to the external");
211: }
212: for (int i = 0, I = sqlFiles.size(); i < I; i++) {
213: File f = (File) sqlFiles.get(i);
214: if (f != null) {
215: engineFileGen.addSQLDefinition(f.getName(), dbConn);
216: }
217: }
218: engineFileGen.persistEngineFile(jndi_name);
219: //call generate wsdl in model.
220: WSDLGenerator wsdlgen = new WSDLGenerator(conn, sqlFiles,
221: projectName, srcDirPath, engineFileName);
222:
223: wsdlgen.setDBConnection(dbConn);
224:
225: Definition def = wsdlgen.generateWSDL();
226: SQLMapWriter sqlw = new SQLMapWriter(sqlFiles, def,
227: new File(mBuildDirectoryLocation)
228: .getCanonicalPath());
229: sqlw.writeMap();
230: JBIFileWriter fw = new JBIFileWriter(
231: mBuildDirectoryLocation + "/META-INF/jbi.xml",
232: mBuildDirectoryLocation + "/sqlmap.xml",
233: mBuildDirectoryLocation);
234: fw.writeJBI();
235:
236: } catch (Exception e) {
237: throw new BuildException(e.getMessage());
238: }
239: }
240:
241: private void readConnectionInfo(String fileName)
242: throws FileNotFoundException {
243: if (fileName.endsWith(".xml")) {
244: DocumentBuilderFactory factory = DocumentBuilderFactory
245: .newInstance();
246: try {
247: Document doc = factory.newDocumentBuilder()
248: .parse(
249: new File(mSrcDirectoryLocation,
250: CONNECTION_FILE));
251: NodeList nl = doc.getDocumentElement()
252: .getElementsByTagName("database-url");
253: if (nl != null) {
254: Node n = nl.item(0);
255: if (n != null) {
256: Node n2 = n.getAttributes().getNamedItem(
257: "value");
258: if (n2 != null) {
259: dbURL = n2.getNodeValue();
260: log("Using Database URL value: " + dbURL);
261: }
262: }
263: }
264: NodeList n3 = doc.getDocumentElement()
265: .getElementsByTagName("jndi-name");
266: if (n3 != null) {
267: Node n = n3.item(0);
268: if (n != null) {
269: Node n4 = n.getAttributes().getNamedItem(
270: "value");
271: if (n4 != null) {
272: jndi_name = n4.getNodeValue();
273: log("jndi value: " + jndi_name);
274: }
275: }
276: }
277: } catch (SAXException e) {
278: log(e.getLocalizedMessage());
279: } catch (IOException e) {
280: log(e.getLocalizedMessage());
281: } catch (ParserConfigurationException e) {
282: log(e.getLocalizedMessage());
283: }
284: } else {
285: throw new BuildException(
286: "No File with name connectivityInfo.xml was found in the src directory");
287: }
288: }
289:
290: private void getDatabaseConnection() {
291: DatabaseConnection[] dbConnections = ConnectionManager
292: .getDefault().getConnections();
293: if (dbConn == null) {
294: for (int j = 0; j < dbConnections.length; j++) {
295: if (dbConnections[j].getDatabaseURL().equalsIgnoreCase(
296: dbURL)) {
297: dbConn = dbConnections[j];
298: conn = dbConn.getJDBCConnection();
299: break;
300: }
301: }
302: }
303: if ((dbConn != null) && (conn == null)) {
304: ConnectionManager.getDefault().showConnectionDialog(dbConn);
305: conn = dbConn.getJDBCConnection();
306: }
307: }
308:
309: }
|