Search in sources :

Example 1 with OrderedProperties

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);
}
Also used : Destination(javax.jms.Destination) Connection(javax.jms.Connection) JMSException(javax.jms.JMSException) TreeMap(java.util.TreeMap) JMSException(javax.jms.JMSException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) InvalidSelectorException(javax.jms.InvalidSelectorException) JMSSecurityException(javax.jms.JMSSecurityException) ActiveMQConnectionFactory(org.apache.activemq.ActiveMQConnectionFactory) EnvVars(hudson.EnvVars) ActiveMQPublisherProviderData(com.redhat.jenkins.plugins.ci.provider.data.ActiveMQPublisherProviderData) SendResult(com.redhat.jenkins.plugins.ci.messaging.data.SendResult) StringReader(java.io.StringReader) OrderedProperties(com.redhat.utils.OrderedProperties) MessageProducer(javax.jms.MessageProducer) TextMessage(javax.jms.TextMessage) Session(javax.jms.Session)

Aggregations

SendResult (com.redhat.jenkins.plugins.ci.messaging.data.SendResult)1 ActiveMQPublisherProviderData (com.redhat.jenkins.plugins.ci.provider.data.ActiveMQPublisherProviderData)1 OrderedProperties (com.redhat.utils.OrderedProperties)1 EnvVars (hudson.EnvVars)1 IOException (java.io.IOException)1 StringReader (java.io.StringReader)1 UnknownHostException (java.net.UnknownHostException)1 TreeMap (java.util.TreeMap)1 Connection (javax.jms.Connection)1 Destination (javax.jms.Destination)1 InvalidSelectorException (javax.jms.InvalidSelectorException)1 JMSException (javax.jms.JMSException)1 JMSSecurityException (javax.jms.JMSSecurityException)1 MessageProducer (javax.jms.MessageProducer)1 Session (javax.jms.Session)1 TextMessage (javax.jms.TextMessage)1 ActiveMQConnectionFactory (org.apache.activemq.ActiveMQConnectionFactory)1