From 07dde8de9d6040e60bf59e238444f620dba330c0 Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Sun, 13 Apr 2014 00:04:13 +0200 Subject: wip --- .../src/main/java/io/trygvis/engine/Main.java | 8 +++- .../engine/NexusNewArtifactMessageListener.java | 46 ++++++++++++++++++--- .../java/io/trygvis/engine/domain/Artifact.java | 11 +++++ .../java/io/trygvis/engine/domain/Process.java | 25 ++++++++++++ .../io/trygvis/engine/domain/ProcessTrigger.java | 47 ++++++++++++++++++++++ .../resources/db/public/V001_001__baseline.sql | 27 +++++++++++++ calamus-engine/src/main/resources/logback.xml | 7 +++- 7 files changed, 163 insertions(+), 8 deletions(-) create mode 100644 calamus-engine/src/main/java/io/trygvis/engine/domain/Process.java create mode 100644 calamus-engine/src/main/java/io/trygvis/engine/domain/ProcessTrigger.java (limited to 'calamus-engine') diff --git a/calamus-engine/src/main/java/io/trygvis/engine/Main.java b/calamus-engine/src/main/java/io/trygvis/engine/Main.java index f5bfb8a..c77bb7d 100644 --- a/calamus-engine/src/main/java/io/trygvis/engine/Main.java +++ b/calamus-engine/src/main/java/io/trygvis/engine/Main.java @@ -11,8 +11,14 @@ public class Main { StatefulKnowledgeSession session = calamus.getSession(); +// EntityManager entityManager = calamus.getEntityManagerFactory().createEntityManager(); +// EntityTransaction transaction = entityManager.getTransaction(); +// transaction.begin(); +// entityManager.createQuery("FROM Process"); +// transaction.commit(); + try (MqClient buildResultClient = new MqClient(brokerUrl, "jenkins.build-result", new JenkinsBuildResultMessageListener(session)); - MqClient newDeployClient = new MqClient(brokerUrl, "nexus.new-artifact", new NexusNewArtifactMessageListener(session))) { + MqClient newDeployClient = new MqClient(brokerUrl, "nexus.new-artifact", new NexusNewArtifactMessageListener(calamus.getEntityManagerFactory(), session))) { while (true) { Thread.sleep(1000); diff --git a/calamus-engine/src/main/java/io/trygvis/engine/NexusNewArtifactMessageListener.java b/calamus-engine/src/main/java/io/trygvis/engine/NexusNewArtifactMessageListener.java index f3f9f05..22b2c4c 100644 --- a/calamus-engine/src/main/java/io/trygvis/engine/NexusNewArtifactMessageListener.java +++ b/calamus-engine/src/main/java/io/trygvis/engine/NexusNewArtifactMessageListener.java @@ -1,5 +1,10 @@ package io.trygvis.engine; +import io.trygvis.engine.domain.Artifact; +import io.trygvis.engine.domain.Process; +import io.trygvis.engine.domain.ProcessTrigger; +import org.kie.api.runtime.Environment; +import org.kie.api.runtime.EnvironmentName; import org.kie.internal.runtime.StatefulKnowledgeSession; import org.slf4j.Logger; @@ -7,6 +12,11 @@ import javax.jms.JMSException; import javax.jms.Message; import javax.jms.MessageListener; import javax.jms.TextMessage; +import javax.persistence.EntityManager; +import javax.persistence.EntityManagerFactory; +import javax.persistence.EntityTransaction; +import javax.persistence.NoResultException; +import javax.persistence.TypedQuery; import java.io.IOException; import java.io.StringReader; import java.util.Properties; @@ -16,17 +26,16 @@ import static org.slf4j.LoggerFactory.getLogger; public class NexusNewArtifactMessageListener implements MessageListener { private final Logger log = getLogger(getClass()); + private final EntityManagerFactory entityManagerFactory; private final StatefulKnowledgeSession session; - public NexusNewArtifactMessageListener(StatefulKnowledgeSession session) { + public NexusNewArtifactMessageListener(EntityManagerFactory entityManagerFactory, StatefulKnowledgeSession session) { + this.entityManagerFactory = entityManagerFactory; this.session = session; } @Override public void onMessage(Message message) { - System.out.println("NexusNewArtifactMessageListener.onMessage"); - - System.out.println("message.getClass() = " + message.getClass()); if (!(message instanceof TextMessage)) { return; } @@ -43,7 +52,34 @@ public class NexusNewArtifactMessageListener implements MessageListener { String classifier = p.getProperty("classifier"); String extension = p.getProperty("extension"); - log.info("New artifact: groupId={}, artifactId={}, version={}, classifier={}, extension={}", groupId, artifactId, version, classifier, extension); + log.info("Artifact event: groupId={}, artifactId={}, version={}, classifier={}, extension={}", groupId, artifactId, version, classifier, extension); + + // Check for any processes that will be started because of this + EntityManager entityManager = entityManagerFactory.createEntityManager(); + + TypedQuery query = entityManager.createQuery("FROM Artifact WHERE groupId=:groupId AND artifactId=:artifactId AND version=:version AND extension=:extension", Artifact.class); + query.setParameter("groupId", groupId); + query.setParameter("artifactId", artifactId); + query.setParameter("version", version); + query.setParameter("extension", extension); + Artifact artifact; + try { + artifact = query.getSingleResult(); + } catch (NoResultException e) { + log.info("New artifact!"); + artifact = new Artifact(groupId, artifactId, version, classifier, extension); + entityManager.persist(artifact); + } + + for (Process process : entityManager.createQuery("FROM Process", Process.class).getResultList()) { + System.out.println("process.name = " + process.getName()); + + for (ProcessTrigger trigger : process.getProcessTriggers()) { + System.out.println("trigger.getMvel() = " + trigger.getMvel()); + boolean match = trigger.matches(artifact); + System.out.println("match = " + match); + } + } /* String type; diff --git a/calamus-engine/src/main/java/io/trygvis/engine/domain/Artifact.java b/calamus-engine/src/main/java/io/trygvis/engine/domain/Artifact.java index 922376b..2fb9beb 100644 --- a/calamus-engine/src/main/java/io/trygvis/engine/domain/Artifact.java +++ b/calamus-engine/src/main/java/io/trygvis/engine/domain/Artifact.java @@ -26,6 +26,17 @@ public class Artifact extends AbstractEntity { @Column(length = 100, nullable = false) private String extension; + protected Artifact() { + } + + public Artifact(String groupId, String artifactId, String version, String classifier, String extension) { + this.groupId = groupId; + this.artifactId = artifactId; + this.version = version; + this.classifier = classifier; + this.extension = extension; + } + public String getGroupId() { return groupId; } diff --git a/calamus-engine/src/main/java/io/trygvis/engine/domain/Process.java b/calamus-engine/src/main/java/io/trygvis/engine/domain/Process.java new file mode 100644 index 0000000..16192f2 --- /dev/null +++ b/calamus-engine/src/main/java/io/trygvis/engine/domain/Process.java @@ -0,0 +1,25 @@ +package io.trygvis.engine.domain; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.OneToMany; +import java.util.ArrayList; +import java.util.List; + +@Entity +public class Process extends AbstractEntity { + + @Column(length = 100, nullable = false) + private String name; + + @OneToMany + private List processTriggers = new ArrayList<>(); + + public String getName() { + return name; + } + + public List getProcessTriggers() { + return processTriggers; + } +} diff --git a/calamus-engine/src/main/java/io/trygvis/engine/domain/ProcessTrigger.java b/calamus-engine/src/main/java/io/trygvis/engine/domain/ProcessTrigger.java new file mode 100644 index 0000000..00b1603 --- /dev/null +++ b/calamus-engine/src/main/java/io/trygvis/engine/domain/ProcessTrigger.java @@ -0,0 +1,47 @@ +package io.trygvis.engine.domain; + +import org.mvel2.MVEL; +import org.mvel2.ParserContext; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.ManyToOne; +import java.io.Serializable; +import java.util.HashMap; +import java.util.Map; + +@Entity +public class ProcessTrigger extends AbstractEntity { + + @ManyToOne + private Process process; + + @Column(length = 1000) + private String mvel; + + transient Serializable expression; + + public String getMvel() { + return mvel; + } + + // ----------------------------------------------------------------------- + // + // ----------------------------------------------------------------------- + + private Serializable getExpression() { + if (expression != null) { + return expression; + } + ParserContext ctx = new ParserContext(); + ctx.setStrongTyping(true); + ctx.addInput("artifact", Artifact.class); + return expression = MVEL.compileExpression(mvel, ctx); + } + + public boolean matches(Artifact artifact) { + Map vars = new HashMap<>(); + vars.put("artifact", artifact); + return MVEL.executeExpression(getExpression(), vars, Boolean.class); + } +} diff --git a/calamus-engine/src/main/resources/db/public/V001_001__baseline.sql b/calamus-engine/src/main/resources/db/public/V001_001__baseline.sql index b1e7a02..3e39c06 100644 --- a/calamus-engine/src/main/resources/db/public/V001_001__baseline.sql +++ b/calamus-engine/src/main/resources/db/public/V001_001__baseline.sql @@ -12,3 +12,30 @@ CREATE TABLE artifact ( git_hash CHAR(40) NOT NULL, level VARCHAR(100) NOT NULL ); + +DROP TABLE IF EXISTS process; +CREATE TABLE process ( + id BIGINT NOT NULL PRIMARY KEY, + name VARCHAR(100) NOT NULL, + process_id VARCHAR(100) NOT NULL +); + +DROP TABLE IF EXISTS process_trigger; +CREATE TABLE process_trigger ( + id BIGINT NOT NULL PRIMARY KEY, + process_id BIGINT NOT NULL REFERENCES process, + mvel VARCHAR(1000) NOT NULL +); + +INSERT INTO process (id, name, process_id) +VALUES ((SELECT + nextval('calamus_seq')), 'Deploy', 'io.trygvis.calamus.Deploy'); + +INSERT INTO process_trigger (id, process_id, mvel) +VALUES ((SELECT + nextval('calamus_seq')), + (SELECT + id + FROM process + WHERE process_id = 'io.trygvis.calamus.Deploy'), + 'artifact.groupId == "demo"'); diff --git a/calamus-engine/src/main/resources/logback.xml b/calamus-engine/src/main/resources/logback.xml index d054893..743dd8b 100644 --- a/calamus-engine/src/main/resources/logback.xml +++ b/calamus-engine/src/main/resources/logback.xml @@ -2,10 +2,13 @@ - %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n + %d{HH:mm:ss.SSS} %-5level [%-60thread] %-50logger{36} - [TX=%X{btm-gtrid}] - %msg%n - + + + + -- cgit v1.2.3