From 164ce072f94a0820d6c2256aab8c6ee8d90d8eee Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Sat, 8 Dec 2012 10:52:00 +0100 Subject: o Creating a flag to disable jenkins servers from being polled. o Using proper logging. --- src/main/java/io/trygvis/esper/testing/Daos.java | 4 -- .../esper/testing/jenkins/JenkinsClient.java | 72 +++++++++++----------- .../trygvis/esper/testing/jenkins/JenkinsDao.java | 7 ++- .../esper/testing/jenkins/JenkinsImporter.java | 2 +- .../esper/testing/jenkins/JenkinsServer.java | 16 +++-- .../esper/testing/object/ObjectManager.java | 10 ++- .../trygvis/esper/testing/object/ObjectUtil.java | 6 +- .../io/trygvis/esper/testing/util/HttpClient.java | 21 ++++--- .../trygvis/esper/testing/util/XmlHttpClient.java | 16 ++--- .../io/trygvis/esper/testing/util/XmlParser.java | 17 ++--- 10 files changed, 92 insertions(+), 79 deletions(-) (limited to 'src/main/java/io/trygvis/esper') diff --git a/src/main/java/io/trygvis/esper/testing/Daos.java b/src/main/java/io/trygvis/esper/testing/Daos.java index 0a7a5da..e7faeca 100755 --- a/src/main/java/io/trygvis/esper/testing/Daos.java +++ b/src/main/java/io/trygvis/esper/testing/Daos.java @@ -51,8 +51,4 @@ public class Daos implements Closeable { public void commit() throws SQLException { connection.commit(); } - - public static Daos lookup(DataSource dataSource) throws SQLException { - return new Daos(dataSource.getConnection()); - } } 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 40d7618..ecd8288 100755 --- a/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsClient.java +++ b/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsClient.java @@ -13,6 +13,7 @@ import org.codehaus.httpcache4j.cache.*; import org.jdom2.Document; import org.jdom2.Element; import org.joda.time.DateTime; +import org.slf4j.*; import java.io.*; import java.net.*; @@ -24,6 +25,7 @@ import static io.trygvis.esper.testing.Util.*; import static org.apache.commons.lang.StringUtils.*; public class JenkinsClient { + private static final Logger logger = LoggerFactory.getLogger(JenkinsClient.class); private final XmlHttpClient xmlHttpClient; private final HttpClient> jenkinsEntryXmlClient; private final Parser parser; @@ -101,7 +103,7 @@ public class JenkinsClient { case "mavenModuleSet": return some(JenkinsJobXml.parse(uri, JenkinsJobType.MAVEN, root)); default: - System.out.println("Unknown project type: " + name); + logger.warn("Unknown project type: " + name); return Option.none(); } } @@ -124,11 +126,43 @@ public class JenkinsClient { case "freeStyleBuild": return JenkinsBuildXml.parse(root); default: - System.out.println("Unknown build type: " + name); + logger.warn("Unknown build type: " + name); return Option.none(); } } -} + + public static class JenkinsBuildXml { + + public final URI uri; + public final int number; + public final String result; + public final int duration; + public final long timestamp; + + JenkinsBuildXml(URI uri, int number, String result, int duration, long timestamp) { + this.uri = uri; + this.number = number; + this.result = result; + this.duration = duration; + this.timestamp = timestamp; + } + + public static Option parse(Element root) { + + Option uri = childText(root, "url").bind(Util.parseUri); + Option number = childText(root, "number").bind(Util.parseInt); + Option result = childText(root, "result"); + Option duration = childText(root, "duration").bind(Util.parseInt); + Option timestamp = childText(root, "timestamp").bind(Util.parseLong); + + if(uri.isNone() || number.isNone() || result.isNone() || duration.isNone() || timestamp.isNone()) { + logger.warn("Missing required fields."); + return none(); + } + + return some(new JenkinsBuildXml(uri.some(), number.some(), result.some(), duration.some(), timestamp.some())); + } + }} class JenkinsEntryXml { public final String id; @@ -243,35 +277,3 @@ class JenkinsJobXml { } } -class JenkinsBuildXml { - - public final URI uri; - public final int number; - public final String result; - public final int duration; - public final long timestamp; - - JenkinsBuildXml(URI uri, int number, String result, int duration, long timestamp) { - this.uri = uri; - this.number = number; - this.result = result; - this.duration = duration; - this.timestamp = timestamp; - } - - public static Option parse(Element root) { - - Option uri = childText(root, "url").bind(Util.parseUri); - Option number = childText(root, "number").bind(Util.parseInt); - Option result = childText(root, "result"); - Option duration = childText(root, "duration").bind(Util.parseInt); - Option timestamp = childText(root, "timestamp").bind(Util.parseLong); - - if(uri.isNone() || number.isNone() || result.isNone() || duration.isNone() || timestamp.isNone()) { - System.out.println("Missing required fields."); - return none(); - } - - return some(new JenkinsBuildXml(uri.some(), number.some(), result.some(), duration.some(), timestamp.some())); - } -} 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 05ed567..a063180 100644 --- a/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsDao.java +++ b/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsDao.java @@ -54,8 +54,11 @@ public class JenkinsDao { new DateTime(rs.getTimestamp(i).getTime())); } - public List selectServer() throws SQLException { - try (PreparedStatement s = c.prepareStatement("SELECT " + JENKINS_SERVER + " FROM jenkins_server")) { + public List selectServer(boolean enabledOnly) throws SQLException { + String where = "WHERE "; + where += enabledOnly ? "enabled=true" : ""; + + try (PreparedStatement s = c.prepareStatement("SELECT " + JENKINS_SERVER + " FROM jenkins_server " + where)) { ResultSet rs = s.executeQuery(); List servers = new ArrayList<>(); 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 c273d8a..ffe1421 100755 --- 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 newKeys; try (Connection c = boneCp.getConnection()) { - newKeys = new JenkinsDao(c).selectServer(); + newKeys = new JenkinsDao(c).selectServer(true); } serverManager.update(newKeys); 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 9d12456..a89288d 100644 --- a/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsServer.java +++ b/src/main/java/io/trygvis/esper/testing/jenkins/JenkinsServer.java @@ -2,13 +2,17 @@ package io.trygvis.esper.testing.jenkins; import fj.data.*; import io.trygvis.esper.testing.object.*; +import org.slf4j.*; import java.net.*; import java.sql.*; -import java.util.*; import java.util.List; +import java.util.*; + +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; @@ -27,7 +31,7 @@ public class JenkinsServer implements TransactionalActor { List list = option.some(); - System.out.println("Got " + list.size() + " entries."); + logger.info("Got " + list.size() + " entries."); int i = 0; @@ -35,11 +39,11 @@ public class JenkinsServer implements TransactionalActor { Option o = dao.selectBuildByEntryId(entry.id); if(o.isSome()) { - System.out.println("Old event: " + entry.id); + logger.info("Old event: " + entry.id); continue; } - System.out.println("New event: " + entry.id + ", fetching build info"); + logger.info("New event: " + entry.id + ", fetching build info"); i++; @@ -53,13 +57,13 @@ public class JenkinsServer implements TransactionalActor { UUID uuid = dao.insertBuild(entry.id, build.uri, build.result, build.number, build.duration, build.timestamp); - System.out.println("Build inserted: " + uuid + ", i=" + i); + logger.info("Build inserted: " + uuid + ", i=" + i); // if(i == 1) { // break; // } } - System.out.println("Inserted " + i + " new events."); + logger.info("Inserted " + i + " new events."); } } diff --git a/src/main/java/io/trygvis/esper/testing/object/ObjectManager.java b/src/main/java/io/trygvis/esper/testing/object/ObjectManager.java index e850c61..a4fbc60 100755 --- a/src/main/java/io/trygvis/esper/testing/object/ObjectManager.java +++ b/src/main/java/io/trygvis/esper/testing/object/ObjectManager.java @@ -1,9 +1,13 @@ 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<>(); @@ -28,7 +32,7 @@ public class ObjectManager implements Closeable { for (K k : gone) { try { - System.out.println("Removing " + type + " with id=" + k); + logger.debug("Removing " + type + " with id=" + k); objects.remove(k).close(); } catch (IOException e) { e.printStackTrace(System.out); @@ -36,14 +40,14 @@ public class ObjectManager implements Closeable { } for (K k : found) { - System.out.println("Adding " + type + " with id=" + k); + logger.debug("Adding " + type + " with id=" + k); objects.put(k, objectFactory.create(k)); } } public synchronized void close() throws IOException { if (closed) { - System.out.println("Already closed: type=" + type); + logger.warn("Already closed: type=" + type); return; } update(Collections.emptyList()); diff --git a/src/main/java/io/trygvis/esper/testing/object/ObjectUtil.java b/src/main/java/io/trygvis/esper/testing/object/ObjectUtil.java index f421acf..d70e074 100755 --- a/src/main/java/io/trygvis/esper/testing/object/ObjectUtil.java +++ b/src/main/java/io/trygvis/esper/testing/object/ObjectUtil.java @@ -1,5 +1,7 @@ package io.trygvis.esper.testing.object; +import org.slf4j.*; + import javax.sql.*; import java.io.*; import java.sql.*; @@ -16,6 +18,8 @@ public class ObjectUtil { } 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; @@ -47,7 +51,7 @@ public class ObjectUtil { c.close(); } } catch (Throwable e) { - System.out.println("Exception in thread " + Thread.currentThread().getName()); + logger.warn("Exception in thread " + Thread.currentThread().getName()); e.printStackTrace(System.out); } } 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 420a949..9596fd1 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.slf4j.*; import java.io.*; import java.net.*; @@ -18,6 +19,8 @@ import static java.lang.System.*; public class HttpClient { + private static final Logger logger = LoggerFactory.getLogger(HttpClient.class); + private final HTTPCache http; private final F> f; @@ -26,25 +29,25 @@ public class HttpClient { this.f = f; } - public static HttpClient httpClient(HTTPCache http, final F> f) { - return new HttpClient<>(http, new F>() { + public static F> inputStreamOnly(final F> f) { + return new F>() { @Override public Option f(HTTPResponse response) { return f.f(response.getPayload().getInputStream()); } - }); + }; } public Option fetch(URI uri) throws IOException { HTTPResponse response = null; try { - System.out.println("Fetching " + uri); - long start = currentTimeMillis(); +// logger.debug("Fetching " + uri); +// long start = currentTimeMillis(); response = http.execute(new HTTPRequest(uri)); - long end = currentTimeMillis(); +// long end = currentTimeMillis(); int code = response.getStatus().getCode(); - System.out.println("Fetched in " + (end - start) + "ms. Status: " + code); +// logger.debug("Fetched in " + (end - start) + "ms. Status: " + code); if (code != 200) { throw new IOException("Did not get 200 back, got " + code); @@ -88,7 +91,7 @@ public class HttpClient { } public HTTPResponse resolve(HTTPRequest request) throws IOException { - System.out.println(request.getRequestURI() + ": Executing"); + logger.debug(request.getRequestURI() + ": Executing"); long start = currentTimeMillis(); Status status = null; try { @@ -106,7 +109,7 @@ public class HttpClient { s += "with exception"; } - System.out.println(s); + logger.debug(s); } } diff --git a/src/main/java/io/trygvis/esper/testing/util/XmlHttpClient.java b/src/main/java/io/trygvis/esper/testing/util/XmlHttpClient.java index dc3b46f..7e5e298 100755 --- a/src/main/java/io/trygvis/esper/testing/util/XmlHttpClient.java +++ b/src/main/java/io/trygvis/esper/testing/util/XmlHttpClient.java @@ -6,22 +6,16 @@ import org.codehaus.httpcache4j.cache.*; import org.jdom2.*; import java.io.*; -import java.net.*; -public class XmlHttpClient { - - private final HttpClient httpClient; +public class XmlHttpClient extends HttpClient { public XmlHttpClient(HTTPCache http) { - final XmlParser parser = new XmlParser(); - httpClient = HttpClient.httpClient(http, new F>() { + super(http, HttpClient.inputStreamOnly(new F>() { + final XmlParser parser = new XmlParser(); + public Option f(InputStream inputStream) { return parser.parseDocument(inputStream); } - }); - } - - public Option fetch(URI uri) throws IOException { - return httpClient.fetch(uri); + })); } } diff --git a/src/main/java/io/trygvis/esper/testing/util/XmlParser.java b/src/main/java/io/trygvis/esper/testing/util/XmlParser.java index e1b463f..9c585fc 100755 --- a/src/main/java/io/trygvis/esper/testing/util/XmlParser.java +++ b/src/main/java/io/trygvis/esper/testing/util/XmlParser.java @@ -1,16 +1,20 @@ package io.trygvis.esper.testing.util; import fj.data.*; -import static fj.data.Option.*; -import static javax.xml.stream.XMLStreamConstants.*; import org.h2.util.*; import org.jdom2.*; import org.jdom2.input.*; +import org.slf4j.*; -import java.io.*; import javax.xml.stream.*; +import java.io.*; + +import static fj.data.Option.*; +import static javax.xml.stream.XMLStreamConstants.*; public class XmlParser { + private static final Logger logger = LoggerFactory.getLogger(XmlParser.class); + public static boolean debugXml; private final XMLInputFactory xmlInputFactory; @@ -29,10 +33,9 @@ public class XmlParser { ByteArrayOutputStream buffer = new ByteArrayOutputStream(); IOUtils.copy(stream, buffer); byte[] bytes = buffer.toByteArray(); - System.out.println("------------------------------------------------"); - System.out.write(bytes); - System.out.println(); - System.out.println("------------------------------------------------"); + logger.debug("------------------------------------------------"); + logger.debug(new String(bytes, "utf-8")); + logger.debug("------------------------------------------------"); stream = new ByteArrayInputStream(bytes); } -- cgit v1.2.3