SLING-8421: Allow artifact providers that work with URLs instead of Files
diff --git a/pom.xml b/pom.xml
index c1c4d7b..c61a85a 100644
--- a/pom.xml
+++ b/pom.xml
@@ -146,7 +146,7 @@
<dependency>
<groupId>org.apache.sling</groupId>
<artifactId>org.apache.sling.feature</artifactId>
- <version>1.0.2</version>
+ <version>1.0.3-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
diff --git a/src/main/java/org/apache/sling/feature/io/file/ArtifactHandler.java b/src/main/java/org/apache/sling/feature/io/artifacts/ArtifactHandler.java
similarity index 77%
rename from src/main/java/org/apache/sling/feature/io/file/ArtifactHandler.java
rename to src/main/java/org/apache/sling/feature/io/artifacts/ArtifactHandler.java
index e1d4ab0..044a8ef 100644
--- a/src/main/java/org/apache/sling/feature/io/file/ArtifactHandler.java
+++ b/src/main/java/org/apache/sling/feature/io/artifacts/ArtifactHandler.java
@@ -14,9 +14,9 @@
* License for the specific language governing permissions and limitations under
* the License.
*/
-package org.apache.sling.feature.io.file;
+package org.apache.sling.feature.io.artifacts;
-import java.io.File;
+import java.net.URL;
/**
* A handler provides a file object for an artifact.
@@ -25,17 +25,17 @@
private final String url;
- private final File file;
+ private final URL localURL;
/**
* Create a new handler.
*
* @param url The url of the artifact
- * @param file The file for the artifact
+ * @param localURL The local URL for the artifact
*/
- public ArtifactHandler(final String url, final File file) {
+ public ArtifactHandler(final String url, final URL localURL) {
this.url = url;
- this.file = file;
+ this.localURL = localURL;
}
/**
@@ -48,11 +48,11 @@
}
/**
- * Get a file for the artifact
+ * Get a local url for the artifact
*
* @return The file
*/
- public File getFile() {
- return file;
+ public URL getLocalURL() {
+ return localURL;
}
}
diff --git a/src/main/java/org/apache/sling/feature/io/file/ArtifactManager.java b/src/main/java/org/apache/sling/feature/io/artifacts/ArtifactManager.java
similarity index 91%
rename from src/main/java/org/apache/sling/feature/io/file/ArtifactManager.java
rename to src/main/java/org/apache/sling/feature/io/artifacts/ArtifactManager.java
index 0db5c51..5767c3b 100644
--- a/src/main/java/org/apache/sling/feature/io/file/ArtifactManager.java
+++ b/src/main/java/org/apache/sling/feature/io/artifacts/ArtifactManager.java
@@ -14,26 +14,27 @@
* License for the specific language governing permissions and limitations under
* the License.
*/
-package org.apache.sling.feature.io.file;
+package org.apache.sling.feature.io.artifacts;
+import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
+import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLConnection;
-import java.nio.file.Files;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;
import java.util.ServiceLoader;
import org.apache.sling.feature.ArtifactId;
-import org.apache.sling.feature.io.file.spi.ArtifactProvider;
-import org.apache.sling.feature.io.file.spi.ArtifactProviderContext;
+import org.apache.sling.feature.io.artifacts.spi.ArtifactProvider;
+import org.apache.sling.feature.io.artifacts.spi.ArtifactProviderContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -109,7 +110,7 @@
shutdown();
}
- private final File getArtifactFromProviders(final String url, final String relativeCachePath) throws IOException {
+ private final URL getArtifactFromProviders(final String url, final String relativeCachePath) throws IOException {
final int pos = url.indexOf(":");
final String scheme = url.substring(0, pos);
@@ -149,8 +150,8 @@
while ( url.charAt(pos) == '/') {
pos++;
}
- final File file = this.getArtifactFromProviders(url, url.substring(pos));
- if ( file == null || !file.exists()) {
+ final URL file = this.getArtifactFromProviders(url, url.substring(pos));
+ if ( file == null ) {
throw new IOException("Artifact " + url + " not found.");
}
return new ArtifactHandler(url, file);
@@ -161,7 +162,7 @@
if ( !f.exists()) {
throw new IOException("Artifact " + url + " not found.");
}
- return new ArtifactHandler(f.toURI().toString(), f);
+ return new ArtifactHandler(f.toURI().toString(), f.toURI().toURL());
}
logger.debug("Querying repositories for {}", path);
@@ -185,7 +186,7 @@
logger.debug("Checking {} to get artifact from {}", handler, artifactUrl);
- final File file = handler.getArtifact(artifactUrl, path);
+ final URL file = handler.getArtifact(artifactUrl, path);
if ( file != null ) {
logger.debug("Found artifact {}", artifactUrl);
return new ArtifactHandler(artifactUrl, file);
@@ -211,8 +212,8 @@
while ( fullURL.charAt(pos2) == '/') {
pos2++;
}
- final File file2 = this.getArtifactFromProviders(fullURL, path);
- if ( file2 == null || !file2.exists()) {
+ final URL file2 = this.getArtifactFromProviders(fullURL, path);
+ if ( file2 == null ) {
throw new IOException("Artifact " + fullURL + " not found.");
}
return new ArtifactHandler(artifactUrl, file2);
@@ -228,8 +229,10 @@
protected String getFileContents(final ArtifactHandler handler) throws IOException {
final StringBuilder sb = new StringBuilder();
- for(final String line : Files.readAllLines(handler.getFile().toPath())) {
- sb.append(line).append('\n');
+ try (BufferedReader reader = new BufferedReader(new InputStreamReader(handler.getLocalURL().openStream(), "UTF-8"))) {
+ for(String line = reader.readLine(); line != null; line = reader.readLine()) {
+ sb.append(line).append('\n');
+ }
}
return sb.toString();
@@ -296,14 +299,14 @@
}
@Override
- public File getArtifact(final String url, final String relativeCachePath) {
+ public URL getArtifact(final String url, final String relativeCachePath) {
logger.debug("Checking url to be local file {}", url);
// check if this is already a local file
try {
final File f = new File(new URL(url).toURI());
if ( f.exists() ) {
this.config.incLocalArtifacts();
- return f;
+ return f.toURI().toURL();
}
return null;
} catch ( final URISyntaxException ise) {
@@ -361,7 +364,7 @@
} else {
this.config.incCachedArtifacts();
}
- return cacheFile;
+ return cacheFile.toURI().toURL();
} catch ( final Exception e) {
logger.info("Artifact not found in one repository", e);
// ignore for now
diff --git a/src/main/java/org/apache/sling/feature/io/file/ArtifactManagerConfig.java b/src/main/java/org/apache/sling/feature/io/artifacts/ArtifactManagerConfig.java
similarity index 96%
rename from src/main/java/org/apache/sling/feature/io/file/ArtifactManagerConfig.java
rename to src/main/java/org/apache/sling/feature/io/artifacts/ArtifactManagerConfig.java
index d3c8bea..d03c7e3 100644
--- a/src/main/java/org/apache/sling/feature/io/file/ArtifactManagerConfig.java
+++ b/src/main/java/org/apache/sling/feature/io/artifacts/ArtifactManagerConfig.java
@@ -14,13 +14,13 @@
* License for the specific language governing permissions and limitations under
* the License.
*/
-package org.apache.sling.feature.io.file;
+package org.apache.sling.feature.io.artifacts;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
-import org.apache.sling.feature.io.file.spi.ArtifactProviderContext;
+import org.apache.sling.feature.io.artifacts.spi.ArtifactProviderContext;
/**
* This class holds the configuration of artifact manager.
diff --git a/src/main/java/org/apache/sling/feature/io/file/package-info.java b/src/main/java/org/apache/sling/feature/io/artifacts/package-info.java
similarity index 94%
rename from src/main/java/org/apache/sling/feature/io/file/package-info.java
rename to src/main/java/org/apache/sling/feature/io/artifacts/package-info.java
index 83cd885..08e8d2c 100644
--- a/src/main/java/org/apache/sling/feature/io/file/package-info.java
+++ b/src/main/java/org/apache/sling/feature/io/artifacts/package-info.java
@@ -18,6 +18,6 @@
*/
@org.osgi.annotation.versioning.Version("1.0.0")
-package org.apache.sling.feature.io.file;
+package org.apache.sling.feature.io.artifacts;
diff --git a/src/main/java/org/apache/sling/feature/io/file/spi/ArtifactProvider.java b/src/main/java/org/apache/sling/feature/io/artifacts/spi/ArtifactProvider.java
similarity index 89%
rename from src/main/java/org/apache/sling/feature/io/file/spi/ArtifactProvider.java
rename to src/main/java/org/apache/sling/feature/io/artifacts/spi/ArtifactProvider.java
index c238946..8b27ba5 100644
--- a/src/main/java/org/apache/sling/feature/io/file/spi/ArtifactProvider.java
+++ b/src/main/java/org/apache/sling/feature/io/artifacts/spi/ArtifactProvider.java
@@ -14,10 +14,11 @@
* License for the specific language governing permissions and limitations under
* the License.
*/
-package org.apache.sling.feature.io.file.spi;
+package org.apache.sling.feature.io.artifacts.spi;
import java.io.File;
import java.io.IOException;
+import java.net.URL;
import org.osgi.annotation.versioning.ConsumerType;
@@ -52,7 +53,7 @@
* @param url Artifact url
* @param relativeCachePath A relative path that can be used as a cache path
* by the provider. The path does not start with a slash.
- * @return A file if the artifact exists or {@code null}
+ * @return A local url if the artifact exists or {@code null}
*/
- File getArtifact(String url, String relativeCachePath);
+ URL getArtifact(String url, String relativeCachePath);
}
diff --git a/src/main/java/org/apache/sling/feature/io/file/spi/ArtifactProviderContext.java b/src/main/java/org/apache/sling/feature/io/artifacts/spi/ArtifactProviderContext.java
similarity index 96%
rename from src/main/java/org/apache/sling/feature/io/file/spi/ArtifactProviderContext.java
rename to src/main/java/org/apache/sling/feature/io/artifacts/spi/ArtifactProviderContext.java
index 02a9722..dccfbfc 100644
--- a/src/main/java/org/apache/sling/feature/io/file/spi/ArtifactProviderContext.java
+++ b/src/main/java/org/apache/sling/feature/io/artifacts/spi/ArtifactProviderContext.java
@@ -14,7 +14,7 @@
* License for the specific language governing permissions and limitations under
* the License.
*/
-package org.apache.sling.feature.io.file.spi;
+package org.apache.sling.feature.io.artifacts.spi;
import java.io.File;
diff --git a/src/main/java/org/apache/sling/feature/io/file/package-info.java b/src/main/java/org/apache/sling/feature/io/artifacts/spi/package-info.java
similarity index 94%
copy from src/main/java/org/apache/sling/feature/io/file/package-info.java
copy to src/main/java/org/apache/sling/feature/io/artifacts/spi/package-info.java
index 83cd885..bb0c1f0 100644
--- a/src/main/java/org/apache/sling/feature/io/file/package-info.java
+++ b/src/main/java/org/apache/sling/feature/io/artifacts/spi/package-info.java
@@ -18,6 +18,6 @@
*/
@org.osgi.annotation.versioning.Version("1.0.0")
-package org.apache.sling.feature.io.file;
+package org.apache.sling.feature.io.artifacts.spi;
diff --git a/src/main/java/org/apache/sling/feature/io/file/spi/package-info.java b/src/main/java/org/apache/sling/feature/io/file/spi/package-info.java
deleted file mode 100644
index eef08c6..0000000
--- a/src/main/java/org/apache/sling/feature/io/file/spi/package-info.java
+++ /dev/null
@@ -1,23 +0,0 @@
-/*
- * 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.
- */
-
-@org.osgi.annotation.versioning.Version("1.0.0")
-package org.apache.sling.feature.io.file.spi;
-
-
diff --git a/src/test/java/org/apache/sling/feature/io/file/ArtifactManagerTest.java b/src/test/java/org/apache/sling/feature/io/artifacts/ArtifactManagerTest.java
similarity index 85%
rename from src/test/java/org/apache/sling/feature/io/file/ArtifactManagerTest.java
rename to src/test/java/org/apache/sling/feature/io/artifacts/ArtifactManagerTest.java
index 69d9d32..5894734 100644
--- a/src/test/java/org/apache/sling/feature/io/file/ArtifactManagerTest.java
+++ b/src/test/java/org/apache/sling/feature/io/artifacts/ArtifactManagerTest.java
@@ -14,22 +14,22 @@
* License for the specific language governing permissions and limitations under
* the License.
*/
-package org.apache.sling.feature.io.file;
+package org.apache.sling.feature.io.artifacts;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
-import java.io.File;
import java.io.IOException;
+import java.net.URL;
import java.util.HashMap;
import java.util.Map;
-import org.apache.sling.feature.io.file.ArtifactHandler;
-import org.apache.sling.feature.io.file.ArtifactManager;
-import org.apache.sling.feature.io.file.ArtifactManagerConfig;
-import org.apache.sling.feature.io.file.spi.ArtifactProvider;
+import org.apache.sling.feature.io.artifacts.ArtifactHandler;
+import org.apache.sling.feature.io.artifacts.ArtifactManager;
+import org.apache.sling.feature.io.artifacts.ArtifactManagerConfig;
+import org.apache.sling.feature.io.artifacts.spi.ArtifactProvider;
import org.junit.Test;
public class ArtifactManagerTest {
@@ -68,12 +68,9 @@
final ArtifactManagerConfig config = mock(ArtifactManagerConfig.class);
when(config.getRepositoryUrls()).thenReturn(new String[] {REPO});
- final File metadataFile = mock(File.class);
- when(metadataFile.exists()).thenReturn(true);
- when(metadataFile.getPath()).thenReturn("/maven-metadata.xml");
+ final URL metadataFile = new URL("file:/maven-metadata.xml");
- final File artifactFile = mock(File.class);
- when(artifactFile.exists()).thenReturn(true);
+ final URL artifactFile = new URL("file:/artifact");
final ArtifactProvider provider = mock(ArtifactProvider.class);
when(provider.getArtifact(REPO + "/group/artifact/1.0.0-SNAPSHOT/artifact-1.0.0-SNAPSHOT.txt", "group/artifact/1.0.0-SNAPSHOT/artifact-1.0.0-SNAPSHOT.txt")).thenReturn(null);
@@ -87,7 +84,7 @@
@Override
protected String getFileContents(final ArtifactHandler handler) throws IOException {
- final String path = handler.getFile().getPath();
+ final String path = handler.getLocalURL().getPath();
if ( "/maven-metadata.xml".equals(path) ) {
return METADATA;
}
@@ -97,6 +94,6 @@
final ArtifactHandler handler = mgr.getArtifactHandler("mvn:group/artifact/1.0.0-SNAPSHOT/txt");
assertNotNull(handler);
- assertEquals(artifactFile, handler.getFile());
+ assertEquals(artifactFile, handler.getLocalURL());
}
}