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: package org.netbeans.modules.sun.manager.jbi.nodes;
043:
044: import com.sun.esb.management.api.deployment.DeploymentService;
045: import com.sun.esb.management.common.ManagementRemoteException;
046: import java.awt.Image;
047: import java.io.File;
048: import java.util.ArrayList;
049: import java.util.List;
050: import java.util.Map;
051: import java.util.ResourceBundle;
052:
053: import javax.management.Attribute;
054: import javax.management.MBeanAttributeInfo;
055: import javax.swing.Action;
056: import javax.swing.JFileChooser;
057: import javax.swing.SwingUtilities;
058: import javax.xml.parsers.DocumentBuilder;
059: import javax.xml.parsers.DocumentBuilderFactory;
060: import javax.xml.parsers.ParserConfigurationException;
061:
062: import org.netbeans.modules.sun.manager.jbi.GenericConstants;
063: import org.netbeans.modules.sun.manager.jbi.management.JBIMBeanTaskResultHandler;
064: import org.netbeans.modules.sun.manager.jbi.util.FileFilters;
065: import org.netbeans.modules.sun.manager.jbi.util.ProgressUI;
066: import org.netbeans.modules.sun.manager.jbi.actions.DeployAction;
067: import org.netbeans.modules.sun.manager.jbi.actions.RefreshAction;
068: import org.netbeans.modules.sun.manager.jbi.management.AppserverJBIMgmtController;
069: import org.netbeans.modules.sun.manager.jbi.management.wrapper.api.RuntimeManagementServiceWrapper;
070: import org.netbeans.modules.sun.manager.jbi.util.Utils;
071: import org.openide.DialogDisplayer;
072: import org.openide.NotifyDescriptor;
073: import org.openide.util.NbBundle;
074: import org.openide.util.actions.SystemAction;
075: import org.openide.util.HelpCtx;
076:
077: /**
078: * Container node for all JBI Service Assemblies.
079: *
080: * @author jqian
081: */
082: public class JBIServiceAssembliesNode extends
083: AppserverJBIMgmtContainerNode implements Deployable {
084:
085: private static String lastInstallDir = null;
086:
087: private boolean busy;
088:
089: public JBIServiceAssembliesNode(
090: final AppserverJBIMgmtController controller) {
091: super (controller, NodeType.SERVICE_ASSEMBLIES);
092:
093: setDisplayName(NbBundle.getMessage(
094: JBIServiceAssembliesNode.class, "SERVICE_ASSEMBLIES")); // NOI18N
095: }
096:
097: /**
098: * Return the actions associated with the menu drop down seen when
099: * a user right-clicks on a node in the plugin.
100: *
101: * @param boolean true/false
102: * @return An array of Action objects.
103: */
104: @Override
105: public Action[] getActions(boolean flag) {
106: return new SystemAction[] {
107: SystemAction.get(DeployAction.DeployOnly.class),
108: SystemAction.get(DeployAction.DeployAndStart.class),
109: null, SystemAction.get(RefreshAction.class), };
110: }
111:
112: /**
113: *
114: */
115: @Override
116: public Image getIcon(int type) {
117: String iconName = IconConstants.FOLDER_ICON;
118: String badgeIconName = IconConstants.SERVICE_ASSEMBLIES_BADGE_ICON;
119: String externalBadgeIconName = busy ? IconConstants.BUSY_ICON
120: : null;
121: return Utils.getBadgedIcon(getClass(), iconName, badgeIconName,
122: externalBadgeIconName);
123: }
124:
125: /**
126: *
127: */
128: @Override
129: public Image getOpenedIcon(int type) {
130: return getIcon(type);
131: }
132:
133: @Override
134: public void refresh() {
135: // clear the cache first
136: RuntimeManagementServiceWrapper mgmtService = getRuntimeManagementServiceWrapper();
137:
138: mgmtService.clearServiceAssemblyStatusCache();
139:
140: super .refresh();
141: }
142:
143: /**
144: *
145: * @param busy
146: */
147: private void setBusy(boolean busy) {
148: this .busy = busy;
149: fireIconChange();
150: }
151:
152: public void deploy(boolean start) {
153:
154: DeploymentService deploymentService = getDeploymentService();
155: assert deploymentService != null;
156:
157: JFileChooser chooser = getJFileChooser();
158:
159: int returnValue = chooser.showDialog(null, NbBundle.getMessage(
160: JBIServiceAssembliesNode.class,
161: "LBL_Deploy_Service_Assembly_Button")); //NOI18N
162:
163: if (returnValue != JFileChooser.APPROVE_OPTION) {
164: return;
165: }
166:
167: File[] selectedFiles = chooser.getSelectedFiles();
168: if (selectedFiles.length > 0) {
169: lastInstallDir = selectedFiles[0].getParent();
170: }
171:
172: List<File> files = filterSelectedFiles(selectedFiles);
173: if (files.size() == 0) {
174: return;
175: }
176:
177: String message = NbBundle.getMessage(
178: JBIServiceAssembliesNode.class,
179: "LBL_Deploying_Service_Assembly"); // NOI18N
180: final ProgressUI progressUI = new ProgressUI(message, false);
181:
182: SwingUtilities.invokeLater(new Runnable() {
183: public void run() {
184: setBusy(true);
185: progressUI.start();
186: }
187: });
188:
189: for (File file : files) {
190: final String zipFilePath = file.getAbsolutePath();
191:
192: try {
193: String result = deploymentService
194: .deployServiceAssembly(zipFilePath,
195: SERVER_TARGET);
196: assert result != null;
197:
198: String lowerCaseResult = result.toLowerCase();
199: if (!lowerCaseResult.contains("error") && // NOI18N
200: !lowerCaseResult.contains("warning") && // NOI18N
201: !lowerCaseResult.contains("exception") && // NOI18N
202: !lowerCaseResult.contains("info")) { // NOI18N
203: if (start) {
204: // Start component automatically only upon
205: // successful installation.
206: // The successful installation result is the
207: // service assembly name.
208: String assembly = result;
209: RuntimeManagementServiceWrapper mgmtService = getRuntimeManagementServiceWrapper();
210: result = mgmtService.startServiceAssembly(
211: assembly, SERVER_TARGET);
212:
213: JBIMBeanTaskResultHandler
214: .showRemoteInvokationResult(
215: GenericConstants.START_SERVICE_ASSEMBLY_OPERATION_NAME,
216: assembly, result);
217: }
218: } else {
219: // Failed to deploy
220: JBIMBeanTaskResultHandler
221: .showRemoteInvokationResult(
222: GenericConstants.DEPLOY_SERVICE_ASSEMBLY_OPERATION_NAME,
223: zipFilePath, result);
224: }
225: } catch (ManagementRemoteException e) {
226: JBIMBeanTaskResultHandler
227: .showRemoteInvokationResult(
228: GenericConstants.DEPLOY_SERVICE_ASSEMBLY_OPERATION_NAME,
229: zipFilePath, e.getMessage());
230: }
231: }
232:
233: SwingUtilities.invokeLater(new Runnable() {
234: public void run() {
235: progressUI.finish();
236: setBusy(false);
237: }
238: });
239: }
240:
241: protected Map<Attribute, MBeanAttributeInfo> getGeneralSheetSetProperties() {
242: return null;
243: }
244:
245: public Attribute setSheetProperty(String attrName, Object value) {
246: return null;
247: }
248:
249: private JFileChooser getJFileChooser() {
250: JFileChooser chooser = new JFileChooser();
251:
252: ResourceBundle bundle = NbBundle
253: .getBundle(JBIComponentContainerNode.class);
254:
255: chooser.setDialogTitle(bundle
256: .getString("LBL_Deploy_Service_Assembly_Chooser_Name")); //NOI18N
257: chooser.setDialogType(JFileChooser.CUSTOM_DIALOG);
258: chooser.setApproveButtonMnemonic(bundle.getString(
259: "Deploy_Service_Assembly_Button_Mnemonic").charAt(0)); //NOI18N
260: chooser.setMultiSelectionEnabled(true);
261:
262: chooser
263: .addChoosableFileFilter(chooser
264: .getAcceptAllFileFilter());
265: chooser.addChoosableFileFilter(FileFilters.ArchiveFileFilter
266: .getInstance());
267:
268: chooser.setApproveButtonToolTipText(bundle
269: .getString("LBL_Deploy_Service_Assembly_Button")); //NOI18N
270: chooser
271: .getAccessibleContext()
272: .setAccessibleName(
273: bundle
274: .getString("LBL_Deploy_Service_Assembly_Chooser_Name")); //NOI18N
275: chooser
276: .getAccessibleContext()
277: .setAccessibleDescription(
278: bundle
279: .getString("LBL_Deploy_Service_Assembly_Chooser_Name")); //NOI18N
280:
281: if (lastInstallDir != null) {
282: chooser.setCurrentDirectory(new File(lastInstallDir));
283: }
284:
285: return chooser;
286: }
287:
288: private List<File> filterSelectedFiles(File[] files) {
289: List<File> ret = new ArrayList<File>();
290: DocumentBuilderFactory factory = DocumentBuilderFactory
291: .newInstance();
292: DocumentBuilder docBuilder = null;
293: try {
294: docBuilder = factory.newDocumentBuilder();
295: } catch (ParserConfigurationException ex) {
296: ex.printStackTrace();
297: }
298:
299: if (docBuilder != null) {
300: JBIArtifactValidator validator = JBIArtifactValidator
301: .getServiceAssemblyValidator();
302: for (File file : files) {
303: if (validator.validate(file)) {
304: ret.add(file);
305: } else {
306: String msg = NbBundle.getMessage(getClass(),
307: "MSG_INVALID_SERVICE_ASSEMBLY_DEPLOYMENT", // NOI18N
308: file.getName());
309: NotifyDescriptor d = new NotifyDescriptor.Message(
310: msg, NotifyDescriptor.ERROR_MESSAGE);
311: DialogDisplayer.getDefault().notify(d);
312: }
313: }
314: }
315:
316: return ret;
317: }
318:
319: @Override
320: public HelpCtx getHelpCtx() {
321: return new HelpCtx(JBIServiceAssembliesNode.class);
322: }
323: }
|