Skip to content

Commit ad5d545

Browse files
committed
Add LocationService framework:
The LocationService manages LocationResolver plugins that provide translation from URI to Location. The LocationService itself contains convenience methods to translate from String to Location.
1 parent c5b6733 commit ad5d545

File tree

5 files changed

+290
-0
lines changed

5 files changed

+290
-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.resolve;
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: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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.resolve;
34+
35+
import java.net.URI;
36+
import java.net.URISyntaxException;
37+
import java.util.HashMap;
38+
import java.util.Map;
39+
40+
import org.scijava.io.location.Location;
41+
import org.scijava.plugin.AbstractHandlerService;
42+
import org.scijava.plugin.Plugin;
43+
import org.scijava.service.Service;
44+
45+
/**
46+
* Default {@link LocationService} implementation.
47+
*
48+
* @author Gabriel Einsdorf
49+
*/
50+
@Plugin(type = Service.class)
51+
public class DefaultLocationService extends
52+
AbstractHandlerService<URI, LocationResolver> implements
53+
LocationService
54+
{
55+
56+
private final Map<String, LocationResolver> resolvers = new HashMap<>();
57+
58+
@Override
59+
public Class<LocationResolver> getPluginType() {
60+
return LocationResolver.class;
61+
}
62+
63+
@Override
64+
public Class<URI> getType() {
65+
return URI.class;
66+
}
67+
68+
69+
@Override
70+
public LocationResolver getResolver(final URI uri) {
71+
return resolvers.computeIfAbsent(uri.getScheme(), u -> getHandler(uri));
72+
}
73+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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.resolve;
34+
35+
import java.net.URI;
36+
import java.net.URISyntaxException;
37+
38+
import org.scijava.io.location.Location;
39+
import org.scijava.plugin.HandlerPlugin;
40+
41+
/**
42+
* {@link LocationResolver} plugins allow resolving an {@link URI} to a
43+
* {@link Location}. Extending {@link AbstractLocationResolver} is recommended
44+
* for easy implementation.
45+
*
46+
* @author Gabriel Einsdorf
47+
*/
48+
public interface LocationResolver extends HandlerPlugin<URI> {
49+
50+
/**
51+
* Resolves the given {@link URI} to a {@link Location}
52+
*
53+
* @return the resolved Location
54+
* @throws URISyntaxException
55+
*/
56+
Location resolve(URI uri) throws URISyntaxException;
57+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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.resolve;
34+
35+
import java.net.URI;
36+
import java.net.URISyntaxException;
37+
38+
import org.scijava.io.location.Location;
39+
import org.scijava.plugin.HandlerService;
40+
import org.scijava.service.SciJavaService;
41+
42+
/**
43+
* A service that allows resolving of URIs to Locations, using
44+
* {@link LocationResolver} plugins for translation.
45+
*
46+
* @author Gabriel Einsdorf
47+
*/
48+
public interface LocationService extends HandlerService<URI, LocationResolver>,
49+
SciJavaService
50+
{
51+
52+
/**
53+
* Turns the given string into an {@link URI}, then resolves it to a
54+
* {@link Location}
55+
*
56+
* @param uri the uri to resolve
57+
* @return the resolved {@link Location}
58+
* @throws URISyntaxException if the URI is malformed
59+
*/
60+
default Location resolve(final String uri) throws URISyntaxException {
61+
return resolve(new URI(uri));
62+
}
63+
64+
/**
65+
* Resolves the given {@link URI} to a location.
66+
*
67+
* @param uri the uri to resolve
68+
* @return the resolved {@link Location} or <code>null</code> if no resolver
69+
* could be found.
70+
* @throws URISyntaxException if the URI is malformed
71+
*/
72+
default Location resolve(final URI uri) throws URISyntaxException {
73+
final LocationResolver resolver = getResolver(uri);
74+
return resolver != null ? resolver.resolve(uri) : null;
75+
}
76+
77+
/**
78+
* Returns a {@link LocationResolver} capable of resolving URL like the one
79+
* provided to this method. Allows faster repeated resolving of similar URIs
80+
* without going through this service.
81+
*
82+
* @param uri the uri
83+
* @return the {@link LocationResolver} for this uri type, or
84+
* <code>null</code> if no resolver could be found.
85+
*/
86+
LocationResolver getResolver(URI uri);
87+
}

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.resolve.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)