--- src/protocol/jms/org/apache/jmeter/protocol/jms/control/gui/JMSPublisherGui.java (revision 1333378) +++ src/protocol/jms/org/apache/jmeter/protocol/jms/control/gui/JMSPublisherGui.java (working copy) @@ -131,29 +131,11 @@ * @see org.apache.jmeter.gui.JMeterGUIComponent#createTestElement() */ public TestElement createTestElement() { - PublisherSampler sampler = new PublisherSampler(); - this.configureTestElement(sampler); - sampler.setUseJNDIProperties(String.valueOf(useProperties.isSelected())); - sampler.setJNDIIntialContextFactory(jndiICF.getText()); - sampler.setProviderUrl(urlField.getText()); - sampler.setConnectionFactory(jndiConnFac.getText()); - sampler.setDestination(jmsDestination.getText()); - sampler.setUsername(jmsUser.getText()); - sampler.setPassword(jmsPwd.getText()); - sampler.setTextMessage(textMessage.getText()); - sampler.setInputFile(messageFile.getFilename()); - sampler.setRandomPath(randomFile.getFilename()); - sampler.setConfigChoice(configChoice.getText()); - sampler.setMessageChoice(msgChoice.getText()); - sampler.setIterations(iterations.getText()); - sampler.setUseAuth(useAuth.isSelected()); - sampler.setUseNonPersistentDelivery(useNonPersistentDelivery.isSelected()); - Arguments args = (Arguments) jmsPropertiesPanel.createTestElement(); - sampler.setJMSProperties(args); + PublisherSampler sampler = new PublisherSampler(); + setupSamplerProperties(sampler); - return sampler; - } - + return sampler; + } /** * Modifies a given TestElement to mirror the data in the gui components. * @@ -161,26 +143,35 @@ */ public void modifyTestElement(TestElement s) { PublisherSampler sampler = (PublisherSampler) s; - this.configureTestElement(sampler); - sampler.setUseJNDIProperties(String.valueOf(useProperties.isSelected())); - sampler.setJNDIIntialContextFactory(jndiICF.getText()); - sampler.setProviderUrl(urlField.getText()); - sampler.setConnectionFactory(jndiConnFac.getText()); - sampler.setDestination(jmsDestination.getText()); - sampler.setUsername(jmsUser.getText()); - sampler.setPassword(jmsPwd.getText()); - sampler.setTextMessage(textMessage.getText()); - sampler.setInputFile(messageFile.getFilename()); - sampler.setRandomPath(randomFile.getFilename()); - sampler.setConfigChoice(configChoice.getText()); - sampler.setMessageChoice(msgChoice.getText()); - sampler.setIterations(iterations.getText()); - sampler.setUseAuth(useAuth.isSelected()); + setupSamplerProperties(sampler); sampler.setDestinationStatic(destSetup.getText().equals(DEST_SETUP_STATIC)); - sampler.setUseNonPersistentDelivery(useNonPersistentDelivery.isSelected()); - Arguments args = (Arguments) jmsPropertiesPanel.createTestElement(); - sampler.setJMSProperties(args); + } + /** + * Initialize the provided {@link PublisherSampler} with all the values as configured in the GUI. + * + * @param sampler {@link PublisherSampler} instance + */ + private void setupSamplerProperties(final PublisherSampler sampler) { + this.configureTestElement(sampler); + sampler.setUseJNDIProperties(String.valueOf(useProperties.isSelected())); + sampler.setJNDIIntialContextFactory(jndiICF.getText()); + sampler.setProviderUrl(urlField.getText()); + sampler.setConnectionFactory(jndiConnFac.getText()); + sampler.setDestination(jmsDestination.getText()); + sampler.setUsername(jmsUser.getText()); + sampler.setPassword(jmsPwd.getText()); + sampler.setTextMessage(textMessage.getText()); + sampler.setInputFile(messageFile.getFilename()); + sampler.setRandomPath(randomFile.getFilename()); + sampler.setConfigChoice(configChoice.getText()); + sampler.setMessageChoice(msgChoice.getText()); + sampler.setIterations(iterations.getText()); + sampler.setUseAuth(useAuth.isSelected()); + sampler.setUseNonPersistentDelivery(useNonPersistentDelivery.isSelected()); + + Arguments args = (Arguments) jmsPropertiesPanel.createTestElement(); + sampler.setJMSProperties(args); } /** --- src/protocol/jms/org/apache/jmeter/protocol/jms/sampler/PublisherSampler.java (revision 1333378) +++ src/protocol/jms/org/apache/jmeter/protocol/jms/sampler/PublisherSampler.java (working copy) @@ -17,6 +17,8 @@ package org.apache.jmeter.protocol.jms.sampler; +import java.io.FileInputStream; +import java.io.Serializable; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.HashMap; @@ -42,6 +44,8 @@ import org.apache.jorphan.logging.LoggingManager; import org.apache.log.Logger; +import com.thoughtworks.xstream.XStream; + /** * This class implements the JMS Publisher sampler. */ @@ -76,6 +80,9 @@ // Cache for file. Only used by sample() in a single thread private String file_contents = null; + // Cache for object-message, only used when parsing from a file because in text-area + // property replacement might have been used + private Serializable object_msg_file_contents = null; public PublisherSampler() { } @@ -168,7 +175,9 @@ Message msg = publisher.publish(m, getDestination(), getJMSProperties().getArgumentsAsMap()); Utils.messageProperties(propBuffer, msg); } else if (JMSPublisherGui.OBJECT_MSG_RSC.equals(type)){ - throw new JMSException(type+ " is not yet supported"); + Serializable omsg = getObjectContent(); + Message msg = publisher.publish(omsg, getDestination(), getJMSProperties().getArgumentsAsMap()); + Utils.messageProperties(propBuffer, msg); } else { throw new JMSException(type+ " is not recognised"); } @@ -252,6 +261,75 @@ return tf.getText(); } + /** + * This method will load the contents for the JMS Object Message. + * The contents are either loaded from file (might be cached), random file + * or from the GUI text-area. + * + * @return Serialized object as loaded from the specified input file + */ + private Serializable getObjectContent() { + if (getConfigChoice().equals(JMSPublisherGui.USE_FILE_RSC)) { + // in the case the test uses a file, we set it locally and + // prevent loading the file repeatedly + if (object_msg_file_contents == null) { + object_msg_file_contents = getFileObjectContent(getInputFile()); + } + + return object_msg_file_contents; + } else if (getConfigChoice().equals(JMSPublisherGui.USE_RANDOM_RSC)) { + // Maybe we should consider creating a global cache for the + // random files to make JMeter more efficient. + final String fname = FSERVER.getRandomFile(getRandomPath(), new String[] {".txt", ".obj"}) + .getAbsolutePath(); + + return getFileObjectContent(fname); + } else { + final String xmlMessage = getTextMessage(); + return transformXmlToObjectMessage(xmlMessage); + } + } + + /** + * Try to load an object from a provided file, so that it can be used as body + * for a JMS message. + * An {@link IllegalStateException} will be thrown if loading the object fails. + * + * @param path Path to the file that will be serialized + * @return Serialized object instance + */ + private Serializable getFileObjectContent(final String path) { + Serializable readObject = null; + try { + XStream xstream = new XStream(); + readObject = (Serializable) xstream.fromXML(new FileInputStream(path), readObject); + } catch (Exception e) { + log.error(e.getLocalizedMessage(), e); + throw new IllegalStateException("Unable to load object instance from file", e); + } + return readObject; + } + + /** + * Try to load an object via XStream from XML text, so that it can be used as body + * for a JMS message. + * An {@link IllegalStateException} will be thrown if transforming the XML to an object fails. + * + * @param xmlMessage String containing XML text as input for the transformation + * @return Serialized object instance + */ + private Serializable transformXmlToObjectMessage(final String xmlMessage) { + Serializable readObject = null; + try { + XStream xstream = new XStream(); + readObject = (Serializable) xstream.fromXML(xmlMessage, readObject); + } catch (Exception e) { + log.error(e.getLocalizedMessage(), e); + throw new IllegalStateException("Unable to load object instance from text", e); + } + return readObject; + } + // ------------- get/set properties ----------------------// /** * set the source of the message