diff options
author | Trygve Laugstøl <trygvis@inamo.no> | 2014-04-13 00:04:13 +0200 |
---|---|---|
committer | Trygve Laugstøl <trygvis@inamo.no> | 2014-04-13 00:04:13 +0200 |
commit | 07dde8de9d6040e60bf59e238444f620dba330c0 (patch) | |
tree | 0be82f05640bd84db734034f91022aa1532d5217 | |
parent | a521a2d74c480c53c1004e7e5e2addd28a0578f4 (diff) | |
download | calamus-07dde8de9d6040e60bf59e238444f620dba330c0.tar.gz calamus-07dde8de9d6040e60bf59e238444f620dba330c0.tar.bz2 calamus-07dde8de9d6040e60bf59e238444f620dba330c0.tar.xz calamus-07dde8de9d6040e60bf59e238444f620dba330c0.zip |
wip
7 files changed, 163 insertions, 8 deletions
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<Artifact> 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<ProcessTrigger> processTriggers = new ArrayList<>(); + + public String getName() { + return name; + } + + public List<ProcessTrigger> 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<String, Object> 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 @@ <configuration debug="false"> <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender"> <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"> - <Pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</Pattern> + <Pattern>%d{HH:mm:ss.SSS} %-5level [%-60thread] %-50logger{36} - [TX=%X{btm-gtrid}] - %msg%n</Pattern> </encoder> </appender> - <root level="INFO"> + + <logger name="org.hibernate.hql.internal" level="INFO"/> + + <root level="DEBUG"> <appender-ref ref="CONSOLE"/> </root> </configuration> |