summaryrefslogtreecommitdiff
path: root/calamus-engine
diff options
context:
space:
mode:
authorTrygve Laugstøl <trygvis@inamo.no>2014-04-12 20:37:55 +0200
committerTrygve Laugstøl <trygvis@inamo.no>2014-04-12 20:37:55 +0200
commita521a2d74c480c53c1004e7e5e2addd28a0578f4 (patch)
treef6d36cdecb56bc35f18faf496c8251a14f3f3a26 /calamus-engine
parentbfcfcf5e3b4301bc94c27f47bfda61693edf3595 (diff)
downloadcalamus-a521a2d74c480c53c1004e7e5e2addd28a0578f4.tar.gz
calamus-a521a2d74c480c53c1004e7e5e2addd28a0578f4.tar.bz2
calamus-a521a2d74c480c53c1004e7e5e2addd28a0578f4.tar.xz
calamus-a521a2d74c480c53c1004e7e5e2addd28a0578f4.zip
wip
Diffstat (limited to 'calamus-engine')
-rwxr-xr-xcalamus-engine/pom.xml40
-rw-r--r--calamus-engine/src/main/java/io/trygvis/engine/CalamusJbpm.java50
-rw-r--r--calamus-engine/src/main/java/io/trygvis/engine/JenkinsBuildResultMessageListener.java53
-rw-r--r--calamus-engine/src/main/java/io/trygvis/engine/Main.java19
-rw-r--r--calamus-engine/src/main/java/io/trygvis/engine/MqClient.java41
-rw-r--r--calamus-engine/src/main/java/io/trygvis/engine/NexusNewArtifactMessageListener.java58
-rw-r--r--calamus-engine/src/main/java/io/trygvis/engine/domain/AbstractEntity.java19
-rw-r--r--calamus-engine/src/main/java/io/trygvis/engine/domain/Artifact.java48
-rw-r--r--calamus-engine/src/main/resources/META-INF/persistence.xml4
-rw-r--r--calamus-engine/src/main/resources/db/public/V001_000__jbpm.sql (renamed from calamus-engine/src/main/resources/db/postgresql-jbpm-schema.sql)3
-rw-r--r--calamus-engine/src/main/resources/db/public/V001_001__baseline.sql14
-rw-r--r--calamus-engine/src/main/resources/logback.xml11
12 files changed, 302 insertions, 58 deletions
diff --git a/calamus-engine/pom.xml b/calamus-engine/pom.xml
index 4923295..98ad6bf 100755
--- a/calamus-engine/pom.xml
+++ b/calamus-engine/pom.xml
@@ -13,6 +13,7 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<version.activemq>5.9.1</version.activemq>
+ <version.flyway>2.2.1</version.flyway>
<hibernate.version>4.2.11.Final</hibernate.version>
<slf4j.version>1.7.4</slf4j.version>
@@ -143,6 +144,11 @@
<scope>test</scope>
</dependency>
-->
+ <dependency>
+ <groupId>com.googlecode.flyway</groupId>
+ <artifactId>flyway-core</artifactId>
+ <version>${version.flyway}</version>
+ </dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
@@ -340,23 +346,23 @@
</dependency>
</dependencies>
-<!--
- <repositories>
- <repository>
- <id>jboss-public-repository-group</id>
- <name>JBoss Public Repository Group</name>
- <url>http://repository.jboss.org/nexus/content/groups/public/</url>
- <releases>
- <enabled>true</enabled>
- <updatePolicy>never</updatePolicy>
- </releases>
- <snapshots>
- <enabled>true</enabled>
- <updatePolicy>daily</updatePolicy>
- </snapshots>
- </repository>
- </repositories>
--->
+ <!--
+ <repositories>
+ <repository>
+ <id>jboss-public-repository-group</id>
+ <name>JBoss Public Repository Group</name>
+ <url>http://repository.jboss.org/nexus/content/groups/public/</url>
+ <releases>
+ <enabled>true</enabled>
+ <updatePolicy>never</updatePolicy>
+ </releases>
+ <snapshots>
+ <enabled>true</enabled>
+ <updatePolicy>daily</updatePolicy>
+ </snapshots>
+ </repository>
+ </repositories>
+ -->
<build>
<plugins>
<plugin>
diff --git a/calamus-engine/src/main/java/io/trygvis/engine/CalamusJbpm.java b/calamus-engine/src/main/java/io/trygvis/engine/CalamusJbpm.java
index 0a7be62..cc2ff41 100644
--- a/calamus-engine/src/main/java/io/trygvis/engine/CalamusJbpm.java
+++ b/calamus-engine/src/main/java/io/trygvis/engine/CalamusJbpm.java
@@ -2,6 +2,9 @@ package io.trygvis.engine;
import bitronix.tm.TransactionManagerServices;
import bitronix.tm.resource.jdbc.PoolingDataSource;
+import com.googlecode.flyway.core.Flyway;
+import com.googlecode.flyway.core.api.MigrationInfo;
+import com.googlecode.flyway.core.api.MigrationInfoService;
import org.jbpm.bpmn2.handler.ServiceTaskHandler;
import org.jbpm.process.audit.AuditLoggerFactory;
import org.kie.api.KieBase;
@@ -13,11 +16,21 @@ import org.kie.internal.io.ResourceFactory;
import org.kie.internal.persistence.jpa.JPAKnowledgeService;
import org.kie.internal.runtime.StatefulKnowledgeSession;
import org.kie.internal.utils.KieHelper;
+import org.slf4j.Logger;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
+import javax.sql.DataSource;
+import java.util.Date;
+
+import static java.lang.String.format;
+import static org.slf4j.LoggerFactory.getLogger;
public class CalamusJbpm {
+ private final Logger log = getLogger(getClass());
+
+ private final EntityManagerFactory emf;
+
private final StatefulKnowledgeSession session;
public static enum CalamusProcess {
@@ -42,7 +55,9 @@ public class CalamusJbpm {
ds.getDriverProperties().put("driverClassName", org.postgresql.Driver.class.getCanonicalName());
ds.init();
- EntityManagerFactory emf = Persistence.createEntityManagerFactory("org.jbpm.persistence.jpa");
+ runFlyway(log, ds, "", "public");
+
+ emf = Persistence.createEntityManagerFactory("org.jbpm.persistence.jpa");
Environment env = KnowledgeBaseFactory.newEnvironment();
env.set(EnvironmentName.ENTITY_MANAGER_FACTORY, emf);
env.set(EnvironmentName.TRANSACTION_MANAGER, TransactionManagerServices.getTransactionManager());
@@ -63,7 +78,40 @@ public class CalamusJbpm {
return session;
}
+ public EntityManagerFactory getEntityManagerFactory() {
+ return emf;
+ }
+
public void startProcess(CalamusProcess process) {
ProcessInstance processInstance = session.startProcess(process.name);
}
+
+ private static void runFlyway(Logger log, DataSource dataSource, String prefix, String schema) {
+ log.info("Running migrations for {}", schema);
+
+ Flyway flyway = new Flyway();
+ flyway.setDataSource(dataSource);
+ flyway.setSchemas(schema);
+ flyway.setLocations("db/" + prefix);
+ flyway.setInitOnMigrate(true);
+ MigrationInfoService info = flyway.info();
+ log.info(format("%-15s %-10s %-19s %s", "Version", "State", "Installed on", "Description"));
+ for (MigrationInfo mi : info.all()) {
+ Date installedOn = mi.getInstalledOn();
+ log.info(format("%-15s %-10s %-19s %s",
+ mi.getVersion(),
+ trimToEmpty(mi.getState().getDisplayName()),
+ installedOn != null ? new Date(installedOn.getTime()).toString() : "",
+ trimToEmpty(mi.getDescription())));
+ }
+ flyway.migrate();
+ }
+
+ private static String trimToEmpty(String string) {
+ if (string == null) {
+ return "";
+ }
+
+ return string.trim();
+ }
}
diff --git a/calamus-engine/src/main/java/io/trygvis/engine/JenkinsBuildResultMessageListener.java b/calamus-engine/src/main/java/io/trygvis/engine/JenkinsBuildResultMessageListener.java
new file mode 100644
index 0000000..08b5e5b
--- /dev/null
+++ b/calamus-engine/src/main/java/io/trygvis/engine/JenkinsBuildResultMessageListener.java
@@ -0,0 +1,53 @@
+package io.trygvis.engine;
+
+import org.kie.internal.runtime.StatefulKnowledgeSession;
+import org.slf4j.Logger;
+
+import javax.jms.JMSException;
+import javax.jms.Message;
+import javax.jms.MessageListener;
+import javax.jms.TextMessage;
+import java.io.IOException;
+import java.io.StringReader;
+import java.util.Properties;
+
+import static java.lang.Integer.parseInt;
+import static org.slf4j.LoggerFactory.getLogger;
+
+public class JenkinsBuildResultMessageListener implements MessageListener {
+ private final Logger log = getLogger(getClass());
+
+ private StatefulKnowledgeSession session;
+
+ public JenkinsBuildResultMessageListener(StatefulKnowledgeSession session) {
+ this.session = session;
+ }
+
+ @Override
+ public void onMessage(Message message) {
+ if (!(message instanceof TextMessage)) {
+ return;
+ }
+
+ try {
+ TextMessage m = (TextMessage) message;
+ Properties p = new Properties();
+ p.load(new StringReader(m.getText()));
+
+ String jobName = p.getProperty("jobName");
+ int buildNumber = parseInt(p.getProperty("buildNumber"));
+ String result = p.getProperty("result");
+
+ log.info("New build: jobName={}, buildNumber={}, result={}", jobName, buildNumber, result);
+
+ /*
+ String type;
+ Object event;
+ long processInstanceId;
+ jbpm.signalEvent(type, event, processInstanceId);
+ */
+ } catch (JMSException | IOException e) {
+ e.printStackTrace();
+ }
+ }
+}
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 bc322e8..f5bfb8a 100644
--- a/calamus-engine/src/main/java/io/trygvis/engine/Main.java
+++ b/calamus-engine/src/main/java/io/trygvis/engine/Main.java
@@ -1,13 +1,22 @@
package io.trygvis.engine;
-import static io.trygvis.engine.CalamusJbpm.CalamusProcess.DEPLOY;
+import org.kie.internal.runtime.StatefulKnowledgeSession;
public class Main {
- public static void main(String[] args) {
- CalamusJbpm jbpm = new CalamusJbpm();
+ public static void main(String[] args) throws Exception {
- MqClient mqClient = new MqClient(jbpm.getSession(), "tcp://localhost:61616");
+ CalamusJbpm calamus = new CalamusJbpm();
- jbpm.startProcess(DEPLOY);
+ String brokerUrl = "tcp://localhost:61616";
+
+ StatefulKnowledgeSession session = calamus.getSession();
+
+ try (MqClient buildResultClient = new MqClient(brokerUrl, "jenkins.build-result", new JenkinsBuildResultMessageListener(session));
+ MqClient newDeployClient = new MqClient(brokerUrl, "nexus.new-artifact", new NexusNewArtifactMessageListener(session))) {
+
+ while (true) {
+ Thread.sleep(1000);
+ }
+ }
}
}
diff --git a/calamus-engine/src/main/java/io/trygvis/engine/MqClient.java b/calamus-engine/src/main/java/io/trygvis/engine/MqClient.java
index b609c9f..91eec37 100644
--- a/calamus-engine/src/main/java/io/trygvis/engine/MqClient.java
+++ b/calamus-engine/src/main/java/io/trygvis/engine/MqClient.java
@@ -6,23 +6,24 @@ import org.kie.internal.runtime.StatefulKnowledgeSession;
import javax.jms.Connection;
import javax.jms.Destination;
import javax.jms.JMSException;
-import javax.jms.MapMessage;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.Session;
+import javax.jms.TextMessage;
+import java.io.IOException;
+import java.io.StringReader;
+import java.util.Properties;
+import static java.lang.Integer.parseInt;
import static javax.jms.Session.AUTO_ACKNOWLEDGE;
public class MqClient implements AutoCloseable {
private Connection connection;
-// private final ActiveMQConnectionFactory connectionFactory;
-// private final StatefulKnowledgeSession jbpm;
- public MqClient(final StatefulKnowledgeSession jbpm, String brukerUrl) {
-// this.jbpm = jbpm;
- ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(brukerUrl);
+ public MqClient(String brokerUrl, String queueName, MessageListener messageListener) {
+ ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(brokerUrl);
try {
connection = connectionFactory.createConnection();
@@ -30,34 +31,10 @@ public class MqClient implements AutoCloseable {
Session session = connection.createSession(false, AUTO_ACKNOWLEDGE);
- Destination destination = session.createQueue("jenkins.build-result");
+ Destination destination = session.createQueue(queueName);
MessageConsumer consumer = session.createConsumer(destination);
- consumer.setMessageListener(new MessageListener() {
- @Override
- public void onMessage(Message message) {
- if (!(message instanceof MapMessage)) {
- return;
- }
-
- MapMessage m = (MapMessage) message;
-
- try {
- String jobName = m.getString("jobName");
- int buildNumber = m.getInt("buildNumber");
- String result = m.getString("result");
-
-/*
- String type;
- Object event;
- long processInstanceId;
- jbpm.signalEvent(type, event, processInstanceId);
-*/
- } catch (JMSException e) {
- e.printStackTrace();
- }
- }
- });
+ consumer.setMessageListener(messageListener);
} catch (JMSException e) {
e.printStackTrace();
}
diff --git a/calamus-engine/src/main/java/io/trygvis/engine/NexusNewArtifactMessageListener.java b/calamus-engine/src/main/java/io/trygvis/engine/NexusNewArtifactMessageListener.java
new file mode 100644
index 0000000..f3f9f05
--- /dev/null
+++ b/calamus-engine/src/main/java/io/trygvis/engine/NexusNewArtifactMessageListener.java
@@ -0,0 +1,58 @@
+package io.trygvis.engine;
+
+import org.kie.internal.runtime.StatefulKnowledgeSession;
+import org.slf4j.Logger;
+
+import javax.jms.JMSException;
+import javax.jms.Message;
+import javax.jms.MessageListener;
+import javax.jms.TextMessage;
+import java.io.IOException;
+import java.io.StringReader;
+import java.util.Properties;
+
+import static org.slf4j.LoggerFactory.getLogger;
+
+public class NexusNewArtifactMessageListener implements MessageListener {
+ private final Logger log = getLogger(getClass());
+
+ private final StatefulKnowledgeSession session;
+
+ public NexusNewArtifactMessageListener(StatefulKnowledgeSession session) {
+ 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;
+ }
+
+ try {
+ TextMessage m = (TextMessage) message;
+ Properties p = new Properties();
+ p.load(new StringReader(m.getText()));
+
+ String repository = p.getProperty("repository");
+ String groupId = p.getProperty("groupId");
+ String artifactId = p.getProperty("artifactId");
+ String version = p.getProperty("version");
+ String classifier = p.getProperty("classifier");
+ String extension = p.getProperty("extension");
+
+ log.info("New artifact: groupId={}, artifactId={}, version={}, classifier={}, extension={}", groupId, artifactId, version, classifier, extension);
+
+ /*
+ String type;
+ Object event;
+ long processInstanceId;
+ jbpm.signalEvent(type, event, processInstanceId);
+ */
+ } catch (JMSException | IOException e) {
+ e.printStackTrace();
+ }
+ }
+}
diff --git a/calamus-engine/src/main/java/io/trygvis/engine/domain/AbstractEntity.java b/calamus-engine/src/main/java/io/trygvis/engine/domain/AbstractEntity.java
new file mode 100644
index 0000000..f1357b4
--- /dev/null
+++ b/calamus-engine/src/main/java/io/trygvis/engine/domain/AbstractEntity.java
@@ -0,0 +1,19 @@
+package io.trygvis.engine.domain;
+
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.GenerationType;
+import javax.persistence.Id;
+import javax.persistence.SequenceGenerator;
+
+@Entity
+@SequenceGenerator(name = "calamusSeq", sequenceName = "calamus_seq")
+public abstract class AbstractEntity {
+ @Id
+ @GeneratedValue(strategy = GenerationType.AUTO, generator = "calamusSeq")
+ private Long id;
+
+ public Long getId() {
+ return id;
+ }
+}
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
new file mode 100644
index 0000000..922376b
--- /dev/null
+++ b/calamus-engine/src/main/java/io/trygvis/engine/domain/Artifact.java
@@ -0,0 +1,48 @@
+package io.trygvis.engine.domain;
+
+import javax.persistence.Column;
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.GenerationType;
+import javax.persistence.Id;
+import java.util.Optional;
+
+import static java.util.Optional.ofNullable;
+
+@Entity
+public class Artifact extends AbstractEntity {
+ @Column(length = 100, nullable = false)
+ private String groupId;
+
+ @Column(length = 100, nullable = false)
+ private String artifactId;
+
+ @Column(length = 100, nullable = false)
+ private String version;
+
+ @Column(length = 100)
+ private String classifier;
+
+ @Column(length = 100, nullable = false)
+ private String extension;
+
+ public String getGroupId() {
+ return groupId;
+ }
+
+ public String getArtifactId() {
+ return artifactId;
+ }
+
+ public String getVersion() {
+ return version;
+ }
+
+ public Optional<String> getClassifier() {
+ return ofNullable(classifier);
+ }
+
+ public String getExtension() {
+ return extension;
+ }
+}
diff --git a/calamus-engine/src/main/resources/META-INF/persistence.xml b/calamus-engine/src/main/resources/META-INF/persistence.xml
index a8a11af..5591390 100644
--- a/calamus-engine/src/main/resources/META-INF/persistence.xml
+++ b/calamus-engine/src/main/resources/META-INF/persistence.xml
@@ -16,14 +16,12 @@
<class>org.jbpm.process.audit.NodeInstanceLog</class>
<class>org.jbpm.process.audit.VariableInstanceLog</class>
<class>org.jbpm.runtime.manager.impl.jpa.ContextMappingInfo</class>
+ <class>io.trygvis.engine.domain.Artifact</class>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQL82Dialect"/>
<property name="hibernate.max_fetch_depth" value="3"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
<property name="hibernate.show_sql" value="true"/>
- <!--
- <property name="hibernate.jdbc.use_streams_for_binary" value="true"/>
- -->
<property name="hibernate.transaction.jta.platform"
value="org.hibernate.service.jta.platform.internal.BitronixJtaPlatform"/>
</properties>
diff --git a/calamus-engine/src/main/resources/db/postgresql-jbpm-schema.sql b/calamus-engine/src/main/resources/db/public/V001_000__jbpm.sql
index e13e6fb..4d2a194 100644
--- a/calamus-engine/src/main/resources/db/postgresql-jbpm-schema.sql
+++ b/calamus-engine/src/main/resources/db/public/V001_000__jbpm.sql
@@ -1,3 +1,6 @@
+DROP SCHEMA public CASCADE;
+CREATE SCHEMA public;
+
CREATE TABLE Attachment (
AttachmentId INT8 NOT NULL,
accessType INT4,
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
new file mode 100644
index 0000000..b1e7a02
--- /dev/null
+++ b/calamus-engine/src/main/resources/db/public/V001_001__baseline.sql
@@ -0,0 +1,14 @@
+DROP SEQUENCE IF EXISTS calamus_seq;
+CREATE SEQUENCE calamus_seq;
+
+DROP TABLE IF EXISTS artifact;
+CREATE TABLE artifact (
+ id BIGINT NOT NULL PRIMARY KEY,
+ group_id VARCHAR(100) NOT NULL,
+ artifact_id VARCHAR(100) NOT NULL,
+ version VARCHAR(100) NOT NULL,
+ classifier VARCHAR(100),
+ extension VARCHAR(100) NOT NULL,
+ git_hash CHAR(40) NOT NULL,
+ level VARCHAR(100) NOT NULL
+);
diff --git a/calamus-engine/src/main/resources/logback.xml b/calamus-engine/src/main/resources/logback.xml
new file mode 100644
index 0000000..d054893
--- /dev/null
+++ b/calamus-engine/src/main/resources/logback.xml
@@ -0,0 +1,11 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<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>
+ </encoder>
+ </appender>
+ <root level="INFO">
+ <appender-ref ref="CONSOLE"/>
+ </root>
+</configuration>