package org.jenkinsci.plugins.activemq; import hudson.Extension; import hudson.Plugin; import hudson.model.AbstractProject; import hudson.model.Build; import hudson.model.Cause; import hudson.model.TaskListener; import jenkins.model.GlobalConfiguration; import jenkins.model.Jenkins; import org.slf4j.Logger; import javax.annotation.Nonnull; import javax.jms.JMSException; import java.net.URISyntaxException; import java.util.Map; import static org.slf4j.LoggerFactory.getLogger; @Extension public class ActiveMqPlugin extends Plugin implements ActiveMqClient.BuildRequestListener /*implements ReconfigurableDescribable*/ { // public static final String DISPLAY_NAME = "ActiveMQ Plugin Display Name"; private static final Logger log = getLogger(ActiveMqPlugin.class); private transient ActiveMqClient client; @Override public void start() throws Exception { reconfigure(); } public void postInitialize() throws Exception { log.info("ActiveMqPlugin.postInitialize"); reconfigure(); } public void onCompleted(Build build, @Nonnull TaskListener listener) { log.info("ActiveMqPlugin.onCompleted: listener={}", listener); @SuppressWarnings("unchecked") Map buildVariables = build.getBuildVariables(); log.info("Build variables"); 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 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); try { client = new ActiveMqClient(brokerUrl, config.getTopicName(), this); } catch (JMSException e) { log.warn("Unable to connect to queue"); } catch (URISyntaxException e) { log.warn("Unable to connect to queue"); } } else { if (client != null) { log.info("Disposing current JMS client."); client.close(); client = null; } } } @Override public void onBuildRequest(ActiveMqClient.BuildRequest req) { log.info("ActiveMqPlugin.onBuildRequest"); log.info(req.parameters.toString()); for (AbstractProject project : Jenkins.getInstance().getAllItems(AbstractProject.class)) { log.info("Checking project " + project.getDisplayName()); ActiveMqBuildTrigger trigger = (ActiveMqBuildTrigger) project.getTrigger(ActiveMqBuildTrigger.class); if (trigger == null) { log.debug("No trigger configured"); continue; } if (trigger.matches(req)) { log.debug("MVEL matched!"); project.scheduleBuild(new ActiveMqCause()); } else { log.debug("MVEL did not match"); } } } public static class ActiveMqCause extends Cause { @Override public String getShortDescription() { return "ActiveMQ cause"; } } }