001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: /*
042: * EjbDataSourceSelectionDialog.java
043: *
044: * Created on August 31, 2004, 10:31 AM
045: */
046:
047: package org.netbeans.modules.visualweb.ejb.ui;
048:
049: import org.netbeans.modules.visualweb.ejb.datamodel.EjbDataModel;
050: import org.netbeans.modules.visualweb.ejb.datamodel.EjbGroup;
051: import org.netbeans.modules.visualweb.ejb.load.EjbDataSourceXmlCreator;
052: import java.awt.Dialog;
053: import java.awt.event.ActionListener;
054: import java.io.BufferedInputStream;
055: import java.io.BufferedOutputStream;
056: import java.io.BufferedWriter;
057: import java.io.File;
058: import java.io.FileInputStream;
059: import java.io.FileOutputStream;
060: import java.io.OutputStreamWriter;
061: import java.util.*;
062: import java.util.jar.JarEntry;
063: import java.util.jar.JarOutputStream;
064: import javax.swing.JButton;
065: import org.openide.DialogDescriptor;
066: import org.openide.DialogDisplayer;
067: import org.openide.NotifyDescriptor;
068: import org.openide.util.HelpCtx;
069: import org.openide.util.NbBundle;
070:
071: /**
072: * A dialog for exporting EJB datasources
073: *
074: * @author cao
075: */
076: public class ExportEjbDataSourceDialog implements ActionListener {
077:
078: private Dialog dialog;
079:
080: private JButton okButton;
081: private JButton cancelButton;
082:
083: // The panel for exporting EJB datasources
084: private ExportEjbDataSourcesPanel exportPanel;
085:
086: private PortableEjbDataSource[] ejbDatasources;
087:
088: public ExportEjbDataSourceDialog() {
089: ejbDatasources = getPortableEjbDataSources(null);
090: createDialog();
091: }
092:
093: public ExportEjbDataSourceDialog(Collection ejbGroupNames) {
094: ejbDatasources = getPortableEjbDataSources(ejbGroupNames);
095: createDialog();
096: }
097:
098: private void createDialog() {
099:
100: exportPanel = new ExportEjbDataSourcesPanel();
101: exportPanel.setEjbDataSources(ejbDatasources);
102:
103: DialogDescriptor dialogDescriptor = new DialogDescriptor(
104: exportPanel, NbBundle.getMessage(
105: ExportEjbDataSourceDialog.class,
106: "EXPORT_EJB_DATASOURCES"), true,
107: (ActionListener) this );
108:
109: okButton = new JButton(NbBundle.getMessage(
110: ImportEjbDataSourcesDialog.class, "OK_BUTTON_LABEL"));
111:
112: okButton.getAccessibleContext().setAccessibleDescription(
113: java.util.ResourceBundle.getBundle(
114: "org/netbeans/modules/visualweb/ejb/ui/Bundle")
115: .getString("OK_BUTTON_DESC"));
116: okButton.setMnemonic(NbBundle.getMessage(
117: ImportEjbDataSourcesDialog.class, "OK_BUTTON_MNEMONIC")
118: .charAt(0));
119: cancelButton = new JButton(NbBundle.getMessage(
120: ExportEjbDataSourceDialog.class, "CANCEL_BUTTON_LABEL"));
121: cancelButton.getAccessibleContext().setAccessibleDescription(
122: java.util.ResourceBundle.getBundle(
123: "org/netbeans/modules/visualweb/ejb/ui/Bundle")
124: .getString("CANCEL_BUTTON_DESC"));
125: cancelButton.setMnemonic(NbBundle.getMessage(
126: ImportEjbDataSourcesDialog.class,
127: "CANCEL_BUTTON_MNEMONIC").charAt(0));
128: dialogDescriptor.setOptions(new Object[] { okButton,
129: cancelButton });
130: dialogDescriptor
131: .setClosingOptions(new Object[] { cancelButton });
132:
133: // help
134: dialogDescriptor.setHelpCtx(new HelpCtx(
135: "projrave_ejb_howtoejbs_ejb_export")); // NOI18N
136:
137: dialog = DialogDisplayer.getDefault().createDialog(
138: dialogDescriptor);
139: dialog.setResizable(true);
140: }
141:
142: public void showDialog() {
143: dialog.pack();
144: dialog.setVisible(true);
145: }
146:
147: public void actionPerformed(java.awt.event.ActionEvent e) {
148: if (e.getSource() == okButton) {
149: // Before anything, save the changes the user made if any
150: if (!exportPanel.saveChange())
151: return;
152:
153: // Get the selected EJB groups
154: ArrayList selectedEjbGrps = new ArrayList();
155: for (int i = 0; i < ejbDatasources.length; i++) {
156: if (ejbDatasources[i].isPortable())
157: selectedEjbGrps
158: .add(ejbDatasources[i].getEjbGroup());
159: }
160:
161: // Make sure at least one ejb datasource is selected
162: if (selectedEjbGrps.isEmpty()) {
163: String msg = NbBundle.getMessage(
164: ExportEjbDataSourceDialog.class,
165: "NO_EXPORT_SELECTION");
166: NotifyDescriptor d = new NotifyDescriptor.Message(msg,
167: NotifyDescriptor.INFORMATION_MESSAGE);
168: DialogDisplayer.getDefault().notify(d);
169: return;
170: }
171:
172: String filePath = exportPanel.getFilePath();
173:
174: // Make sure the user has entered a file to export to
175: if (filePath == null || filePath.length() == 0) {
176: String msg = NbBundle.getMessage(
177: ExportEjbDataSourceDialog.class,
178: "NO_EXPORT_FILE");
179: NotifyDescriptor d = new NotifyDescriptor.Message(msg,
180: NotifyDescriptor.INFORMATION_MESSAGE);
181: DialogDisplayer.getDefault().notify(d);
182: return;
183: }
184:
185: // If the filename is not ended with .jar, lets append .jar to it
186: if (!filePath.toLowerCase().endsWith(".jar")) // NOI18N
187: filePath = filePath + ".jar"; // NOI18N
188:
189: // Make the file name is not existed
190: if (new File(filePath).exists()) {
191: String msg = NbBundle.getMessage(
192: ExportEjbDataSourceDialog.class, "FILE_EXISTS",
193: filePath);
194: NotifyDescriptor confDialog = new NotifyDescriptor.Confirmation(
195: msg, NotifyDescriptor.OK_CANCEL_OPTION);
196: if (!(DialogDisplayer.getDefault().notify(confDialog) == NotifyDescriptor.OK_OPTION))
197: return;
198: }
199:
200: // Looks like everything is OK
201: // Lets start export
202: if (export(selectedEjbGrps, filePath)) {
203: // Done. Notify the user
204:
205: String successMsg = null;
206: if (selectedEjbGrps.size() == 1)
207: successMsg = NbBundle.getMessage(
208: ExportEjbDataSourceDialog.class,
209: "EXPORT_SUCCESSFULLY_ONE", filePath);
210: else
211: successMsg = NbBundle.getMessage(
212: ExportEjbDataSourceDialog.class,
213: "EXPORT_SUCCESSFULLY", Integer
214: .toString(selectedEjbGrps.size()),
215: filePath);
216:
217: NotifyDescriptor d = new NotifyDescriptor.Message(
218: successMsg,
219: NotifyDescriptor.INFORMATION_MESSAGE);
220: DialogDisplayer.getDefault().notify(d);
221:
222: dialog.dispose();
223: }
224: }
225: }
226:
227: // Export the give EJB groups as a jar file
228: private boolean export(Collection ejbGroups, String filePath) {
229: // Make user this is called before createXmlFile() before createXmlFile()
230: // modifies the jar location
231: Collection toBeJaredFiles = getJarFilePathes(ejbGroups);
232:
233: // Something is not right with at least one of the jar files
234: if (toBeJaredFiles == null)
235: return false;
236:
237: // First, write the metadata to a ejbsources.xml file in a tmp directory
238: String xmlFile = createXmlFile(ejbGroups);
239: if (xmlFile != null) {
240: // Then, jar the ejbsources.xml with client jars and wrapper jars into the file the user entered
241:
242: toBeJaredFiles.add(xmlFile);
243:
244: if (jarThemUp(filePath, toBeJaredFiles))
245: return true;
246: else
247: return false;
248: } else
249: return false;
250: }
251:
252: // Writes the given EJB groups to a ejbsources.xml file
253: private String createXmlFile(Collection ejbGroups) {
254: String xmlFile = System.getProperty("java.io.tmpdir")
255: + File.separator + "ejbsources.xml"; // NOI18N
256: //String xmlFile = "d:/home/cao" + File.separator + "ejbsources.xml"; // NOI18N
257:
258: try {
259: File file = new File(xmlFile);
260: file.deleteOnExit();
261:
262: OutputStreamWriter outputStreamWriter = new OutputStreamWriter(
263: new FileOutputStream(file), "UTF-8");
264: BufferedWriter bufferWriter = new BufferedWriter(
265: outputStreamWriter);
266:
267: // going to only write the jar filenames (not path) to xml file
268: for (Iterator iter = ejbGroups.iterator(); iter.hasNext();) {
269: EjbGroup grp = (EjbGroup) iter.next();
270: grp.setClientJarFiles(grp.getClientJarFileNames());
271: grp
272: .setClientWrapperBeanJar(org.netbeans.modules.visualweb.ejb.util.Util
273: .getFileName(grp
274: .getClientWrapperBeanJar()));
275: grp
276: .setDesignInfoJar(org.netbeans.modules.visualweb.ejb.util.Util
277: .getFileName(grp.getDesignInfoJar()));
278: }
279:
280: EjbDataSourceXmlCreator creator = new EjbDataSourceXmlCreator(
281: ejbGroups, bufferWriter);
282: creator.toXml();
283: bufferWriter.flush();
284: bufferWriter.close();
285:
286: return xmlFile;
287: } catch (java.io.IOException ex) {
288: ex.printStackTrace();
289: String msg = NbBundle.getMessage(
290: ExportEjbDataSourceDialog.class, "ERROR_EXPORT");
291: NotifyDescriptor d = new NotifyDescriptor.Message(msg,
292: NotifyDescriptor.ERROR_MESSAGE);
293: DialogDisplayer.getDefault().notify(d);
294: return null;
295: }
296: }
297:
298: private Collection getJarFilePathes(Collection ejbGroups) {
299: // Use set to eliminate dup jars
300: Set pathes = new HashSet();
301:
302: for (Iterator iter = ejbGroups.iterator(); iter.hasNext();) {
303: EjbGroup grp = (EjbGroup) iter.next();
304: pathes.addAll(grp.getClientJarFiles());
305: pathes.add(grp.getClientWrapperBeanJar());
306: pathes.add(grp.getDesignInfoJar());
307: }
308:
309: // Make sure the files exists
310: return checkJarFileExistence(pathes);
311: }
312:
313: private Collection checkJarFileExistence(Collection files) {
314: StringBuffer notFoundFiles = new StringBuffer();
315: boolean first = true;
316: for (Iterator iter = files.iterator(); iter.hasNext();) {
317: String path = (String) iter.next();
318:
319: if (!(new File(path)).exists()) {
320: if (first)
321: first = false;
322: else
323: notFoundFiles.append(", "); // NOI18N
324:
325: notFoundFiles.append(path);
326: }
327: }
328:
329: if (notFoundFiles.length() != 0) {
330: // Failed to export the selected EJB set(s). File(s) {0} not found.
331: String msg = NbBundle.getMessage(
332: ExportEjbDataSourceDialog.class, "FILES_NOT_FOUND",
333: notFoundFiles);
334: NotifyDescriptor d = new NotifyDescriptor.Message(msg,
335: NotifyDescriptor.ERROR_MESSAGE);
336: DialogDisplayer.getDefault().notify(d);
337:
338: return null;
339: } else
340: return files;
341: }
342:
343: // Jar the given collection of files to the given destJarFile
344: private boolean jarThemUp(String destJarFile, Collection files) {
345: try {
346: int BUFFER = 2048;
347:
348: BufferedInputStream origin = null;
349: FileOutputStream dest = new FileOutputStream(destJarFile);
350: JarOutputStream jarOutputStream = new JarOutputStream(
351: new BufferedOutputStream(dest));
352: jarOutputStream.setMethod(JarOutputStream.DEFLATED);
353:
354: byte data[] = new byte[BUFFER];
355:
356: Set alreadyAddedFileNames = new HashSet();
357:
358: for (Iterator iter = files.iterator(); iter.hasNext();) {
359: String filePath = (String) iter.next();
360:
361: // Since the entry name for the jar file is only the filename, not the whole path
362: // need to make sure that we do not add the same jar file more than once.
363: // It doesn't matter wheter the same file is from a different path or not
364: String fileName = new File(filePath).getName();
365: if (alreadyAddedFileNames.contains(fileName))
366: continue;
367: else {
368: alreadyAddedFileNames.add(fileName);
369:
370: // Add it to the jar
371:
372: FileInputStream fileInputStream = new FileInputStream(
373: filePath);
374: origin = new BufferedInputStream(fileInputStream,
375: BUFFER);
376:
377: JarEntry entry = new JarEntry(new File(filePath)
378: .getName());
379: jarOutputStream.putNextEntry(entry);
380:
381: int count;
382: while ((count = origin.read(data, 0, BUFFER)) != -1) {
383: jarOutputStream.write(data, 0, count);
384: }
385:
386: origin.close();
387: }
388: }
389:
390: jarOutputStream.close();
391:
392: return true;
393:
394: } catch (Exception e) {
395: e.printStackTrace();
396: String msg = NbBundle.getMessage(
397: ExportEjbDataSourceDialog.class, "ERROR_EXPORT");
398: NotifyDescriptor d = new NotifyDescriptor.Message(msg,
399: NotifyDescriptor.ERROR_MESSAGE);
400: DialogDisplayer.getDefault().notify(d);
401: return false;
402: }
403: }
404:
405: private PortableEjbDataSource[] getPortableEjbDataSources(
406: Collection defaultSelectedGroupNames) {
407: Set ejbGrps = EjbDataModel.getInstance().getEjbGroups();
408:
409: if (ejbGrps != null) {
410: PortableEjbDataSource[] ejbSrcs = new PortableEjbDataSource[ejbGrps
411: .size()];
412:
413: int i = 0;
414: for (Iterator iter = ejbGrps.iterator(); iter.hasNext();) {
415: EjbGroup grp = (EjbGroup) iter.next();
416:
417: // Should it be selected or not
418: // If the defaultSelectedGroupNames is null, that means everything should be selected
419: // Otherwise check whehter the group name is in the defaultSelectedGroupNames or not.
420: boolean selected = false;
421: if (defaultSelectedGroupNames == null)
422: selected = true;
423: else if (defaultSelectedGroupNames.contains(grp
424: .getName()))
425: selected = true;
426:
427: // Since the user can change some information on the ejb groups when exporting,
428: // we'll clone the ejb group here
429: ejbSrcs[i++] = new PortableEjbDataSource((EjbGroup) grp
430: .clone(), selected);
431: }
432:
433: return ejbSrcs;
434: }
435:
436: return null;
437: }
438: }
|