Skip to content

Commit 8b9f05c

Browse files
gab1onectrueden
authored andcommitted
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 8b9f05c

File tree

5 files changed

+287
-0
lines changed

5 files changed

+287
-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: 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+
import java.net.URISyntaxException;
37+
import java.util.HashMap;
38+
import java.util.Map;
39+
40+
import org.scijava.plugin.AbstractHandlerService;
41+
import org.scijava.plugin.Plugin;
42+
import org.scijava.service.Service;
43+
44+
/**
45+
* Default {@link LocationService} implementation.
46+
*
47+
* @author Gabriel Einsdorf
48+
*/
49+
@Plugin(type = Service.class)
50+
public class DefaultLocationService extends
51+
AbstractHandlerService<URI, LocationResolver> implements
52+
LocationService
53+
{
54+
55+
private final Map<String, LocationResolver> resolvers = new HashMap<>();
56+
57+
@Override
58+
public Class<LocationResolver> getPluginType() {
59+
return LocationResolver.class;
60+
}
61+
62+
@Override
63+
public Class<URI> getType() {
64+
return URI.class;
65+
}
66+
67+
68+
@Override
69+
public LocationResolver getResolver(final URI uri) {
70+
return resolvers.computeIfAbsent(uri.getScheme(), u -> getHandler(uri));
71+
}
72+
}
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: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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+
}

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)