From 5c762750101f12abd51621355a871e42fd9c7a33 Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Sun, 4 May 2014 23:34:16 +0200 Subject: o Getting the web configuration to work. --- .../jenkinsci/plugins/activemq/ActiveMqClient.java | 24 +++- .../plugins/activemq/ActiveMqGlobalConfig.java | 124 +++++++++++++++++++++ .../jenkinsci/plugins/activemq/ActiveMqPlugin.java | 91 +++++++++------ .../plugins/activemq/ActiveMqRunListener.java | 17 +-- .../activemq/ActiveMqGlobalConfig/config.groovy | 15 +++ .../plugins/activemq/ActiveMqPlugin/config.jelly | 10 -- .../plugins/activemq/ActiveMqPlugin/global.jelly | 21 ---- .../activemq/ActiveMqPlugin/help-activeMqUrl.html | 6 - .../plugins/activemq/ActiveMqPlugin/help-name.html | 6 - 9 files changed, 217 insertions(+), 97 deletions(-) create mode 100644 src/main/java/org/jenkinsci/plugins/activemq/ActiveMqGlobalConfig.java create mode 100644 src/main/resources/org/jenkinsci/plugins/activemq/ActiveMqGlobalConfig/config.groovy delete mode 100644 src/main/resources/org/jenkinsci/plugins/activemq/ActiveMqPlugin/config.jelly delete mode 100644 src/main/resources/org/jenkinsci/plugins/activemq/ActiveMqPlugin/global.jelly delete mode 100644 src/main/resources/org/jenkinsci/plugins/activemq/ActiveMqPlugin/help-activeMqUrl.html delete mode 100644 src/main/resources/org/jenkinsci/plugins/activemq/ActiveMqPlugin/help-name.html (limited to 'src/main') diff --git a/src/main/java/org/jenkinsci/plugins/activemq/ActiveMqClient.java b/src/main/java/org/jenkinsci/plugins/activemq/ActiveMqClient.java index a40f3e6..ffb4313 100644 --- a/src/main/java/org/jenkinsci/plugins/activemq/ActiveMqClient.java +++ b/src/main/java/org/jenkinsci/plugins/activemq/ActiveMqClient.java @@ -1,6 +1,6 @@ package org.jenkinsci.plugins.activemq; -import org.apache.activemq.ActiveMQConnectionFactory; +import org.apache.activemq.pool.PooledConnectionFactory; import javax.jms.Connection; import javax.jms.DeliveryMode; @@ -8,6 +8,7 @@ import javax.jms.Destination; import javax.jms.JMSException; import javax.jms.MessageProducer; import javax.jms.Session; +import javax.jms.TextMessage; import java.io.CharArrayWriter; import java.io.IOException; import java.util.Properties; @@ -17,20 +18,24 @@ import static javax.jms.Session.AUTO_ACKNOWLEDGE; public class ActiveMqClient { - ActiveMQConnectionFactory connectionFactory; + public static final String TOPIC_NAME = "jenkins.build-result"; + + public final String brokerUrl; + private final PooledConnectionFactory connectionFactory; public ActiveMqClient(String brokerUrl) { - this.connectionFactory = new ActiveMQConnectionFactory(brokerUrl); + this.brokerUrl = brokerUrl; + this.connectionFactory = new PooledConnectionFactory(brokerUrl); } - public void sendMessage(String jobName, int buildNumber, String result) { + public synchronized 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"); + Destination destination = session.createTopic(TOPIC_NAME); MessageProducer producer = session.createProducer(destination); producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT); @@ -42,7 +47,10 @@ public class ActiveMqClient { CharArrayWriter buf = new CharArrayWriter(); properties.store(buf, null); - producer.send(session.createTextMessage(buf.toString())); + TextMessage message = session.createTextMessage(buf.toString()); + System.out.println("message.getJMSMessageID() = " + message.getJMSMessageID()); + producer.send(message); + System.out.println("message.getJMSMessageID() = " + message.getJMSMessageID()); session.close(); connection.close(); @@ -52,4 +60,8 @@ public class ActiveMqClient { e.printStackTrace(); } } + + public synchronized void close() { + connectionFactory.clear(); + } } diff --git a/src/main/java/org/jenkinsci/plugins/activemq/ActiveMqGlobalConfig.java b/src/main/java/org/jenkinsci/plugins/activemq/ActiveMqGlobalConfig.java new file mode 100644 index 0000000..4bc8066 --- /dev/null +++ b/src/main/java/org/jenkinsci/plugins/activemq/ActiveMqGlobalConfig.java @@ -0,0 +1,124 @@ +package org.jenkinsci.plugins.activemq; + +import hudson.Extension; +import hudson.util.FormValidation; +import jenkins.model.GlobalConfiguration; +import jenkins.model.Jenkins; +import net.sf.json.JSONObject; +import org.apache.activemq.ActiveMQConnectionFactory; +import org.kohsuke.stapler.QueryParameter; +import org.kohsuke.stapler.StaplerRequest; +import org.slf4j.Logger; + +import javax.jms.Connection; +import javax.jms.JMSException; +import java.net.URI; +import java.net.URISyntaxException; + +import static org.slf4j.LoggerFactory.getLogger; + +@Extension +public class ActiveMqGlobalConfig extends GlobalConfiguration { + private static final Logger log = getLogger(ActiveMqPlugin.class); + + private String brokerUrl; + private boolean enable; + + public ActiveMqGlobalConfig() { + log.info("ActiveMqGlobalConfig.ActiveMqGlobalConfig"); + load(); + log.info("this = " + this); + } + + public String getUrl() { + log.info("ActiveMqGlobalConfig.getUrl"); + return brokerUrl; + } + + public void setUrl(String url) { + log.info("ActiveMqGlobalConfig.setUrl"); + log.info("url = " + url); + this.brokerUrl = url; + } + + public FormValidation doCheckUrl(@QueryParameter String value) { + log.info("ActiveMqGlobalConfig.doCheckUrl"); + return doCheckBrokerUrl(value); + } + + public String getBrokerUrl() { + log.info("ActiveMqGlobalConfig.getBrokerUrl"); + return brokerUrl; + } + + public void setBrokerUrl(String brokerUrl) { + log.info("ActiveMqGlobalConfig.setBrokerUrl"); + this.brokerUrl = brokerUrl; + save(); + } + + public FormValidation doCheckBrokerUrl(@QueryParameter String value) { + log.info("ActiveMqPlugin.doCheckUrl"); + log.info("value = " + value); + + value = value.trim(); + + if (value.isEmpty()) { + return FormValidation.ok(); + } + + ActiveMQConnectionFactory connectionFactory; + + try { + URI uri = new URI(value); + connectionFactory = new ActiveMQConnectionFactory(uri); + } catch (URISyntaxException e) { + return FormValidation.error("Invalid URI: " + e.getMessage()); + } + + Connection connection; + try { + connection = connectionFactory.createConnection(); + } catch (JMSException e) { + return FormValidation.warning("Unable to connect to broker"); + } + + try { + connection.close(); + } catch (JMSException ignore) { + } + + return FormValidation.ok("Successfully connected to broker"); + } + + public boolean isEnable() { + log.info("ActiveMqGlobalConfig.isEnable"); + return enable; + } + + public void setEnable(boolean enable) { + log.info("ActiveMqGlobalConfig.setEnable"); + this.enable = enable; + save(); + } + + @Override + public boolean configure(StaplerRequest req, JSONObject json) throws FormException { + log.info("ActiveMqGlobalConfig.configure"); + req.bindJSON(this, json); + + ActiveMqPlugin plugin = Jenkins.getInstance().getPlugin(ActiveMqPlugin.class); + + plugin.reconfigure(); + + return true; + } + + @Override + public String toString() { + return "ActiveMqGlobalConfig{" + + ", brokerUrl='" + brokerUrl + '\'' + + ", enable=" + enable + + "} " + super.toString(); + } +} diff --git a/src/main/java/org/jenkinsci/plugins/activemq/ActiveMqPlugin.java b/src/main/java/org/jenkinsci/plugins/activemq/ActiveMqPlugin.java index 74f3504..6ec65c0 100644 --- a/src/main/java/org/jenkinsci/plugins/activemq/ActiveMqPlugin.java +++ b/src/main/java/org/jenkinsci/plugins/activemq/ActiveMqPlugin.java @@ -1,57 +1,78 @@ package org.jenkinsci.plugins.activemq; +import hudson.Extension; 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 hudson.model.Build; +import hudson.model.TaskListener; +import jenkins.model.GlobalConfiguration; import org.slf4j.Logger; -import javax.servlet.ServletException; -import java.io.IOException; +import javax.annotation.Nonnull; +import java.util.Map; import static org.slf4j.LoggerFactory.getLogger; -public class ActiveMqPlugin extends Plugin { +@Extension +public class ActiveMqPlugin extends Plugin /*implements ReconfigurableDescribable*/ { - private final Logger logger = getLogger(getClass()); +// public static final String DISPLAY_NAME = "ActiveMQ Plugin Display Name"; - public static final String DISPLAY_NAME = "ActiveMQ Plugin"; + private static final Logger log = getLogger(ActiveMqPlugin.class); - private boolean enable; - - private String brokerUrl = "tcp://127.0.0.1:61616"; + private transient ActiveMqClient client; @Override public void start() throws Exception { - load(); + reconfigure(); } - @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 void postInitialize() throws Exception { + log.info("ActiveMqPlugin.postInitialize"); + reconfigure(); } - public String getBrokerUrl() { - System.out.println("ActiveMqPlugin.getBrokerUrl"); - return brokerUrl; + public void onCompleted(Build build, @Nonnull TaskListener listener) { + log.info("ActiveMqPlugin.onCompleted"); + log.info("build variables"); + @SuppressWarnings("unchecked") Map buildVariables = build.getBuildVariables(); + for (Map.Entry e : buildVariables.entrySet()) { + log.info(e.getKey() + " = " + e.getValue()); + } + + log.info("client = " + client); + if (client == null) { + log.debug("client is null"); + return; + } + + client.sendMessage(build.getProject().getName(), build.getNumber(), build.getResult().toString()); } - public void setBrokerUrl(String brokerUrl) { - System.out.println("ActiveMqPlugin.setBrokerUrl"); - System.out.println("brokerUrl = " + brokerUrl); - this.brokerUrl = brokerUrl; + public void reconfigure() { + log.info("ActiveMqPlugin.reconfigure"); + ActiveMqGlobalConfig config = GlobalConfiguration.all().get(ActiveMqGlobalConfig.class); + log.info("config = " + config); + if (config == null) { + return; + } + String brokerUrl = config.getBrokerUrl(); + boolean enable = config.isEnable(); + + if (enable) { + if (client != null && !client.brokerUrl.equals(brokerUrl)) { + log.info("Disposing current JMS client."); + client.close(); + client = null; + } + + log.info("Creating client of broker {}", brokerUrl); + client = new ActiveMqClient(brokerUrl); + } else { + if (client != null) { + log.info("Disposing current JMS client."); + client.close(); + client = null; + } + } } } diff --git a/src/main/java/org/jenkinsci/plugins/activemq/ActiveMqRunListener.java b/src/main/java/org/jenkinsci/plugins/activemq/ActiveMqRunListener.java index 62a2673..43bc6d8 100644 --- a/src/main/java/org/jenkinsci/plugins/activemq/ActiveMqRunListener.java +++ b/src/main/java/org/jenkinsci/plugins/activemq/ActiveMqRunListener.java @@ -4,33 +4,24 @@ import hudson.Extension; import hudson.model.Build; import hudson.model.TaskListener; import hudson.model.listeners.RunListener; +import jenkins.model.Jenkins; 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); + ActiveMqPlugin plugin = Jenkins.getInstance().getPlugin(ActiveMqPlugin.class); + plugin.onCompleted(build, listener); - 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()); +// activeMqClient.sendMessage(build.getProject().getName(), build.getNumber(), build.getResult().toString()); // Jenkins jenkins = Jenkins.getInstance(); // List projects = jenkins.getAllItems(AbstractProject.class); diff --git a/src/main/resources/org/jenkinsci/plugins/activemq/ActiveMqGlobalConfig/config.groovy b/src/main/resources/org/jenkinsci/plugins/activemq/ActiveMqGlobalConfig/config.groovy new file mode 100644 index 0000000..7c5cae3 --- /dev/null +++ b/src/main/resources/org/jenkinsci/plugins/activemq/ActiveMqGlobalConfig/config.groovy @@ -0,0 +1,15 @@ +package org.jenkinsci.plugins.activemq.ActiveMqGlobalConfig + +def f=namespace(lib.FormTagLib) + +f.section(title:_("ActiveMQ Configuration")) { + f.entry(title:_("Enable"), field:"enable") { + f.checkbox() + } + f.entry(title:_("Broker URL"), field:"brokerUrl") { + f.textbox() + } +// f.entry(title:_("URL"), field:"url") { +// f.textbox(default: "my url") +// } +} diff --git a/src/main/resources/org/jenkinsci/plugins/activemq/ActiveMqPlugin/config.jelly b/src/main/resources/org/jenkinsci/plugins/activemq/ActiveMqPlugin/config.jelly deleted file mode 100644 index 879ae01..0000000 --- a/src/main/resources/org/jenkinsci/plugins/activemq/ActiveMqPlugin/config.jelly +++ /dev/null @@ -1,10 +0,0 @@ - - - - - - - - - - diff --git a/src/main/resources/org/jenkinsci/plugins/activemq/ActiveMqPlugin/global.jelly b/src/main/resources/org/jenkinsci/plugins/activemq/ActiveMqPlugin/global.jelly deleted file mode 100644 index 6da81e0..0000000 --- a/src/main/resources/org/jenkinsci/plugins/activemq/ActiveMqPlugin/global.jelly +++ /dev/null @@ -1,21 +0,0 @@ - - - - diff --git a/src/main/resources/org/jenkinsci/plugins/activemq/ActiveMqPlugin/help-activeMqUrl.html b/src/main/resources/org/jenkinsci/plugins/activemq/ActiveMqPlugin/help-activeMqUrl.html deleted file mode 100644 index df2e815..0000000 --- a/src/main/resources/org/jenkinsci/plugins/activemq/ActiveMqPlugin/help-activeMqUrl.html +++ /dev/null @@ -1,6 +0,0 @@ -
- This HTML fragment will be injected into the configuration screen - when the user clicks the 'help' icon. See global.jelly for how the - form decides which page to load. - You can have any HTML fragment here. -
diff --git a/src/main/resources/org/jenkinsci/plugins/activemq/ActiveMqPlugin/help-name.html b/src/main/resources/org/jenkinsci/plugins/activemq/ActiveMqPlugin/help-name.html deleted file mode 100644 index 288f214..0000000 --- a/src/main/resources/org/jenkinsci/plugins/activemq/ActiveMqPlugin/help-name.html +++ /dev/null @@ -1,6 +0,0 @@ -
- Help file for fields are discovered through a file name convention. This file is - help for the "name" field. You can have arbitrary HTML here. You can write - this file as a Jelly script if you need a dynamic content (but if you do so, change - the extension to .jelly). -
-- cgit v1.2.3