Pull all interfaces into a single interface: Filter.
[sonitus.git] / src / main / java / net / pterodactylus / sonitus / data / filter / ExternalFilter.java
1 /*
2  * Sonitus - ExternalFilter.java - Copyright © 2013 David Roden
3  *
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.
8  *
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.
13  *
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/>.
16  */
17
18 package net.pterodactylus.sonitus.data.filter;
19
20 import java.io.IOException;
21 import java.io.InputStream;
22 import java.io.OutputStream;
23 import java.util.logging.Logger;
24
25 import net.pterodactylus.sonitus.data.AbstractFilter;
26 import net.pterodactylus.sonitus.data.Filter;
27 import net.pterodactylus.sonitus.data.Metadata;
28 import net.pterodactylus.sonitus.io.InputStreamDrainer;
29
30 import com.google.common.collect.ImmutableList;
31 import com.google.common.collect.Iterables;
32
33 /**
34  * {@link net.pterodactylus.sonitus.data.Filter} implementation that runs its
35  * {@link net.pterodactylus.sonitus.data.Source} through an external program.
36  *
37  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
38  */
39 public abstract class ExternalFilter extends AbstractFilter implements Filter {
40
41         /** The logger. */
42         private final Logger logger = Logger.getLogger(getClass().getName());
43
44         /** The external process. */
45         private Process process;
46
47         /**
48          * Creates a new external filter with the given name.
49          *
50          * @param name
51          *              The name of the filter
52          */
53         protected ExternalFilter(String name) {
54                 super(name);
55         }
56
57         //
58         // FILTER METHODS
59         //
60
61         @Override
62         public void open(Metadata metadata) throws IOException {
63                 process = Runtime.getRuntime().exec(Iterables.toArray(ImmutableList.<String>builder().add(binary(metadata)).addAll(parameters(metadata)).build(), String.class));
64                 InputStream processError = process.getErrorStream();
65                 new Thread(new InputStreamDrainer(processError)).start();
66                 super.open(metadata);
67         }
68
69         @Override
70         public void close() {
71                 process.destroy();
72         }
73
74         @Override
75         protected InputStream createInputStream() throws IOException {
76                 return process.getInputStream();
77         }
78
79         @Override
80         protected OutputStream createOutputStream() throws IOException {
81                 return process.getOutputStream();
82         }
83
84         //
85         // SUBCLASS METHODS
86         //
87
88         /**
89          * Returns the location of the binary to execute.
90          *
91          * @param metadata
92          *              The metadata being processed
93          * @return The location of the binary to execute
94          */
95         protected abstract String binary(Metadata metadata);
96
97         /**
98          * Returns the parameters for the binary.
99          *
100          * @param metadata
101          *              The metadata being processed
102          * @return The parameters for the binary
103          */
104         protected abstract Iterable<String> parameters(Metadata metadata);
105
106 }