From a521a2d74c480c53c1004e7e5e2addd28a0578f4 Mon Sep 17 00:00:00 2001 From: Trygve Laugstøl Date: Sat, 12 Apr 2014 20:37:55 +0200 Subject: wip --- .../calamus/nexus/CalamusEventListener.java | 52 ++++++++++++++++++++++ .../trygvis/calamus/nexus/CalamusNexusPlugin.java | 14 ++++-- .../calamus/nexus/CalamusUiContributor.java | 2 + .../io/trygvis/calamus/nexus/EventListener.java | 51 --------------------- .../java/io/trygvis/calamus/nexus/MqClient.java | 35 +++++++++------ 5 files changed, 87 insertions(+), 67 deletions(-) create mode 100644 calamus-nexus-plugin/src/main/java/io/trygvis/calamus/nexus/CalamusEventListener.java delete mode 100644 calamus-nexus-plugin/src/main/java/io/trygvis/calamus/nexus/EventListener.java (limited to 'calamus-nexus-plugin/src/main/java/io') diff --git a/calamus-nexus-plugin/src/main/java/io/trygvis/calamus/nexus/CalamusEventListener.java b/calamus-nexus-plugin/src/main/java/io/trygvis/calamus/nexus/CalamusEventListener.java new file mode 100644 index 0000000..ddfd171 --- /dev/null +++ b/calamus-nexus-plugin/src/main/java/io/trygvis/calamus/nexus/CalamusEventListener.java @@ -0,0 +1,52 @@ +package io.trygvis.calamus.nexus; + +import com.google.common.eventbus.Subscribe; +import org.slf4j.Logger; +import org.sonatype.nexus.events.EventSubscriber; +import org.sonatype.nexus.proxy.events.RepositoryItemEventStoreCreate; +import org.sonatype.nexus.proxy.maven.gav.Gav; +import org.sonatype.nexus.proxy.maven.gav.GavCalculator; + +import javax.inject.Inject; +import javax.inject.Named; +import javax.inject.Singleton; + +import static org.slf4j.LoggerFactory.getLogger; + +@Singleton +@Named("CalamusEventListener") +public class CalamusEventListener implements EventSubscriber { + + private final Logger log = getLogger(getClass()); + + private final MqClient mqClient; + private final GavCalculator gavCalculator; + + @Inject + public CalamusEventListener(CalamusConfig config, @Named("maven2") GavCalculator gavCalculator) { + log.info("CalamusEventListener.CalamusEventListener"); + log.info("gavCalculator = " + gavCalculator); + + this.mqClient = new MqClient(config.getBrokerUrl()); + this.gavCalculator = gavCalculator; + } + + @Subscribe + public void onRepositoryItemEventStoreCreate(RepositoryItemEventStoreCreate event) { + log.info("EventListener.onRepositoryItemEventStoreCreate"); + + Gav gav = gavCalculator.pathToGav(event.getItem().getPath()); + + if (gav == null || gav.isHash() || gav.isSignature()) { + return; + } + + String groupId = gav.getGroupId(); + String artifactId = gav.getArtifactId(); + String version = gav.getVersion(); + String extension = gav.getExtension(); + String classifier = gav.getClassifier(); + log.info("New artifact: groupId={}, artifactId={}, version={}, classifier={}, extension={}", groupId, artifactId, version, classifier, extension); + mqClient.sendMessage(event.getRepository().getId(), groupId, artifactId, version, classifier, extension); + } +} diff --git a/calamus-nexus-plugin/src/main/java/io/trygvis/calamus/nexus/CalamusNexusPlugin.java b/calamus-nexus-plugin/src/main/java/io/trygvis/calamus/nexus/CalamusNexusPlugin.java index e60fed7..96d2c1d 100644 --- a/calamus-nexus-plugin/src/main/java/io/trygvis/calamus/nexus/CalamusNexusPlugin.java +++ b/calamus-nexus-plugin/src/main/java/io/trygvis/calamus/nexus/CalamusNexusPlugin.java @@ -1,17 +1,25 @@ package io.trygvis.calamus.nexus; -import org.eclipse.sisu.EagerSingleton; +//import org.eclipse.sisu.EagerSingleton; + +import org.slf4j.Logger; import org.sonatype.nexus.plugin.PluginIdentity; import javax.inject.Inject; import javax.inject.Named; -@EagerSingleton -@Named("calamus") +import static org.slf4j.LoggerFactory.getLogger; + +@Named +//@EagerSingleton public class CalamusNexusPlugin extends PluginIdentity { + private final Logger log = getLogger(getClass()); + @Inject public CalamusNexusPlugin() throws Exception { super("io.trygvis.calamus", "calamus-nexus-plugin"); + + System.out.println("CalamusNexusPlugin.CalamusNexusPlugin"); } } diff --git a/calamus-nexus-plugin/src/main/java/io/trygvis/calamus/nexus/CalamusUiContributor.java b/calamus-nexus-plugin/src/main/java/io/trygvis/calamus/nexus/CalamusUiContributor.java index ed2f745..18e1c68 100644 --- a/calamus-nexus-plugin/src/main/java/io/trygvis/calamus/nexus/CalamusUiContributor.java +++ b/calamus-nexus-plugin/src/main/java/io/trygvis/calamus/nexus/CalamusUiContributor.java @@ -1,10 +1,12 @@ package io.trygvis.calamus.nexus; +/* import org.sonatype.nexus.plugins.ui.contribution.UiContributorSupport; import javax.inject.Inject; import javax.inject.Named; import javax.inject.Singleton; +*/ //@Named //@Singleton diff --git a/calamus-nexus-plugin/src/main/java/io/trygvis/calamus/nexus/EventListener.java b/calamus-nexus-plugin/src/main/java/io/trygvis/calamus/nexus/EventListener.java deleted file mode 100644 index 4770974..0000000 --- a/calamus-nexus-plugin/src/main/java/io/trygvis/calamus/nexus/EventListener.java +++ /dev/null @@ -1,51 +0,0 @@ -package io.trygvis.calamus.nexus; - -import com.google.common.eventbus.Subscribe; -import org.slf4j.Logger; -import org.sonatype.nexus.events.EventSubscriber; -import org.sonatype.nexus.proxy.events.RepositoryItemEventStoreCreate; -import org.sonatype.nexus.proxy.maven.gav.Gav; -import org.sonatype.nexus.proxy.maven.gav.GavCalculator; - -import javax.inject.Inject; -import javax.inject.Named; -import javax.inject.Singleton; - -import static org.slf4j.LoggerFactory.getLogger; - -@Singleton -@Named -public class EventListener implements EventSubscriber { - - private final Logger log = getLogger(getClass()); - - private final MqClient mqClient; - private final GavCalculator gavCalculator; - - @Inject - public EventListener(CalamusConfig config, @Named("maven2") GavCalculator gavCalculator) { - log.info("CalamusNexusPlugin.CalamusNexusPlugin"); - log.info("gavCalculator = " + gavCalculator); - - this.mqClient = new MqClient(config.getBrokerUrl()); - this.gavCalculator = gavCalculator; - } - - @Subscribe - public void onRepositoryItemEventStoreCreate(RepositoryItemEventStoreCreate event) { - Gav gav = gavCalculator.pathToGav(event.getItem().getPath()); - - if (gav == null) { - return; - } - - String groupId = gav.getGroupId(); - String artifactId = gav.getArtifactId(); - String version = gav.getVersion(); - String type = gav.getExtension(); - String classifier = gav.getClassifier(); - mqClient.sendMessage(event.getRepository().getId(), groupId, artifactId, version, type, classifier); - - log.info("New artifact: groupId={}, artifactId={}, version={}, type={}, classifier={}", groupId, artifactId, version, type, classifier); - } -} diff --git a/calamus-nexus-plugin/src/main/java/io/trygvis/calamus/nexus/MqClient.java b/calamus-nexus-plugin/src/main/java/io/trygvis/calamus/nexus/MqClient.java index 51b2b3a..2564ed9 100644 --- a/calamus-nexus-plugin/src/main/java/io/trygvis/calamus/nexus/MqClient.java +++ b/calamus-nexus-plugin/src/main/java/io/trygvis/calamus/nexus/MqClient.java @@ -1,26 +1,33 @@ package io.trygvis.calamus.nexus; import org.apache.activemq.ActiveMQConnectionFactory; +import org.slf4j.Logger; import javax.jms.Connection; import javax.jms.DeliveryMode; import javax.jms.Destination; import javax.jms.JMSException; -import javax.jms.MapMessage; import javax.jms.MessageProducer; import javax.jms.Session; +import java.io.CharArrayWriter; +import java.io.IOException; +import java.util.Properties; import static javax.jms.Session.AUTO_ACKNOWLEDGE; +import static org.apache.commons.lang.StringUtils.trimToEmpty; +import static org.slf4j.LoggerFactory.getLogger; public class MqClient { - ActiveMQConnectionFactory connectionFactory; + private final Logger log = getLogger(getClass()); + + private final ActiveMQConnectionFactory connectionFactory; public MqClient(String brokerUrl) { this.connectionFactory = new ActiveMQConnectionFactory(brokerUrl); } - public void sendMessage(String repository, String groupId, String artifactId, String version, String type, String classifier) { + public void sendMessage(String repository, String groupId, String artifactId, String version, String classifier, String extension) { try { Connection connection = connectionFactory.createConnection(); connection.start(); @@ -32,20 +39,22 @@ public class MqClient { MessageProducer producer = session.createProducer(destination); producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT); - MapMessage message = session.createMapMessage(); - message.setString("repository", repository); - message.setString("groupId", groupId); - message.setString("artifactId", artifactId); - message.setString("version", version); - message.setString("type", type); - message.setString("classifier", classifier); + Properties p = new Properties(); + p.setProperty("repository", repository); + p.setProperty("groupId", groupId); + p.setProperty("artifactId", artifactId); + p.setProperty("version", version); + p.setProperty("classifier", trimToEmpty(classifier)); + p.setProperty("extension", extension); - producer.send(message); + CharArrayWriter buf = new CharArrayWriter(); + p.store(buf, null); + producer.send(session.createTextMessage(buf.toString())); session.close(); connection.close(); - } catch (JMSException e) { - e.printStackTrace(); + } catch (JMSException | IOException e) { + log.warn("Unable to send message to broker", e); } } } -- cgit v1.2.3