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