Repository: camel Updated Branches: refs/heads/master bb25ff806 -> 39926008f
CAMEL-10828: camel-catalog-nexus - Initial work Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/fbbe4b4c Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/fbbe4b4c Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/fbbe4b4c Branch: refs/heads/master Commit: fbbe4b4cc4cce218c86c9a2c5c067c3a6584cf70 Parents: b4f9c7e Author: Claus Ibsen <[email protected]> Authored: Tue Feb 14 15:44:35 2017 +0100 Committer: Claus Ibsen <[email protected]> Committed: Tue Feb 14 19:11:59 2017 +0100 ---------------------------------------------------------------------- platforms/catalog-nexus/pom.xml | 1 - .../catalog/nexus/BaseNexusRepository.java | 54 ++++++++++++-------- .../catalog/nexus/LocalFileNexusRepository.java | 31 +++++++++++ .../catalog/nexus/LocalNexusRepositoryTest.java | 53 +++++++++++++++++++ .../src/test/resources/log4j2.properties | 28 ++++++++++ .../src/test/resources/nexus-sample-result.xml | 37 ++++++++++++++ 6 files changed, 183 insertions(+), 21 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/fbbe4b4c/platforms/catalog-nexus/pom.xml ---------------------------------------------------------------------- diff --git a/platforms/catalog-nexus/pom.xml b/platforms/catalog-nexus/pom.xml index 97d7d61..e3bfe15 100644 --- a/platforms/catalog-nexus/pom.xml +++ b/platforms/catalog-nexus/pom.xml @@ -75,7 +75,6 @@ <scope>test</scope> </dependency> - </dependencies> <build> http://git-wip-us.apache.org/repos/asf/camel/blob/fbbe4b4c/platforms/catalog-nexus/src/main/java/org/apache/camel/catalog/nexus/BaseNexusRepository.java ---------------------------------------------------------------------- diff --git a/platforms/catalog-nexus/src/main/java/org/apache/camel/catalog/nexus/BaseNexusRepository.java b/platforms/catalog-nexus/src/main/java/org/apache/camel/catalog/nexus/BaseNexusRepository.java index 77cf43f..25b7b07 100644 --- a/platforms/catalog-nexus/src/main/java/org/apache/camel/catalog/nexus/BaseNexusRepository.java +++ b/platforms/catalog-nexus/src/main/java/org/apache/camel/catalog/nexus/BaseNexusRepository.java @@ -19,6 +19,7 @@ package org.apache.camel.catalog.nexus; import java.io.Closeable; import java.io.IOException; import java.io.InputStream; +import java.net.MalformedURLException; import java.net.URL; import java.util.LinkedHashSet; import java.util.Set; @@ -49,7 +50,8 @@ public abstract class BaseNexusRepository { private AtomicBoolean started = new AtomicBoolean(); private CamelCatalog camelCatalog; - private Long delay = 60L; // use 60 second delay between index runs + private int initialDelay = 10; + private int delay = 60; private String nexusUrl = "http://nexus/service/local/data_index"; private String classifier; @@ -77,14 +79,25 @@ public abstract class BaseNexusRepository { this.nexusUrl = nexusUrl; } - public Long getDelay() { + public int getInitialDelay() { + return initialDelay; + } + + /** + * Delay in seconds before the initial (first) scan. + */ + public void setInitialDelay(int initialDelay) { + this.initialDelay = initialDelay; + } + + public int getDelay() { return delay; } /** * Delay in seconds between scanning. */ - public void setDelay(Long delay) { + public void setDelay(int delay) { this.delay = delay; } @@ -107,19 +120,18 @@ public abstract class BaseNexusRepository { throw new IllegalArgumentException("CamelCatalog must be configured"); } - if (started.compareAndSet(false, true)) { - log.info("NexusRepository is already started"); + if (nexusUrl == null || nexusUrl.isEmpty()) { + log.warn("Nexus service not found. Indexing Nexus is not enabled!"); return; } - log.info("Starting NexusRepository"); - - if (nexusUrl == null || nexusUrl.isEmpty()) { - log.warn("Nexus service not found. Indexing Nexus is not enabled!"); + if (!started.compareAndSet(false, true)) { + log.info("NexusRepository is already started"); return; } - log.info("Indexing Nexus every {} seconds interval", delay); + log.info("Starting NexusRepository to scan every {} seconds", delay); + executorService = Executors.newScheduledThreadPool(1); executorService.scheduleWithFixedDelay(() -> { @@ -136,7 +148,7 @@ public abstract class BaseNexusRepository { } finally { log.debug("Indexing Nexus {} +++ end +++", nexusUrl); } - }, 10, delay, TimeUnit.SECONDS); + }, initialDelay, delay, TimeUnit.SECONDS); } /** @@ -158,13 +170,16 @@ public abstract class BaseNexusRepository { */ abstract void onNewArtifacts(Set<NexusArtifactDto> newArtifacts); + protected URL createNexusUrl() throws MalformedURLException { + String query = nexusUrl + "?q=" + getClassifier(); + return new URL(query); + } + /** * Runs the task to index nexus for new artifacts */ protected void indexNexus() throws Exception { // must have q parameter so use component to find all component - String query = nexusUrl + "?q=" + getClassifier(); - URL url = new URL(query); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true); @@ -173,24 +188,23 @@ public abstract class BaseNexusRepository { DocumentBuilder documentBuilder = factory.newDocumentBuilder(); + URL url = createNexusUrl(); InputStream is = url.openStream(); try { Document dom = documentBuilder.parse(is); XPathFactory xpFactory = XPathFactory.newInstance(); XPath exp = xpFactory.newXPath(); - // TODO: we dont have classifier for components - NodeList list = (NodeList) exp.evaluate("//classifier[text() = '" + getClassifier() + "']", dom, XPathConstants.NODESET); + NodeList list = (NodeList) exp.evaluate("//data/artifact", dom, XPathConstants.NODESET); Set<NexusArtifactDto> newArtifacts = new LinkedHashSet<>(); for (int i = 0; i < list.getLength(); i++) { Node node = list.item(i); - Node parent = node.getParentNode(); - String g = getNodeText(parent.getChildNodes(), "groupId"); - String a = getNodeText(parent.getChildNodes(), "artifactId"); - String v = getNodeText(parent.getChildNodes(), "version"); - String l = getNodeText(parent.getChildNodes(), "artifactLink"); + String g = getNodeText(node.getChildNodes(), "groupId"); + String a = getNodeText(node.getChildNodes(), "artifactId"); + String v = getNodeText(node.getChildNodes(), "version"); + String l = getNodeText(node.getChildNodes(), "artifactLink"); if (g != null & a != null & v != null & l != null) { NexusArtifactDto dto = new NexusArtifactDto(); http://git-wip-us.apache.org/repos/asf/camel/blob/fbbe4b4c/platforms/catalog-nexus/src/test/java/org/apache/camel/catalog/nexus/LocalFileNexusRepository.java ---------------------------------------------------------------------- diff --git a/platforms/catalog-nexus/src/test/java/org/apache/camel/catalog/nexus/LocalFileNexusRepository.java b/platforms/catalog-nexus/src/test/java/org/apache/camel/catalog/nexus/LocalFileNexusRepository.java new file mode 100644 index 0000000..dd10306 --- /dev/null +++ b/platforms/catalog-nexus/src/test/java/org/apache/camel/catalog/nexus/LocalFileNexusRepository.java @@ -0,0 +1,31 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * <p> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p> + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.catalog.nexus; + +import java.io.File; +import java.net.MalformedURLException; +import java.net.URL; + +public class LocalFileNexusRepository extends ComponentNexusRepository { + + @Override + protected URL createNexusUrl() throws MalformedURLException { + File file = new File("src/test/resources/nexus-sample-result.xml"); + return new URL("file:" + file.getAbsolutePath()); + } + +} http://git-wip-us.apache.org/repos/asf/camel/blob/fbbe4b4c/platforms/catalog-nexus/src/test/java/org/apache/camel/catalog/nexus/LocalNexusRepositoryTest.java ---------------------------------------------------------------------- diff --git a/platforms/catalog-nexus/src/test/java/org/apache/camel/catalog/nexus/LocalNexusRepositoryTest.java b/platforms/catalog-nexus/src/test/java/org/apache/camel/catalog/nexus/LocalNexusRepositoryTest.java new file mode 100644 index 0000000..7f7d7c5 --- /dev/null +++ b/platforms/catalog-nexus/src/test/java/org/apache/camel/catalog/nexus/LocalNexusRepositoryTest.java @@ -0,0 +1,53 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * <p> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p> + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.catalog.nexus; + +import junit.framework.TestCase; +import org.apache.camel.catalog.CamelCatalog; +import org.apache.camel.catalog.DefaultCamelCatalog; +import org.junit.Test; + +public class LocalNexusRepositoryTest extends TestCase { + + private LocalFileNexusRepository repo = new LocalFileNexusRepository(); + private CamelCatalog catalog = new DefaultCamelCatalog(); + + @Override + protected void setUp() throws Exception { + super.setUp(); + + repo.setCamelCatalog(catalog); + repo.setInitialDelay(1); + repo.setNexusUrl("dummy"); + } + + @Test + public void testLocalNexus() throws Exception { + int before = catalog.findComponentNames().size(); + + repo.start(); + + // TODO: wait 5 sec + Thread.sleep(5000); + + repo.stop(); + + int after = catalog.findComponentNames().size(); + + assertTrue("There should be 1 component found", after - before == 1); + } +} http://git-wip-us.apache.org/repos/asf/camel/blob/fbbe4b4c/platforms/catalog-nexus/src/test/resources/log4j2.properties ---------------------------------------------------------------------- diff --git a/platforms/catalog-nexus/src/test/resources/log4j2.properties b/platforms/catalog-nexus/src/test/resources/log4j2.properties new file mode 100644 index 0000000..775c567 --- /dev/null +++ b/platforms/catalog-nexus/src/test/resources/log4j2.properties @@ -0,0 +1,28 @@ +## --------------------------------------------------------------------------- +## Licensed to the Apache Software Foundation (ASF) under one or more +## contributor license agreements. See the NOTICE file distributed with +## this work for additional information regarding copyright ownership. +## The ASF licenses this file to You under the Apache License, Version 2.0 +## (the "License"); you may not use this file except in compliance with +## the License. You may obtain a copy of the License at +## +## http://www.apache.org/licenses/LICENSE-2.0 +## +## Unless required by applicable law or agreed to in writing, software +## distributed under the License is distributed on an "AS IS" BASIS, +## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +## See the License for the specific language governing permissions and +## limitations under the License. +## --------------------------------------------------------------------------- + +appender.file.type = File +appender.file.name = file +appender.file.fileName = target/camel-catalog-nexus-test.log +appender.file.layout.type = PatternLayout +appender.file.layout.pattern = %d [%-15.15t] %-5p %-30.30c{1} - %m%n +appender.out.type = Console +appender.out.name = out +appender.out.layout.type = PatternLayout +appender.out.layout.pattern = %d [%-15.15t] %-5p %-30.30c{1} - %m%n +rootLogger.level = INFO +rootLogger.appenderRef.file.ref = file http://git-wip-us.apache.org/repos/asf/camel/blob/fbbe4b4c/platforms/catalog-nexus/src/test/resources/nexus-sample-result.xml ---------------------------------------------------------------------- diff --git a/platforms/catalog-nexus/src/test/resources/nexus-sample-result.xml b/platforms/catalog-nexus/src/test/resources/nexus-sample-result.xml new file mode 100644 index 0000000..16b60b0 --- /dev/null +++ b/platforms/catalog-nexus/src/test/resources/nexus-sample-result.xml @@ -0,0 +1,37 @@ +<!-- + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +--> +<search-results> + <totalCount>1</totalCount> + <from>-1</from> + <count>-1</count> + <tooManyResults>false</tooManyResults> + <data> + <artifact> + <resourceURI>http://nexus.dummy/service/local/repositories/staging/content/org/foo/beverage-component/2.19.0/beverage-component-2.19.0.jar</resourceURI> + <groupId>org.foo</groupId> + <artifactId>beverage-component</artifactId> + <version>2.19.0</version> + <packaging>jar</packaging> + <extension>jar</extension> + <repoId>staging</repoId> + <contextId>Staging</contextId> + <pomLink>http://nexus.dummy/service/local/artifact/maven/redirect?r=staging&g=org.foo&a=beverage-component&v=2.19.0&e=pom</pomLink> + <artifactLink>http://nexus.dummy/service/local/artifact/maven/redirect?r=staging&g=org.foo&a=beverage-component&v=2.19.0&e=jar</artifactLink> + <highlightedFragment><blockquote>Artifact ID<UL><LI>beverage-<B>component</B></LI></UL></blockquote></highlightedFragment> + </artifact> + </data> +</search-results> \ No newline at end of file
