From 58e2f4c4ef4655c88270b2c3a6b53ef642f82c57 Mon Sep 17 00:00:00 2001
From: Trygve Laugstøl <trygvis@inamo.no>
Date: Sat, 8 Dec 2012 11:36:31 +0100
Subject: o Closing the HTTP streams so we don't run out of connections. o
 Attaching the build to a server.

---
 src/main/java/io/trygvis/esper/testing/Config.java      |  2 ++
 .../io/trygvis/esper/testing/jenkins/JenkinsClient.java |  9 +++++----
 .../io/trygvis/esper/testing/jenkins/JenkinsDao.java    | 13 +++++++++----
 .../trygvis/esper/testing/jenkins/JenkinsImporter.java  |  2 +-
 .../io/trygvis/esper/testing/jenkins/JenkinsServer.java | 17 ++++++++++++-----
 .../java/io/trygvis/esper/testing/util/HttpClient.java  | 12 +++++++-----
 src/main/resources/ddl-jenkins.sql                      |  3 +++
 src/main/resources/logback-test.xml                     |  2 +-
 8 files changed, 40 insertions(+), 20 deletions(-)

(limited to 'src/main')

diff --git a/src/main/java/io/trygvis/esper/testing/Config.java b/src/main/java/io/trygvis/esper/testing/Config.java
index 95e5a99..4ff034a 100755
--- a/src/main/java/io/trygvis/esper/testing/Config.java
+++ b/src/main/java/io/trygvis/esper/testing/Config.java
@@ -1,5 +1,7 @@
 package io.trygvis.esper.testing;
 
+import ch.qos.logback.classic.*;
+import ch.qos.logback.core.util.*;
 import com.jolbox.bonecp.*;
 import fj.data.*;
 import static fj.data.Option.*;
diff --git a/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsClient.java b/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsClient.java
index ecd8288..59c4f30 100755
--- a/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsClient.java
+++ b/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsClient.java
@@ -22,6 +22,7 @@ import java.util.List;
 
 import static fj.data.Option.*;
 import static io.trygvis.esper.testing.Util.*;
+import static io.trygvis.esper.testing.util.HttpClient.inputStreamOnly;
 import static org.apache.commons.lang.StringUtils.*;
 
 public class JenkinsClient {
@@ -34,9 +35,9 @@ public class JenkinsClient {
         this.xmlHttpClient = new XmlHttpClient(http);
         this.parser = abdera.getParser();
 
-        jenkinsEntryXmlClient = new HttpClient<>(http, new F<HTTPResponse, Option<List<JenkinsEntryXml>>>() {
-            public Option<List<JenkinsEntryXml>> f(HTTPResponse response) {
-                Feed feed = (Feed) parser.parse(response.getPayload().getInputStream()).getRoot();
+        jenkinsEntryXmlClient = new HttpClient<>(http, inputStreamOnly(new F<InputStream, Option<List<JenkinsEntryXml>>>() {
+            public Option<List<JenkinsEntryXml>> f(InputStream inputStream) {
+                Feed feed = (Feed) parser.parse(inputStream).getRoot();
 
                 List<JenkinsEntryXml> list = new ArrayList<>();
 
@@ -49,7 +50,7 @@ public class JenkinsClient {
 
                 return some(list);
             }
-        });
+        }));
     }
 
     public Option<List<JenkinsEntryXml>> fetchRss(URI uri) throws IOException {
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 a063180..8a7480b 100644
--- a/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsDao.java
+++ b/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsDao.java
@@ -19,7 +19,7 @@ public class JenkinsDao {
 
     private static final String JENKINS_SERVER = "uuid, created_date, url";
 
-    private static final String JENKINS_BUILD = "uuid, created_date, entry_id, url, result, number, duration, timestamp";
+    private static final String JENKINS_BUILD = "uuid, created_date, server, entry_id, url, result, number, duration, timestamp";
 
     public JenkinsDao(Connection c) {
         this.c = c;
@@ -46,6 +46,7 @@ public class JenkinsDao {
         return new JenkinsBuildDto(
                 UUID.fromString(rs.getString(i++)),
                 new DateTime(rs.getTimestamp(i++).getTime()),
+                UUID.fromString(rs.getString(i++)),
                 rs.getString(i++),
                 URI.create(rs.getString(i++)),
                 rs.getString(i++),
@@ -83,12 +84,13 @@ public class JenkinsDao {
         }
     }
 
-    public UUID insertBuild(String entryId, URI uri, String result, int number, int duration, long timestamp) throws SQLException {
-        try (PreparedStatement s = c.prepareStatement("INSERT INTO jenkins_build(" + JENKINS_BUILD + ") VALUES(?, ?, ?, ?, ?, ?, ?, ?)")) {
+    public UUID insertBuild(UUID server, String entryId, URI uri, String result, int number, int duration, long timestamp) throws SQLException {
+        try (PreparedStatement s = c.prepareStatement("INSERT INTO jenkins_build(" + JENKINS_BUILD + ") VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?)")) {
             UUID uuid = UUID.randomUUID();
             int i = 1;
             s.setString(i++, uuid.toString());
             s.setTimestamp(i++, new Timestamp(currentTimeMillis()));
+            s.setString(i++, server.toString());
             s.setString(i++, entryId);
             s.setString(i++, uri.toASCIIString());
             s.setString(i++, result);
@@ -146,6 +148,8 @@ class JenkinsJobDto {
 class JenkinsBuildDto {
     public final UUID uuid;
     public final DateTime created_date;
+    // TODO: should be job
+    public final UUID server;
     public final String entryId;
     public final URI uri;
     public final String result;
@@ -153,9 +157,10 @@ class JenkinsBuildDto {
     public final int duration;
     public final DateTime timestamp;
 
-    JenkinsBuildDto(UUID uuid, DateTime created_date, String entryId, URI uri, String result, int number, int duration, DateTime timestamp) {
+    JenkinsBuildDto(UUID uuid, DateTime created_date, UUID server, String entryId, URI uri, String result, int number, int duration, DateTime timestamp) {
         this.uuid = uuid;
         this.created_date = created_date;
+        this.server = server;
         this.entryId = entryId;
         this.uri = uri;
         this.result = result;
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 ffe1421..c9a28b2 100755
--- a/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsImporter.java
+++ b/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsImporter.java
@@ -29,7 +29,7 @@ public class JenkinsImporter {
         ObjectManager<JenkinsServerDto, ActorRef<JenkinsServer>> serverManager = new ObjectManager<>("JenkinsServerOld", servers, new ObjectFactory<JenkinsServerDto, ActorRef<JenkinsServer>>() {
             public ActorRef<JenkinsServer> create(JenkinsServerDto server) {
                 String name = "Jenkins: " + server.uri;
-                return threadedActor(name, config.jenkinsUpdateInterval, boneCp, name, new JenkinsServer(jenkinsClient, server.uri));
+                return threadedActor(name, config.jenkinsUpdateInterval, boneCp, name, new JenkinsServer(jenkinsClient, server));
             }
         });
 
diff --git a/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsServer.java b/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsServer.java
index a89288d..92c0569 100644
--- a/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsServer.java
+++ b/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsServer.java
@@ -14,16 +14,16 @@ import static io.trygvis.esper.testing.jenkins.JenkinsClient.*;
 public class JenkinsServer implements TransactionalActor {
     private static final Logger logger = LoggerFactory.getLogger(JenkinsServer.class);
     private final JenkinsClient client;
-    public final URI uri;
+    public final JenkinsServerDto server;
 
-    public JenkinsServer(JenkinsClient client, URI uri) {
+    public JenkinsServer(JenkinsClient client, JenkinsServerDto server) {
         this.client = client;
-        this.uri = uri;
+        this.server = server;
     }
 
     public void act(Connection c) throws Exception {
         JenkinsDao dao = new JenkinsDao(c);
-        Option<List<JenkinsEntryXml>> option = client.fetchRss(URI.create(uri.toASCIIString() + "/rssAll"));
+        Option<List<JenkinsEntryXml>> option = client.fetchRss(URI.create(server.uri.toASCIIString() + "/rssAll"));
 
         if(option.isNone()) {
             return;
@@ -55,7 +55,14 @@ public class JenkinsServer implements TransactionalActor {
 
             JenkinsBuildXml build = o2.some();
 
-            UUID uuid = dao.insertBuild(entry.id, build.uri, build.result, build.number, build.duration, build.timestamp);
+            UUID uuid = dao.insertBuild(
+                    server.uuid,
+                    entry.id,
+                    build.uri,
+                    build.result,
+                    build.number,
+                    build.duration,
+                    build.timestamp);
 
             logger.info("Build inserted: " + uuid + ", i=" + i);
 
diff --git a/src/main/java/io/trygvis/esper/testing/util/HttpClient.java b/src/main/java/io/trygvis/esper/testing/util/HttpClient.java
index 9596fd1..79159ac 100755
--- a/src/main/java/io/trygvis/esper/testing/util/HttpClient.java
+++ b/src/main/java/io/trygvis/esper/testing/util/HttpClient.java
@@ -10,6 +10,7 @@ import org.apache.http.params.*;
 import org.codehaus.httpcache4j.*;
 import org.codehaus.httpcache4j.cache.*;
 import org.codehaus.httpcache4j.resolver.*;
+import org.h2.util.*;
 import org.slf4j.*;
 
 import java.io.*;
@@ -33,7 +34,12 @@ public class HttpClient<A> {
         return new F<HTTPResponse, Option<A>>() {
             @Override
             public Option<A> f(HTTPResponse response) {
-                return f.f(response.getPayload().getInputStream());
+                InputStream inputStream = response.getPayload().getInputStream();
+                try {
+                    return f.f(inputStream);
+                } finally {
+                    IOUtils.closeSilently(inputStream);
+                }
             }
         };
     }
@@ -42,12 +48,8 @@ public class HttpClient<A> {
         HTTPResponse response = null;
 
         try {
-//            logger.debug("Fetching " + uri);
-//            long start = currentTimeMillis();
             response = http.execute(new HTTPRequest(uri));
-//            long end = currentTimeMillis();
             int code = response.getStatus().getCode();
-//            logger.debug("Fetched in " + (end - start) + "ms. Status: " + code);
 
             if (code != 200) {
                 throw new IOException("Did not get 200 back, got " + code);
diff --git a/src/main/resources/ddl-jenkins.sql b/src/main/resources/ddl-jenkins.sql
index c34e8cb..df98185 100755
--- a/src/main/resources/ddl-jenkins.sql
+++ b/src/main/resources/ddl-jenkins.sql
@@ -16,6 +16,8 @@ CREATE TABLE jenkins_build (
   uuid         CHAR(36)      NOT NULL,
   created_date TIMESTAMP     NOT NULL,
 
+  server       CHAR(36)      NOT NULL,
+
   entry_id     VARCHAR(1000) NOT NULL,
   url          VARCHAR(1000) NOT NULL,
   result       VARCHAR(100)  NOT NULL,
@@ -24,6 +26,7 @@ CREATE TABLE jenkins_build (
   timestamp    TIMESTAMP     NOT NULL,
 
   CONSTRAINT pk_jenkins_build PRIMARY KEY (UUID),
+  CONSTRAINT fk_jenkins_build__server FOREIGN KEY (server) REFERENCES jenkins_server (uuid),
   CONSTRAINT uq_jenkins_build__id UNIQUE (entry_id)
 );
 
diff --git a/src/main/resources/logback-test.xml b/src/main/resources/logback-test.xml
index b972562..49aa8bc 100755
--- a/src/main/resources/logback-test.xml
+++ b/src/main/resources/logback-test.xml
@@ -12,7 +12,7 @@
   <!-- Gitorious spews out a few invalid cookies -->
   <logger name="org.apache.http.client.protocol.ResponseProcessCookies" level="ERROR"/>
 
-  <root level="DEBUG">
+  <root>
     <appender-ref ref="STDOUT"/>
   </root>
 </configuration>
-- 
cgit v1.2.3