From c8c863ce36f57954369a0b4a15e6c5e720f03f87 Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Sat, 22 Dec 2012 00:31:00 +0100 Subject: o Moving stuff to utils package. --- src/main/java/io/trygvis/esper/testing/Daos.java | 2 +- src/main/java/io/trygvis/esper/testing/Util.java | 2 +- .../io/trygvis/esper/testing/core/BuildDto.java | 21 -- .../io/trygvis/esper/testing/core/CoreDao.java | 73 ------- .../esper/testing/core/JenkinsBuildPoller.java | 53 ----- .../io/trygvis/esper/testing/core/PersonDto.java | 15 -- .../io/trygvis/esper/testing/core/TablePoller.java | 6 +- .../io/trygvis/esper/testing/core/db/BuildDto.java | 21 ++ .../io/trygvis/esper/testing/core/db/CoreDao.java | 73 +++++++ .../trygvis/esper/testing/core/db/PersonDto.java | 15 ++ .../testing/core/jenkins/JenkinsBuildPoller.java | 55 +++++ .../gitorious/GitoriousProjectDiscovery.java | 10 +- .../trygvis/esper/testing/jenkins/JenkinsDao.java | 4 +- .../esper/testing/jenkins/JenkinsImporter.java | 4 +- .../esper/testing/jenkins/JenkinsServerActor.java | 4 +- .../esper/testing/jenkins/JenkinsServerOld.java | 2 +- .../trygvis/esper/testing/nexus/NexusImporter.java | 2 +- .../io/trygvis/esper/testing/object/ActorRef.java | 7 - .../esper/testing/object/ObjectFactory.java | 7 - .../esper/testing/object/ObjectManager.java | 64 ------ .../trygvis/esper/testing/object/ObjectUtil.java | 132 ------------ .../esper/testing/object/TransactionalActor.java | 7 - .../io/trygvis/esper/testing/sql/ResultSetF.java | 18 -- .../java/io/trygvis/esper/testing/sql/SqlF.java | 15 -- .../io/trygvis/esper/testing/sql/SqlOption.java | 109 ---------- .../io/trygvis/esper/testing/task/TaskDao.java | 26 --- .../io/trygvis/esper/testing/task/TaskManager.java | 224 --------------------- .../esper/testing/util/object/ActorRef.java | 7 + .../esper/testing/util/object/ObjectFactory.java | 7 + .../esper/testing/util/object/ObjectManager.java | 64 ++++++ .../esper/testing/util/object/ObjectUtil.java | 132 ++++++++++++ .../testing/util/object/TransactionalActor.java | 7 + .../trygvis/esper/testing/util/sql/ResultSetF.java | 18 ++ .../io/trygvis/esper/testing/util/sql/SqlF.java | 15 ++ .../trygvis/esper/testing/util/sql/SqlOption.java | 109 ++++++++++ 35 files changed, 541 insertions(+), 789 deletions(-) delete mode 100644 src/main/java/io/trygvis/esper/testing/core/BuildDto.java delete mode 100644 src/main/java/io/trygvis/esper/testing/core/CoreDao.java delete mode 100644 src/main/java/io/trygvis/esper/testing/core/JenkinsBuildPoller.java delete mode 100644 src/main/java/io/trygvis/esper/testing/core/PersonDto.java create mode 100644 src/main/java/io/trygvis/esper/testing/core/db/BuildDto.java create mode 100644 src/main/java/io/trygvis/esper/testing/core/db/CoreDao.java create mode 100644 src/main/java/io/trygvis/esper/testing/core/db/PersonDto.java create mode 100644 src/main/java/io/trygvis/esper/testing/core/jenkins/JenkinsBuildPoller.java delete mode 100644 src/main/java/io/trygvis/esper/testing/object/ActorRef.java delete mode 100644 src/main/java/io/trygvis/esper/testing/object/ObjectFactory.java delete mode 100644 src/main/java/io/trygvis/esper/testing/object/ObjectManager.java delete mode 100644 src/main/java/io/trygvis/esper/testing/object/ObjectUtil.java delete mode 100644 src/main/java/io/trygvis/esper/testing/object/TransactionalActor.java delete mode 100644 src/main/java/io/trygvis/esper/testing/sql/ResultSetF.java delete mode 100644 src/main/java/io/trygvis/esper/testing/sql/SqlF.java delete mode 100644 src/main/java/io/trygvis/esper/testing/sql/SqlOption.java delete mode 100644 src/main/java/io/trygvis/esper/testing/task/TaskDao.java delete mode 100644 src/main/java/io/trygvis/esper/testing/task/TaskManager.java create mode 100644 src/main/java/io/trygvis/esper/testing/util/object/ActorRef.java create mode 100644 src/main/java/io/trygvis/esper/testing/util/object/ObjectFactory.java create mode 100644 src/main/java/io/trygvis/esper/testing/util/object/ObjectManager.java create mode 100644 src/main/java/io/trygvis/esper/testing/util/object/ObjectUtil.java create mode 100644 src/main/java/io/trygvis/esper/testing/util/object/TransactionalActor.java create mode 100644 src/main/java/io/trygvis/esper/testing/util/sql/ResultSetF.java create mode 100644 src/main/java/io/trygvis/esper/testing/util/sql/SqlF.java create mode 100644 src/main/java/io/trygvis/esper/testing/util/sql/SqlOption.java (limited to 'src') diff --git a/src/main/java/io/trygvis/esper/testing/Daos.java b/src/main/java/io/trygvis/esper/testing/Daos.java index 5ddb75b..967dae4 100644 --- a/src/main/java/io/trygvis/esper/testing/Daos.java +++ b/src/main/java/io/trygvis/esper/testing/Daos.java @@ -1,6 +1,6 @@ package io.trygvis.esper.testing; -import io.trygvis.esper.testing.core.*; +import io.trygvis.esper.testing.core.db.*; import io.trygvis.esper.testing.gitorious.*; import io.trygvis.esper.testing.jenkins.*; diff --git a/src/main/java/io/trygvis/esper/testing/Util.java b/src/main/java/io/trygvis/esper/testing/Util.java index 4e1bf93..c33e64c 100644 --- a/src/main/java/io/trygvis/esper/testing/Util.java +++ b/src/main/java/io/trygvis/esper/testing/Util.java @@ -4,7 +4,7 @@ import fj.*; import fj.data.*; import static fj.data.Option.*; -import io.trygvis.esper.testing.sql.*; +import io.trygvis.esper.testing.util.sql.*; import org.jdom2.*; import org.joda.time.*; diff --git a/src/main/java/io/trygvis/esper/testing/core/BuildDto.java b/src/main/java/io/trygvis/esper/testing/core/BuildDto.java deleted file mode 100644 index 37de1b3..0000000 --- a/src/main/java/io/trygvis/esper/testing/core/BuildDto.java +++ /dev/null @@ -1,21 +0,0 @@ -package io.trygvis.esper.testing.core; - -import io.trygvis.esper.testing.*; -import org.joda.time.*; - -import java.util.*; - -class BuildDto extends AbstractEntity { - public final DateTime timestamp; - public final boolean success; - public final UUID referenceUuid; - public final String referenceType; - - BuildDto(UUID uuid, DateTime createdDate, DateTime timestamp, boolean success, UUID referenceUuid, String referenceType) { - super(uuid, createdDate); - this.timestamp = timestamp; - this.success = success; - this.referenceUuid = referenceUuid; - this.referenceType = referenceType; - } -} diff --git a/src/main/java/io/trygvis/esper/testing/core/CoreDao.java b/src/main/java/io/trygvis/esper/testing/core/CoreDao.java deleted file mode 100644 index a010dad..0000000 --- a/src/main/java/io/trygvis/esper/testing/core/CoreDao.java +++ /dev/null @@ -1,73 +0,0 @@ -package io.trygvis.esper.testing.core; - -import io.trygvis.esper.testing.*; -import io.trygvis.esper.testing.sql.*; -import org.joda.time.*; - -import java.sql.*; -import java.util.*; - -import static io.trygvis.esper.testing.sql.SqlOption.*; -import static java.lang.System.*; - -public class CoreDao { - private final Connection c; - - public static final String PERSON = "uuid, created_date, name"; - - public static final SqlF person = new SqlF() { - public PersonDto apply(ResultSet rs) throws SQLException { - int i = 1; - return new PersonDto( - UUID.fromString(rs.getString(i++)), - new DateTime(rs.getTimestamp(i++).getTime()), - rs.getString(i)); - } - }; - - public static final String BUILD = "uuid, created_date, timestamp, success, reference_type, reference_uuid"; - - public CoreDao(Connection c) { - this.c = c; - } - - public SqlOption selectPerson(String id) throws SQLException { - try (PreparedStatement s = c.prepareStatement("SELECT " + PERSON + " FROM person WHERE id=?")) { - int i = 1; - s.setString(i, id); - return fromRs(s.executeQuery()).map(person); - } - } - - public SqlOption selectPersonByJenkinsUuid(UUID jenkinsUser) throws SQLException { - try (PreparedStatement s = c.prepareStatement("SELECT " + PERSON + " FROM person WHERE uuid=(SELECT person FROM person_jenkins_user WHERE jenkins_user=?)")) { - int i = 1; - s.setString(i, jenkinsUser.toString()); - return fromRs(s.executeQuery()).map(person); - } - } - - public UUID insertBuild(DateTime timestamp, boolean success, EntityRef ref) throws SQLException { - try (PreparedStatement s = c.prepareStatement("INSERT INTO build(" + BUILD + ") VALUES(?, ?, ?, ?, ?, ?)")) { - UUID uuid = UUID.randomUUID(); - int i = 1; - s.setString(i++, uuid.toString()); - s.setTimestamp(i++, new Timestamp(currentTimeMillis())); - s.setTimestamp(i++, new Timestamp(timestamp.getMillis())); - s.setBoolean(i++, success); - s.setString(i++, ref.type); - s.setString(i, ref.uuid.toString()); - s.executeUpdate(); - return uuid; - } - } - - public void insertBuildParticipant(UUID build, UUID person) throws SQLException { - try (PreparedStatement s = c.prepareStatement("INSERT INTO build_participant(build, person) VALUES(?, ?)")) { - int i = 1; - s.setString(i++, build.toString()); - s.setString(i, person.toString()); - s.executeUpdate(); - } - } -} diff --git a/src/main/java/io/trygvis/esper/testing/core/JenkinsBuildPoller.java b/src/main/java/io/trygvis/esper/testing/core/JenkinsBuildPoller.java deleted file mode 100644 index cb9157c..0000000 --- a/src/main/java/io/trygvis/esper/testing/core/JenkinsBuildPoller.java +++ /dev/null @@ -1,53 +0,0 @@ -package io.trygvis.esper.testing.core; - -import com.jolbox.bonecp.*; -import io.trygvis.esper.testing.*; -import io.trygvis.esper.testing.jenkins.*; -import io.trygvis.esper.testing.sql.*; -import org.slf4j.*; - -import java.sql.*; -import java.util.*; - -import static fj.data.Option.some; -import static io.trygvis.esper.testing.Config.*; -import static io.trygvis.esper.testing.EntityRef.jenkinsRef; - -public class JenkinsBuildPoller implements TablePoller.NewRowCallback { - Logger logger = LoggerFactory.getLogger(getClass()); - - public static void main(String[] args) throws Exception { - String pollerName = "jenkins_build"; - String tableName = "jenkins_build"; - String columnNames = JenkinsDao.JENKINS_BUILD; - SqlF f = JenkinsDao.jenkinsBuild; - TablePoller.NewRowCallback callback = new JenkinsBuildPoller(); - - Config config = loadFromDisk(); - - BoneCPDataSource dataSource = config.createBoneCp(); - - new TablePoller<>(pollerName, tableName, columnNames, some("array_length(users, 1) > 0"), f, callback).work(dataSource); - } - - public void process(Connection c, JenkinsBuildDto jenkinsBuild) throws SQLException { - Daos daos = new Daos(c); - CoreDao coreDao = daos.coreDao; - - UUID uuid = coreDao.insertBuild(jenkinsBuild.timestamp, "SUCCESS".equals(jenkinsBuild.result), jenkinsRef(jenkinsBuild.uuid)); - logger.info("Created build uuid={}", uuid); - - for (UUID user : jenkinsBuild.users) { - SqlOption personO = coreDao.selectPersonByJenkinsUuid(user); - - // This happens if no one has claimed the user id. - if(personO.isNone()) { - continue; - } - - UUID person = personO.get().uuid; - logger.info("Created build participant, person={}", person); - coreDao.insertBuildParticipant(uuid, person); - } - } -} diff --git a/src/main/java/io/trygvis/esper/testing/core/PersonDto.java b/src/main/java/io/trygvis/esper/testing/core/PersonDto.java deleted file mode 100644 index 33355a3..0000000 --- a/src/main/java/io/trygvis/esper/testing/core/PersonDto.java +++ /dev/null @@ -1,15 +0,0 @@ -package io.trygvis.esper.testing.core; - -import io.trygvis.esper.testing.*; -import org.joda.time.*; - -import java.util.*; - -public class PersonDto extends AbstractEntity { - public final String name; - - public PersonDto(UUID uuid, DateTime createdDate, String name) { - super(uuid, createdDate); - this.name = name; - } -} diff --git a/src/main/java/io/trygvis/esper/testing/core/TablePoller.java b/src/main/java/io/trygvis/esper/testing/core/TablePoller.java index c21b5ae..062f5e7 100644 --- a/src/main/java/io/trygvis/esper/testing/core/TablePoller.java +++ b/src/main/java/io/trygvis/esper/testing/core/TablePoller.java @@ -1,14 +1,14 @@ package io.trygvis.esper.testing.core; import fj.data.*; -import io.trygvis.esper.testing.sql.*; +import io.trygvis.esper.testing.util.sql.*; import org.slf4j.*; import javax.sql.*; import java.sql.*; -import static io.trygvis.esper.testing.sql.ResultSetF.*; -import static io.trygvis.esper.testing.sql.SqlOption.*; +import static io.trygvis.esper.testing.util.sql.ResultSetF.*; +import static io.trygvis.esper.testing.util.sql.SqlOption.*; import static java.lang.System.*; public class TablePoller { diff --git a/src/main/java/io/trygvis/esper/testing/core/db/BuildDto.java b/src/main/java/io/trygvis/esper/testing/core/db/BuildDto.java new file mode 100644 index 0000000..5cc6b0a --- /dev/null +++ b/src/main/java/io/trygvis/esper/testing/core/db/BuildDto.java @@ -0,0 +1,21 @@ +package io.trygvis.esper.testing.core.db; + +import io.trygvis.esper.testing.*; +import org.joda.time.*; + +import java.util.*; + +class BuildDto extends AbstractEntity { + public final DateTime timestamp; + public final boolean success; + public final UUID referenceUuid; + public final String referenceType; + + BuildDto(UUID uuid, DateTime createdDate, DateTime timestamp, boolean success, UUID referenceUuid, String referenceType) { + super(uuid, createdDate); + this.timestamp = timestamp; + this.success = success; + this.referenceUuid = referenceUuid; + this.referenceType = referenceType; + } +} diff --git a/src/main/java/io/trygvis/esper/testing/core/db/CoreDao.java b/src/main/java/io/trygvis/esper/testing/core/db/CoreDao.java new file mode 100644 index 0000000..5eab2c9 --- /dev/null +++ b/src/main/java/io/trygvis/esper/testing/core/db/CoreDao.java @@ -0,0 +1,73 @@ +package io.trygvis.esper.testing.core.db; + +import io.trygvis.esper.testing.*; +import io.trygvis.esper.testing.util.sql.*; +import org.joda.time.*; + +import java.sql.*; +import java.util.*; + +import static io.trygvis.esper.testing.util.sql.SqlOption.*; +import static java.lang.System.*; + +public class CoreDao { + private final Connection c; + + public static final String PERSON = "uuid, created_date, name"; + + public static final SqlF person = new SqlF() { + public PersonDto apply(ResultSet rs) throws SQLException { + int i = 1; + return new PersonDto( + UUID.fromString(rs.getString(i++)), + new DateTime(rs.getTimestamp(i++).getTime()), + rs.getString(i)); + } + }; + + public static final String BUILD = "uuid, created_date, timestamp, success, reference_type, reference_uuid"; + + public CoreDao(Connection c) { + this.c = c; + } + + public SqlOption selectPerson(String id) throws SQLException { + try (PreparedStatement s = c.prepareStatement("SELECT " + PERSON + " FROM person WHERE id=?")) { + int i = 1; + s.setString(i, id); + return fromRs(s.executeQuery()).map(person); + } + } + + public SqlOption selectPersonByJenkinsUuid(UUID jenkinsUser) throws SQLException { + try (PreparedStatement s = c.prepareStatement("SELECT " + PERSON + " FROM person WHERE uuid=(SELECT person FROM person_jenkins_user WHERE jenkins_user=?)")) { + int i = 1; + s.setString(i, jenkinsUser.toString()); + return fromRs(s.executeQuery()).map(person); + } + } + + public UUID insertBuild(DateTime timestamp, boolean success, EntityRef ref) throws SQLException { + try (PreparedStatement s = c.prepareStatement("INSERT INTO build(" + BUILD + ") VALUES(?, ?, ?, ?, ?, ?)")) { + UUID uuid = UUID.randomUUID(); + int i = 1; + s.setString(i++, uuid.toString()); + s.setTimestamp(i++, new Timestamp(currentTimeMillis())); + s.setTimestamp(i++, new Timestamp(timestamp.getMillis())); + s.setBoolean(i++, success); + s.setString(i++, ref.type); + s.setString(i, ref.uuid.toString()); + s.executeUpdate(); + return uuid; + } + } + + public void insertBuildParticipant(UUID build, UUID person) throws SQLException { + try (PreparedStatement s = c.prepareStatement("INSERT INTO build_participant(build, person) VALUES(?, ?)")) { + int i = 1; + s.setString(i++, build.toString()); + s.setString(i, person.toString()); + s.executeUpdate(); + } + } +} diff --git a/src/main/java/io/trygvis/esper/testing/core/db/PersonDto.java b/src/main/java/io/trygvis/esper/testing/core/db/PersonDto.java new file mode 100644 index 0000000..c643fc7 --- /dev/null +++ b/src/main/java/io/trygvis/esper/testing/core/db/PersonDto.java @@ -0,0 +1,15 @@ +package io.trygvis.esper.testing.core.db; + +import io.trygvis.esper.testing.*; +import org.joda.time.*; + +import java.util.*; + +public class PersonDto extends AbstractEntity { + public final String name; + + public PersonDto(UUID uuid, DateTime createdDate, String name) { + super(uuid, createdDate); + this.name = name; + } +} diff --git a/src/main/java/io/trygvis/esper/testing/core/jenkins/JenkinsBuildPoller.java b/src/main/java/io/trygvis/esper/testing/core/jenkins/JenkinsBuildPoller.java new file mode 100644 index 0000000..90a4ef4 --- /dev/null +++ b/src/main/java/io/trygvis/esper/testing/core/jenkins/JenkinsBuildPoller.java @@ -0,0 +1,55 @@ +package io.trygvis.esper.testing.core.jenkins; + +import com.jolbox.bonecp.*; +import io.trygvis.esper.testing.*; +import io.trygvis.esper.testing.core.*; +import io.trygvis.esper.testing.core.db.*; +import io.trygvis.esper.testing.jenkins.*; +import io.trygvis.esper.testing.util.sql.*; +import org.slf4j.*; + +import java.sql.*; +import java.util.*; + +import static fj.data.Option.some; +import static io.trygvis.esper.testing.Config.*; +import static io.trygvis.esper.testing.EntityRef.jenkinsRef; + +public class JenkinsBuildPoller implements TablePoller.NewRowCallback { + Logger logger = LoggerFactory.getLogger(getClass()); + + public static void main(String[] args) throws Exception { + String pollerName = "jenkins_build"; + String tableName = "jenkins_build"; + String columnNames = JenkinsDao.JENKINS_BUILD; + SqlF f = JenkinsDao.jenkinsBuild; + TablePoller.NewRowCallback callback = new JenkinsBuildPoller(); + + Config config = loadFromDisk(); + + BoneCPDataSource dataSource = config.createBoneCp(); + + new TablePoller<>(pollerName, tableName, columnNames, some("array_length(users, 1) > 0"), f, callback).work(dataSource); + } + + public void process(Connection c, JenkinsBuildDto jenkinsBuild) throws SQLException { + Daos daos = new Daos(c); + CoreDao coreDao = daos.coreDao; + + UUID uuid = coreDao.insertBuild(jenkinsBuild.timestamp, "SUCCESS".equals(jenkinsBuild.result), jenkinsRef(jenkinsBuild.uuid)); + logger.info("Created build uuid={}", uuid); + + for (UUID user : jenkinsBuild.users) { + SqlOption personO = coreDao.selectPersonByJenkinsUuid(user); + + // This happens if no one has claimed the user id. + if(personO.isNone()) { + continue; + } + + UUID person = personO.get().uuid; + logger.info("Created build participant, person={}", person); + coreDao.insertBuildParticipant(uuid, person); + } + } +} diff --git a/src/main/java/io/trygvis/esper/testing/gitorious/GitoriousProjectDiscovery.java b/src/main/java/io/trygvis/esper/testing/gitorious/GitoriousProjectDiscovery.java index 295f6a6..1429f83 100644 --- a/src/main/java/io/trygvis/esper/testing/gitorious/GitoriousProjectDiscovery.java +++ b/src/main/java/io/trygvis/esper/testing/gitorious/GitoriousProjectDiscovery.java @@ -4,11 +4,11 @@ import com.jolbox.bonecp.*; import fj.data.Option; import static fj.data.Option.*; import io.trygvis.esper.testing.*; -import io.trygvis.esper.testing.object.ActorRef; -import io.trygvis.esper.testing.object.ObjectFactory; -import io.trygvis.esper.testing.object.ObjectManager; -import io.trygvis.esper.testing.object.ObjectUtil; -import io.trygvis.esper.testing.object.TransactionalActor; +import io.trygvis.esper.testing.util.object.ActorRef; +import io.trygvis.esper.testing.util.object.ObjectFactory; +import io.trygvis.esper.testing.util.object.ObjectManager; +import io.trygvis.esper.testing.util.object.ObjectUtil; +import io.trygvis.esper.testing.util.object.TransactionalActor; import io.trygvis.esper.testing.util.*; import static java.lang.System.*; diff --git a/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsDao.java b/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsDao.java index 7f52bd0..a31c6a0 100644 --- a/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsDao.java +++ b/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsDao.java @@ -1,7 +1,7 @@ package io.trygvis.esper.testing.jenkins; import fj.data.*; -import io.trygvis.esper.testing.sql.*; +import io.trygvis.esper.testing.util.sql.*; import org.joda.time.*; import java.net.*; @@ -11,7 +11,7 @@ import java.util.List; import static fj.data.Option.*; import static io.trygvis.esper.testing.Util.toUuidArray; -import static io.trygvis.esper.testing.sql.SqlOption.fromRs; +import static io.trygvis.esper.testing.util.sql.SqlOption.fromRs; import static java.lang.System.*; public class JenkinsDao { diff --git a/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsImporter.java b/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsImporter.java index e30dca7..3a688eb 100644 --- a/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsImporter.java +++ b/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsImporter.java @@ -2,7 +2,7 @@ package io.trygvis.esper.testing.jenkins; import com.jolbox.bonecp.*; import io.trygvis.esper.testing.*; -import io.trygvis.esper.testing.object.*; +import io.trygvis.esper.testing.util.object.*; import io.trygvis.esper.testing.util.*; import org.apache.abdera.*; import org.codehaus.httpcache4j.cache.*; @@ -12,7 +12,7 @@ import java.util.*; import java.util.concurrent.*; import java.util.concurrent.atomic.*; -import static io.trygvis.esper.testing.object.ObjectUtil.*; +import static io.trygvis.esper.testing.util.object.ObjectUtil.*; import static java.lang.Thread.*; public class JenkinsImporter { diff --git a/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsServerActor.java b/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsServerActor.java index 8f04faf..cf95c4e 100644 --- a/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsServerActor.java +++ b/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsServerActor.java @@ -1,8 +1,8 @@ package io.trygvis.esper.testing.jenkins; import fj.data.*; -import io.trygvis.esper.testing.object.*; -import io.trygvis.esper.testing.sql.*; +import io.trygvis.esper.testing.util.object.*; +import io.trygvis.esper.testing.util.sql.*; import org.slf4j.*; import java.net.*; diff --git a/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsServerOld.java b/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsServerOld.java index cfd9939..9f48191 100644 --- a/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsServerOld.java +++ b/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsServerOld.java @@ -5,7 +5,7 @@ import fj.data.*; import static fj.data.Option.*; import static io.trygvis.esper.testing.jenkins.JenkinsClient.apiXml; -import io.trygvis.esper.testing.object.*; +import io.trygvis.esper.testing.util.object.*; import org.joda.time.*; import java.io.*; diff --git a/src/main/java/io/trygvis/esper/testing/nexus/NexusImporter.java b/src/main/java/io/trygvis/esper/testing/nexus/NexusImporter.java index 3bf93c4..4042108 100644 --- a/src/main/java/io/trygvis/esper/testing/nexus/NexusImporter.java +++ b/src/main/java/io/trygvis/esper/testing/nexus/NexusImporter.java @@ -3,7 +3,7 @@ package io.trygvis.esper.testing.nexus; import com.jolbox.bonecp.*; import fj.data.*; import io.trygvis.esper.testing.*; -import io.trygvis.esper.testing.object.*; +import io.trygvis.esper.testing.util.object.*; import io.trygvis.esper.testing.util.*; import static java.lang.Thread.*; import static java.util.regex.Pattern.quote; diff --git a/src/main/java/io/trygvis/esper/testing/object/ActorRef.java b/src/main/java/io/trygvis/esper/testing/object/ActorRef.java deleted file mode 100644 index bc64da3..0000000 --- a/src/main/java/io/trygvis/esper/testing/object/ActorRef.java +++ /dev/null @@ -1,7 +0,0 @@ -package io.trygvis.esper.testing.object; - -import java.io.*; - -public interface ActorRef extends Closeable { - A underlying(); -} diff --git a/src/main/java/io/trygvis/esper/testing/object/ObjectFactory.java b/src/main/java/io/trygvis/esper/testing/object/ObjectFactory.java deleted file mode 100644 index 8e7d4b0..0000000 --- a/src/main/java/io/trygvis/esper/testing/object/ObjectFactory.java +++ /dev/null @@ -1,7 +0,0 @@ -package io.trygvis.esper.testing.object; - -import java.io.*; - -public interface ObjectFactory { - V create(K k); -} diff --git a/src/main/java/io/trygvis/esper/testing/object/ObjectManager.java b/src/main/java/io/trygvis/esper/testing/object/ObjectManager.java deleted file mode 100644 index a4fbc60..0000000 --- a/src/main/java/io/trygvis/esper/testing/object/ObjectManager.java +++ /dev/null @@ -1,64 +0,0 @@ -package io.trygvis.esper.testing.object; - -import org.slf4j.*; - -import java.io.*; -import java.util.*; - -public class ObjectManager implements Closeable { - private static final Logger logger = LoggerFactory.getLogger(ObjectManager.class); - - private final String type; - private final ObjectFactory objectFactory; - private Map objects = new HashMap<>(); - private boolean closed = false; - - public ObjectManager(String type, Set initialKeys, ObjectFactory objectFactory) { - this.type = type; - this.objectFactory = objectFactory; - - update(new HashSet<>(initialKeys)); - } - - public synchronized void update(Collection newKeys) { - if (closed) { - throw new RuntimeException("This instance is closed: type=" + type); - } - Set found = new HashSet<>(newKeys); - found.removeAll(objects.keySet()); - - Set gone = new HashSet<>(objects.keySet()); - gone.removeAll(newKeys); - - for (K k : gone) { - try { - logger.debug("Removing " + type + " with id=" + k); - objects.remove(k).close(); - } catch (IOException e) { - e.printStackTrace(System.out); - } - } - - for (K k : found) { - logger.debug("Adding " + type + " with id=" + k); - objects.put(k, objectFactory.create(k)); - } - } - - public synchronized void close() throws IOException { - if (closed) { - logger.warn("Already closed: type=" + type); - return; - } - update(Collections.emptyList()); - closed = true; - } - - public synchronized Collection getObjects() { - return new ArrayList<>(objects.values()); - } - - public void setObjects(Map objects) { - this.objects = objects; - } -} diff --git a/src/main/java/io/trygvis/esper/testing/object/ObjectUtil.java b/src/main/java/io/trygvis/esper/testing/object/ObjectUtil.java deleted file mode 100644 index 0e9912b..0000000 --- a/src/main/java/io/trygvis/esper/testing/object/ObjectUtil.java +++ /dev/null @@ -1,132 +0,0 @@ -package io.trygvis.esper.testing.object; - -import org.slf4j.*; - -import javax.sql.*; -import java.io.*; -import java.sql.*; -import java.util.concurrent.*; - -import static java.lang.System.currentTimeMillis; - -public class ObjectUtil { - - public static ActorRef threadedActor(String threadName, long delay, DataSource dataSource, String name, A actor) { - return new ThreadedActor<>(dataSource, threadName, name, actor, delay); - } - - public static ActorRef scheduledActorWithFixedDelay(ScheduledExecutorService scheduledExecutorService, long initialDelay, long delay, TimeUnit unit, DataSource dataSource, String name, A actor) { - return new ScheduledActor<>(scheduledExecutorService, initialDelay, delay, unit, dataSource, name, actor); - } - - private static class TransactionalActorWrapper implements Runnable { - private static final Logger logger = LoggerFactory.getLogger(TransactionalActorWrapper.class); - - private final DataSource dataSource; - private final String name; - private final A actor; - - TransactionalActorWrapper(DataSource dataSource, String name, A actor) { - this.dataSource = dataSource; - this.name = name; - this.actor = actor; - } - - public void run() { - try { - Connection c = dataSource.getConnection(); - - try { - try (PreparedStatement s = c.prepareStatement("set application_name = 'Actor: " + name + "';")) { -// s.setString(1, "Actor: " + name); - s.executeUpdate(); - s.close(); - } - - actor.act(c); - long start = currentTimeMillis(); - c.commit(); - long end = currentTimeMillis(); - logger.debug("COMMIT performed in in " + (end - start) + "ms."); - } - catch(SQLException e) { - c.rollback(); - throw e; - } finally { - c.close(); - } - } catch (Throwable e) { - logger.warn("Exception in thread " + Thread.currentThread().getName()); - e.printStackTrace(System.out); - } - } - } - - static class ScheduledActor implements ActorRef, Runnable { - private final ScheduledFuture future; - - private final TransactionalActorWrapper actor; - - ScheduledActor(ScheduledExecutorService executorService, long initialDelay, long delay, TimeUnit unit, DataSource dataSource, String name, A actor) { - future = executorService.scheduleWithFixedDelay(this, initialDelay, delay, unit); - this.actor = new TransactionalActorWrapper<>(dataSource, name, actor); - } - - public A underlying() { - return actor.actor; - } - - public void close() throws IOException { - future.cancel(true); - } - - @Override - public void run() { - actor.run(); - } - } - - static class ThreadedActor implements ActorRef, Runnable, Closeable { - - private final TransactionalActorWrapper actor; - private final long delay; - private final Thread thread; - private boolean shouldRun = true; - - ThreadedActor(DataSource dataSource, String threadName, String name, A actor, long delay) { - this.actor = new TransactionalActorWrapper<>(dataSource, name, actor); - this.delay = delay; - thread = new Thread(this, threadName); - thread.setDaemon(true); - thread.start(); - } - - public A underlying() { - return actor.actor; - } - - @SuppressWarnings("ConstantConditions") - public void run() { - while (shouldRun) { - actor.run(); - - try { - Thread.sleep(delay); - } catch (InterruptedException e) { - // ignore - } - } - } - - public void close() throws IOException { - shouldRun = false; - thread.interrupt(); - while (thread.isAlive()) { - try { - thread.join(); - } catch (InterruptedException ignore) { - } - } - } - } -} diff --git a/src/main/java/io/trygvis/esper/testing/object/TransactionalActor.java b/src/main/java/io/trygvis/esper/testing/object/TransactionalActor.java deleted file mode 100644 index 4d3cdce..0000000 --- a/src/main/java/io/trygvis/esper/testing/object/TransactionalActor.java +++ /dev/null @@ -1,7 +0,0 @@ -package io.trygvis.esper.testing.object; - -import java.sql.*; - -public interface TransactionalActor { - void act(Connection c) throws Exception; -} diff --git a/src/main/java/io/trygvis/esper/testing/sql/ResultSetF.java b/src/main/java/io/trygvis/esper/testing/sql/ResultSetF.java deleted file mode 100644 index e5a9e4e..0000000 --- a/src/main/java/io/trygvis/esper/testing/sql/ResultSetF.java +++ /dev/null @@ -1,18 +0,0 @@ -package io.trygvis.esper.testing.sql; - -import java.sql.*; - -public class ResultSetF { - public static final SqlF getInt = new SqlF() { - public Integer apply(ResultSet rs) throws SQLException { - return rs.getInt(1); - } - }; - - public static final SqlF getInteger = new SqlF() { - public Integer apply(ResultSet rs) throws SQLException { - int i = rs.getInt(1); - return rs.wasNull() ? null : i; - } - }; -} diff --git a/src/main/java/io/trygvis/esper/testing/sql/SqlF.java b/src/main/java/io/trygvis/esper/testing/sql/SqlF.java deleted file mode 100644 index fb05e4c..0000000 --- a/src/main/java/io/trygvis/esper/testing/sql/SqlF.java +++ /dev/null @@ -1,15 +0,0 @@ -package io.trygvis.esper.testing.sql; - -import java.sql.*; - -public abstract class SqlF { - public abstract B apply(A a) throws SQLException; - - public SqlF andThen(final SqlF f) { - return new SqlF() { - public C apply(A a) throws SQLException { - return f.apply(SqlF.this.apply(a)); - } - }; - } -} diff --git a/src/main/java/io/trygvis/esper/testing/sql/SqlOption.java b/src/main/java/io/trygvis/esper/testing/sql/SqlOption.java deleted file mode 100644 index 288735a..0000000 --- a/src/main/java/io/trygvis/esper/testing/sql/SqlOption.java +++ /dev/null @@ -1,109 +0,0 @@ -package io.trygvis.esper.testing.sql; - -import java.sql.*; - -public abstract class SqlOption { - public static SqlOption none() { - return new None<>(); - } - - public static SqlOption some(A a) { - return new Some<>(a); - } - - public static SqlOption fromRs(ResultSet rs) throws SQLException { - if (!rs.next()) { - return none(); - } - - return some(rs); - } - - // ----------------------------------------------------------------------- - // - // ----------------------------------------------------------------------- - - public abstract SqlOption map(SqlF f) throws SQLException; - - public SqlOption flatMap(SqlF> f) throws SQLException { - SqlOption> x = map(f); - - if (x.isNone()) { - return none(); - } - - return x.get(); - } - - public abstract A get() throws SQLException; - - public abstract boolean isSome(); - - public boolean isNone() { - return !isSome(); - } - - public abstract A getOrElse(A a); - - public static SqlOption fromNull(A a) { - if (a != null) { - return some(a); - } else { - return none(); - } - } - - // ----------------------------------------------------------------------- - // - // ----------------------------------------------------------------------- - - private static class None extends SqlOption { - public SqlOption map(SqlF f) { - return none(); - } - - public A get() throws SQLException { - throw new SQLException("get() on None"); - } - - public boolean isSome() { - return false; - } - - public A getOrElse(A a) { - return a; - } - - public String toString() { - return "None"; - } - } - - private static class Some extends SqlOption { - private final A a; - - private Some(A a) { - this.a = a; - } - - public SqlOption map(SqlF f) throws SQLException { - return some(f.apply(a)); - } - - public A get() { - return a; - } - - public boolean isSome() { - return true; - } - - public A getOrElse(A a) { - return this.a; - } - - public String toString() { - return "Some(" + a + ")"; - } - } -} diff --git a/src/main/java/io/trygvis/esper/testing/task/TaskDao.java b/src/main/java/io/trygvis/esper/testing/task/TaskDao.java deleted file mode 100644 index 242eb2a..0000000 --- a/src/main/java/io/trygvis/esper/testing/task/TaskDao.java +++ /dev/null @@ -1,26 +0,0 @@ -package io.trygvis.esper.testing.task; - -import java.sql.*; -import java.util.*; - -public class TaskDao { - private final Connection c; - private final String table; - - public TaskDao(Connection c, String table) { - this.c = c; - this.table = table; - } - - public List findTasks() throws SQLException { - try (PreparedStatement s = c.prepareStatement("SELECT task_id FROM ? FOR UPDATE")) { - s.setString(1, table); - ResultSet rs = s.executeQuery(); - List list = new ArrayList<>(); - while(rs.next()) { - list.add(rs.getString(1)); - } - return list; - } - } -} diff --git a/src/main/java/io/trygvis/esper/testing/task/TaskManager.java b/src/main/java/io/trygvis/esper/testing/task/TaskManager.java deleted file mode 100644 index e4daba5..0000000 --- a/src/main/java/io/trygvis/esper/testing/task/TaskManager.java +++ /dev/null @@ -1,224 +0,0 @@ -package io.trygvis.esper.testing.task; - -import com.jolbox.bonecp.*; -import org.slf4j.*; -import org.slf4j.helpers.*; - -import java.sql.*; -import java.util.*; -import java.util.concurrent.*; - -public class TaskManager { - public final String table; - public final TaskExecutorFactory taskExecutorFactory; - public final Executor executor; - public final BoneCP boneCP; - - private final Set inProgress = new HashSet<>(); - - public TaskManager(String table, TaskExecutorFactory taskExecutorFactory, Executor executor, BoneCP boneCP) { - this.table = table; - this.taskExecutorFactory = taskExecutorFactory; - this.executor = executor; - this.boneCP = boneCP; - - Thread thread = new Thread(new Runnable() { - public void run() { - TaskManager.this.run(); - } - }); - thread.start(); - } - - private void run() { - while (true) { - try { - try (Connection c = boneCP.getConnection()) { - singleRun(c); - } - } catch (SQLException e) { - e.printStackTrace(System.out); - } - } - } - - private void singleRun(Connection c) throws SQLException { - TaskDao taskDao = new TaskDao(c, table); - - List ids = taskDao.findTasks(); - - System.out.println("Found " + ids.size() + " new tasks."); - - synchronized (inProgress) { - System.out.println("Have " + inProgress.size() + " tasks in progress already"); - ids.removeAll(inProgress); - - List runnables = new ArrayList<>(ids.size()); - - for (final String id : ids) { - System.out.println("Scheduling " + id); - - final TaskExecutor executor = taskExecutorFactory.create(); - runnables.add(new Runnable() { - public void run() { - System.out.println("Executing " + id); - try { - try (Connection c2 = boneCP.getConnection()) { - SqlLogger logger = new SqlLogger(); - executor.execute(id, c2, logger); - // TODO: insert log statements - System.out.println("Executing " + id); - c2.commit(); - } - } catch (SQLException e) { - e.printStackTrace(); - } finally { - synchronized (inProgress) { - inProgress.remove(id); - } - } - } - }); - inProgress.add(id); - } - for (Runnable runnable : runnables) { - executor.execute(runnable); - } - } - } - - /** - * See MessageFormatter - */ - private static class SqlLogger extends MarkerIgnoringBase { - - public boolean isTraceEnabled() { - throw new RuntimeException("Not implemented"); - } - - public void trace(String msg) { - throw new RuntimeException("Not implemented"); - } - - public void trace(String format, Object arg) { - throw new RuntimeException("Not implemented"); - } - - public void trace(String format, Object arg1, Object arg2) { - throw new RuntimeException("Not implemented"); - } - - public void trace(String format, Object[] argArray) { - throw new RuntimeException("Not implemented"); - } - - public void trace(String msg, Throwable t) { - throw new RuntimeException("Not implemented"); - } - - public boolean isDebugEnabled() { - throw new RuntimeException("Not implemented"); - } - - public void debug(String msg) { - throw new RuntimeException("Not implemented"); - } - - public void debug(String format, Object arg) { - throw new RuntimeException("Not implemented"); - } - - public void debug(String format, Object arg1, Object arg2) { - throw new RuntimeException("Not implemented"); - } - - public void debug(String format, Object[] argArray) { - throw new RuntimeException("Not implemented"); - } - - public void debug(String msg, Throwable t) { - throw new RuntimeException("Not implemented"); - } - - public boolean isInfoEnabled() { - throw new RuntimeException("Not implemented"); - } - - public void info(String msg) { - throw new RuntimeException("Not implemented"); - } - - public void info(String format, Object arg) { - throw new RuntimeException("Not implemented"); - } - - public void info(String format, Object arg1, Object arg2) { - throw new RuntimeException("Not implemented"); - } - - public void info(String format, Object[] argArray) { - throw new RuntimeException("Not implemented"); - } - - public void info(String msg, Throwable t) { - throw new RuntimeException("Not implemented"); - } - - public boolean isWarnEnabled() { - throw new RuntimeException("Not implemented"); - } - - public void warn(String msg) { - throw new RuntimeException("Not implemented"); - } - - public void warn(String format, Object arg) { - throw new RuntimeException("Not implemented"); - } - - public void warn(String format, Object[] argArray) { - throw new RuntimeException("Not implemented"); - } - - public void warn(String format, Object arg1, Object arg2) { - throw new RuntimeException("Not implemented"); - } - - public void warn(String msg, Throwable t) { - throw new RuntimeException("Not implemented"); - } - - public boolean isErrorEnabled() { - throw new RuntimeException("Not implemented"); - } - - public void error(String msg) { - throw new RuntimeException("Not implemented"); - } - - public void error(String format, Object arg) { - throw new RuntimeException("Not implemented"); - } - - public void error(String format, Object arg1, Object arg2) { - throw new RuntimeException("Not implemented"); - } - - public void error(String format, Object[] argArray) { - throw new RuntimeException("Not implemented"); - } - - public void error(String msg, Throwable t) { - throw new RuntimeException("Not implemented"); - } - } -} - -interface TaskExecutorFactory { - TaskExecutor create(); -} - -interface TaskExecutor { - void execute(String id, Connection c, Logger logger) - throws SQLException; -} diff --git a/src/main/java/io/trygvis/esper/testing/util/object/ActorRef.java b/src/main/java/io/trygvis/esper/testing/util/object/ActorRef.java new file mode 100644 index 0000000..49986e6 --- /dev/null +++ b/src/main/java/io/trygvis/esper/testing/util/object/ActorRef.java @@ -0,0 +1,7 @@ +package io.trygvis.esper.testing.util.object; + +import java.io.*; + +public interface ActorRef extends Closeable { + A underlying(); +} diff --git a/src/main/java/io/trygvis/esper/testing/util/object/ObjectFactory.java b/src/main/java/io/trygvis/esper/testing/util/object/ObjectFactory.java new file mode 100644 index 0000000..ea53a46 --- /dev/null +++ b/src/main/java/io/trygvis/esper/testing/util/object/ObjectFactory.java @@ -0,0 +1,7 @@ +package io.trygvis.esper.testing.util.object; + +import java.io.*; + +public interface ObjectFactory { + V create(K k); +} diff --git a/src/main/java/io/trygvis/esper/testing/util/object/ObjectManager.java b/src/main/java/io/trygvis/esper/testing/util/object/ObjectManager.java new file mode 100644 index 0000000..5b9d740 --- /dev/null +++ b/src/main/java/io/trygvis/esper/testing/util/object/ObjectManager.java @@ -0,0 +1,64 @@ +package io.trygvis.esper.testing.util.object; + +import org.slf4j.*; + +import java.io.*; +import java.util.*; + +public class ObjectManager implements Closeable { + private static final Logger logger = LoggerFactory.getLogger(ObjectManager.class); + + private final String type; + private final ObjectFactory objectFactory; + private Map objects = new HashMap<>(); + private boolean closed = false; + + public ObjectManager(String type, Set initialKeys, ObjectFactory objectFactory) { + this.type = type; + this.objectFactory = objectFactory; + + update(new HashSet<>(initialKeys)); + } + + public synchronized void update(Collection newKeys) { + if (closed) { + throw new RuntimeException("This instance is closed: type=" + type); + } + Set found = new HashSet<>(newKeys); + found.removeAll(objects.keySet()); + + Set gone = new HashSet<>(objects.keySet()); + gone.removeAll(newKeys); + + for (K k : gone) { + try { + logger.debug("Removing " + type + " with id=" + k); + objects.remove(k).close(); + } catch (IOException e) { + e.printStackTrace(System.out); + } + } + + for (K k : found) { + logger.debug("Adding " + type + " with id=" + k); + objects.put(k, objectFactory.create(k)); + } + } + + public synchronized void close() throws IOException { + if (closed) { + logger.warn("Already closed: type=" + type); + return; + } + update(Collections.emptyList()); + closed = true; + } + + public synchronized Collection getObjects() { + return new ArrayList<>(objects.values()); + } + + public void setObjects(Map objects) { + this.objects = objects; + } +} diff --git a/src/main/java/io/trygvis/esper/testing/util/object/ObjectUtil.java b/src/main/java/io/trygvis/esper/testing/util/object/ObjectUtil.java new file mode 100644 index 0000000..143a181 --- /dev/null +++ b/src/main/java/io/trygvis/esper/testing/util/object/ObjectUtil.java @@ -0,0 +1,132 @@ +package io.trygvis.esper.testing.util.object; + +import org.slf4j.*; + +import javax.sql.*; +import java.io.*; +import java.sql.*; +import java.util.concurrent.*; + +import static java.lang.System.currentTimeMillis; + +public class ObjectUtil { + + public static ActorRef threadedActor(String threadName, long delay, DataSource dataSource, String name, A actor) { + return new ThreadedActor<>(dataSource, threadName, name, actor, delay); + } + + public static ActorRef scheduledActorWithFixedDelay(ScheduledExecutorService scheduledExecutorService, long initialDelay, long delay, TimeUnit unit, DataSource dataSource, String name, A actor) { + return new ScheduledActor<>(scheduledExecutorService, initialDelay, delay, unit, dataSource, name, actor); + } + + private static class TransactionalActorWrapper implements Runnable { + private static final Logger logger = LoggerFactory.getLogger(TransactionalActorWrapper.class); + + private final DataSource dataSource; + private final String name; + private final A actor; + + TransactionalActorWrapper(DataSource dataSource, String name, A actor) { + this.dataSource = dataSource; + this.name = name; + this.actor = actor; + } + + public void run() { + try { + Connection c = dataSource.getConnection(); + + try { + try (PreparedStatement s = c.prepareStatement("set application_name = 'Actor: " + name + "';")) { +// s.setString(1, "Actor: " + name); + s.executeUpdate(); + s.close(); + } + + actor.act(c); + long start = currentTimeMillis(); + c.commit(); + long end = currentTimeMillis(); + logger.debug("COMMIT performed in in " + (end - start) + "ms."); + } + catch(SQLException e) { + c.rollback(); + throw e; + } finally { + c.close(); + } + } catch (Throwable e) { + logger.warn("Exception in thread " + Thread.currentThread().getName()); + e.printStackTrace(System.out); + } + } + } + + static class ScheduledActor implements ActorRef, Runnable { + private final ScheduledFuture future; + + private final TransactionalActorWrapper actor; + + ScheduledActor(ScheduledExecutorService executorService, long initialDelay, long delay, TimeUnit unit, DataSource dataSource, String name, A actor) { + future = executorService.scheduleWithFixedDelay(this, initialDelay, delay, unit); + this.actor = new TransactionalActorWrapper<>(dataSource, name, actor); + } + + public A underlying() { + return actor.actor; + } + + public void close() throws IOException { + future.cancel(true); + } + + @Override + public void run() { + actor.run(); + } + } + + static class ThreadedActor implements ActorRef, Runnable, Closeable { + + private final TransactionalActorWrapper actor; + private final long delay; + private final Thread thread; + private boolean shouldRun = true; + + ThreadedActor(DataSource dataSource, String threadName, String name, A actor, long delay) { + this.actor = new TransactionalActorWrapper<>(dataSource, name, actor); + this.delay = delay; + thread = new Thread(this, threadName); + thread.setDaemon(true); + thread.start(); + } + + public A underlying() { + return actor.actor; + } + + @SuppressWarnings("ConstantConditions") + public void run() { + while (shouldRun) { + actor.run(); + + try { + Thread.sleep(delay); + } catch (InterruptedException e) { + // ignore + } + } + } + + public void close() throws IOException { + shouldRun = false; + thread.interrupt(); + while (thread.isAlive()) { + try { + thread.join(); + } catch (InterruptedException ignore) { + } + } + } + } +} diff --git a/src/main/java/io/trygvis/esper/testing/util/object/TransactionalActor.java b/src/main/java/io/trygvis/esper/testing/util/object/TransactionalActor.java new file mode 100644 index 0000000..0799695 --- /dev/null +++ b/src/main/java/io/trygvis/esper/testing/util/object/TransactionalActor.java @@ -0,0 +1,7 @@ +package io.trygvis.esper.testing.util.object; + +import java.sql.*; + +public interface TransactionalActor { + void act(Connection c) throws Exception; +} diff --git a/src/main/java/io/trygvis/esper/testing/util/sql/ResultSetF.java b/src/main/java/io/trygvis/esper/testing/util/sql/ResultSetF.java new file mode 100644 index 0000000..9e42242 --- /dev/null +++ b/src/main/java/io/trygvis/esper/testing/util/sql/ResultSetF.java @@ -0,0 +1,18 @@ +package io.trygvis.esper.testing.util.sql; + +import java.sql.*; + +public class ResultSetF { + public static final SqlF getInt = new SqlF() { + public Integer apply(ResultSet rs) throws SQLException { + return rs.getInt(1); + } + }; + + public static final SqlF getInteger = new SqlF() { + public Integer apply(ResultSet rs) throws SQLException { + int i = rs.getInt(1); + return rs.wasNull() ? null : i; + } + }; +} diff --git a/src/main/java/io/trygvis/esper/testing/util/sql/SqlF.java b/src/main/java/io/trygvis/esper/testing/util/sql/SqlF.java new file mode 100644 index 0000000..e4e8197 --- /dev/null +++ b/src/main/java/io/trygvis/esper/testing/util/sql/SqlF.java @@ -0,0 +1,15 @@ +package io.trygvis.esper.testing.util.sql; + +import java.sql.*; + +public abstract class SqlF { + public abstract B apply(A a) throws SQLException; + + public SqlF andThen(final SqlF f) { + return new SqlF() { + public C apply(A a) throws SQLException { + return f.apply(SqlF.this.apply(a)); + } + }; + } +} diff --git a/src/main/java/io/trygvis/esper/testing/util/sql/SqlOption.java b/src/main/java/io/trygvis/esper/testing/util/sql/SqlOption.java new file mode 100644 index 0000000..286a872 --- /dev/null +++ b/src/main/java/io/trygvis/esper/testing/util/sql/SqlOption.java @@ -0,0 +1,109 @@ +package io.trygvis.esper.testing.util.sql; + +import java.sql.*; + +public abstract class SqlOption { + public static SqlOption none() { + return new None<>(); + } + + public static SqlOption some(A a) { + return new Some<>(a); + } + + public static SqlOption fromRs(ResultSet rs) throws SQLException { + if (!rs.next()) { + return none(); + } + + return some(rs); + } + + // ----------------------------------------------------------------------- + // + // ----------------------------------------------------------------------- + + public abstract SqlOption map(SqlF f) throws SQLException; + + public SqlOption flatMap(SqlF> f) throws SQLException { + SqlOption> x = map(f); + + if (x.isNone()) { + return none(); + } + + return x.get(); + } + + public abstract A get() throws SQLException; + + public abstract boolean isSome(); + + public boolean isNone() { + return !isSome(); + } + + public abstract A getOrElse(A a); + + public static SqlOption fromNull(A a) { + if (a != null) { + return some(a); + } else { + return none(); + } + } + + // ----------------------------------------------------------------------- + // + // ----------------------------------------------------------------------- + + private static class None extends SqlOption { + public SqlOption map(SqlF f) { + return none(); + } + + public A get() throws SQLException { + throw new SQLException("get() on None"); + } + + public boolean isSome() { + return false; + } + + public A getOrElse(A a) { + return a; + } + + public String toString() { + return "None"; + } + } + + private static class Some extends SqlOption { + private final A a; + + private Some(A a) { + this.a = a; + } + + public SqlOption map(SqlF f) throws SQLException { + return some(f.apply(a)); + } + + public A get() { + return a; + } + + public boolean isSome() { + return true; + } + + public A getOrElse(A a) { + return this.a; + } + + public String toString() { + return "Some(" + a + ")"; + } + } +} -- cgit v1.2.3