From 345c0d8f7738aab20d2b45fd6c5ebf911b75e2fa Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Fri, 2 May 2014 18:26:50 +0200 Subject: o Initial import of ActiveMQ plugin for Jenkins. --- .../jenkinsci/plugins/activemq/ActiveMqClient.java | 55 +++++++++++++++++++++ .../jenkinsci/plugins/activemq/ActiveMqPlugin.java | 57 ++++++++++++++++++++++ .../plugins/activemq/ActiveMqRunListener.java | 42 ++++++++++++++++ 3 files changed, 154 insertions(+) create mode 100644 src/main/java/org/jenkinsci/plugins/activemq/ActiveMqClient.java create mode 100644 src/main/java/org/jenkinsci/plugins/activemq/ActiveMqPlugin.java create mode 100644 src/main/java/org/jenkinsci/plugins/activemq/ActiveMqRunListener.java (limited to 'src/main/java/org') diff --git a/src/main/java/org/jenkinsci/plugins/activemq/ActiveMqClient.java b/src/main/java/org/jenkinsci/plugins/activemq/ActiveMqClient.java new file mode 100644 index 0000000..a40f3e6 --- /dev/null +++ b/src/main/java/org/jenkinsci/plugins/activemq/ActiveMqClient.java @@ -0,0 +1,55 @@ +package org.jenkinsci.plugins.activemq; + +import org.apache.activemq.ActiveMQConnectionFactory; + +import javax.jms.Connection; +import javax.jms.DeliveryMode; +import javax.jms.Destination; +import javax.jms.JMSException; +import javax.jms.MessageProducer; +import javax.jms.Session; +import java.io.CharArrayWriter; +import java.io.IOException; +import java.util.Properties; + +import static java.lang.String.valueOf; +import static javax.jms.Session.AUTO_ACKNOWLEDGE; + +public class ActiveMqClient { + + ActiveMQConnectionFactory connectionFactory; + + public ActiveMqClient(String brokerUrl) { + this.connectionFactory = new ActiveMQConnectionFactory(brokerUrl); + } + + public void sendMessage(String jobName, int buildNumber, String result) { + try { + Connection connection = connectionFactory.createConnection(); + connection.start(); + + Session session = connection.createSession(false, AUTO_ACKNOWLEDGE); + + Destination destination = session.createQueue("jenkins.build-result"); + + MessageProducer producer = session.createProducer(destination); + producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT); + + Properties properties = new Properties(); + properties.setProperty("jobName", jobName); + properties.setProperty("buildNumber", valueOf(buildNumber)); + properties.setProperty("result", result); + + CharArrayWriter buf = new CharArrayWriter(); + properties.store(buf, null); + producer.send(session.createTextMessage(buf.toString())); + + session.close(); + connection.close(); + } catch (JMSException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + } +} diff --git a/src/main/java/org/jenkinsci/plugins/activemq/ActiveMqPlugin.java b/src/main/java/org/jenkinsci/plugins/activemq/ActiveMqPlugin.java new file mode 100644 index 0000000..74f3504 --- /dev/null +++ b/src/main/java/org/jenkinsci/plugins/activemq/ActiveMqPlugin.java @@ -0,0 +1,57 @@ +package org.jenkinsci.plugins.activemq; + +import hudson.Plugin; +import hudson.model.Descriptor; +import hudson.util.FormValidation; +import net.sf.json.JSONObject; +import org.kohsuke.stapler.QueryParameter; +import org.kohsuke.stapler.StaplerRequest; +import org.slf4j.Logger; + +import javax.servlet.ServletException; +import java.io.IOException; + +import static org.slf4j.LoggerFactory.getLogger; + +public class ActiveMqPlugin extends Plugin { + + private final Logger logger = getLogger(getClass()); + + public static final String DISPLAY_NAME = "ActiveMQ Plugin"; + + private boolean enable; + + private String brokerUrl = "tcp://127.0.0.1:61616"; + + @Override + public void start() throws Exception { + load(); + } + + @Override + public void configure(StaplerRequest req, JSONObject formData) throws IOException, ServletException, Descriptor.FormException { + brokerUrl = formData.getString("brokerUrl"); + enable = formData.getBoolean("enable"); + + System.out.println("brokerUrl = " + brokerUrl); + + save(); + } + + public FormValidation doCheckBrokerUrl(@QueryParameter String activeMqUrl) { + System.out.println("ActiveMqPlugin.doCheckActiveMqUrl"); + + return FormValidation.error("wat"); + } + + public String getBrokerUrl() { + System.out.println("ActiveMqPlugin.getBrokerUrl"); + return brokerUrl; + } + + public void setBrokerUrl(String brokerUrl) { + System.out.println("ActiveMqPlugin.setBrokerUrl"); + System.out.println("brokerUrl = " + brokerUrl); + this.brokerUrl = brokerUrl; + } +} diff --git a/src/main/java/org/jenkinsci/plugins/activemq/ActiveMqRunListener.java b/src/main/java/org/jenkinsci/plugins/activemq/ActiveMqRunListener.java new file mode 100644 index 0000000..62a2673 --- /dev/null +++ b/src/main/java/org/jenkinsci/plugins/activemq/ActiveMqRunListener.java @@ -0,0 +1,42 @@ +package org.jenkinsci.plugins.activemq; + +import hudson.Extension; +import hudson.model.Build; +import hudson.model.TaskListener; +import hudson.model.listeners.RunListener; + +import javax.annotation.Nonnull; + +@Extension +public class ActiveMqRunListener extends RunListener { + + private final ActiveMqClient activeMqClient; + + public ActiveMqRunListener() { + super(Build.class); + + System.out.println("JbpmRunListener.JbpmRunListener"); + + activeMqClient = new ActiveMqClient("tcp://localhost:61616"); + } + + @Override + public void onCompleted(Build build, @Nonnull TaskListener listener) { + System.out.println("JbpmRunListener.onCompleted"); + System.out.println("build = " + build); + + System.out.println("build variables"); + for (Object o : build.getBuildVariables().entrySet()) { + System.out.println(o); + } + + activeMqClient.sendMessage(build.getProject().getName(), build.getNumber(), build.getResult().toString()); + +// Jenkins jenkins = Jenkins.getInstance(); +// List projects = jenkins.getAllItems(AbstractProject.class); +// for (AbstractProject project : projects) { +// System.out.println("project.getPronoun() = " + project.getPronoun()); +// System.out.println("project.getName() = " + project.getName()); +// } + } +} -- cgit v1.2.3