001: package org.apache.jmeter.module;
002:
003: import java.io.File;
004: import java.io.IOException;
005: import java.util.Collection;
006: import org.netbeans.modules.loadgenerator.api.EngineManager;
007: import org.netbeans.modules.loadgenerator.spi.Engine;
008: import org.openide.filesystems.FileObject;
009: import org.openide.filesystems.FileUtil;
010: import org.openide.loaders.DataObject;
011: import org.openide.nodes.Node;
012: import org.openide.util.HelpCtx;
013: import org.openide.util.Lookup;
014: import org.openide.util.NbBundle;
015: import org.openide.util.actions.CookieAction;
016:
017: public final class JMeterRunAction extends CookieAction {
018:
019: protected void performAction(Node[] activatedNodes) {
020: DataObject c = (DataObject) activatedNodes[0]
021: .getCookie(DataObject.class);
022: try {
023: File script = FileUtil.toFile(c.getPrimaryFile());
024: // JMeterIntegrationEngine.getDefault().runTestPlan(script.getCanonicalPath());
025: final String path = script.getCanonicalPath();
026:
027: EngineManager manager = Lookup.getDefault().lookup(
028: EngineManager.class);
029: Collection<Engine> loadgens = manager.findEngines(FileUtil
030: .getExtension(path));
031: if (loadgens.size() == 1) {
032: Engine provider = loadgens.iterator().next();
033:
034: manager.startProcess(provider.createProcess(path));
035: }
036: } catch (Exception e) {
037: e.printStackTrace();
038: }
039: }
040:
041: protected int mode() {
042: return CookieAction.MODE_EXACTLY_ONE;
043: }
044:
045: public String getName() {
046: return NbBundle.getMessage(JMeterRunAction.class,
047: "CTL_JMeterRunAction");
048: }
049:
050: protected Class[] cookieClasses() {
051: return new Class[] { DataObject.class };
052: }
053:
054: protected void initialize() {
055: super .initialize();
056: // see org.openide.util.actions.SystemAction.iconResource() javadoc for more details
057: putValue("noIconInMenu", Boolean.TRUE);
058: }
059:
060: public HelpCtx getHelpCtx() {
061: return HelpCtx.DEFAULT_HELP;
062: }
063:
064: protected boolean enable(Node[] node) {
065: boolean retValue;
066: if (node.length < 1)
067: return false;
068:
069: retValue = super .enable(node);
070: if (node == null || node[0] == null)
071: return retValue;
072:
073: DataObject c = (DataObject) node[0].getCookie(DataObject.class);
074: FileObject primaryFile = c != null ? c.getPrimaryFile() : null;
075: /* according to http://www.netbeans.org/issues/show_bug.cgi?id=94823
076: * using "c" without checking for NULL causes NPE randomly; this check should prevent NPE
077: */
078: if (primaryFile == null)
079: return false;
080:
081: try {
082: File script = FileUtil.toFile(primaryFile);
083: // JMeterIntegrationEngine.getDefault().runTestPlan(script.getCanonicalPath());
084: final String path = script.getCanonicalPath();
085:
086: EngineManager manager = Lookup.getDefault().lookup(
087: EngineManager.class);
088: Collection<Engine> loadgens = manager.findEngines(FileUtil
089: .getExtension(path));
090: for (Engine provider : loadgens) {
091: retValue = retValue
092: && provider.isReady()
093: && (provider.getProcessByName(path) == null || !provider
094: .getProcessByName(path).isRunning());
095: }
096: } catch (IOException ex) {
097: }
098:
099: return retValue;
100: }
101:
102: protected boolean asynchronous() {
103: return false;
104: }
105:
106: }
|