javatools.http
Class HttpCommander

java.lang.Object
  |
  +--java.lang.Thread
        |
        +--javatools.http.HttpCommander
All Implemented Interfaces:
Resetable, java.lang.Runnable, Server

public class HttpCommander
extends java.lang.Thread
implements Server, Resetable

A class which takes commands via a http port. The idea is to start this class as a thread in your server class and it will allow you to communicate with your server via a http port. e.g. shutdown your server, ping it, get it to re-read config files. etc.

You pass a port number and a Java object to the constructor. Any method in that command object that has a HttpWriter as the first argument will be able to be called remotely via http and receive a response. This can be done from a browser.

e.g. If you have a class...

 public class Foo() {
     public void ping(HttpWriter w) {
         w.println("Server is alive");
     }
 }
 ...
 new HttpCommander(5000, new Foo()).start();
 
Now if we visit http://localhost:5000/ping we get returned the message in our browser "Server is alive". We can also achieve the same thing by telnetting to localhost 5000 and entering "GET /ping".

A useful extension would be to take http arguments via the ?arg=value&arg=value syntax and translate that into method arguments.


Field Summary
(package private)  java.lang.Object commands
           
(package private)  java.lang.Class commandsClass
           
(package private)  java.io.IOException exception
           
(package private)  java.io.BufferedReader in
           
static java.lang.String NAME
           
(package private)  java.io.PrintWriter out
           
(package private)  java.net.ServerSocket serverSocket
           
(package private)  int serverSocketPort
           
 
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
 
Constructor Summary
HttpCommander()
           
HttpCommander(int serverSocketPort, java.lang.Object commands)
          Create a new HttpCommander.
 
Method Summary
 void reset()
           
 void run()
          Run the HttpCommander thread loop.
 void setCommandObject(java.lang.Object c)
           
 void setServerSocketPort(int p)
           
 void shutdown()
          Shutdown the HttpCommander.
 
Methods inherited from class java.lang.Thread
activeCount, checkAccess, countStackFrames, currentThread, destroy, dumpStack, enumerate, getContextClassLoader, getName, getPriority, getThreadGroup, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, resume, setContextClassLoader, setDaemon, setName, setPriority, sleep, sleep, start, stop, stop, suspend, toString, yield
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

NAME

public static final java.lang.String NAME
See Also:
Constant Field Values

commands

java.lang.Object commands

commandsClass

java.lang.Class commandsClass

serverSocketPort

int serverSocketPort

serverSocket

java.net.ServerSocket serverSocket

in

java.io.BufferedReader in

out

java.io.PrintWriter out

exception

java.io.IOException exception
Constructor Detail

HttpCommander

public HttpCommander()

HttpCommander

public HttpCommander(int serverSocketPort,
                     java.lang.Object commands)
              throws java.io.IOException
Create a new HttpCommander.

Parameters:
serverSocketPort - the http socket to wait for connections
commands - the Java object containing http methods
Throws:
java.io.IOException - Description of Exception
Method Detail

setServerSocketPort

public void setServerSocketPort(int p)

setCommandObject

public void setCommandObject(java.lang.Object c)

reset

public void reset()
Specified by:
reset in interface Resetable

run

public void run()
Run the HttpCommander thread loop. Wait for connections on the given socket.

Specified by:
run in interface java.lang.Runnable
Overrides:
run in class java.lang.Thread

shutdown

public void shutdown()
Shutdown the HttpCommander.

Specified by:
shutdown in interface Server