Creating a filter requires extending either the
cgl.hpsearch.wsproxy.RunnableProxyWebService
or the
cgl.hpsearch.wsproxy.WrapperProxyWebService
class. The description of each of these classes and differences is given
here .
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import org.apache.log4j.Logger;
import cgl.hpsearch.wsproxy.WrapperProxyWebService;
/**
* @author Harshawardhan Gadgil (hgadgil@grids.ucs.indiana.edu)
*/
public class PIFilterRunner extends WrapperProxyWebService {
static Logger log = Logger.getLogger("PIFilterRunner");
private BufferedReader br;
private BufferedWriter bw;
private String MinimumMagnitude;
public void process() throws Exception {
double MAGNITUDE_THRESHOLD = Double.parseDouble(MinimumMagnitude);
// Step 1: Accumulate data
String line;
while (true) {
try {
line = br.readLine();
}
catch (IOException e) {
line = null;
}
if (line == null) {// EOF reached...
break;
}
else {
System.out.println("READ:] " + line);
// We do not write this line to output
if (line.startsWith("#")) continue;
if (line.startsWith("SOF")) continue;
if (line.startsWith("EOF")) {
// EOF reached... bail out...
break;
}
// A sample line would look like this
// "1932" "12" "20" "-115.990" "34.545" "3.01"
line = line.replaceAll("\"\t\"", " ");
line = line.substring(1, line.indexOf('"', 2));
// Find the last token in the string. This is the magnitude !!
String magnitude = line.substring(line.lastIndexOf(" "));
double dbl = Double.parseDouble(magnitude);
if (dbl >= MAGNITUDE_THRESHOLD) {
try {
bw.write(line + "\n");
bw.flush();
}
catch (IOException e1) {
log.error("", e1);
return;
}
}
bw.write(line + "\n");
bw.flush();
}
}
// MUST close the catalog file writer since we opened the file
bw.close();
// My jobz over...
serviceFinished();
}
public void initService(InputStream[] arg0, OutputStream[] arg1) {
MinimumMagnitude = getProperty("MinimumMagnitude");
// Wrap streams for easy access
br = new BufferedReader(new InputStreamReader(arg0[0]));
bw = new BufferedWriter(new OutputStreamWriter(arg1[0]);
}
public String[] serviceExceptions() {
return new String[0];
}
}
"1932" "12" "20" "-115.990" "34.545" "3.01")
where the individual fields are (year, month, day, longitude, latitude, magnitude) of observations from a GPS database.
The input line contains these fields spearated by tab (\t) characters.
Depending on the value of the service parameter MinimumMagnitude, the service only outputs those
lines which have the value of the last column (magnitude >= MinimumMagnitude).SOF) and end of file (EOF) which
are also removed before writing the final output.
initService(InputStream[] arg0, OutputStream[] arg1) function that takes in an
array of input and output stream objects. These are automatically mapped to various URIs
(Refer here for a list of supported URIs).
String[] serviceExceptions() function that returns an array of possible execeptions
that might be thrown by the service and which should have actions defined in the user's script. If no actions are defined
the service simply ignores the error and continues. For most services we simply return an empty array of strings. An example
of usage of this feature may be found in the Network Sensor Demo.