2 * Sonitus - ExternalFilter.java - Copyright © 2013 David Roden
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package net.pterodactylus.sonitus.data.filter;
20 import java.io.IOException;
21 import java.io.InputStream;
22 import java.io.OutputStream;
23 import java.util.logging.Logger;
25 import net.pterodactylus.sonitus.data.Metadata;
26 import net.pterodactylus.sonitus.io.InputStreamDrainer;
28 import com.google.common.collect.ImmutableList;
29 import com.google.common.collect.Iterables;
32 * {@link net.pterodactylus.sonitus.data.Filter} implementation that runs its
33 * {@link net.pterodactylus.sonitus.data.Source} through an external program.
35 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
37 public abstract class ExternalFilter extends DummyFilter {
40 private final Logger logger = Logger.getLogger(getClass().getName());
42 /** The external process. */
43 private Process process;
46 * Creates a new external filter with the given name.
49 * The name of the filter
51 protected ExternalFilter(String name) {
60 public void open(Metadata metadata) throws IOException {
61 process = Runtime.getRuntime().exec(Iterables.toArray(ImmutableList.<String>builder().add(binary(metadata)).addAll(parameters(metadata)).build(), String.class));
62 InputStream processError = process.getErrorStream();
63 new Thread(new InputStreamDrainer(processError)).start();
73 // DUMMYFILTER METHODS
77 protected InputStream createInputStream() throws IOException {
78 return process.getInputStream();
82 protected OutputStream createOutputStream() throws IOException {
83 return process.getOutputStream();
91 * Returns the location of the binary to execute.
94 * The metadata being processed
95 * @return The location of the binary to execute
97 protected abstract String binary(Metadata metadata);
100 * Returns the parameters for the binary.
103 * The metadata being processed
104 * @return The parameters for the binary
106 protected abstract Iterable<String> parameters(Metadata metadata);