001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018: package org.apache.jmeter.sampler;
019:
020: import org.apache.jmeter.samplers.AbstractSampler;
021: import org.apache.jmeter.samplers.Entry;
022: import org.apache.jmeter.samplers.SampleResult;
023: import org.apache.jmeter.testelement.property.IntegerProperty;
024: import org.apache.jmeter.testelement.property.StringProperty;
025: import org.apache.jmeter.threads.JMeterContext;
026: import org.apache.jmeter.threads.JMeterContextService;
027: import org.apache.jorphan.logging.LoggingManager;
028: import org.apache.log.Logger;
029:
030: /**
031: * Dummy Sampler used to pause or stop a thread or the test;
032: * intended for use in Conditional Controllers.
033: *
034: */
035: public class TestAction extends AbstractSampler {
036:
037: private static final Logger log = LoggingManager
038: .getLoggerForClass();
039:
040: // Actions
041: public final static int STOP = 0;
042: public final static int PAUSE = 1;
043:
044: // Action targets
045: public final static int THREAD = 0;
046: // public final static int THREAD_GROUP = 1;
047: public final static int TEST = 2;
048:
049: // Identifiers
050: private final static String TARGET = "ActionProcessor.target"; //$NON-NLS-1$
051: private final static String ACTION = "ActionProcessor.action"; //$NON-NLS-1$
052: private final static String DURATION = "ActionProcessor.duration"; //$NON-NLS-1$
053:
054: public TestAction() {
055: super ();
056: }
057:
058: /*
059: * (non-Javadoc)
060: *
061: * @see org.apache.jmeter.samplers.Sampler#sample(org.apache.jmeter.samplers.Entry)
062: */
063: public SampleResult sample(Entry e) {
064: JMeterContext context = JMeterContextService.getContext();
065:
066: int target = getTarget();
067: int action = getAction();
068: if (action == PAUSE) {
069: pause(getDurationAsString());
070: } else if (action == STOP) {
071: if (target == THREAD) {
072: context.getThread().stop();
073: }
074: // Not yet implemented
075: // else if (target==THREAD_GROUP)
076: // {
077: // }
078: else if (target == TEST) {
079: context.getEngine().askThreadsToStop();
080: }
081: }
082:
083: return null; // This means no sample is saved
084: }
085:
086: private void pause(String mili_s) {
087: int milis;
088: try {
089: milis = Integer.parseInt(mili_s);
090: } catch (NumberFormatException e) {
091: log.warn("Could not create number from " + mili_s);
092: milis = 0;
093: }
094: try {
095: Thread.sleep(milis);
096: } catch (InterruptedException e) {
097: }
098: }
099:
100: public void setTarget(int target) {
101: setProperty(new IntegerProperty(TARGET, target));
102: }
103:
104: public int getTarget() {
105: return getPropertyAsInt(TARGET);
106: }
107:
108: public void setAction(int action) {
109: setProperty(new IntegerProperty(ACTION, action));
110: }
111:
112: public int getAction() {
113: return getPropertyAsInt(ACTION);
114: }
115:
116: public void setDuration(String duration) {
117: setProperty(new StringProperty(DURATION, duration));
118: }
119:
120: public String getDurationAsString() {
121: return getPropertyAsString(DURATION);
122: }
123: }
|