KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jmeter > module > loadgenerator > spi > impl > JMeterProcess


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.apache.jmeter.module.loadgenerator.spi.impl;
21
22 import java.awt.Image JavaDoc;
23 import java.beans.PropertyChangeEvent JavaDoc;
24 import java.beans.PropertyChangeListener JavaDoc;
25 import java.io.File JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.util.concurrent.Semaphore JavaDoc;
28 import org.apache.jmeter.engine.JMeterEngineException;
29 import org.apache.jmeter.module.exceptions.InitializationException;
30 import org.apache.jmeter.module.integration.*;
31 import org.apache.jmeter.util.JMeterUtils;
32 import org.netbeans.modules.loadgenerator.spi.ProcessInstance;
33 import org.netbeans.modules.loadgenerator.spi.Engine;
34 import org.openide.ErrorManager;
35
36 /**
37  *
38  * @author Jaroslav Bachorik
39  */

40 public class JMeterProcess extends ProcessInstance {
41   private Semaphore JavaDoc runSwitchLock = new Semaphore JavaDoc(1);
42   private boolean running = false;
43   
44   private ProcessDescriptor runningProcess = null;
45   
46   private PropertyChangeListener JavaDoc processStateChangeListener = new PropertyChangeListener JavaDoc() {
47     public void propertyChange(PropertyChangeEvent JavaDoc evt) {
48       final boolean state = ((Boolean JavaDoc)evt.getNewValue()).booleanValue();
49       
50       setRunning(state);
51       if (state) {
52         getWriter().println("JMeter test plan running");
53         publishStart(getEngine().getLogPath());
54       } else {
55         getWriter().println("JMeter test plan stopped");
56         publishStop();
57         runningProcess.removePropertyChangeListener(ProcessDescriptor.RUNNING, this);
58       }
59     }
60   };
61   
62   public JMeterProcess(final Engine factory) {
63     super(factory);
64   }
65   
66   public boolean isRunning() {
67     try {
68       try {
69         runSwitchLock.acquire();
70       } catch (InterruptedException JavaDoc e) {}
71       return running;
72     } finally {
73       runSwitchLock.release();
74     }
75   }
76   
77   public String JavaDoc getDisplayName() {
78     if (getCurrentScript() == null)
79       return "";
80     
81     String JavaDoc filename = getCurrentScript();
82     filename = filename.substring(filename.lastIndexOf(File.separatorChar) + 1);
83     return filename;
84   }
85   
86   public Image JavaDoc getIcon() {
87     return JMeterUtils.getImage("beaker.gif").getImage();
88   }
89   
90   private void setRunning(final boolean value) {
91     try {
92       try {
93         runSwitchLock.acquire();
94       } catch (InterruptedException JavaDoc e) {}
95       running = value;
96     } finally {
97       runSwitchLock.release();
98     }
99   }
100   
101   private JMeterIntegrationEngine getEngine() {
102     try {
103       return JMeterIntegrationEngine.getDefault();
104     } catch (InitializationException e) {
105       ErrorManager.getDefault().notify(ErrorManager.EXCEPTION, e);
106     }
107     return null;
108   }
109   
110   public void performStart(final String JavaDoc scriptFileName) {
111     try {
112       getWriter().reset();
113       getWriter().print("Starting JMeter subsystem... ");
114       runningProcess = getEngine().prepareTest(scriptFileName);
115       
116       getWriter().println("Done");
117       if (runningProcess != null) {
118         runningProcess.addPropertyChangeListener(ProcessDescriptor.RUNNING, processStateChangeListener);
119         getEngine().clearLog();
120         getWriter().println("Starting JMeter test plan named " + runningProcess.getDisplayName() + " (" + scriptFileName + ")");
121         getWriter().println("Simulating " + runningProcess.getThreadsCount() + " users with ramp-up time of " + runningProcess.getRampup() + "s");
122         runningProcess.start();
123       } else {
124         throw new JMeterEngineException("Can't start JMeter script " + scriptFileName);
125       }
126     } catch (JMeterEngineException e) {
127       ErrorManager.getDefault().log(ErrorManager.EXCEPTION, e.getMessage());
128     } catch (IOException JavaDoc e) {
129       ErrorManager.getDefault().log(ErrorManager.EXCEPTION, e.getMessage());
130     }
131   }
132   
133   public void performStop(final boolean force) {
134     if (runningProcess != null) {
135       getWriter().println("Stopping JMeter test plan");
136       runningProcess.stop();
137     } else {
138       ErrorManager.getDefault().log(ErrorManager.WARNING, "Stopping a non-running instance");
139     }
140   }
141 }
142
Popular Tags