diff options
Diffstat (limited to 'src/main/java/io/trygvis/esper/testing')
15 files changed, 293 insertions, 124 deletions
diff --git a/src/main/java/io/trygvis/esper/testing/DaoUtil.java b/src/main/java/io/trygvis/esper/testing/DaoUtil.java deleted file mode 100644 index 84b1afa..0000000 --- a/src/main/java/io/trygvis/esper/testing/DaoUtil.java +++ /dev/null @@ -1,26 +0,0 @@ -package io.trygvis.esper.testing; - -import fj.*; -import org.joda.time.*; - -import java.sql.*; - -public class DaoUtil { - public static final F<Timestamp, java.util.Date> timestampToDate = new F<Timestamp, java.util.Date>() { - public java.util.Date f(Timestamp timestamp) { - return new java.util.Date(timestamp.getTime()); - } - }; - - public static final F<Timestamp, LocalDateTime> timestampToLocalDateTime = new F<Timestamp, LocalDateTime>() { - public LocalDateTime f(Timestamp timestamp) { - return new LocalDateTime(timestamp.getTime()); - } - }; - - public static final F<java.util.Date, Timestamp> dateToTimestamp = new F<java.util.Date, Timestamp>() { - public Timestamp f(java.util.Date date) { - return new Timestamp(date.getTime()); - } - }; -} diff --git a/src/main/java/io/trygvis/esper/testing/Daos.java b/src/main/java/io/trygvis/esper/testing/Daos.java index e7faeca..c80f10c 100644 --- a/src/main/java/io/trygvis/esper/testing/Daos.java +++ b/src/main/java/io/trygvis/esper/testing/Daos.java @@ -1,8 +1,8 @@ package io.trygvis.esper.testing; import io.trygvis.esper.testing.gitorious.*; +import io.trygvis.esper.testing.jenkins.*; -import javax.sql.*; import java.io.*; import java.sql.*; @@ -27,15 +27,17 @@ public class Daos implements Closeable { public final GitoriousEventDao gitoriousEventDao; public final GitoriousProjectDao gitoriousProjectDao; public final GitoriousRepositoryDao gitoriousRepositoryDao; + public final JenkinsDao jenkinsDao; public final int seq; public static int counter = 1; - public Daos(Connection connection) throws SQLException { - this.connection = connection; + public Daos(Connection c) throws SQLException { + this.connection = c; this.seq = counter++; - gitoriousEventDao = new GitoriousEventDao(connection); - gitoriousProjectDao = new GitoriousProjectDao(connection); - gitoriousRepositoryDao = new GitoriousRepositoryDao(connection); + gitoriousEventDao = new GitoriousEventDao(c); + gitoriousProjectDao = new GitoriousProjectDao(c); + gitoriousRepositoryDao = new GitoriousRepositoryDao(c); + jenkinsDao = new JenkinsDao(c); } public void close() throws IOException { diff --git a/src/main/java/io/trygvis/esper/testing/DatabaseAccess.java b/src/main/java/io/trygvis/esper/testing/DatabaseAccess.java new file mode 100644 index 0000000..6979b66 --- /dev/null +++ b/src/main/java/io/trygvis/esper/testing/DatabaseAccess.java @@ -0,0 +1,49 @@ +package io.trygvis.esper.testing; + +import org.slf4j.*; + +import javax.sql.*; +import java.sql.*; + +public class DatabaseAccess { + + private final Logger logger = LoggerFactory.getLogger(getClass()); + + private final DataSource dataSource; + + public static interface DaosCallback<A> { + A run(Daos daos) throws SQLException; + } + + public DatabaseAccess(DataSource dataSource) { + this.dataSource = dataSource; + } + + public <A> A inTransaction(DaosCallback<A> callback) throws SQLException { + try { + Connection c = dataSource.getConnection(); + + try { +// try (PreparedStatement s = c.prepareStatement("set application_name = 'Actor: " + name + "';")) { +// s.executeUpdate(); +// s.close(); +// } + + Daos daos = new Daos(c); + A a = callback.run(daos); + daos.commit(); + + return a; + } 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); + throw e; + } + } +} diff --git a/src/main/java/io/trygvis/esper/testing/Util.java b/src/main/java/io/trygvis/esper/testing/Util.java index 57288d5..5e4b2a5 100644 --- a/src/main/java/io/trygvis/esper/testing/Util.java +++ b/src/main/java/io/trygvis/esper/testing/Util.java @@ -4,10 +4,30 @@ import fj.*; import fj.data.*; import static fj.data.Option.*; import org.jdom2.*; +import org.joda.time.*; import java.net.*; +import java.sql.*; public class Util { + public static final F<Timestamp, java.util.Date> timestampToDate = new F<Timestamp, java.util.Date>() { + public java.util.Date f(Timestamp timestamp) { + return new java.util.Date(timestamp.getTime()); + } + }; + + public static final F<Timestamp, LocalDateTime> timestampToLocalDateTime = new F<Timestamp, LocalDateTime>() { + public LocalDateTime f(Timestamp timestamp) { + return new LocalDateTime(timestamp.getTime()); + } + }; + + public static final F<java.util.Date, Timestamp> dateToTimestamp = new F<java.util.Date, Timestamp>() { + public Timestamp f(java.util.Date date) { + return new Timestamp(date.getTime()); + } + }; + public static F<String, Option<Integer>> parseInt = new F<String, Option<Integer>>() { public Option<Integer> f(String s) { try { diff --git a/src/main/java/io/trygvis/esper/testing/gitorious/GitoriousEventDao.java b/src/main/java/io/trygvis/esper/testing/gitorious/GitoriousEventDao.java index 737358e..137eb6b 100644 --- a/src/main/java/io/trygvis/esper/testing/gitorious/GitoriousEventDao.java +++ b/src/main/java/io/trygvis/esper/testing/gitorious/GitoriousEventDao.java @@ -1,6 +1,6 @@ package io.trygvis.esper.testing.gitorious; -import static io.trygvis.esper.testing.DaoUtil.*; +import static io.trygvis.esper.testing.Util.*; import java.sql.*; 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 0294896..295f6a6 100644 --- a/src/main/java/io/trygvis/esper/testing/gitorious/GitoriousProjectDiscovery.java +++ b/src/main/java/io/trygvis/esper/testing/gitorious/GitoriousProjectDiscovery.java @@ -52,9 +52,11 @@ public class GitoriousProjectDiscovery { ObjectUtil.scheduledActorWithFixedDelay(service, config.gitorious.projectListUpdateDelay, config.gitorious.projectListUpdateInterval, TimeUnit.MILLISECONDS, boneCp, "Gitorious", new TransactionalActor() { @Override public void act(Connection c) throws Exception { - Daos daos = new Daos(c); - discoverProjects(daos); - repositoryManager.update(daos.gitoriousRepositoryDao.select(Daos.OrderDirection.NONE)); + try (Daos daos = new Daos(c)) { + discoverProjects(daos); + repositoryManager.update(daos.gitoriousRepositoryDao.select(Daos.OrderDirection.NONE)); + daos.commit(); + } } }); } @@ -121,8 +123,6 @@ public class GitoriousProjectDiscovery { projectDao.delete(project); } - daos.commit(); - long end = currentTimeMillis(); System.out.println("Processed in " + (end - start) + " ms"); } diff --git a/src/main/java/io/trygvis/esper/testing/gitorious/GitoriousRepositoryDao.java b/src/main/java/io/trygvis/esper/testing/gitorious/GitoriousRepositoryDao.java index bdc0e4e..903d74c 100644 --- a/src/main/java/io/trygvis/esper/testing/gitorious/GitoriousRepositoryDao.java +++ b/src/main/java/io/trygvis/esper/testing/gitorious/GitoriousRepositoryDao.java @@ -2,8 +2,8 @@ package io.trygvis.esper.testing.gitorious; import fj.data.*; import io.trygvis.esper.testing.*; -import static io.trygvis.esper.testing.DaoUtil.dateToTimestamp; -import static io.trygvis.esper.testing.DaoUtil.timestampToDate; +import static io.trygvis.esper.testing.Util.dateToTimestamp; +import static io.trygvis.esper.testing.Util.timestampToDate; import java.net.*; import java.sql.*; diff --git a/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsBuildDto.java b/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsBuildDto.java new file mode 100644 index 0000000..28f597d --- /dev/null +++ b/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsBuildDto.java @@ -0,0 +1,30 @@ +package io.trygvis.esper.testing.jenkins; + +import org.joda.time.*; + +import java.net.*; +import java.util.*; + +public class JenkinsBuildDto { + public final UUID uuid; + public final DateTime created_date; + public final UUID job; + public final String entryId; + public final URI url; + public final String result; + public final int number; + public final int duration; + public final DateTime timestamp; + + JenkinsBuildDto(UUID uuid, DateTime created_date, UUID job, String entryId, URI url, String result, int number, int duration, DateTime timestamp) { + this.uuid = uuid; + this.created_date = created_date; + this.job = job; + this.entryId = entryId; + this.url = url; + this.result = result; + this.number = number; + this.duration = duration; + this.timestamp = timestamp; + } +} 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 0195900..ab4b515 100644 --- a/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsDao.java +++ b/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsDao.java @@ -3,22 +3,19 @@ package io.trygvis.esper.testing.jenkins; import fj.data.*; import org.joda.time.*; -import java.net.URI; +import java.net.*; import java.sql.*; -import java.util.ArrayList; +import java.util.*; import java.util.List; -import java.util.UUID; -import static fj.data.Option.fromNull; -import static fj.data.Option.none; -import static fj.data.Option.some; -import static java.lang.System.currentTimeMillis; +import static fj.data.Option.*; +import static java.lang.System.*; public class JenkinsDao { private final Connection c; - public static final String JENKINS_SERVER = "uuid, created_date, url"; + public static final String JENKINS_SERVER = "uuid, created_date, url, enabled"; public static final String JENKINS_JOB = "uuid, created_date, server, url, job_type, display_name"; @@ -33,10 +30,11 @@ public class JenkinsDao { return new JenkinsServerDto( UUID.fromString(rs.getString(i++)), new DateTime(rs.getTimestamp(i++).getTime()), - URI.create(rs.getString(i))); + URI.create(rs.getString(i++)), + rs.getBoolean(i)); } - public List<JenkinsServerDto> toServerList(ResultSet rs) throws SQLException { + private List<JenkinsServerDto> toServerList(ResultSet rs) throws SQLException { List<JenkinsServerDto> list = new ArrayList<>(); while (rs.next()) { list.add(jenkinsServer(rs)); @@ -76,21 +74,37 @@ public class JenkinsDao { new DateTime(rs.getTimestamp(i).getTime())); } - public List<JenkinsServerDto> selectServer(boolean enabledOnly) throws SQLException { - String where = "WHERE "; - where += enabledOnly ? "enabled=true" : ""; + public List<JenkinsServerDto> selectServers(boolean enabledOnly) throws SQLException { + String sql = "SELECT " + JENKINS_SERVER + " FROM jenkins_server "; + + if (enabledOnly) { + sql += " WHERE enabled=true"; + } - try (PreparedStatement s = c.prepareStatement("SELECT " + JENKINS_SERVER + " FROM jenkins_server " + where)) { + sql += " ORDER BY url"; + + try (PreparedStatement s = c.prepareStatement(sql)) { return toServerList(s.executeQuery()); } } + public Option<JenkinsServerDto> selectServer(UUID uuid) throws SQLException { + try (PreparedStatement s = c.prepareStatement("SELECT " + JENKINS_SERVER + " FROM jenkins_server WHERE uuid=?")) { + s.setString(1, uuid.toString()); + ResultSet rs = s.executeQuery(); + if (!rs.next()) { + return none(); + } + return some(jenkinsServer(rs)); + } + } + public Option<JenkinsJobDto> selectJobByUrl(URI url) throws SQLException { try (PreparedStatement s = c.prepareStatement("SELECT " + JENKINS_JOB + " FROM jenkins_job WHERE url=?")) { s.setString(1, url.toASCIIString()); ResultSet rs = s.executeQuery(); - if(!rs.next()) { + if (!rs.next()) { return none(); } @@ -98,6 +112,15 @@ public class JenkinsDao { } } + public Integer selectJobCountForServer(UUID uuid) throws SQLException { + try (PreparedStatement s = c.prepareStatement("SELECT count(1) FROM jenkins_job WHERE server=?")) { + s.setString(1, uuid.toString()); + ResultSet rs = s.executeQuery(); + + return rs.getInt(1); + } + } + public UUID insertJob(UUID server, URI url, JenkinsJobXml.JenkinsJobType type, Option<String> displayName) throws SQLException { try (PreparedStatement s = c.prepareStatement("INSERT INTO jenkins_job(" + JENKINS_JOB + ") VALUES(?, ?, ?, ?, ?, ?)")) { UUID uuid = UUID.randomUUID(); @@ -147,70 +170,3 @@ public class JenkinsDao { } } } - -class JenkinsServerDto { - public final UUID uuid; - public final DateTime created_date; - public final URI url; - - JenkinsServerDto(UUID uuid, DateTime created_date, URI url) { - this.uuid = uuid; - this.created_date = created_date; - this.url = url; - } - - @Override - public boolean equals(Object o) { - if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; - - JenkinsServerDto that = (JenkinsServerDto) o; - - return uuid.equals(that.uuid); - } - - @Override - public int hashCode() { - return uuid.hashCode(); - } -} - -class JenkinsJobDto { - public final UUID uuid; - public final DateTime created_date; - public final UUID server; - public final URI url; - public final Option<String> displayName; - - JenkinsJobDto(UUID uuid, DateTime created_date, UUID server, URI url, Option<String> displayName) { - this.uuid = uuid; - this.created_date = created_date; - this.server = server; - this.url = url; - this.displayName = displayName; - } -} - -class JenkinsBuildDto { - public final UUID uuid; - public final DateTime created_date; - public final UUID job; - public final String entryId; - public final URI url; - public final String result; - public final int number; - public final int duration; - public final DateTime timestamp; - - JenkinsBuildDto(UUID uuid, DateTime created_date, UUID job, String entryId, URI url, String result, int number, int duration, DateTime timestamp) { - this.uuid = uuid; - this.created_date = created_date; - this.job = job; - this.entryId = entryId; - this.url = url; - this.result = result; - this.number = number; - this.duration = duration; - this.timestamp = timestamp; - } -} 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 680e593..e30dca7 100644 --- a/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsImporter.java +++ b/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsImporter.java @@ -41,7 +41,7 @@ public class JenkinsImporter { java.util.List<JenkinsServerDto> newKeys; try (Connection c = boneCp.getConnection()) { - newKeys = new JenkinsDao(c).selectServer(true); + newKeys = new JenkinsDao(c).selectServers(true); } serverManager.update(newKeys); diff --git a/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsJobDto.java b/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsJobDto.java new file mode 100644 index 0000000..8ee731c --- /dev/null +++ b/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsJobDto.java @@ -0,0 +1,23 @@ +package io.trygvis.esper.testing.jenkins; + +import fj.data.*; +import org.joda.time.*; + +import java.net.*; +import java.util.*; + +public class JenkinsJobDto { + public final UUID uuid; + public final DateTime created_date; + public final UUID server; + public final URI url; + public final Option<String> displayName; + + JenkinsJobDto(UUID uuid, DateTime created_date, UUID server, URI url, Option<String> displayName) { + this.uuid = uuid; + this.created_date = created_date; + this.server = server; + this.url = url; + this.displayName = displayName; + } +} diff --git a/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsJobXml.java b/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsJobXml.java index 8cbe049..ca661b8 100644 --- a/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsJobXml.java +++ b/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsJobXml.java @@ -14,7 +14,7 @@ import static io.trygvis.esper.testing.Util.childText; class JenkinsJobXml { enum JenkinsJobType { - FREE_STYLE, MAVEN_MODULE_SET, MAVEN_MODULE, MATRIX + FREE_STYLE, MAVEN_MODULE_SET, MAVEN_MODULE, MATRIX, MATRIX_CONFIGURATION } public final JenkinsJobType type; diff --git a/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsServerDto.java b/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsServerDto.java new file mode 100644 index 0000000..225b786 --- /dev/null +++ b/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsServerDto.java @@ -0,0 +1,35 @@ +package io.trygvis.esper.testing.jenkins; + +import org.joda.time.*; + +import java.net.*; +import java.util.*; + +public class JenkinsServerDto { + public final UUID uuid; + public final DateTime created_date; + public final URI url; + public final boolean enabled; + + JenkinsServerDto(UUID uuid, DateTime created_date, URI url, boolean enabled) { + this.uuid = uuid; + this.created_date = created_date; + this.url = url; + this.enabled = enabled; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + JenkinsServerDto that = (JenkinsServerDto) o; + + return uuid.equals(that.uuid); + } + + @Override + public int hashCode() { + return uuid.hashCode(); + } +} diff --git a/src/main/java/io/trygvis/esper/testing/web/JenkinsResource.java b/src/main/java/io/trygvis/esper/testing/web/JenkinsResource.java new file mode 100644 index 0000000..a1e2f51 --- /dev/null +++ b/src/main/java/io/trygvis/esper/testing/web/JenkinsResource.java @@ -0,0 +1,57 @@ +package io.trygvis.esper.testing.web; + +import fj.data.*; +import io.trygvis.esper.testing.*; +import io.trygvis.esper.testing.jenkins.*; + +import javax.ws.rs.*; +import javax.ws.rs.core.*; +import java.sql.*; +import java.util.*; +import java.util.List; + +@Path("/") +public class JenkinsResource { + + private final DatabaseAccess da; + + public JenkinsResource(DatabaseAccess da) { + this.da = da; + } + + @GET + @Path("/resource/jenkins/server") + @Produces(MediaType.APPLICATION_JSON) + public List<JenkinsServerDto> getServers() throws Exception { + System.out.println("JenkinsResource.getServers"); + return da.inTransaction(new DatabaseAccess.DaosCallback<List<JenkinsServerDto>>() { + @Override + public List<JenkinsServerDto> run(Daos daos) throws SQLException { + return daos.jenkinsDao.selectServers(false); + } + }); + } + + @GET + @Path("/resource/jenkins/server/{uuid}") + @Produces(MediaType.APPLICATION_JSON) + public JenkinsServerDto getServer(@PathParam("uuid") String s) throws Exception { + try { + final UUID uuid = UUID.fromString(s); + System.out.println("JenkinsResource.getServers"); + Option<JenkinsServerDto> server = da.inTransaction(new DatabaseAccess.DaosCallback<Option<JenkinsServerDto>>() { + public Option<JenkinsServerDto> run(Daos daos) throws SQLException { + return daos.jenkinsDao.selectServer(uuid); + } + }); + + if(server.isNone()) { + throw new WebApplicationException(Response.Status.NOT_FOUND); + } + + return server.some(); + } catch (IllegalArgumentException e) { + throw new WebApplicationException(Response.Status.BAD_REQUEST); + } + } +} diff --git a/src/main/java/io/trygvis/esper/testing/web/JerseyApplication.java b/src/main/java/io/trygvis/esper/testing/web/JerseyApplication.java new file mode 100644 index 0000000..8d071a9 --- /dev/null +++ b/src/main/java/io/trygvis/esper/testing/web/JerseyApplication.java @@ -0,0 +1,23 @@ +package io.trygvis.esper.testing.web; + +import io.trygvis.esper.testing.*; + +import javax.ws.rs.core.*; +import java.util.*; + +public class JerseyApplication extends Application { + + private final DatabaseAccess da; + + public JerseyApplication() throws Exception { + Config config = Config.loadFromDisk(); + this.da = new DatabaseAccess(config.createBoneCp()); + } + + @Override + public Set<Object> getSingletons() { + return new HashSet<Object>(Arrays.asList( + new JenkinsResource(da) + )); + } +} |