use of com.redhat.utils.OrderedProperties in project jms-messaging-plugin by jenkinsci.
the class ActiveMqMessagingWorker method sendMessage.
@Override
public SendResult sendMessage(Run<?, ?> build, TaskListener listener, ProviderData pdata) {
ActiveMQPublisherProviderData pd = (ActiveMQPublisherProviderData) pdata;
Connection connection = null;
Session session = null;
MessageProducer publisher = null;
TextMessage message;
String mesgId = "0";
String mesgContent = "";
try {
String ltopic = PluginUtils.getSubstitutedValue(getTopic(provider), build.getEnvironment(listener));
if (provider.getAuthenticationMethod() != null && ltopic != null && provider.getBroker() != null) {
ActiveMQConnectionFactory connectionFactory = provider.getConnectionFactory();
connection = connectionFactory.createConnection();
connection.start();
session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Destination destination = session.createTopic(ltopic);
publisher = session.createProducer(destination);
message = session.createTextMessage("");
message.setJMSType(JSON_TYPE);
TreeMap<String, String> envVarParts = new TreeMap<>();
message.setStringProperty("CI_NAME", build.getParent().getName());
envVarParts.put("CI_NAME", build.getParent().getName());
if (pd.getMessageType() != null) {
message.setStringProperty("CI_TYPE", pd.getMessageType().getMessage());
envVarParts.put("CI_TYPE", pd.getMessageType().getMessage());
}
if (!build.isBuilding()) {
String ciStatus = (build.getResult() == Result.SUCCESS ? "passed" : "failed");
message.setStringProperty("CI_STATUS", ciStatus);
envVarParts.put("CI_STATUS", ciStatus);
envVarParts.put("BUILD_STATUS", build.getResult().toString());
}
EnvVars baseEnvVars = build.getEnvironment(listener);
EnvVars envVars = new EnvVars();
envVars.putAll(baseEnvVars);
envVars.putAll(envVarParts);
if (!StringUtils.isEmpty(pd.getMessageProperties())) {
OrderedProperties p = new OrderedProperties();
p.load(new StringReader(PluginUtils.getSubstitutedValue(pd.getMessageProperties(), envVars)));
@SuppressWarnings("unchecked") Enumeration<String> e = p.propertyNames();
while (e.hasMoreElements()) {
String key = e.nextElement();
if (!key.toLowerCase().startsWith("jms") || !setMessageHeader(message, key, p.getProperty(key), session)) {
EnvVars envVars2 = new EnvVars();
envVars2.putAll(baseEnvVars);
envVars2.putAll(envVarParts);
// This allows us to use recently added key/vals
// to be substituted
String val = PluginUtils.getSubstitutedValue(p.getProperty(key), envVars2);
message.setStringProperty(key, val);
envVarParts.put(key, val);
}
}
}
EnvVars envVars2 = new EnvVars();
envVars2.putAll(baseEnvVars);
envVars2.putAll(envVarParts);
message.setText(PluginUtils.getSubstitutedValue(pd.getMessageContent(), envVars2));
publisher.send(message);
mesgId = message.getJMSMessageID();
mesgContent = message.getText();
String messageType = "unknown type";
if (pd.getMessageType() != null) {
messageType = pd.getMessageType().toString();
}
log.info("Sent " + messageType + " message for job '" + build.getParent().getName() + "' to topic '" + ltopic + "':\n" + formatMessage(message));
} else {
log.severe("One or more of the following is invalid (null): user, password, topic, broker.");
return new SendResult(false, mesgId, mesgContent);
}
} catch (Exception e) {
if (pd.isFailOnError()) {
log.severe("Unhandled exception in perform: ");
log.severe(ExceptionUtils.getStackTrace(e));
listener.fatalError("Unhandled exception in perform: ");
listener.fatalError(ExceptionUtils.getStackTrace(e));
return new SendResult(false, mesgId, mesgContent);
} else {
log.warning("Unhandled exception in perform: ");
log.warning(ExceptionUtils.getStackTrace(e));
listener.error("Unhandled exception in perform: ");
listener.error(ExceptionUtils.getStackTrace(e));
return new SendResult(true, mesgId, mesgContent);
}
} finally {
if (publisher != null) {
try {
publisher.close();
} catch (JMSException e) {
}
}
if (session != null) {
try {
session.close();
} catch (JMSException e) {
}
}
if (connection != null) {
try {
connection.close();
} catch (JMSException e) {
}
}
}
return new SendResult(true, mesgId, mesgContent);
}
Aggregations