aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/io/trygvis/esper/testing/nexus
diff options
context:
space:
mode:
authorTrygve Laugstøl <trygvis@inamo.no>2012-11-20 20:02:47 +0100
committerTrygve Laugstøl <trygvis@inamo.no>2012-11-20 20:02:47 +0100
commitcac8228f38136cfc41673458c58c25f168b1e1ff (patch)
tree34f482cfd506a3b217f9f62fc00719b7f36e4a9e /src/main/java/io/trygvis/esper/testing/nexus
parent8596b9b566745ca65b3a75fe8b6d4c091369fedc (diff)
downloadesper-testing-cac8228f38136cfc41673458c58c25f168b1e1ff.tar.gz
esper-testing-cac8228f38136cfc41673458c58c25f168b1e1ff.tar.bz2
esper-testing-cac8228f38136cfc41673458c58c25f168b1e1ff.tar.xz
esper-testing-cac8228f38136cfc41673458c58c25f168b1e1ff.zip
o Adding BoneCP init to Config.
o Starting on an actor-like structure for the running jobs. o Loading Nexus servers and group ids to look for from the database.
Diffstat (limited to 'src/main/java/io/trygvis/esper/testing/nexus')
-rwxr-xr-xsrc/main/java/io/trygvis/esper/testing/nexus/NexusClient.java6
-rwxr-xr-xsrc/main/java/io/trygvis/esper/testing/nexus/NexusDao.java107
-rwxr-xr-xsrc/main/java/io/trygvis/esper/testing/nexus/NexusImporter.java76
3 files changed, 163 insertions, 26 deletions
diff --git a/src/main/java/io/trygvis/esper/testing/nexus/NexusClient.java b/src/main/java/io/trygvis/esper/testing/nexus/NexusClient.java
index 6477a80..02b5e3a 100755
--- a/src/main/java/io/trygvis/esper/testing/nexus/NexusClient.java
+++ b/src/main/java/io/trygvis/esper/testing/nexus/NexusClient.java
@@ -16,9 +16,9 @@ import javax.xml.stream.*;
public class NexusClient {
private final HTTPCache http;
- private final String nexusUrl;
+ private final URI nexusUrl;
- public NexusClient(HTTPCache http, String nexusUrl) {
+ public NexusClient(HTTPCache http, URI nexusUrl) {
this.http = http;
this.nexusUrl = nexusUrl;
}
@@ -36,7 +36,7 @@ public class NexusClient {
}
public ArtifactSearchResult fetchIndexPage(String groupId, Option<String> repositoryId, Option<Integer> from) throws IOException {
- URIBuilder uriBuilder = URIBuilder.fromURI(URI.create(nexusUrl)).
+ URIBuilder uriBuilder = URIBuilder.fromURI(nexusUrl).
addRawPath("/service/local/lucene/search").
addParameter("g", groupId + ".*");
diff --git a/src/main/java/io/trygvis/esper/testing/nexus/NexusDao.java b/src/main/java/io/trygvis/esper/testing/nexus/NexusDao.java
index 39d4233..00e15fe 100755
--- a/src/main/java/io/trygvis/esper/testing/nexus/NexusDao.java
+++ b/src/main/java/io/trygvis/esper/testing/nexus/NexusDao.java
@@ -5,21 +5,43 @@ import static fj.data.Option.*;
import io.trygvis.esper.testing.*;
import org.joda.time.*;
+import java.net.*;
+import java.sql.Array;
import java.sql.*;
+import java.util.*;
+import java.util.List;
public class NexusDao extends Dao {
protected NexusDao(Connection c) {
super(c);
}
- public void insertRepository(String repositoryId, LocalDateTime discoveryDate) throws SQLException {
- PreparedStatement s = prepareStatement("INSERT INTO nexus_repository(id) VALUES(?)");
- s.setString(1, repositoryId);
- s.executeUpdate();
+ /*
+ public void insertRepository(String repositoryId, URI nexusUri, LocalDateTime discoveryDate) throws SQLException {
+ int i = 1;
+ try (PreparedStatement s = prepareStatement("INSERT INTO nexus_repository(id, uri, discovered_date) VALUES(?, ?, ?)")) {
+ s.setString(i++, repositoryId);
+ s.setString(i++, nexusUri.toASCIIString());
+ s.setTimestamp(i, new Timestamp(discoveryDate.toDateTime().getMillis()));
+ s.executeUpdate();
+ }
+ }
+ */
+
+ public List<NexusServerDto> selectServer() throws SQLException {
+ try (PreparedStatement s = c.prepareStatement("SELECT url FROM nexus_server")) {
+ ResultSet rs = s.executeQuery();
+
+ List<NexusServerDto> servers = new ArrayList<>();
+ while(rs.next()) {
+ servers.add(new NexusServerDto(uri(rs.getString(1))));
+ }
+ return servers;
+ }
}
- public Option<NexusRepository> selectRepository(String repositoryId) throws SQLException {
- PreparedStatement s = prepareStatement("SELECT id, discovery_date, last_update, last_successful_update FROM nexus_repository WHERE id=?");
+ public Option<NexusRepositoryDto> selectRepository(String repositoryId) throws SQLException {
+ PreparedStatement s = prepareStatement("SELECT id, nexus_server_url, group_ids, discovery_date, last_update, last_successful_update FROM nexus_repository WHERE id=?");
s.setString(1, repositoryId);
try (ResultSet rs = s.executeQuery()) {
@@ -27,24 +49,81 @@ public class NexusDao extends Dao {
return Option.none();
}
- return some(new NexusRepository(
- rs.getString(1),
- fromNull(rs.getTimestamp(2)).map(timestampToLocalDateTime),
- fromNull(rs.getTimestamp(3)).map(timestampToLocalDateTime),
- fromNull(rs.getTimestamp(4)).map(timestampToLocalDateTime)
- ));
+ return some(toRepository(rs));
+ }
+ }
+
+ public List<NexusRepositoryDto> findRepositories(URI nexusUrl) throws SQLException {
+ PreparedStatement s = prepareStatement("SELECT id, nexus_server_url, group_ids, created_date, last_update, last_successful_update FROM nexus_repository WHERE nexus_server_url=?");
+ s.setString(1, nexusUrl.toASCIIString());
+
+ List<NexusRepositoryDto> list = new ArrayList<>();
+ try (ResultSet rs = s.executeQuery()) {
+ while(rs.next()) {
+ list.add(toRepository(rs));
+ }
+ }
+ return list;
+ }
+
+ private NexusRepositoryDto toRepository(ResultSet rs) throws SQLException {
+ int i = 1;
+
+ return new NexusRepositoryDto(
+ rs.getString(i++),
+ uri(rs.getString(i++)),
+ (String[]) rs.getArray(i++).getArray(),
+ fromNull(rs.getTimestamp(i++)).map(timestampToLocalDateTime),
+ fromNull(rs.getTimestamp(i++)).map(timestampToLocalDateTime),
+ fromNull(rs.getTimestamp(i)).map(timestampToLocalDateTime)
+ );
+ }
+
+ private URI uri(String s) {
+ try {
+ return URI.create(s);
+ } catch (Exception e) {
+ e.printStackTrace(System.out);
+ return null;
}
}
}
-class NexusRepository {
+class NexusServerDto {
+ public final URI url;
+
+ NexusServerDto(URI url) {
+ this.url = url;
+ }
+
+ public boolean equals(Object o) {
+ if (this == o) return true;
+ if (!(o instanceof NexusServerDto)) return false;
+
+ NexusServerDto that = (NexusServerDto) o;
+
+ if (!url.equals(that.url)) return false;
+
+ return true;
+ }
+
+ public int hashCode() {
+ return url.hashCode();
+ }
+}
+
+class NexusRepositoryDto {
public final String repositoryId;
+ public final URI nexusUrl;
+ public final String[] groupIds;
public final Option<LocalDateTime> discoveryDate;
public final Option<LocalDateTime> lastUpdate;
public final Option<LocalDateTime> lastSuccessfulUpdate;
- NexusRepository(String repositoryId, Option<LocalDateTime> discoveryDate, Option<LocalDateTime> lastUpdate, Option<LocalDateTime> lastSuccessfulUpdate) {
+ NexusRepositoryDto(String repositoryId, URI nexusUrl, String[] groupIds, Option<LocalDateTime> discoveryDate, Option<LocalDateTime> lastUpdate, Option<LocalDateTime> lastSuccessfulUpdate) {
this.repositoryId = repositoryId;
+ this.nexusUrl = nexusUrl;
+ this.groupIds = groupIds;
this.discoveryDate = discoveryDate;
this.lastUpdate = lastUpdate;
this.lastSuccessfulUpdate = lastSuccessfulUpdate;
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 896f0e2..12d8c9c 100755
--- a/src/main/java/io/trygvis/esper/testing/nexus/NexusImporter.java
+++ b/src/main/java/io/trygvis/esper/testing/nexus/NexusImporter.java
@@ -1,24 +1,82 @@
package io.trygvis.esper.testing.nexus;
import com.google.common.collect.*;
+import com.jolbox.bonecp.*;
import fj.data.*;
import io.trygvis.esper.testing.*;
+import io.trygvis.esper.testing.object.*;
+import static java.lang.Thread.*;
import org.apache.commons.lang.*;
+import org.codehaus.httpcache4j.cache.*;
-import java.io.*;
+import java.sql.*;
import java.util.*;
+import java.util.concurrent.atomic.*;
public class NexusImporter {
- public static void main(String[] args) throws IOException {
- Config config = Config.loadFromDisk();
+ public static void main(String[] args) throws Exception {
+ final Config config = Config.loadFromDisk();
- NexusClient client = new NexusClient(HttpClient.createHttpClient(config), config.nexusUrl);
+ final HTTPCache http = HttpClient.createHttpClient(config);
- ArtifactSearchResult result = client.fetchIndex("eu.nets", Option.<String>none());
- ArrayList<ArtifactXml> artifacts = Lists.newArrayList(result.artifacts);
- Collections.sort(artifacts);
- for (ArtifactXml artifact : artifacts) {
- System.out.println("repo=" + StringUtils.join(artifact.repositories(), ", ") + ", artifact=" + artifact.getId());
+ final BoneCPDataSource boneCp = config.createBoneCp();
+
+ ObjectManager<NexusServerDto, ActorRef<NexusServer>> serverManager = new ObjectManager<>("Nexus server", Collections.<NexusServerDto>emptySet(), new ObjectFactory<NexusServerDto, ActorRef<NexusServer>>() {
+ public ActorRef<NexusServer> create(NexusServerDto server) {
+ final NexusClient client = new NexusClient(http, server.url);
+
+ return ObjectUtil.threadedActor(boneCp, config.nexusUpdateInterval, new NexusServer(client, server));
+ }
+ });
+
+ final AtomicBoolean shouldRun = new AtomicBoolean(true);
+ config.addShutdownHook(currentThread(), shouldRun);
+
+ while (shouldRun.get()) {
+ try {
+ try (Connection c = boneCp.getConnection()) {
+ serverManager.update(new NexusDao(c).selectServer());
+ }
+ } catch (SQLException e) {
+ e.printStackTrace(System.out);
+ }
+
+ synchronized (shouldRun) {
+ shouldRun.wait(1000);
+ }
}
+
+ serverManager.close();
+ }
+}
+
+class NexusServer implements TransactionalActor {
+
+ public final NexusClient client;
+ public final NexusServerDto server;
+
+ NexusServer(NexusClient client, NexusServerDto server) {
+ this.client = client;
+ this.server = server;
+ }
+
+ public void act(Connection c) throws Exception {
+ NexusDao dao = new NexusDao(c);
+
+ for (NexusRepositoryDto repository : dao.findRepositories(server.url)) {
+ System.out.println("Updating repository: " + repository.repositoryId);
+ for (String groupId : repository.groupIds) {
+ System.out.println("Updating groupId: " + groupId);
+ ArtifactSearchResult result = client.fetchIndex(groupId, Option.<String>none());
+
+ ArrayList<ArtifactXml> artifacts = Lists.newArrayList(result.artifacts);
+ Collections.sort(artifacts);
+ for (ArtifactXml artifact : artifacts) {
+ System.out.println("repo=" + StringUtils.join(artifact.repositories(), ", ") + ", artifact=" + artifact.getId());
+ }
+ }
+ }
+
+ c.commit();
}
}