import java.util.StringTokenizer;
/**
- *
+ *
* @author Christophe Marchand <christophe.marchand@axyus.com>
*/
public class BatchInformationListener {
- private boolean stop = false;
- private BatchRunner runner;
- private ServerSocket server_socket = null;
- private static BatchInformationListener instance = null;
- private int port;
-
- public BatchInformationListener(BatchRunner runner, int port) {
- super();
- BatchInformationListener.instance=this;
- this.runner = runner;
- this.port = port;
- Thread starter = new Thread() {
- public void run() {
- try {
- server_socket = new ServerSocket(BatchInformationListener.this.port);
- while (!stop) {
- Socket socket = server_socket.accept();
-
- // Construct handler to process the HTTP request message.
- try {
- httpRequestHandler request = new httpRequestHandler(socket, BatchInformationListener.this.runner);
- Thread thread = new Thread(request);
-
- // Start the thread.
- thread.start();
- } catch (Exception e) {
- }
- }
- } catch (IOException e) {
- } finally {
- try {
- server_socket.close();
- } catch (Throwable t) {
- }
- }
- }
- };
- starter.start();
- }
-
- public void stop() {
- stop = true;
- try {
- server_socket.close();
- } catch (IOException ioEx) {
- // on ignore
- }
-
- }
- public static BatchInformationListener getInstance() { return instance; }
+ private boolean stop = false;
+ private final BatchRunner runner;
+ private ServerSocket server_socket = null;
+ private static BatchInformationListener instance = null;
+ private final int port;
+
+ public BatchInformationListener(final BatchRunner runner, final int port) {
+ super();
+ BatchInformationListener.instance = this;
+ this.runner = runner;
+ this.port = port;
+ final Thread starter = new Thread() {
+ @Override
+ public void run() {
+ try {
+ BatchInformationListener.this.server_socket = new ServerSocket(BatchInformationListener.this.port);
+ while (!BatchInformationListener.this.stop) {
+ final Socket socket = BatchInformationListener.this.server_socket.accept();
+
+ // Construct handler to process the HTTP request message.
+ try {
+ final httpRequestHandler request = new httpRequestHandler(socket, BatchInformationListener.this.runner);
+ final Thread thread = new Thread(request);
+
+ // Start the thread.
+ thread.start();
+ } catch (final Exception e) {
+ }
+ }
+ } catch (final IOException e) {
+ } finally {
+ try {
+ BatchInformationListener.this.server_socket.close();
+ } catch (final Throwable t) {
+ }
+ }
+ }
+ };
+ starter.start();
+ }
+
+ public void stop() {
+ this.stop = true;
+ try {
+ this.server_socket.close();
+ } catch (final IOException ioEx) {
+ // on ignore
+ }
+
+ }
+
+ public static BatchInformationListener getInstance() {
+ return instance;
+ }
}
class httpRequestHandler implements Runnable {
- final static String CRLF = "\r\n";
- Socket socket;
- OutputStream output;
- BufferedReader br;
- private BatchRunner runner;
-
- // Constructor
- public httpRequestHandler(Socket socket, BatchRunner runner) throws Exception {
- this.socket = socket;
- this.runner = runner;
- this.output = socket.getOutputStream();
- this.br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
- }
-
- // Implement the run() method of the Runnable interface.
- public void run() {
- try {
- processRequest();
- } catch (Exception e) {
- System.out.println(e);
- }
- }
-
- private void processRequest() throws Exception {
- String headerLine = br.readLine();
- if (CRLF.equals(headerLine) || "".equals(headerLine)) {
- return;
- }
-
- StringTokenizer s = new StringTokenizer(headerLine);
- String temp = s.nextToken();
-
- if (temp.equals("GET")) {
- String path = s.nextToken();
- String serverLine = "XeMeLios batch information serveur";
- String statusLine = null;
- String contentTypeLine = null;
- String entityBody = null;
- String contentLengthLine = "error";
- String informations = null;
- if("/".equals(path)) {
- if (runner != null) {
- statusLine = "HTTP/1.0 200 OK" + CRLF;
- contentTypeLine = "Content-type: text/plain" + CRLF;
- informations = runner.getInformations();
- contentLengthLine = "Content-Length: " + Integer.toString(informations.length()) + CRLF;
- } else {
- statusLine = "HTTP/1.0 404 Not Found" + CRLF;
- contentTypeLine = "text/html";
- entityBody = "<HTML>" +
- "<HEAD><TITLE>404 Not Found</TITLE></HEAD>" +
- "<BODY>404 Not Found" + "<br>usage: http://server:" + socket.getPort() + "/</BODY></HTML>";
- }
- } else {
- statusLine = "HTTP/1.0 404 Not Found" + CRLF;
- contentTypeLine = "text/html";
- entityBody = "<HTML>" +
- "<HEAD><TITLE>404 Not Found</TITLE></HEAD>" +
- "<BODY>404 Not Found" + "<br>usage: http://server:" + socket.getPort() + "/</BODY></HTML>";
- }
-
- output.write(statusLine.getBytes());
- output.write(serverLine.getBytes());
- // Send the content type line.
- output.write(contentTypeLine.getBytes());
- // Send the Content-Length
- output.write(contentLengthLine.getBytes());
- // Send a blank line to indicate the end of the header lines.
- output.write(CRLF.getBytes());
-
- // Send the entity body.
- if (runner != null) {
- output.write(informations.getBytes());
- } else {
- output.write(entityBody.getBytes());
- }
- }
-
- try {
- output.flush();
- output.close();
- br.close();
- socket.close();
- } catch (Exception e) {
- }
- }
-
- private static String contentType(String fileName) {
- if (fileName.endsWith(".htm") || fileName.endsWith(".html")) {
- return "text/html";
- } else if (fileName.endsWith(".zip")) {
- return "multipart/x-zip";
- }
-
- return "";
- }
+ final static String CRLF = "\r\n";
+ Socket socket;
+ OutputStream output;
+ BufferedReader br;
+ private final BatchRunner runner;
+
+ // Constructor
+ public httpRequestHandler(final Socket socket, final BatchRunner runner) throws Exception {
+ this.socket = socket;
+ this.runner = runner;
+ this.output = socket.getOutputStream();
+ this.br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
+ }
+
+ // Implement the run() method of the Runnable interface.
+ @Override
+ public void run() {
+ try {
+ this.processRequest();
+ } catch (final Exception e) {
+ System.out.println(e);
+ }
+ }
+
+ private void processRequest() throws Exception {
+ final String headerLine = this.br.readLine();
+ if (CRLF.equals(headerLine) || "".equals(headerLine)) {
+ return;
+ }
+
+ final StringTokenizer s = new StringTokenizer(headerLine);
+ final String temp = s.nextToken();
+
+ if (temp.equals("GET")) {
+ final String path = s.nextToken();
+ final String serverLine = "XeMeLios batch information serveur";
+ String statusLine = null;
+ String contentTypeLine = null;
+ String entityBody = null;
+ String contentLengthLine = "error";
+ String informations = null;
+ if ("/".equals(path)) {
+ if (this.runner != null) {
+ statusLine = "HTTP/1.0 200 OK" + CRLF;
+ contentTypeLine = "Content-type: text/plain" + CRLF;
+ informations = this.runner.getInformations();
+ contentLengthLine = "Content-Length: " + Integer.toString(informations.length()) + CRLF;
+ } else {
+ statusLine = "HTTP/1.0 404 Not Found" + CRLF;
+ contentTypeLine = "text/html";
+ entityBody = "<HTML>" + "<HEAD><TITLE>404 Not Found</TITLE></HEAD>" + "<BODY>404 Not Found" + "<br>usage: http://server:" + this.socket.getPort() + "/</BODY></HTML>";
+ }
+ } else {
+ statusLine = "HTTP/1.0 404 Not Found" + CRLF;
+ contentTypeLine = "text/html";
+ entityBody = "<HTML>" + "<HEAD><TITLE>404 Not Found</TITLE></HEAD>" + "<BODY>404 Not Found" + "<br>usage: http://server:" + this.socket.getPort() + "/</BODY></HTML>";
+ }
+
+ this.output.write(statusLine.getBytes());
+ this.output.write(serverLine.getBytes());
+ // Send the content type line.
+ this.output.write(contentTypeLine.getBytes());
+ // Send the Content-Length
+ this.output.write(contentLengthLine.getBytes());
+ // Send a blank line to indicate the end of the header lines.
+ this.output.write(CRLF.getBytes());
+
+ // Send the entity body.
+ if (this.runner != null) {
+ this.output.write(informations.getBytes());
+ } else {
+ this.output.write(entityBody.getBytes());
+ }
+ }
+
+ try {
+ this.output.flush();
+ this.output.close();
+ this.br.close();
+ this.socket.close();
+ } catch (final Exception e) {
+ }
+ }
+
+ private static String contentType(final String fileName) {
+ if (fileName.endsWith(".htm") || fileName.endsWith(".html")) {
+ return "text/html";
+ } else if (fileName.endsWith(".zip")) {
+ return "multipart/x-zip";
+ }
+
+ return "";
+ }
}