Skip to content

Commit fead1fa

Browse files
hinermctrueden
authored andcommitted
Added Gateway interface
The Gateway interface facilitates convenience classes that want to provide one-line service instantiation through a wrapped Context. The provided get() methods allow simple and consistent Service lookup, with a NoSuchServiceException thrown if the requested Service is not found.
1 parent a303cb0 commit fead1fa

File tree

3 files changed

+214
-0
lines changed

3 files changed

+214
-0
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* #%L
3+
* SciJava Common shared library for SciJava software.
4+
* %%
5+
* Copyright (C) 2009 - 2013 Board of Regents of the University of
6+
* Wisconsin-Madison, Broad Institute of MIT and Harvard, and Max Planck
7+
* Institute of Molecular Cell Biology and Genetics.
8+
* %%
9+
* Redistribution and use in source and binary forms, with or without
10+
* modification, are permitted provided that the following conditions are met:
11+
*
12+
* 1. Redistributions of source code must retain the above copyright notice,
13+
* this list of conditions and the following disclaimer.
14+
* 2. Redistributions in binary form must reproduce the above copyright notice,
15+
* this list of conditions and the following disclaimer in the documentation
16+
* and/or other materials provided with the distribution.
17+
*
18+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
22+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28+
* POSSIBILITY OF SUCH DAMAGE.
29+
*
30+
* The views and conclusions contained in the software and documentation are
31+
* those of the authors and should not be interpreted as representing official
32+
* policies, either expressed or implied, of any organization.
33+
* #L%
34+
*/
35+
36+
package org.scijava;
37+
38+
import org.scijava.service.Service;
39+
40+
/**
41+
* Abstract superclass for {@link Gateway} implementations.
42+
*
43+
* @author Mark Hiner
44+
* @author Curtis Rueden
45+
*/
46+
public abstract class AbstractGateway extends AbstractContextual implements
47+
Gateway
48+
{
49+
50+
// -- Gateway methods --
51+
52+
@Override
53+
public <S extends Service> S get(final Class<S> serviceClass) {
54+
final S service = getContext().getService(serviceClass);
55+
checkNull(service, serviceClass.getName());
56+
return service;
57+
}
58+
59+
@Override
60+
public Service get(final String serviceClassName) {
61+
final Service service = getContext().getService(serviceClassName);
62+
checkNull(service, serviceClassName);
63+
return service;
64+
}
65+
66+
// -- Helper methods --
67+
68+
/** Throws {@link NoSuchServiceException} if the service wasn't found. */
69+
private void checkNull(final Service service, final String name) {
70+
if (service == null) {
71+
throw new NoSuchServiceException("Service " + name + " not found.");
72+
}
73+
}
74+
75+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* #%L
3+
* SciJava Common shared library for SciJava software.
4+
* %%
5+
* Copyright (C) 2009 - 2013 Board of Regents of the University of
6+
* Wisconsin-Madison, Broad Institute of MIT and Harvard, and Max Planck
7+
* Institute of Molecular Cell Biology and Genetics.
8+
* %%
9+
* Redistribution and use in source and binary forms, with or without
10+
* modification, are permitted provided that the following conditions are met:
11+
*
12+
* 1. Redistributions of source code must retain the above copyright notice,
13+
* this list of conditions and the following disclaimer.
14+
* 2. Redistributions in binary form must reproduce the above copyright notice,
15+
* this list of conditions and the following disclaimer in the documentation
16+
* and/or other materials provided with the distribution.
17+
*
18+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
22+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28+
* POSSIBILITY OF SUCH DAMAGE.
29+
*
30+
* The views and conclusions contained in the software and documentation are
31+
* those of the authors and should not be interpreted as representing official
32+
* policies, either expressed or implied, of any organization.
33+
* #L%
34+
*/
35+
36+
package org.scijava;
37+
38+
import org.scijava.service.Service;
39+
40+
/**
41+
* Interface for convenience classes that wrap a {@link Context} to provide
42+
* one-line access to a suite of {@link Service}s.
43+
* <p>
44+
* The {@link #get} methods provide consistent {@link Service} instantiation,
45+
* while throwing {@link NoSuchServiceException} if the requested
46+
* {@link Service} is not found.
47+
* </p>
48+
*
49+
* @see Context
50+
* @author Mark Hiner
51+
* @author Curtis Rueden
52+
*/
53+
public interface Gateway extends Contextual {
54+
55+
/**
56+
* Returns an implementation of the requested {@link Service}, if it exists in
57+
* the underlying {@link Context}.
58+
*
59+
* @param serviceClass the requested {@link Service}
60+
* @return The singleton instance of the given class
61+
* @throws NoSuchServiceException if there is no service of the given class.
62+
*/
63+
<S extends Service> S get(Class<S> serviceClass);
64+
65+
/**
66+
* Returns an implementation of the {@link Service} with the given class name,
67+
* if it exists in the underlying {@link Context}.
68+
*
69+
* @param serviceClassName name of the requested {@link Service}
70+
* @return The singleton instance of the requested {@link Service}
71+
* @throws NoSuchServiceException if there is no service matching
72+
* serviceClassName.
73+
*/
74+
Service get(final String serviceClassName);
75+
76+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* #%L
3+
* SciJava Common shared library for SciJava software.
4+
* %%
5+
* Copyright (C) 2009 - 2013 Board of Regents of the University of
6+
* Wisconsin-Madison, Broad Institute of MIT and Harvard, and Max Planck
7+
* Institute of Molecular Cell Biology and Genetics.
8+
* %%
9+
* Redistribution and use in source and binary forms, with or without
10+
* modification, are permitted provided that the following conditions are met:
11+
*
12+
* 1. Redistributions of source code must retain the above copyright notice,
13+
* this list of conditions and the following disclaimer.
14+
* 2. Redistributions in binary form must reproduce the above copyright notice,
15+
* this list of conditions and the following disclaimer in the documentation
16+
* and/or other materials provided with the distribution.
17+
*
18+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
22+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28+
* POSSIBILITY OF SUCH DAMAGE.
29+
*
30+
* The views and conclusions contained in the software and documentation are
31+
* those of the authors and should not be interpreted as representing official
32+
* policies, either expressed or implied, of any organization.
33+
* #L%
34+
*/
35+
36+
package org.scijava;
37+
38+
/**
39+
* An exception thrown when a {@link Gateway} cannot obtain a
40+
* {@link org.scijava.service.Service}.
41+
*
42+
* @author Mark Hiner
43+
* @author Curtis Rueden
44+
*/
45+
public class NoSuchServiceException extends RuntimeException {
46+
47+
public NoSuchServiceException() {
48+
super();
49+
}
50+
51+
public NoSuchServiceException(final String s) {
52+
super(s);
53+
}
54+
55+
public NoSuchServiceException(final String s, final Throwable cause) {
56+
super(s, cause);
57+
}
58+
59+
public NoSuchServiceException(final Throwable cause) {
60+
super(cause);
61+
}
62+
63+
}

0 commit comments

Comments
 (0)