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-2006 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.apache.jmeter.module.loadgenerator.spi.impl;
043:
044: import java.awt.Image;
045: import java.beans.PropertyChangeEvent;
046: import java.beans.PropertyChangeListener;
047: import java.io.File;
048: import java.io.IOException;
049: import java.util.concurrent.Semaphore;
050: import java.util.concurrent.atomic.AtomicBoolean;
051: import org.apache.jmeter.engine.JMeterEngineException;
052: import org.apache.jmeter.module.exceptions.InitializationException;
053: import org.apache.jmeter.module.integration.*;
054: import org.apache.jmeter.util.JMeterUtils;
055: import org.netbeans.modules.loadgenerator.spi.ProcessInstance;
056: import org.netbeans.modules.loadgenerator.spi.Engine;
057: import org.openide.ErrorManager;
058:
059: /**
060: *
061: * @author Jaroslav Bachorik
062: */
063: public class JMeterProcess extends ProcessInstance
064:
065: {
066:
067: private AtomicBoolean runningState = new AtomicBoolean(
068: Boolean.FALSE);
069:
070: private ProcessDescriptor runningProcess = null;
071: private Semaphore runningProcessSemaphore = new Semaphore(1);
072:
073: private PropertyChangeListener processStateChangeListener = new PropertyChangeListener
074:
075: () {
076:
077: public void propertyChange(PropertyChangeEvent evt) {
078: final boolean state = ((Boolean) evt.getNewValue())
079: .booleanValue();
080:
081: try {
082: runningProcessSemaphore.acquire();
083: runningState.set(state);
084:
085: if (state) {
086: getWriter().println("JMeter test plan running");
087: publishStart(getEngine().getLogPath());
088: } else {
089: getWriter().println("JMeter test plan stopped");
090: publishStop();
091: runningProcess.removePropertyChangeListener(
092: ProcessDescriptor.RUNNING, this );
093: }
094: } catch (InterruptedException e) {
095: Thread.currentThread().interrupt();
096: } finally {
097: runningProcessSemaphore.release();
098: }
099: }
100: };
101:
102: public JMeterProcess(final Engine factory) {
103: super (factory);
104: }
105:
106: public boolean isRunning() {
107: return runningState.get();
108: }
109:
110: public String getDisplayName() {
111: if (getCurrentScript() == null) {
112: return "";
113: }
114: String filename = getCurrentScript();
115: filename = filename.substring(filename
116: .lastIndexOf(File.separatorChar) + 1);
117: return filename;
118: }
119:
120: public Image getIcon() {
121: return JMeterUtils.getImage("beaker.gif").getImage();
122: }
123:
124: private void setRunning(final boolean value) {
125: runningState.set(value);
126: }
127:
128: private JMeterIntegrationEngine getEngine() {
129: try {
130: return JMeterIntegrationEngine.getDefault();
131: } catch (InitializationException e) {
132: ErrorManager.getDefault().notify(ErrorManager.EXCEPTION, e);
133: }
134: return null;
135: }
136:
137: public void performStart(final String scriptFileName) {
138: try {
139: runningProcessSemaphore.acquire();
140: if (runningState.compareAndSet(false, true)) {
141: getWriter().reset();
142: getWriter().print("Starting JMeter subsystem... ");
143: runningProcess = getEngine()
144: .prepareTest(scriptFileName);
145:
146: getWriter().println("Done");
147: if (runningProcess != null) {
148: runningProcess.addPropertyChangeListener(
149: ProcessDescriptor.RUNNING,
150: processStateChangeListener);
151: getEngine().clearLog();
152: getWriter().println(
153: "Starting JMeter test plan named "
154: + runningProcess.getDisplayName()
155: + " (" + scriptFileName + ")");
156: getWriter().println(
157: "Simulating "
158: + runningProcess.getThreadsCount()
159: + " users with ramp-up time of "
160: + runningProcess.getRampup() + "s");
161: runningProcess.start();
162: } else {
163: throw new JMeterEngineException(
164: "Can't start JMeter script "
165: + scriptFileName);
166: }
167: }
168: } catch (JMeterEngineException e) {
169: ErrorManager.getDefault().log(ErrorManager.EXCEPTION,
170: e.getMessage());
171: } catch (IOException e) {
172: ErrorManager.getDefault().log(ErrorManager.EXCEPTION,
173: e.getMessage());
174: } catch (InterruptedException ex) {
175: Thread.currentThread().interrupt();
176: } finally {
177: runningProcessSemaphore.release();
178: }
179: }
180:
181: public void performStop(final boolean force) {
182: try {
183: runningProcessSemaphore.acquire();
184: if (runningProcess != null
185: && runningState.compareAndSet(true, false)) {
186: getWriter().println("Stopping JMeter test plan");
187: runningProcess.stop();
188: } else {
189: ErrorManager.getDefault().log(ErrorManager.WARNING,
190: "Stopping a non-running instance");
191: }
192: } catch (InterruptedException ex) {
193: Thread.currentThread().interrupt();
194: } finally {
195: runningProcessSemaphore.release();
196: }
197: }
198: }
|