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(); } }