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.protocol.jms.sampler;
019:
020: import org.apache.jorphan.io.TextFile;
021: import org.apache.jmeter.samplers.Entry;
022: import org.apache.jmeter.samplers.SampleResult;
023: import org.apache.jmeter.services.FileServer;
024: import org.apache.jmeter.testelement.TestListener;
025: import org.apache.jmeter.engine.event.LoopIterationEvent;
026:
027: import org.apache.jmeter.protocol.jms.control.gui.JMSPublisherGui;
028: import org.apache.jmeter.protocol.jms.client.ClientPool;
029: import org.apache.jmeter.protocol.jms.client.Publisher;
030:
031: import org.apache.jorphan.logging.LoggingManager;
032: import org.apache.log.Logger;
033:
034: public class PublisherSampler extends BaseJMSSampler implements
035: TestListener {
036:
037: private static final Logger log = LoggingManager
038: .getLoggerForClass();
039:
040: //++ These are JMX file names and must not be changed
041: private static final String INPUT_FILE = "jms.input_file"; //$NON-NLS-1$
042:
043: private static final String RANDOM_PATH = "jms.random_path"; //$NON-NLS-1$
044:
045: private static final String TEXT_MSG = "jms.text_message"; //$NON-NLS-1$
046:
047: private static final String CONFIG_CHOICE = "jms.config_choice"; //$NON-NLS-1$
048:
049: private static final String MESSAGE_CHOICE = "jms.config_msg_type"; //$NON-NLS-1$
050: //--
051:
052: private transient Publisher PUB = null;
053:
054: private StringBuffer BUFFER = new StringBuffer();
055:
056: private static FileServer FSERVER = FileServer.getFileServer();
057:
058: private String file_contents = null;
059:
060: public PublisherSampler() {
061: }
062:
063: /**
064: * the implementation calls testStarted() without any parameters.
065: */
066: public void testStarted(String test) {
067: testStarted();
068: }
069:
070: /**
071: * the implementation calls testEnded() without any parameters.
072: */
073: public void testEnded(String test) {
074: testEnded();
075: }
076:
077: /**
078: * endTest cleans up the client
079: *
080: * @see junit.framework.TestListener#endTest(junit.framework.Test)
081: */
082: public void testEnded() {
083: log.info("PublisherSampler.testEnded called");
084: Thread.currentThread().interrupt();
085: this .PUB = null;
086: this .BUFFER.setLength(0);
087: this .BUFFER = null;
088: ClientPool.clearClient();
089: }
090:
091: /**
092: * the implementation creates a new StringBuffer
093: */
094: public void testStarted() {
095: this .BUFFER = new StringBuffer();
096: }
097:
098: /**
099: * NO implementation provided for the sampler. It is necessary in this case.
100: */
101: public void testIterationStart(LoopIterationEvent event) {
102: }
103:
104: /**
105: * initialize the Publisher client.
106: *
107: */
108: public synchronized void initClient() {
109: this .PUB = new Publisher(this .getUseJNDIPropertiesAsBoolean(),
110: this .getJNDIInitialContextFactory(), this
111: .getProviderUrl(), this .getConnectionFactory(),
112: this .getTopic(), this .getUseAuth(), this .getUsername(),
113: this .getPassword());
114: ClientPool.addClient(this .PUB);
115: log.info("PublisherSampler.initClient called");
116: }
117:
118: /**
119: * The implementation calls sample() without any parameters
120: */
121: public SampleResult sample(Entry e) {
122: return this .sample();
123: }
124:
125: /**
126: * The implementation will publish n messages within a for loop. Once n
127: * messages are published, it sets the attributes of SampleResult.
128: *
129: * @return the populated sample result
130: */
131: public SampleResult sample() {
132: SampleResult result = new SampleResult();
133: result.setSampleLabel(getName());
134: if (this .PUB == null) {
135: this .initClient();
136: }
137: int loop = this .getIterationCount();
138: if (this .PUB != null) {
139: result.sampleStart();
140: for (int idx = 0; idx < loop; idx++) {
141: String tmsg = this .getMessageContent();
142: this .PUB.publish(tmsg);
143: this .BUFFER.append(tmsg);
144: }
145: result.sampleEnd();
146: String content = this .BUFFER.toString();
147: result.setBytes(content.getBytes().length);
148: result.setResponseCode("message published successfully");
149: result.setResponseMessage(loop + " messages published");
150: result.setSuccessful(true);
151: result.setResponseData(content.getBytes());
152: result.setSampleCount(loop);
153: this .BUFFER.setLength(0);
154: }
155: return result;
156: }
157:
158: /**
159: * Method will check the setting and get the contents for the message.
160: *
161: * @return the contents for the message
162: */
163: public String getMessageContent() {
164: if (this .getConfigChoice().equals(JMSPublisherGui.use_file)) {
165: // in the case the test uses a file, we set it locally and
166: // prevent loading the file repeatedly
167: if (this .file_contents == null) {
168: this .file_contents = this .getFileContent(this
169: .getInputFile());
170: }
171: return this .file_contents;
172: } else if (this .getConfigChoice().equals(
173: JMSPublisherGui.use_random)) {
174: // Maybe we should consider creating a global cache for the
175: // random files to make JMeter more efficient.
176: String fname = FSERVER.getRandomFile(this .getRandomPath(),
177: new String[] { ".txt", ".obj" }).getAbsolutePath();
178: return this .getFileContent(fname);
179: } else {
180: return this .getTextMessage();
181: }
182: }
183:
184: /**
185: * The implementation uses TextFile to load the contents of the file and
186: * returns a string.
187: *
188: * @param path
189: * @return the contents of the file
190: */
191: public String getFileContent(String path) {
192: TextFile tf = new TextFile(path);
193: return tf.getText();
194: }
195:
196: // ------------- get/set properties ----------------------//
197: /**
198: * set the config choice
199: *
200: * @param choice
201: */
202: public void setConfigChoice(String choice) {
203: setProperty(CONFIG_CHOICE, choice);
204: }
205:
206: /**
207: * return the config choice
208: *
209: */
210: public String getConfigChoice() {
211: return getPropertyAsString(CONFIG_CHOICE);
212: }
213:
214: /**
215: * set the source of the message
216: *
217: * @param choice
218: */
219: public void setMessageChoice(String choice) {
220: setProperty(MESSAGE_CHOICE, choice);
221: }
222:
223: /**
224: * return the source of the message
225: *
226: */
227: public String getMessageChoice() {
228: return getPropertyAsString(MESSAGE_CHOICE);
229: }
230:
231: /**
232: * set the input file for the publisher
233: *
234: * @param file
235: */
236: public void setInputFile(String file) {
237: setProperty(INPUT_FILE, file);
238: }
239:
240: /**
241: * return the path of the input file
242: *
243: */
244: public String getInputFile() {
245: return getPropertyAsString(INPUT_FILE);
246: }
247:
248: /**
249: * set the random path for the messages
250: *
251: * @param path
252: */
253: public void setRandomPath(String path) {
254: setProperty(RANDOM_PATH, path);
255: }
256:
257: /**
258: * return the random path for messages
259: *
260: */
261: public String getRandomPath() {
262: return getPropertyAsString(RANDOM_PATH);
263: }
264:
265: /**
266: * set the text for the message
267: *
268: * @param message
269: */
270: public void setTextMessage(String message) {
271: setProperty(TEXT_MSG, message);
272: }
273:
274: /**
275: * return the text for the message
276: *
277: */
278: public String getTextMessage() {
279: return getPropertyAsString(TEXT_MSG);
280: }
281: }
|