Skip to content

Commit 80ce9ac

Browse files
authored
Merge pull request #311 from scijava/uri-to-location
Add LocationResolver framework
2 parents c5b6733 + e3babc8 commit 80ce9ac

File tree

8 files changed

+482
-0
lines changed

8 files changed

+482
-0
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* #%L
3+
* SciJava Common shared library for SciJava software.
4+
* %%
5+
* Copyright (C) 2009 - 2017 Board of Regents of the University of
6+
* Wisconsin-Madison, Broad Institute of MIT and Harvard, Max Planck
7+
* Institute of Molecular Cell Biology and Genetics, University of
8+
* Konstanz, and KNIME GmbH.
9+
* %%
10+
* Redistribution and use in source and binary forms, with or without
11+
* modification, are permitted provided that the following conditions are met:
12+
*
13+
* 1. Redistributions of source code must retain the above copyright notice,
14+
* this list of conditions and the following disclaimer.
15+
* 2. Redistributions in binary form must reproduce the above copyright notice,
16+
* this list of conditions and the following disclaimer in the documentation
17+
* and/or other materials provided with the distribution.
18+
*
19+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
23+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29+
* POSSIBILITY OF SUCH DAMAGE.
30+
* #L%
31+
*/
32+
33+
package org.scijava.io.location;
34+
35+
import java.net.URI;
36+
37+
import org.scijava.plugin.AbstractHandlerPlugin;
38+
39+
/**
40+
* Abstract super class for {@link LocationResolver} plugins.
41+
*
42+
* @author Gabriel Einsdorf
43+
*/
44+
public abstract class AbstractLocationResolver extends
45+
AbstractHandlerPlugin<URI> implements LocationResolver
46+
{
47+
48+
private final String[] schemes;
49+
50+
/**
51+
* @param schemes the uri schmemes that the implementing sub-type supports
52+
*/
53+
public AbstractLocationResolver(String... schemes) {
54+
assert schemes.length > 0;
55+
this.schemes = schemes;
56+
}
57+
58+
@Override
59+
public boolean supports(URI uri) {
60+
boolean supports = false;
61+
for (final String scheme : schemes) {
62+
supports = supports || scheme.equals(uri.getScheme());
63+
}
64+
return supports;
65+
}
66+
67+
@Override
68+
public Class<URI> getType() {
69+
return URI.class;
70+
}
71+
72+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* #%L
3+
* SciJava Common shared library for SciJava software.
4+
* %%
5+
* Copyright (C) 2009 - 2017 Board of Regents of the University of
6+
* Wisconsin-Madison, Broad Institute of MIT and Harvard, Max Planck
7+
* Institute of Molecular Cell Biology and Genetics, University of
8+
* Konstanz, and KNIME GmbH.
9+
* %%
10+
* Redistribution and use in source and binary forms, with or without
11+
* modification, are permitted provided that the following conditions are met:
12+
*
13+
* 1. Redistributions of source code must retain the above copyright notice,
14+
* this list of conditions and the following disclaimer.
15+
* 2. Redistributions in binary form must reproduce the above copyright notice,
16+
* this list of conditions and the following disclaimer in the documentation
17+
* and/or other materials provided with the distribution.
18+
*
19+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
23+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29+
* POSSIBILITY OF SUCH DAMAGE.
30+
* #L%
31+
*/
32+
33+
package org.scijava.io.location;
34+
35+
import java.net.URI;
36+
import java.util.HashMap;
37+
import java.util.Map;
38+
39+
import org.scijava.plugin.AbstractHandlerService;
40+
import org.scijava.plugin.Plugin;
41+
import org.scijava.service.Service;
42+
43+
/**
44+
* Default {@link LocationService} implementation.
45+
*
46+
* @author Gabriel Einsdorf
47+
*/
48+
@Plugin(type = Service.class)
49+
public class DefaultLocationService extends
50+
AbstractHandlerService<URI, LocationResolver> implements
51+
LocationService
52+
{
53+
54+
private final Map<String, LocationResolver> resolvers = new HashMap<>();
55+
56+
@Override
57+
public LocationResolver getResolver(final URI uri) {
58+
return resolvers.computeIfAbsent(uri.getScheme(), u -> getHandler(uri));
59+
}
60+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* #%L
3+
* SciJava Common shared library for SciJava software.
4+
* %%
5+
* Copyright (C) 2009 - 2017 Board of Regents of the University of
6+
* Wisconsin-Madison, Broad Institute of MIT and Harvard, Max Planck
7+
* Institute of Molecular Cell Biology and Genetics, University of
8+
* Konstanz, and KNIME GmbH.
9+
* %%
10+
* Redistribution and use in source and binary forms, with or without
11+
* modification, are permitted provided that the following conditions are met:
12+
*
13+
* 1. Redistributions of source code must retain the above copyright notice,
14+
* this list of conditions and the following disclaimer.
15+
* 2. Redistributions in binary form must reproduce the above copyright notice,
16+
* this list of conditions and the following disclaimer in the documentation
17+
* and/or other materials provided with the distribution.
18+
*
19+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
23+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29+
* POSSIBILITY OF SUCH DAMAGE.
30+
* #L%
31+
*/
32+
33+
package org.scijava.io.location;
34+
35+
import java.net.URI;
36+
37+
import org.scijava.plugin.Plugin;
38+
39+
/**
40+
* Implementation of {@link LocationResolver} for {@link FileLocation}.
41+
*
42+
* @author Gabriel Einsdorf
43+
*/
44+
@Plugin(type = LocationResolver.class)
45+
public class FileLocationResolver extends AbstractLocationResolver {
46+
47+
public FileLocationResolver() {
48+
super("file");
49+
}
50+
51+
@Override
52+
public Location resolve(URI uri) {
53+
return new FileLocation(uri);
54+
}
55+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* #%L
3+
* SciJava Common shared library for SciJava software.
4+
* %%
5+
* Copyright (C) 2009 - 2017 Board of Regents of the University of
6+
* Wisconsin-Madison, Broad Institute of MIT and Harvard, Max Planck
7+
* Institute of Molecular Cell Biology and Genetics, University of
8+
* Konstanz, and KNIME GmbH.
9+
* %%
10+
* Redistribution and use in source and binary forms, with or without
11+
* modification, are permitted provided that the following conditions are met:
12+
*
13+
* 1. Redistributions of source code must retain the above copyright notice,
14+
* this list of conditions and the following disclaimer.
15+
* 2. Redistributions in binary form must reproduce the above copyright notice,
16+
* this list of conditions and the following disclaimer in the documentation
17+
* and/or other materials provided with the distribution.
18+
*
19+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
23+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29+
* POSSIBILITY OF SUCH DAMAGE.
30+
* #L%
31+
*/
32+
33+
package org.scijava.io.location;
34+
35+
import java.net.URI;
36+
import java.net.URISyntaxException;
37+
38+
import org.scijava.plugin.HandlerPlugin;
39+
40+
/**
41+
* {@link LocationResolver} plugins allow resolving an {@link URI} to a
42+
* {@link Location}. Extending {@link AbstractLocationResolver} is recommended
43+
* for easy implementation.
44+
*
45+
* @author Gabriel Einsdorf
46+
*/
47+
public interface LocationResolver extends HandlerPlugin<URI> {
48+
49+
/**
50+
* Resolves the given {@link URI} to a {@link Location}
51+
*
52+
* @return the resolved Location
53+
* @throws URISyntaxException
54+
*/
55+
Location resolve(URI uri) throws URISyntaxException;
56+
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/*
2+
* #%L
3+
* SciJava Common shared library for SciJava software.
4+
* %%
5+
* Copyright (C) 2009 - 2017 Board of Regents of the University of
6+
* Wisconsin-Madison, Broad Institute of MIT and Harvard, Max Planck
7+
* Institute of Molecular Cell Biology and Genetics, University of
8+
* Konstanz, and KNIME GmbH.
9+
* %%
10+
* Redistribution and use in source and binary forms, with or without
11+
* modification, are permitted provided that the following conditions are met:
12+
*
13+
* 1. Redistributions of source code must retain the above copyright notice,
14+
* this list of conditions and the following disclaimer.
15+
* 2. Redistributions in binary form must reproduce the above copyright notice,
16+
* this list of conditions and the following disclaimer in the documentation
17+
* and/or other materials provided with the distribution.
18+
*
19+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
23+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29+
* POSSIBILITY OF SUCH DAMAGE.
30+
* #L%
31+
*/
32+
33+
package org.scijava.io.location;
34+
35+
import java.net.URI;
36+
import java.net.URISyntaxException;
37+
38+
import org.scijava.plugin.HandlerService;
39+
import org.scijava.service.SciJavaService;
40+
41+
/**
42+
* A service that allows resolving of URIs to Locations, using
43+
* {@link LocationResolver} plugins for translation.
44+
*
45+
* @author Gabriel Einsdorf
46+
*/
47+
public interface LocationService extends HandlerService<URI, LocationResolver>,
48+
SciJavaService
49+
{
50+
51+
/**
52+
* Turns the given string into an {@link URI}, then resolves it to a
53+
* {@link Location}
54+
*
55+
* @param uri the uri to resolve
56+
* @return the resolved {@link Location}
57+
* @throws URISyntaxException if the URI is malformed
58+
*/
59+
default Location resolve(final String uri) throws URISyntaxException {
60+
return resolve(new URI(uri));
61+
}
62+
63+
/**
64+
* Resolves the given {@link URI} to a location.
65+
*
66+
* @param uri the uri to resolve
67+
* @return the resolved {@link Location} or <code>null</code> if no resolver
68+
* could be found.
69+
* @throws URISyntaxException if the URI is malformed
70+
*/
71+
default Location resolve(final URI uri) throws URISyntaxException {
72+
final LocationResolver resolver = getResolver(uri);
73+
return resolver != null ? resolver.resolve(uri) : null;
74+
}
75+
76+
/**
77+
* Returns a {@link LocationResolver} capable of resolving URL like the one
78+
* provided to this method. Allows faster repeated resolving of similar URIs
79+
* without going through this service.
80+
*
81+
* @param uri the uri
82+
* @return the {@link LocationResolver} for this uri type, or
83+
* <code>null</code> if no resolver could be found.
84+
*/
85+
LocationResolver getResolver(URI uri);
86+
87+
// -- PTService methods --
88+
89+
@Override
90+
default Class<LocationResolver> getPluginType() {
91+
return LocationResolver.class;
92+
}
93+
94+
// -- Typed methods --
95+
96+
@Override
97+
default Class<URI> getType() {
98+
return URI.class;
99+
}
100+
}

src/test/java/org/scijava/ContextCreationTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ public void testFull() {
100100
org.scijava.io.DefaultIOService.class,
101101
org.scijava.io.DefaultRecentFileService.class,
102102
org.scijava.io.handle.DefaultDataHandleService.class,
103+
org.scijava.io.location.DefaultLocationService.class,
103104
org.scijava.io.nio.DefaultNIOService.class,
104105
org.scijava.main.DefaultMainService.class,
105106
org.scijava.menu.DefaultMenuService.class,

0 commit comments

Comments
 (0)