Skip to content

Commit

Permalink
[resolver] Added a URI resolver interface
Browse files Browse the repository at this point in the history
Sometimes the standard template or default URL calculation is not sufficient. When a URI resolver is set, it takes over and it can calculate any URI based on the file under consideration.

Signed-off-by: Peter Kriens <peter.kriens@aqute.biz>
  • Loading branch information
pkriens committed Jul 10, 2015
1 parent 3d45e3c commit c7f8b88
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 5 deletions.
Expand Up @@ -3,6 +3,7 @@
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
Expand Down Expand Up @@ -189,6 +190,26 @@ private String calculateLocation(Resource resource) throws IOException {

GeneratorState state = getStateLocal();
if (state != null) {

//
// Check if a resolver was specified
// If so, we give that preference over the automatic
// and template calculation
//

URLResolver resolver = state.getResolver();
if (resolver != null) {
try {
URI uri = resolver.resolver(path);
if (uri != null)
return uri.toString();
}
catch (Exception e) {
if (log != null)
log.log(LogService.LOG_ERROR, "Resolver failed on " + path + ", falling back to old method");
}
}

String rootUrl = state.getRootUrl().toString();
if (!rootUrl.endsWith("/"))
rootUrl += "/";
Expand Down
Expand Up @@ -6,10 +6,12 @@ class GeneratorState {

private final URL rootUrl;
private final String urlTemplate;
private URLResolver resolver;

public GeneratorState(URL rootUrl, String urlTemplate) {
public GeneratorState(URL rootUrl, String urlTemplate, URLResolver resolver) {
this.rootUrl = rootUrl;
this.urlTemplate = urlTemplate;
this.resolver = resolver;
}

public URL getRootUrl() {
Expand Down Expand Up @@ -56,4 +58,7 @@ public String toString() {
return "GeneratorState [rootUrl=" + rootUrl + ", urlTemplate=" + urlTemplate + "]";
}

public URLResolver getResolver() {
return resolver;
}
}
Expand Up @@ -66,6 +66,8 @@ public class RepoIndex implements ResourceIndexer {
*/
private final List<Pair<ResourceAnalyzer, Filter>> analyzers = new LinkedList<Pair<ResourceAnalyzer, Filter>>();

private URLResolver resolver;

/**
* Construct a default instance that uses a console logger.
*/
Expand Down Expand Up @@ -249,7 +251,7 @@ private Tag generateResource(File file, Map<String, String> config) throws Excep
rootURL = new File(System.getProperty("user.dir")).toURI().toURL();

String urlTemplate = config.get(ResourceIndexer.URL_TEMPLATE);
bundleAnalyzer.setStateLocal(new GeneratorState(rootURL, urlTemplate));
bundleAnalyzer.setStateLocal(new GeneratorState(rootURL, urlTemplate, resolver));
} else {
bundleAnalyzer.setStateLocal(null);
}
Expand Down Expand Up @@ -363,4 +365,13 @@ private long getSignature() {
}
return value;
}

/**
* Set a URL resolver that calculates the reference to the file
*/

public void setURLResolver(URLResolver resolver) {
this.resolver = resolver;
}

}
@@ -0,0 +1,22 @@
package org.osgi.service.indexer.impl;

import java.io.File;
import java.net.URI;

/**
* Override the calculation of the URL with a specific function.
*/
public interface URLResolver {

/**
* Calculate the URL for the given artifact. If this returns null or throws
* an exception, the automatic calculation will be used. Exceptions are
* logged so should not be used for flow control.
*
* @param artifact
* The artifact being analyzed
* @return Either a URI to be used in the content capability or null if the
* default method should be used
*/
URI resolver(File artifact) throws Exception;
}
Expand Up @@ -11,14 +11,18 @@
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.StringWriter;
import java.net.URI;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import junit.framework.TestCase;

Expand All @@ -34,6 +38,66 @@

public class TestIndexer extends TestCase {

/**
* Test if the resolver is not affected if we use a dummy resolver
*/
public void testResolverUnaffected() throws Exception {

RepoIndex indexer = new RepoIndex();
String without = index(indexer);

indexer = new RepoIndex();
indexer.setURLResolver(new URLResolver() {

@Override
public URI resolver(File artifact) throws Exception {
return null;
}
});

String with = index(indexer);

assertEquals("Should be the same without a resolver or a resolver that returns null", without, with);

indexer = new RepoIndex();
indexer.setURLResolver(new URLResolver() {

@Override
public URI resolver(File artifact) throws Exception {
throw new Exception();
}
});

with = index(indexer);
assertEquals("Should be the same without a resolver or a resolver that returns null", without, with);
}

/**
* Test if the resolver can change the URLs
*
*/
public void testResolverEffect() throws Exception {

RepoIndex indexer = new RepoIndex();
indexer.setURLResolver(new URLResolver() {

@Override
public URI resolver(File artifact) throws Exception {
return new URI("xyz://FOOBAR/" + artifact.getName());
}
});

String with = index(indexer);

Pattern p = Pattern.compile("xyz://FOOBAR/(03-export.jar|06-requirebundle.jar)(?:\"|')");
Matcher m = p.matcher(with);
int n = 0;
while (m.find()) {
n++;
}
assertEquals("There are two files indexed", 2, n);
}

public void testFragmentBsnVersion() throws Exception {
assertFragmentMatch("testdata/fragment-01.txt", "testdata/01-bsn+version.jar");
}
Expand Down Expand Up @@ -146,6 +210,14 @@ public void testEmptyIndex() throws Exception {
public void testFullIndex() throws Exception {
RepoIndex indexer = new RepoIndex();

String decompressed = index(indexer);

String unpackedXML = Utils.readStream(new FileInputStream("testdata/unpacked.xml"));
String expected = unpackedXML.replaceAll("\\r?\\n|\\t", "");
assertEquals(expected, decompressed);
}

private String index(RepoIndex indexer) throws Exception, IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
Set<File> files = new LinkedHashSet<File>();
files.add(new File("testdata/03-export.jar"));
Expand All @@ -156,9 +228,8 @@ public void testFullIndex() throws Exception {
config.put(ResourceIndexer.REPOSITORY_NAME, "full-c+f");
indexer.index(files, out, config);

String unpackedXML = Utils.readStream(new FileInputStream("testdata/unpacked.xml"));
String expected = unpackedXML.replaceAll("\\r?\\n|\\t", "");
assertEquals(expected, Utils.decompress(out.toByteArray()));
String decompressed = Utils.decompress(out.toByteArray());
return decompressed;
}

public void testFullIndexPrettyCompressedPermutations() throws Exception {
Expand Down

0 comments on commit c7f8b88

Please sign in to comment.