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: package org.netbeans.modules.sun.manager.jbi.nodes;
042:
043: import com.sun.esb.management.api.installation.InstallationService;
044: import com.sun.esb.management.common.ManagementRemoteException;
045: import java.awt.Frame;
046: import java.io.IOException;
047: import org.netbeans.modules.sun.manager.jbi.management.JBIComponentType;
048: import java.awt.Image;
049: import java.io.BufferedReader;
050: import java.io.File;
051: import java.io.FileReader;
052: import java.io.InputStream;
053: import java.io.InputStreamReader;
054: import java.io.StringReader;
055: import java.util.ArrayList;
056: import java.util.List;
057: import java.util.Map;
058: import java.util.Properties;
059: import java.util.ResourceBundle;
060: import java.util.jar.JarFile;
061: import java.util.logging.Logger;
062: import java.util.zip.ZipEntry;
063: import javax.management.Attribute;
064: import javax.management.MBeanAttributeInfo;
065: import javax.swing.Action;
066: import javax.swing.JFileChooser;
067: import javax.swing.SwingUtilities;
068: import javax.xml.parsers.DocumentBuilder;
069: import javax.xml.parsers.DocumentBuilderFactory;
070: import javax.xml.parsers.ParserConfigurationException;
071: import org.netbeans.modules.sun.manager.jbi.GenericConstants;
072: import org.netbeans.modules.sun.manager.jbi.management.JBIMBeanTaskResultHandler;
073: import org.netbeans.modules.sun.manager.jbi.util.ProgressUI;
074: import org.netbeans.modules.sun.manager.jbi.actions.InstallAction;
075: import org.netbeans.modules.sun.manager.jbi.actions.RefreshAction;
076: import org.netbeans.modules.sun.manager.jbi.management.AppserverJBIMgmtController;
077: import org.netbeans.modules.sun.manager.jbi.management.model.JBIComponentConfigurationDescriptor;
078: import org.netbeans.modules.sun.manager.jbi.management.model.JBIComponentConfigurationParser;
079: import org.netbeans.modules.sun.manager.jbi.management.wrapper.api.RuntimeManagementServiceWrapper;
080: import org.netbeans.modules.sun.manager.jbi.util.FileFilters;
081: import org.netbeans.modules.sun.manager.jbi.util.Utils;
082: import org.openide.DialogDisplayer;
083: import org.openide.NotifyDescriptor;
084: import org.openide.util.Exceptions;
085: import org.openide.util.NbBundle;
086: import org.openide.util.actions.SystemAction;
087: import org.openide.util.HelpCtx;
088: import org.openide.windows.WindowManager;
089: import org.w3c.dom.Document;
090: import org.w3c.dom.Element;
091: import org.w3c.dom.NodeList;
092: import org.xml.sax.InputSource;
093: import org.xml.sax.SAXException;
094:
095: /**
096: * Container node for all the JBI Components (SE, BC, or Shared Library)
097: * of the same type.
098: *
099: * @author jqian
100: */
101: public abstract class JBIComponentContainerNode extends
102: AppserverJBIMgmtContainerNode implements Installable {
103:
104: private boolean busy;
105:
106: private static Logger logger = Logger
107: .getLogger("org.netbeans.modules.sun.manager.jbi.node.JBIComponentContainerNode"); // NOI18N
108:
109: public JBIComponentContainerNode(
110: final AppserverJBIMgmtController controller, NodeType type,
111: String name) {
112: super (controller, type);
113: setDisplayName(name);
114: }
115:
116: @Override
117: public Image getIcon(int type) {
118: String iconName = IconConstants.FOLDER_ICON;
119: String badgeIconName = getBadgeIconName();
120: String externalBadgeIconName = busy ? IconConstants.BUSY_ICON
121: : null;
122: return Utils.getBadgedIcon(getClass(), iconName, badgeIconName,
123: externalBadgeIconName);
124: }
125:
126: @Override
127: public Image getOpenedIcon(int type) {
128: return getIcon(type);
129: }
130:
131: @Override
132: public void refresh() {
133: // clear the cache first
134: RuntimeManagementServiceWrapper mgmtService = getRuntimeManagementServiceWrapper();
135: mgmtService.clearJBIComponentStatusCache(getComponentType());
136:
137: super .refresh();
138: }
139:
140: /**
141: *
142: * @param busy
143: */
144: private void setBusy(boolean busy) {
145: this .busy = busy;
146: fireIconChange();
147: }
148:
149: protected Map<Attribute, MBeanAttributeInfo> getGeneralSheetSetProperties() {
150: return null;
151: }
152:
153: public Attribute setSheetProperty(String attrName, Object value) {
154: return null;
155: }
156:
157: public void install(boolean start) {
158:
159: InstallationService installationService = getInstallationService();
160: assert installationService != null;
161:
162: JFileChooser chooser = getJFileChooser();
163: int returnValue = chooser.showDialog(null, NbBundle.getMessage(
164: JBIComponentContainerNode.class,
165: "LBL_Install_JBI_Component_Button")); //NOI18N
166:
167: if (returnValue != JFileChooser.APPROVE_OPTION) {
168: return;
169: }
170:
171: File[] selectedFiles = chooser.getSelectedFiles();
172: if (selectedFiles.length > 0) {
173: System
174: .setProperty(
175: JBIComponentNode.LAST_JBI_COMPONENT_INSTALLATION_DIRECTORY,
176: selectedFiles[0].getParent());
177: }
178:
179: List<File> files = filterSelectedFiles(selectedFiles);
180: if (files.size() == 0) {
181: return;
182: }
183:
184: String progressLabel = getInstallProgressMessageLabel();
185: String message = NbBundle.getMessage(
186: JBIComponentContainerNode.class, progressLabel);
187: final ProgressUI progressUI = new ProgressUI(message, false);
188:
189: SwingUtilities.invokeLater(new Runnable() {
190:
191: public void run() {
192: setBusy(true);
193: progressUI.start();
194: }
195: });
196:
197: for (File file : files) {
198: final String jarFilePath = file.getAbsolutePath();
199:
200: try {
201: String result = installComponent(jarFilePath);
202:
203: if (result != null) {
204: String lowerCaseResult = result.toLowerCase();
205: if (!lowerCaseResult.contains("error") && // NOI18N
206: !lowerCaseResult.contains("warning") && // NOI18N
207: !lowerCaseResult.contains("exception") && // NOI18N
208: !lowerCaseResult.contains("info")) { // NOI18N
209: if (start) {
210: // Start component automatically only upon
211: // successful installation.
212: // The successful installation result is the
213: // component ID.
214: try {
215: String componentID = result;
216: RuntimeManagementServiceWrapper mgmtService = getRuntimeManagementServiceWrapper();
217: result = mgmtService.startComponent(
218: componentID, SERVER_TARGET);
219:
220: JBIMBeanTaskResultHandler
221: .showRemoteInvokationResult(
222: GenericConstants.START_COMPONENT_OPERATION_NAME,
223: componentID, result);
224: } catch (ManagementRemoteException e) {
225: JBIMBeanTaskResultHandler
226: .showRemoteInvokationResult(
227: GenericConstants.START_COMPONENT_OPERATION_NAME,
228: jarFilePath, e
229: .getMessage());
230: }
231: }
232: } else {
233: // Failed to install
234: JBIMBeanTaskResultHandler
235: .showRemoteInvokationResult(
236: GenericConstants.INSTALL_COMPONENT_OPERATION_NAME,
237: jarFilePath, result);
238: }
239: }
240: } catch (ManagementRemoteException e) {
241: JBIMBeanTaskResultHandler
242: .showRemoteInvokationResult(
243: GenericConstants.INSTALL_COMPONENT_OPERATION_NAME,
244: jarFilePath, e.getMessage());
245: }
246: }
247:
248: SwingUtilities.invokeLater(new Runnable() {
249:
250: public void run() {
251: progressUI.finish();
252: setBusy(false);
253: }
254: });
255: }
256:
257: private List<File> filterSelectedFiles(File[] files) {
258: List<File> ret = new ArrayList<File>();
259: DocumentBuilderFactory factory = DocumentBuilderFactory
260: .newInstance();
261: DocumentBuilder docBuilder = null;
262: try {
263: docBuilder = factory.newDocumentBuilder();
264: } catch (ParserConfigurationException ex) {
265: ex.printStackTrace();
266: }
267:
268: if (docBuilder != null) {
269: JBIArtifactValidator validator = getValidator();
270: for (File file : files) {
271: if (validator.validate(file)) {
272: ret.add(file);
273: } else {
274: String compType = NbBundle.getMessage(getClass(),
275: getComponentTypeLabel());
276: String msg = NbBundle.getMessage(getClass(),
277: "MSG_INVALID_COMPONENT_INSTALLATION", // NOI18N
278: file.getName(), compType);
279: NotifyDescriptor d = new NotifyDescriptor.Message(
280: msg, NotifyDescriptor.ERROR_MESSAGE);
281: DialogDisplayer.getDefault().notify(d);
282: }
283: }
284: }
285:
286: return ret;
287: }
288:
289: private JFileChooser getJFileChooser() {
290: JFileChooser chooser = new JFileChooser();
291:
292: ResourceBundle bundle = NbBundle
293: .getBundle(JBIComponentContainerNode.class);
294:
295: String titleLabel = getFileChooserTitleLabel();
296: chooser.setDialogTitle(bundle.getString(titleLabel));
297: chooser.setDialogType(JFileChooser.CUSTOM_DIALOG);
298:
299: chooser.setApproveButtonMnemonic(bundle.getString(
300: "Install_JBI_Component_Button_Mnemonic").charAt(0)); //NOI18N
301: chooser.setMultiSelectionEnabled(true);
302:
303: chooser
304: .addChoosableFileFilter(chooser
305: .getAcceptAllFileFilter());
306: chooser.addChoosableFileFilter(FileFilters.JarFileFilter
307: .getInstance());
308:
309: chooser.setApproveButtonToolTipText(bundle
310: .getString("LBL_Install_JBI_Component_Button")); //NOI18N
311:
312: chooser.getAccessibleContext().setAccessibleName(
313: bundle.getString(titleLabel));
314: chooser.getAccessibleContext().setAccessibleDescription(
315: bundle.getString(titleLabel));
316:
317: String lastInstallDir = System
318: .getProperty(JBIComponentNode.LAST_JBI_COMPONENT_INSTALLATION_DIRECTORY);
319: if (lastInstallDir != null) {
320: chooser.setCurrentDirectory(new File(lastInstallDir));
321: }
322:
323: return chooser;
324: }
325:
326: /**
327: * Installs the given JBI Component or Shared Library.
328: *
329: * @param jarFilePath absolute path for the component jar file
330: * @return the component ID if the installation is successful;
331: * or the error string in XML if the installation is failed;
332: * or <code>null</code> if the component installation is cancelled.
333: * @throws com.sun.esb.management.common.ManagementRemoteException
334: */
335: protected abstract String installComponent(String jarFilePath)
336: throws ManagementRemoteException;
337:
338: protected abstract String getFileChooserTitleLabel();
339:
340: protected abstract String getInstallProgressMessageLabel();
341:
342: protected abstract String getBadgeIconName();
343:
344: protected abstract JBIArtifactValidator getValidator();
345:
346: protected abstract String getComponentTypeLabel();
347:
348: protected abstract JBIComponentType getComponentType();
349:
350: //==========================================================================
351: /**
352: * Container node for all Service Engines or Binding Components.
353: */
354: static abstract class RealJBIComponentContainerNode extends
355: JBIComponentContainerNode {
356:
357: RealJBIComponentContainerNode(
358: final AppserverJBIMgmtController controller,
359: NodeType type, String name) {
360: super (controller, type, name);
361: }
362:
363: @Override
364: public Action[] getActions(boolean flag) {
365: return new SystemAction[] {
366: SystemAction.get(InstallAction.InstallOnly.class),
367: SystemAction
368: .get(InstallAction.InstallAndStart.class),
369: null, SystemAction.get(RefreshAction.class) };
370: }
371:
372: protected String installComponent(String jarFilePath)
373: throws ManagementRemoteException {
374: Properties properties = new Properties();
375:
376: InstallationService installationService = getInstallationService();
377:
378: try {
379: // Get jbi.xml from component jar file
380: JarFile jarFile = new JarFile(jarFilePath);
381: ZipEntry jbiEntry = jarFile
382: .getEntry("META-INF/jbi.xml");
383: InputStream is = jarFile.getInputStream(jbiEntry);
384: String jbiXml = getContent(is);
385:
386: DocumentBuilderFactory factory = DocumentBuilderFactory
387: .newInstance();
388: factory.setNamespaceAware(true);
389: DocumentBuilder builder = factory.newDocumentBuilder();
390: Document jbiDoc = builder.parse(new InputSource(
391: new StringReader(jbiXml)));
392:
393: // Get configuration descriptor from jbi.xml
394: final JBIComponentConfigurationDescriptor rootDescriptor = JBIComponentConfigurationParser
395: .parse(jbiDoc);
396: if (rootDescriptor != null
397: && rootDescriptor
398: .showDisplayAtInstallation(true)) {
399: String componentName = getComponentIDFromJbiDoc(jbiDoc);
400:
401: // Show installation configuration dialog.
402: JBIComponentInstallationConfigurationDialog dialog = new JBIComponentInstallationConfigurationDialog(
403: componentName, rootDescriptor);
404: dialog.pack();
405: dialog.setLocationRelativeTo(null);
406: dialog.setVisible(true);
407: if (dialog.isCancelled()) {
408: return null;
409: }
410:
411: properties = dialog.getProperties();
412:
413: logger.info("Install " + componentName + // NOI18N
414: " with these properties:" + properties); // NOI18N
415: }
416: } catch (Exception ex) {
417: logger.severe(ex.getMessage());
418: NotifyDescriptor d = new NotifyDescriptor.Message(ex
419: .getMessage(), NotifyDescriptor.ERROR_MESSAGE);
420: DialogDisplayer.getDefault().notify(d);
421: }
422:
423: return installationService.installComponent(jarFilePath,
424: properties, SERVER_TARGET);
425: }
426:
427: private String getComponentIDFromJbiDoc(Document jbiDoc) {
428: NodeList ns = jbiDoc.getElementsByTagName("component"); // NOI18N
429: if (ns.getLength() > 0) {
430: Element e = (Element) ns.item(0);
431: NodeList ids = e.getElementsByTagName("identification"); // NOI18N
432: if (ids.getLength() > 0) {
433: Element id = (Element) ids.item(0);
434: NodeList names = id.getElementsByTagName("name"); // NOI18N
435: if (names.getLength() > 0) {
436: Element n = (Element) names.item(0);
437: return n.getFirstChild().getNodeValue();
438: }
439: }
440: }
441:
442: return null;
443: }
444:
445: private String getContent(InputStream is) {
446: try {
447: BufferedReader in = new BufferedReader(
448: new InputStreamReader(is));
449: StringBuffer buffer = new StringBuffer();
450: String line;
451: while ((line = in.readLine()) != null) {
452: buffer.append(line);
453: }
454: return buffer.toString();
455: } catch (IOException ex) {
456: Exceptions.printStackTrace(ex);
457: }
458: return null;
459: }
460:
461: private String getContent(File file) {
462: String ret = "";
463:
464: BufferedReader is = null;
465: try {
466: is = new BufferedReader(new FileReader(file));
467: String inputLine;
468: while ((inputLine = is.readLine()) != null) {
469: ret += inputLine;
470: }
471: } catch (IOException e) {
472: System.out.println("IOException: " + e);
473: } finally {
474: if (is != null) {
475: try {
476: is.close();
477: } catch (Exception e) {
478: }
479: }
480: }
481:
482: return ret;
483: }
484: }
485:
486: /**
487: * Container node for all JBI Service Engines.
488: */
489: static class ServiceEngines extends RealJBIComponentContainerNode {
490:
491: ServiceEngines(final AppserverJBIMgmtController controller) {
492: super (controller, NodeType.SERVICE_ENGINES, NbBundle
493: .getMessage(JBIComponentContainerNode.class,
494: "SERVICE_ENGINES")); // NOI18N
495: }
496:
497: protected JBIArtifactValidator getValidator() {
498: return JBIArtifactValidator.getServiceEngineValidator(null);
499: }
500:
501: protected String getFileChooserTitleLabel() {
502: return "LBL_Install_Service_Engine_Chooser_Name"; // NOI18N
503: }
504:
505: protected String getInstallProgressMessageLabel() {
506: return "LBL_Installing_Service_Engine"; // NOI18N
507: }
508:
509: protected String getBadgeIconName() {
510: return IconConstants.SERVICE_ENGINES_BADGE_ICON;
511: }
512:
513: protected String getComponentTypeLabel() {
514: return "SERVICE_ENGINE"; // NOI18N
515: }
516:
517: protected JBIComponentType getComponentType() {
518: return JBIComponentType.SERVICE_ENGINE;
519: }
520:
521: @Override
522: public HelpCtx getHelpCtx() {
523: return new HelpCtx(this .getClass());
524: }
525: }
526:
527: /**
528: * Container node for all JBI Binding Components.
529: */
530: static class BindingComponents extends
531: RealJBIComponentContainerNode {
532:
533: BindingComponents(final AppserverJBIMgmtController controller) {
534: super (controller, NodeType.BINDING_COMPONENTS, NbBundle
535: .getMessage(JBIComponentContainerNode.class,
536: "BINDING_COMPONENTS")); // NOI18N
537: }
538:
539: protected JBIArtifactValidator getValidator() {
540: return JBIArtifactValidator
541: .getBindingComponentValidator(null);
542: }
543:
544: protected String getFileChooserTitleLabel() {
545: return "LBL_Install_Binding_Component_Chooser_Name"; // NOI18N
546: }
547:
548: protected String getInstallProgressMessageLabel() {
549: return "LBL_Installing_Binding_Component"; // NOI18N
550: }
551:
552: protected String getBadgeIconName() {
553: return IconConstants.BINDING_COMPONENTS_BADGE_ICON;
554: }
555:
556: protected String getComponentTypeLabel() {
557: return "BINDING_COMPONENT"; // NOI18N
558: }
559:
560: protected JBIComponentType getComponentType() {
561: return JBIComponentType.BINDING_COMPONENT;
562: }
563:
564: @Override
565: public HelpCtx getHelpCtx() {
566: return new HelpCtx(this .getClass());
567: }
568: }
569:
570: /**
571: * Container node for all JBI Shared Libraries.
572: */
573: static class SharedLibraries extends JBIComponentContainerNode {
574:
575: SharedLibraries(final AppserverJBIMgmtController controller) {
576: super (controller, NodeType.SHARED_LIBRARIES, NbBundle
577: .getMessage(JBIComponentContainerNode.class,
578: "SHARED_LIBRARIES")); // NOI18N
579: }
580:
581: @Override
582: public Action[] getActions(boolean flag) {
583: return new SystemAction[] {
584: SystemAction.get(InstallAction.InstallOnly.class),
585: null, SystemAction.get(RefreshAction.class) };
586: }
587:
588: protected String installComponent(String jarFilePath)
589: throws ManagementRemoteException {
590: InstallationService installationService = getInstallationService();
591: return installationService.installSharedLibrary(
592: jarFilePath, SERVER_TARGET);
593: }
594:
595: protected JBIArtifactValidator getValidator() {
596: return JBIArtifactValidator.getSharedLibraryValidator();
597: }
598:
599: protected String getFileChooserTitleLabel() {
600: return "LBL_Install_Shared_Library_Chooser_Name"; // NOI18N
601: }
602:
603: protected String getInstallProgressMessageLabel() {
604: return "LBL_Installing_Shared_Library"; // NOI18N
605: }
606:
607: protected String getBadgeIconName() {
608: return IconConstants.SHARED_LIBRARIES_BADGE_ICON;
609: }
610:
611: protected String getComponentTypeLabel() {
612: return "SHARED_LIBRARY"; // NOI18N
613: }
614:
615: protected JBIComponentType getComponentType() {
616: return JBIComponentType.SHARED_LIBRARY;
617: }
618:
619: @Override
620: public HelpCtx getHelpCtx() {
621: return new HelpCtx(this.getClass());
622: }
623: }
624: }
|