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.kohsuke.stapler.QueryParameter; import org.kohsuke.stapler.StaplerRequest; import org.slf4j.Logger; import javax.jms.JMSException; import java.net.URISyntaxException; import static org.apache.commons.lang.StringUtils.trimToNull; import static org.apache.commons.lang.builder.ToStringBuilder.reflectionToString; import static org.slf4j.LoggerFactory.getLogger; @SuppressWarnings("UnusedDeclaration") @Extension public class ActiveMqGlobalConfig extends GlobalConfiguration { public static final String DEFAULT_TOPIC_NAME = "jenkins"; private static final Logger log = getLogger(ActiveMqPlugin.class); private String brokerUrl; private boolean enable; private String topicName; public ActiveMqGlobalConfig() { log.info("ActiveMqGlobalConfig.ActiveMqGlobalConfig"); load(); log.info("this = " + this); } // ----------------------------------------------------------------------- // Broker url // ----------------------------------------------------------------------- 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(); } ActiveMqClient client; try { client = new ActiveMqClient(brokerUrl, DEFAULT_TOPIC_NAME, null); } catch (URISyntaxException e) { return FormValidation.error("Invalid URI: " + e.getMessage()); } catch (JMSException e) { return FormValidation.warning("Unable to connect to broker"); } client.close(); return FormValidation.ok("Successfully connected to broker"); } // ----------------------------------------------------------------------- // Enable // ----------------------------------------------------------------------- public boolean isEnable() { log.info("ActiveMqGlobalConfig.isEnable"); return enable; } public void setEnable(boolean enable) { log.info("ActiveMqGlobalConfig.setEnable"); this.enable = enable; save(); } // ----------------------------------------------------------------------- // Topic Name // ----------------------------------------------------------------------- public String getTopicName() { if (topicName == null || doCheckBrokerUrl(topicName).kind != FormValidation.Kind.OK) { topicName = DEFAULT_TOPIC_NAME; } return topicName; } public void setTopicName(String topicName) { this.topicName = topicName; save(); } public FormValidation doCheckTopicName(@QueryParameter String value) { value = trimToNull(value); if (value == null) { return FormValidation.ok(); } if (value.matches("^[a-zA-Z]+")) { return FormValidation.ok(); } return FormValidation.error("Invalid topic name."); } @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 reflectionToString(this); } }