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.wsdl;
034:
035: import java.awt.event.ActionEvent;
036: import java.beans.PropertyChangeListener;
037: import java.io.File;
038: import java.io.FileFilter;
039: import java.io.IOException;
040: import java.sql.Connection;
041: import java.util.ArrayList;
042: import java.util.HashMap;
043: import java.util.List;
044: import java.util.Map;
045: import javax.swing.JFrame;
046: import javax.swing.JOptionPane;
047: import javax.wsdl.Definition;
048: import javax.xml.parsers.DocumentBuilderFactory;
049: import javax.xml.parsers.ParserConfigurationException;
050:
051: import org.netbeans.api.db.explorer.ConnectionManager;
052: import org.netbeans.api.db.explorer.DatabaseConnection;
053: import org.netbeans.api.project.Project;
054: import org.openide.util.HelpCtx;
055: import org.openide.util.actions.CallableSystemAction;
056: import org.openide.filesystems.FileObject;
057: import org.w3c.dom.Document;
058: import org.w3c.dom.Node;
059: import org.w3c.dom.NodeList;
060: import org.xml.sax.SAXException;
061: import org.openide.util.NbBundle;
062:
063: public class GenFiles extends CallableSystemAction {
064:
065: /**
066: *
067: */
068: private static final long serialVersionUID = 1L;
069: public static final String NAME = "Name";
070: private String mSrcDirectoryLocation;
071: private String mBuildDirectoryLocation;
072: private String mBaseDirectoryLocation;
073: private String dbURL = "";
074: private String jndi_name = "";
075: private String transactionRequired = "";
076: private static final String CONNECTION_FILE = "connectivityInfo.xml";
077: private String engineFileName = "sqlse_engine.xml";
078: private JFrame frame;
079: DatabaseConnection dbConn = null;
080: Connection conn = null;
081: Project project = null;
082:
083: public GenFiles() {
084:
085: }
086:
087: public void setProject(Project project) {
088: this .project = project;
089: }
090:
091: /* public void addPropertyChangeListener(PropertyChangeListener listener) {
092: this.listener = listener;
093: }
094:
095: public Object getValue(String key) {
096: return null;
097: }
098:
099: public boolean isEnabled() {
100: return false;
101: }
102:
103: public void putValue(String key, Object value) {
104:
105: }*/
106:
107: public String getName() {
108: //return NbBundle.getMessage(ActionImpl.class, "CTL_Connect");
109: //return "Generate WSDL...";
110: return NbBundle.getMessage(GenFiles.class, "LBL_Generate_WSDL");
111: }
112:
113: /*public void removePropertyChangeListener(PropertyChangeListener listener) {
114: if(this.listener.equals(listener)){
115: this.listener = null;
116:
117:
118: }*/
119: /**
120: * Default implementation of getHelpCtx().
121: *
122: * @return The HelpCtx value
123: */
124: public HelpCtx getHelpCtx() {
125: return HelpCtx.DEFAULT_HELP;
126: }
127:
128: /**
129: * Action to perform
130: */
131: public void performAction() {
132:
133: }
134:
135: /**
136: * Override to perform action synchronous
137: */
138: public boolean asynchronous() {
139: return false;
140: }
141:
142: public void setEnabled(boolean b) {
143:
144: }
145:
146: public void actionPerformed(ActionEvent e) {
147: try {
148: execute();
149: } catch (Exception e1) {
150: //e1.printStackTrace();
151: }
152: }
153:
154: /**
155: * @return Returns the srcDirectoryLocation.
156: */
157: public String getSrcDirectoryLocation() {
158: return mSrcDirectoryLocation;
159: }
160:
161: /**
162: * @param srcDirectoryLocation The srcDirectoryLocation to set.
163: */
164: public void setSrcDirectoryLocation(String srcDirectoryLocation) {
165: mSrcDirectoryLocation = srcDirectoryLocation;
166: }
167:
168: /**
169: * @param srcDirectoryLocation The srcDirectoryLocation to set.
170: */
171: public void setBaseDirectoryLocation(String baseDirectoryLocation) {
172: mBaseDirectoryLocation = baseDirectoryLocation;
173: }
174:
175: public String getBuildDirectoryLocation() {
176: return mBuildDirectoryLocation;
177: }
178:
179: /**
180: * @param buildDirectoryLocation The buildDirectoryLocation to set.
181: */
182: public void setBuildDirectoryLocation(String buildDirectoryLocation) {
183: mBuildDirectoryLocation = buildDirectoryLocation;
184: engineFileName = "sqlse_engine.xml";
185: }
186:
187: public static List getFilesRecursively(File dir, FileFilter filter) {
188: List ret = new ArrayList();
189: if (!dir.isDirectory()) {
190: return ret;
191: }
192: File[] fileNdirs = dir.listFiles(filter);
193: for (int i = 0, I = fileNdirs.length; i < I; i++) {
194: if (fileNdirs[i].isDirectory()) {
195: ret.addAll(getFilesRecursively(fileNdirs[i], filter));
196: } else {
197: ret.add(fileNdirs[i]);
198: }
199: }
200: return ret;
201: }
202:
203: public static List getFilesRecursively(File dir, String[] extensions) {
204: FileFilter filter = null;
205: if (extensions[0].equals(".sql")) {
206: filter = new ExtensionFilter(extensions);
207: }
208: return getFilesRecursively(dir, filter);
209: }
210:
211: public void execute() throws Exception {
212: Map wsdlMap = new HashMap();
213:
214: try {
215: String baseDir = org.openide.filesystems.FileUtil
216: .getFileDisplayName(project.getProjectDirectory());
217: String OS = System.getProperty("os.name").toLowerCase();
218: if (OS.indexOf("windows") > -1) {
219: //do nothing
220: } else {
221: baseDir = '/' + baseDir;
222: }
223: mBuildDirectoryLocation = baseDir + "/build";
224: mSrcDirectoryLocation = baseDir + "/src";
225: File srcDir = new File(mSrcDirectoryLocation);
226: if (!srcDir.exists()) {
227: throw new Exception("Directory "
228: + mSrcDirectoryLocation + " does not exit.");
229: }
230: //String baseDir = mBaseDirectoryLocation;
231: int length = baseDir.length();
232: String projectName = baseDir.substring(baseDir
233: .lastIndexOf(File.separator) + 1, baseDir.length());
234: if (projectName.length() == length) {
235: projectName = baseDir.substring(baseDir
236: .lastIndexOf("/") + 1, baseDir.length());
237: }
238: boolean b = (new File(mBuildDirectoryLocation + "/META-INF"))
239: .mkdirs();
240: SQLEngineFileGenerator engineFileGen = new SQLEngineFileGenerator(
241: mBuildDirectoryLocation + "/" + engineFileName,
242: projectName);
243: String srcDirPath = srcDir.getAbsolutePath();
244: String[] ext = new String[] { ".sql" };
245: List sqlFiles = getFilesRecursively(srcDir, ext);
246: readConnectionInfo(CONNECTION_FILE);
247: getDatabaseConnection();
248: if (conn == null) {
249: throw new Exception(
250: "Unable to retrieve any database connections with the url "
251: + dbURL
252: + "\n Please associate a Connection for the sql file in netbeans runtime tab or if it exists, connect to the external");
253: }
254:
255: for (int i = 0, I = sqlFiles.size(); i < I; i++) {
256: File f = (File) sqlFiles.get(i);
257: if (f != null) {
258: engineFileGen.addSQLDefinition(f.getName(), dbConn);
259: }
260: }
261: engineFileGen.persistEngineFile(jndi_name,
262: transactionRequired);
263:
264: //call generate wsdl in model.
265: WSDLGenerator wsdlgen = new WSDLGenerator(conn, sqlFiles,
266: projectName, srcDirPath, engineFileName);
267:
268: wsdlgen.setDBConnection(dbConn);
269:
270: Definition def = wsdlgen.generateWSDL();
271:
272: SQLMapWriter sqlw = new SQLMapWriter(sqlFiles, def,
273: new File(mBuildDirectoryLocation)
274: .getCanonicalPath());
275: sqlw.writeMap();
276: JBIFileWriter fw = new JBIFileWriter(
277: mBuildDirectoryLocation + "/META-INF/jbi.xml",
278: mBuildDirectoryLocation + "/sqlmap.xml",
279: mBuildDirectoryLocation);
280: fw.writeJBI();
281: project.getProjectDirectory().refresh();
282: JOptionPane.showMessageDialog(frame, NbBundle.getMessage(
283: GenFiles.class, "LBL_WSDL_Generated"),
284: "Information", JOptionPane.INFORMATION_MESSAGE);
285: } catch (Exception e) {
286: throw new Exception(e.getMessage());
287: }
288: }
289:
290: private void readConnectionInfo(String fileName) throws Exception {
291: if (fileName.endsWith(".xml")) {
292: DocumentBuilderFactory factory = DocumentBuilderFactory
293: .newInstance();
294: try {
295: Document doc = factory.newDocumentBuilder()
296: .parse(
297: new File(mSrcDirectoryLocation,
298: CONNECTION_FILE));
299: NodeList nl = doc.getDocumentElement()
300: .getElementsByTagName("database-url");
301: if (nl != null) {
302: Node n = nl.item(0);
303: if (n != null) {
304: Node n2 = n.getAttributes().getNamedItem(
305: "value");
306: if (n2 != null) {
307: dbURL = n2.getNodeValue();
308: //log("Using Database URL value: " + dbURL);
309: }
310: }
311: }
312: NodeList n3 = doc.getDocumentElement()
313: .getElementsByTagName("jndi-name");
314: if (n3 != null) {
315: Node n = n3.item(0);
316: if (n != null) {
317: Node n4 = n.getAttributes().getNamedItem(
318: "value");
319: if (n4 != null) {
320: jndi_name = n4.getNodeValue();
321: //log("jndi value: " + jndi_name);
322: }
323: }
324: }
325: NodeList n5 = doc.getDocumentElement()
326: .getElementsByTagName("transaction-required");
327: if (n5 != null) {
328: Node n = n5.item(0);
329: if (n != null) {
330: Node n6 = n.getAttributes().getNamedItem(
331: "value");
332: if (n6 != null) {
333: transactionRequired = n6.getNodeValue();
334: //log("TransactionRequired value: " + transactionRequired);
335: }
336: }
337: }
338:
339: } catch (SAXException e) {
340: //log(e.getLocalizedMessage());
341: } catch (IOException e) {
342: //log(e.getLocalizedMessage());
343: } catch (ParserConfigurationException e) {
344: //log(e.getLocalizedMessage());
345: }
346: } else {
347: throw new Exception(
348: "No File with name connectivityInfo.xml was found in the src directory");
349: }
350: }
351:
352: private void getDatabaseConnection() {
353: DatabaseConnection[] dbConnections = ConnectionManager
354: .getDefault().getConnections();
355: if (dbConn == null) {
356: for (int j = 0; j < dbConnections.length; j++) {
357: if (dbConnections[j].getDatabaseURL().equalsIgnoreCase(
358: dbURL)) {
359: dbConn = dbConnections[j];
360: conn = dbConn.getJDBCConnection();
361: break;
362: }
363: }
364: }
365: if ((dbConn != null) && (conn == null)) {
366: ConnectionManager.getDefault().showConnectionDialog(dbConn);
367: conn = dbConn.getJDBCConnection();
368: }
369: }
370:
371: }
|