diff options
author | Trygve Laugstøl <trygvis@inamo.no> | 2014-05-04 23:34:16 +0200 |
---|---|---|
committer | Trygve Laugstøl <trygvis@inamo.no> | 2014-05-04 23:34:16 +0200 |
commit | 5c762750101f12abd51621355a871e42fd9c7a33 (patch) | |
tree | a7dfd74f16199db618994a10cb33ac05b1a6ac17 /src/main | |
parent | 345c0d8f7738aab20d2b45fd6c5ebf911b75e2fa (diff) | |
download | activemq-plugin-5c762750101f12abd51621355a871e42fd9c7a33.tar.gz activemq-plugin-5c762750101f12abd51621355a871e42fd9c7a33.tar.bz2 activemq-plugin-5c762750101f12abd51621355a871e42fd9c7a33.tar.xz activemq-plugin-5c762750101f12abd51621355a871e42fd9c7a33.zip |
o Getting the web configuration to work.
Diffstat (limited to 'src/main')
9 files changed, 217 insertions, 97 deletions
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<ActiveMqPlugin>*/ { - 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<String, String> buildVariables = build.getBuildVariables(); + for (Map.Entry<String, String> 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<Build> { - 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<AbstractProject> 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 @@ -<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define" xmlns:l="/lib/layout" xmlns:t="/lib/hudson" xmlns:f="/lib/form"> - <f:section title="ActiveMQ Plugin"> - <f:entry field="enable" title="Enable"> - <f:checkbox/> - </f:entry> - <f:entry field="brokerUrl" title="ActiveMQ URL" description="For example tcp://127.0.0.1:61616"> - <f:textbox/> - </f:entry> - </f:section> -</j:jelly> 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 @@ -<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define" xmlns:l="/lib/layout" xmlns:t="/lib/hudson" xmlns:f="/lib/form"> - <!-- - This Jelly script is used to produce the global configuration option. - - Jenkins uses a set of tag libraries to provide uniformity in forms. - To determine where this tag is defined, first check the namespace URI, - and then look under $JENKINS/views/. For example, <f:section> is defined - in $JENKINS/views/lib/form/section.jelly. - - It's also often useful to just check other similar scripts to see what - tags they use. Views are always organized according to its owner class, - so it should be straightforward to find them. - --> -<!-- - <f:section title="ActiveMQ Plugin"> - <f:entry title="ActiveMq URL" field="brokerUrl" description="yeah yeah"> - <f:textbox/> - </f:entry> - </f:section> ---> -</j:jelly> 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 @@ -<div> - 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 <tt>HTML</tt> fragment here. -</div> 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 @@ -<div> - Help file for fields are discovered through a file name convention. This file is - help for the "name" field. You can have <i>arbitrary</i> 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 <tt>.jelly</tt>). -</div> |