Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve classpath modules resolution #2360

Merged
merged 2 commits into from
Jan 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 23 additions & 8 deletions src/org/exist/source/SourceFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,9 @@ public class SourceFactory {
Source source = null;

/* resource: */
if (location.startsWith(ClassLoaderSource.PROTOCOL)) {
source = new ClassLoaderSource(location);
} else if (contextPath != null && contextPath.startsWith(ClassLoaderSource.PROTOCOL)) {
// Pretend it is a file on the local system so we can resolve it easily with URL() class.
final String conPathNoProtocol = contextPath.replace(ClassLoaderSource.PROTOCOL, "file://");
String resolvedURL = new URL(new URL(conPathNoProtocol), location).toString();
resolvedURL = resolvedURL.replaceFirst("file://", ClassLoaderSource.PROTOCOL);
source = new ClassLoaderSource(resolvedURL);
if (location.startsWith(ClassLoaderSource.PROTOCOL)
|| (contextPath != null && contextPath.startsWith(ClassLoaderSource.PROTOCOL))) {
source = getSource_fromClasspath(contextPath, location);
}

/* xmldb */
Expand Down Expand Up @@ -154,6 +149,26 @@ private static String firstPathSegment(final String path) {
.getRawCollectionPath();
}

private static Source getSource_fromClasspath(final String contextPath, final String location) throws IOException {
if (location.startsWith(ClassLoaderSource.PROTOCOL)) {
return new ClassLoaderSource(location);
}

final Path rootPath = Paths.get(contextPath.substring(ClassLoaderSource.PROTOCOL.length()));

// 1) try resolving location as child
final Path childLocation = rootPath.resolve(location);
try {
return new ClassLoaderSource(ClassLoaderSource.PROTOCOL + childLocation.toString().replace('\\', '/'));
} catch (final IOException e) {
// no-op, we will try again below
}

// 2) try resolving location as sibling
final Path siblingLocation = rootPath.resolveSibling(location);
return new ClassLoaderSource(ClassLoaderSource.PROTOCOL + siblingLocation.toString().replace('\\', '/'));
}

/**
* Get the resource source from the database.
*
Expand Down
47 changes: 47 additions & 0 deletions test/src/org/exist/source/SourceFactoryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,53 @@ public void getSourceFromResource_contextAbsoluteFileUrl_locationRelativeUrl_bas
assertEquals(getClass().getResource(location), relativeSource.getKey());
}

@Test
public void getSourceFromResource_contextFolderUrl_locationRelative() throws IOException, PermissionDeniedException {
final String contextPath = "resource:org/exist/source";
final String location = "library.xqm";

final Source source = SourceFactory.getSource(null, contextPath, location, false);

assertTrue(source instanceof ClassLoaderSource);
assertEquals(getClass().getResource("library.xqm"), source.getKey());
}

@Test
public void getSourceFromResource_contextFolderUrl_locationAbsoluteUrl() throws IOException, PermissionDeniedException {
final String contextPath = "resource:org/exist/source";
final String location = "resource:org/exist/source/library.xqm";

final Source source = SourceFactory.getSource(null, contextPath, location, false);

assertTrue(source instanceof ClassLoaderSource);
assertEquals(getClass().getResource("library.xqm"), source.getKey());
}

@Test
public void getSourceFromResource_contextFolderUrl_locationRelativeUrl() throws IOException, PermissionDeniedException {
final String contextPath = "resource:org/exist/source";
final String location = "library.xqm";

final Source source = SourceFactory.getSource(null, contextPath, location, false);

assertTrue(source instanceof ClassLoaderSource);
assertEquals(getClass().getResource("library.xqm"), source.getKey());
}

@Test
public void getSourceFromResource_contextFolderUrl_locationRelativeUrl_basedOnSource() throws IOException, PermissionDeniedException {
final String contextPath = "resource:org/exist/source";
final String location = "library.xqm";

final Source mainSource = SourceFactory.getSource(null, "", contextPath, false);
assertTrue(mainSource instanceof ClassLoaderSource);

final Source relativeSource = SourceFactory.getSource(null, ((ClassLoaderSource)mainSource).getSource(), location, false);

assertTrue(relativeSource instanceof ClassLoaderSource);
assertEquals(getClass().getResource(location), relativeSource.getKey());
}

@Test
public void getSourceFromXmldb_noContext() throws IOException, PermissionDeniedException {
final String contextPath = null;
Expand Down