From cac8228f38136cfc41673458c58c25f168b1e1ff Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Tue, 20 Nov 2012 20:02:47 +0100 Subject: 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. --- .../trygvis/esper/testing/nexus/NexusClient.java | 6 +- .../io/trygvis/esper/testing/nexus/NexusDao.java | 107 ++++++++++++++++++--- .../trygvis/esper/testing/nexus/NexusImporter.java | 76 +++++++++++++-- 3 files changed, 163 insertions(+), 26 deletions(-) (limited to 'src/main/java/io/trygvis/esper/testing/nexus') 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 repositoryId, Option 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 selectServer() throws SQLException { + try (PreparedStatement s = c.prepareStatement("SELECT url FROM nexus_server")) { + ResultSet rs = s.executeQuery(); + + List servers = new ArrayList<>(); + while(rs.next()) { + servers.add(new NexusServerDto(uri(rs.getString(1)))); + } + return servers; + } } - public Option selectRepository(String repositoryId) throws SQLException { - PreparedStatement s = prepareStatement("SELECT id, discovery_date, last_update, last_successful_update FROM nexus_repository WHERE id=?"); + public Option 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 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 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 discoveryDate; public final Option lastUpdate; public final Option lastSuccessfulUpdate; - NexusRepository(String repositoryId, Option discoveryDate, Option lastUpdate, Option lastSuccessfulUpdate) { + NexusRepositoryDto(String repositoryId, URI nexusUrl, String[] groupIds, Option discoveryDate, Option lastUpdate, Option 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.none()); - ArrayList 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> serverManager = new ObjectManager<>("Nexus server", Collections.emptySet(), new ObjectFactory>() { + public ActorRef 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.none()); + + ArrayList 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(); } } -- cgit v1.2.3