summaryrefslogtreecommitdiff
path: root/calamus-jenkins-plugin/src
diff options
context:
space:
mode:
authorTrygve Laugstøl <trygvis@inamo.no>2014-04-12 17:55:28 +0200
committerTrygve Laugstøl <trygvis@inamo.no>2014-04-12 17:55:28 +0200
commitbfcfcf5e3b4301bc94c27f47bfda61693edf3595 (patch)
treed6eedd800b1b34fd66ccbd86e5f215aaa40ccd3d /calamus-jenkins-plugin/src
downloadcalamus-bfcfcf5e3b4301bc94c27f47bfda61693edf3595.tar.gz
calamus-bfcfcf5e3b4301bc94c27f47bfda61693edf3595.tar.bz2
calamus-bfcfcf5e3b4301bc94c27f47bfda61693edf3595.tar.xz
calamus-bfcfcf5e3b4301bc94c27f47bfda61693edf3595.zip
wip
Diffstat (limited to 'calamus-jenkins-plugin/src')
-rw-r--r--calamus-jenkins-plugin/src/main/java/org/jenkinsci/plugins/calamus/HelloWorldBuilder.java152
-rw-r--r--calamus-jenkins-plugin/src/main/java/org/jenkinsci/plugins/calamus/JbpmRunListener.java42
-rw-r--r--calamus-jenkins-plugin/src/main/java/org/jenkinsci/plugins/calamus/MqClient.java48
-rw-r--r--calamus-jenkins-plugin/src/main/resources/index.jelly6
-rw-r--r--calamus-jenkins-plugin/src/main/resources/org/jenkinsci/plugins/calamus/HelloWorldBuilder/config.jelly15
-rw-r--r--calamus-jenkins-plugin/src/main/resources/org/jenkinsci/plugins/calamus/HelloWorldBuilder/global.jelly20
-rw-r--r--calamus-jenkins-plugin/src/main/resources/org/jenkinsci/plugins/calamus/HelloWorldBuilder/help-name.html6
-rw-r--r--calamus-jenkins-plugin/src/main/resources/org/jenkinsci/plugins/calamus/HelloWorldBuilder/help-useFrench.html6
8 files changed, 295 insertions, 0 deletions
diff --git a/calamus-jenkins-plugin/src/main/java/org/jenkinsci/plugins/calamus/HelloWorldBuilder.java b/calamus-jenkins-plugin/src/main/java/org/jenkinsci/plugins/calamus/HelloWorldBuilder.java
new file mode 100644
index 0000000..5844a26
--- /dev/null
+++ b/calamus-jenkins-plugin/src/main/java/org/jenkinsci/plugins/calamus/HelloWorldBuilder.java
@@ -0,0 +1,152 @@
+package org.jenkinsci.plugins.calamus;
+
+import hudson.Launcher;
+import hudson.Extension;
+import hudson.util.FormValidation;
+import hudson.model.AbstractBuild;
+import hudson.model.BuildListener;
+import hudson.model.AbstractProject;
+import hudson.tasks.Builder;
+import hudson.tasks.BuildStepDescriptor;
+import net.sf.json.JSONObject;
+import org.kohsuke.stapler.DataBoundConstructor;
+import org.kohsuke.stapler.StaplerRequest;
+import org.kohsuke.stapler.QueryParameter;
+
+import javax.servlet.ServletException;
+import java.io.IOException;
+
+/**
+ * Sample {@link Builder}.
+ *
+ * <p>
+ * When the user configures the project and enables this builder,
+ * {@link DescriptorImpl#newInstance(StaplerRequest)} is invoked
+ * and a new {@link HelloWorldBuilder} is created. The created
+ * instance is persisted to the project configuration XML by using
+ * XStream, so this allows you to use instance fields (like {@link #name})
+ * to remember the configuration.
+ *
+ * <p>
+ * When a build is performed, the {@link #perform(AbstractBuild, Launcher, BuildListener)}
+ * method will be invoked.
+ *
+ * @author Kohsuke Kawaguchi
+ */
+public class HelloWorldBuilder extends Builder {
+
+ private final String name;
+
+ // Fields in config.jelly must match the parameter names in the "DataBoundConstructor"
+ @DataBoundConstructor
+ public HelloWorldBuilder(String name) {
+ this.name = name;
+ }
+
+ /**
+ * We'll use this from the <tt>config.jelly</tt>.
+ */
+ public String getName() {
+ return name;
+ }
+
+ @Override
+ public boolean perform(AbstractBuild build, Launcher launcher, BuildListener listener) {
+ // This is where you 'build' the project.
+ // Since this is a dummy, we just say 'hello world' and call that a build.
+
+ // This also shows how you can consult the global configuration of the builder
+ if (getDescriptor().getUseFrench())
+ listener.getLogger().println("Bonjour, "+name+"!");
+ else
+ listener.getLogger().println("Hello, "+name+"!");
+ return true;
+ }
+
+ // Overridden for better type safety.
+ // If your plugin doesn't really define any property on Descriptor,
+ // you don't have to do this.
+ @Override
+ public DescriptorImpl getDescriptor() {
+ return (DescriptorImpl)super.getDescriptor();
+ }
+
+ /**
+ * Descriptor for {@link HelloWorldBuilder}. Used as a singleton.
+ * The class is marked as public so that it can be accessed from views.
+ *
+ * <p>
+ * See <tt>src/main/resources/hudson/plugins/hello_world/HelloWorldBuilder/*.jelly</tt>
+ * for the actual HTML fragment for the configuration screen.
+ */
+ @Extension // This indicates to Jenkins that this is an implementation of an extension point.
+ public static final class DescriptorImpl extends BuildStepDescriptor<Builder> {
+ /**
+ * To persist global configuration information,
+ * simply store it in a field and call save().
+ *
+ * <p>
+ * If you don't want fields to be persisted, use <tt>transient</tt>.
+ */
+ private boolean useFrench;
+
+ /**
+ * In order to load the persisted global configuration, you have to
+ * call load() in the constructor.
+ */
+ public DescriptorImpl() {
+ load();
+ }
+
+ /**
+ * Performs on-the-fly validation of the form field 'name'.
+ *
+ * @param value
+ * This parameter receives the value that the user has typed.
+ * @return
+ * Indicates the outcome of the validation. This is sent to the browser.
+ */
+ public FormValidation doCheckName(@QueryParameter String value)
+ throws IOException, ServletException {
+ if (value.length() == 0)
+ return FormValidation.error("Please set a name");
+ if (value.length() < 4)
+ return FormValidation.warning("Isn't the name too short?");
+ return FormValidation.ok();
+ }
+
+ public boolean isApplicable(Class<? extends AbstractProject> aClass) {
+ // Indicates that this builder can be used with all kinds of project types
+ return true;
+ }
+
+ /**
+ * This human readable name is used in the configuration screen.
+ */
+ public String getDisplayName() {
+ return "Say hello world";
+ }
+
+ @Override
+ public boolean configure(StaplerRequest req, JSONObject formData) throws FormException {
+ // To persist global configuration information,
+ // set that to properties and call save().
+ useFrench = formData.getBoolean("useFrench");
+ // ^Can also use req.bindJSON(this, formData);
+ // (easier when there are many fields; need set* methods for this, like setUseFrench)
+ save();
+ return super.configure(req,formData);
+ }
+
+ /**
+ * This method returns true if the global configuration says we should speak French.
+ *
+ * The method name is bit awkward because global.jelly calls this method to determine
+ * the initial state of the checkbox by the naming convention.
+ */
+ public boolean getUseFrench() {
+ return useFrench;
+ }
+ }
+}
+
diff --git a/calamus-jenkins-plugin/src/main/java/org/jenkinsci/plugins/calamus/JbpmRunListener.java b/calamus-jenkins-plugin/src/main/java/org/jenkinsci/plugins/calamus/JbpmRunListener.java
new file mode 100644
index 0000000..2376516
--- /dev/null
+++ b/calamus-jenkins-plugin/src/main/java/org/jenkinsci/plugins/calamus/JbpmRunListener.java
@@ -0,0 +1,42 @@
+package org.jenkinsci.plugins.calamus;
+
+import hudson.Extension;
+import hudson.model.Build;
+import hudson.model.TaskListener;
+import hudson.model.listeners.RunListener;
+
+import javax.annotation.Nonnull;
+
+@Extension
+public class JbpmRunListener extends RunListener<Build> {
+
+ private final MqClient mqClient;
+
+ public JbpmRunListener() {
+ super(Build.class);
+
+ System.out.println("JbpmRunListener.JbpmRunListener");
+
+ mqClient = new MqClient("tcp://localhost:61616");
+ }
+
+ @Override
+ public void onCompleted(Build build, @Nonnull TaskListener listener) {
+ System.out.println("JbpmRunListener.onCompleted");
+ System.out.println("build = " + build);
+
+ System.out.println("build variables");
+ for (Object o : build.getBuildVariables().entrySet()) {
+ System.out.println(o);
+ }
+
+ mqClient.sendMessage(build.getProject().getName(), build.getNumber(), build.getResult().toString());
+
+// Jenkins jenkins = Jenkins.getInstance();
+// List<AbstractProject> projects = jenkins.getAllItems(AbstractProject.class);
+// for (AbstractProject project : projects) {
+// System.out.println("project.getPronoun() = " + project.getPronoun());
+// System.out.println("project.getName() = " + project.getName());
+// }
+ }
+}
diff --git a/calamus-jenkins-plugin/src/main/java/org/jenkinsci/plugins/calamus/MqClient.java b/calamus-jenkins-plugin/src/main/java/org/jenkinsci/plugins/calamus/MqClient.java
new file mode 100644
index 0000000..6ca4264
--- /dev/null
+++ b/calamus-jenkins-plugin/src/main/java/org/jenkinsci/plugins/calamus/MqClient.java
@@ -0,0 +1,48 @@
+package org.jenkinsci.plugins.calamus;
+
+import org.apache.activemq.ActiveMQConnectionFactory;
+
+import javax.jms.Connection;
+import javax.jms.DeliveryMode;
+import javax.jms.Destination;
+import javax.jms.JMSException;
+import javax.jms.MapMessage;
+import javax.jms.MessageProducer;
+import javax.jms.Session;
+
+import static javax.jms.Session.AUTO_ACKNOWLEDGE;
+
+public class MqClient {
+
+ ActiveMQConnectionFactory connectionFactory;
+
+ public MqClient(String brokerUrl) {
+ this.connectionFactory = new ActiveMQConnectionFactory(brokerUrl);
+ }
+
+ public 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");
+
+ MessageProducer producer = session.createProducer(destination);
+ producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
+
+ MapMessage message = session.createMapMessage();
+ message.setString("jobName", jobName);
+ message.setInt("buildNumber", buildNumber);
+ message.setString("result", result);
+
+ producer.send(message);
+
+ session.close();
+ connection.close();
+ } catch (JMSException e) {
+ e.printStackTrace();
+ }
+ }
+}
diff --git a/calamus-jenkins-plugin/src/main/resources/index.jelly b/calamus-jenkins-plugin/src/main/resources/index.jelly
new file mode 100644
index 0000000..538998f
--- /dev/null
+++ b/calamus-jenkins-plugin/src/main/resources/index.jelly
@@ -0,0 +1,6 @@
+<!--
+ This view is used to render the installed plugins page.
+-->
+<div>
+ This plugin is a sample to explain how to write a Jenkins plugin.
+</div>
diff --git a/calamus-jenkins-plugin/src/main/resources/org/jenkinsci/plugins/calamus/HelloWorldBuilder/config.jelly b/calamus-jenkins-plugin/src/main/resources/org/jenkinsci/plugins/calamus/HelloWorldBuilder/config.jelly
new file mode 100644
index 0000000..491280c
--- /dev/null
+++ b/calamus-jenkins-plugin/src/main/resources/org/jenkinsci/plugins/calamus/HelloWorldBuilder/config.jelly
@@ -0,0 +1,15 @@
+<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 for per-project configuration.
+
+ See global.jelly for a general discussion about jelly script.
+ -->
+
+ <!--
+ Creates a text field that shows the value of the "name" property.
+ When submitted, it will be passed to the corresponding constructor parameter.
+ -->
+ <f:entry title="Name" field="name">
+ <f:textbox />
+ </f:entry>
+</j:jelly>
diff --git a/calamus-jenkins-plugin/src/main/resources/org/jenkinsci/plugins/calamus/HelloWorldBuilder/global.jelly b/calamus-jenkins-plugin/src/main/resources/org/jenkinsci/plugins/calamus/HelloWorldBuilder/global.jelly
new file mode 100644
index 0000000..068b0c0
--- /dev/null
+++ b/calamus-jenkins-plugin/src/main/resources/org/jenkinsci/plugins/calamus/HelloWorldBuilder/global.jelly
@@ -0,0 +1,20 @@
+<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="Hello World Builder">
+ <f:entry title="French" field="useFrench"
+ description="Check if we should say hello in French">
+ <f:checkbox />
+ </f:entry>
+ </f:section>
+</j:jelly>
diff --git a/calamus-jenkins-plugin/src/main/resources/org/jenkinsci/plugins/calamus/HelloWorldBuilder/help-name.html b/calamus-jenkins-plugin/src/main/resources/org/jenkinsci/plugins/calamus/HelloWorldBuilder/help-name.html
new file mode 100644
index 0000000..288f214
--- /dev/null
+++ b/calamus-jenkins-plugin/src/main/resources/org/jenkinsci/plugins/calamus/HelloWorldBuilder/help-name.html
@@ -0,0 +1,6 @@
+<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>
diff --git a/calamus-jenkins-plugin/src/main/resources/org/jenkinsci/plugins/calamus/HelloWorldBuilder/help-useFrench.html b/calamus-jenkins-plugin/src/main/resources/org/jenkinsci/plugins/calamus/HelloWorldBuilder/help-useFrench.html
new file mode 100644
index 0000000..df2e815
--- /dev/null
+++ b/calamus-jenkins-plugin/src/main/resources/org/jenkinsci/plugins/calamus/HelloWorldBuilder/help-useFrench.html
@@ -0,0 +1,6 @@
+<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>