Skip to content

Commit 67d0b24

Browse files
committed
Merge pull request #39 from scijava/new-services
Migrate some services from ImageJ
2 parents 4eb5f91 + 294bc96 commit 67d0b24

16 files changed

+1179
-0
lines changed
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 - 2014 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+
* #L%
30+
*/
31+
32+
package org.scijava.console;
33+
34+
import java.util.LinkedList;
35+
36+
import org.scijava.plugin.AbstractHandlerPlugin;
37+
38+
/**
39+
* Abstract superclass of {@link ConsoleArgument} implementations.
40+
*
41+
* @author Curtis Rueden
42+
*/
43+
public abstract class AbstractConsoleArgument extends
44+
AbstractHandlerPlugin<LinkedList<String>> implements ConsoleArgument
45+
{
46+
47+
// -- Typed methods --
48+
49+
@Override
50+
@SuppressWarnings({ "rawtypes", "unchecked" })
51+
public Class<LinkedList<String>> getType() {
52+
return (Class) String.class;
53+
}
54+
55+
}
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 - 2014 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+
* #L%
30+
*/
31+
32+
package org.scijava.console;
33+
34+
import java.util.LinkedList;
35+
36+
import org.scijava.plugin.HandlerPlugin;
37+
import org.scijava.plugin.Plugin;
38+
39+
/**
40+
* A plugin which extends an application's command line argument handling.
41+
* <p>
42+
* Console argument plugins discoverable at runtime must implement this
43+
* interface and be annotated with @{@link Plugin} with attribute
44+
* {@link Plugin#type()} = {@link ConsoleArgument}.class. While it possible to
45+
* create an console argument plugin merely by implementing this interface, it
46+
* is encouraged to instead extend {@link AbstractConsoleArgument}, for
47+
* convenience.
48+
* </p>
49+
*
50+
* @author Curtis Rueden
51+
*/
52+
public interface ConsoleArgument extends HandlerPlugin<LinkedList<String>> {
53+
54+
/** Handles the <em>front</em> of the given list of arguments. */
55+
void handle(final LinkedList<String> args);
56+
57+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* #%L
3+
* SciJava Common shared library for SciJava software.
4+
* %%
5+
* Copyright (C) 2009 - 2014 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+
* #L%
30+
*/
31+
32+
package org.scijava.console;
33+
34+
import java.util.LinkedList;
35+
36+
import org.scijava.plugin.HandlerService;
37+
import org.scijava.service.SciJavaService;
38+
39+
/**
40+
* Interface for service that manages interaction with the console.
41+
* <p>
42+
* In particular, this is the service that defines how command line arguments
43+
* are handled.
44+
* </p>
45+
*
46+
* @author Curtis Rueden
47+
*/
48+
public interface ConsoleService extends
49+
HandlerService<LinkedList<String>, ConsoleArgument>, SciJavaService
50+
{
51+
52+
/**
53+
* Handles arguments to ImageJ coming from an external source such as the
54+
* command line.
55+
*/
56+
void processArgs(String... args);
57+
58+
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/*
2+
* #%L
3+
* SciJava Common shared library for SciJava software.
4+
* %%
5+
* Copyright (C) 2009 - 2014 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+
* #L%
30+
*/
31+
32+
package org.scijava.console;
33+
34+
import java.util.LinkedList;
35+
36+
import org.scijava.log.LogService;
37+
import org.scijava.plugin.AbstractHandlerService;
38+
import org.scijava.plugin.Parameter;
39+
import org.scijava.plugin.Plugin;
40+
import org.scijava.service.Service;
41+
42+
/**
43+
* Default service for managing interaction with the console.
44+
*
45+
* @author Curtis Rueden
46+
*/
47+
@Plugin(type = Service.class)
48+
public class DefaultConsoleService extends
49+
AbstractHandlerService<LinkedList<String>, ConsoleArgument> implements
50+
ConsoleService
51+
{
52+
53+
@Parameter
54+
private LogService log;
55+
56+
// -- ConsoleService methods --
57+
58+
@Override
59+
public void processArgs(final String... args) {
60+
log.debug("Received command line arguments:");
61+
final LinkedList<String> argList = new LinkedList<String>();
62+
for (final String arg : args) {
63+
log.debug("\t" + arg);
64+
argList.add(arg);
65+
}
66+
67+
while (!argList.isEmpty()) {
68+
final ConsoleArgument handler = getHandler(argList);
69+
if (handler == null) {
70+
// ignore invalid command line argument
71+
final String arg = argList.removeFirst();
72+
log.warn("Ignoring invalid argument: " + arg);
73+
continue;
74+
}
75+
handler.handle(argList);
76+
}
77+
}
78+
79+
// -- PTService methods --
80+
81+
@Override
82+
public Class<ConsoleArgument> getPluginType() {
83+
return ConsoleArgument.class;
84+
}
85+
86+
// -- Typed methods --
87+
88+
@Override
89+
@SuppressWarnings({ "rawtypes", "unchecked" })
90+
public Class<LinkedList<String>> getType() {
91+
return (Class) LinkedList.class;
92+
}
93+
94+
}
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 - 2014 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+
* #L%
30+
*/
31+
32+
package org.scijava.io;
33+
34+
import java.io.IOException;
35+
36+
import org.scijava.plugin.AbstractHandlerPlugin;
37+
38+
/**
39+
* Abstract base class for {@link IOPlugin}s.
40+
*
41+
* @author Curtis Rueden
42+
*/
43+
public abstract class AbstractIOPlugin<D> extends AbstractHandlerPlugin<String>
44+
implements IOPlugin<D>
45+
{
46+
47+
// -- IOPlugin methods --
48+
49+
@Override
50+
public boolean supportsOpen(final String source) {
51+
return false;
52+
}
53+
54+
@Override
55+
public boolean supportsSave(final String destination) {
56+
return false;
57+
}
58+
59+
@Override
60+
public boolean supportsSave(final Object data, final String destination) {
61+
return supportsSave(destination) && getDataType().isInstance(data);
62+
}
63+
64+
@Override
65+
public D open(final String source) throws IOException {
66+
throw new UnsupportedOperationException();
67+
}
68+
69+
@Override
70+
public void save(final D data, final String destination) throws IOException {
71+
throw new UnsupportedOperationException();
72+
}
73+
74+
// -- Typed methods --
75+
76+
@Override
77+
public boolean supports(final String descriptor) {
78+
return supportsOpen(descriptor) || supportsSave(descriptor);
79+
}
80+
81+
@Override
82+
public Class<String> getType() {
83+
return String.class;
84+
}
85+
86+
}

0 commit comments

Comments
 (0)