public abstract class ClassServer
extends java.lang.Object
implements java.lang.Runnable
The ClassServer creates a thread that listens on a socket and accepts HTTP GET requests. The HTTP response contains the bytecodes for the class that requested in the GET header.
For loading remote classes, an RMI application can use a concrete subclass of this server in place of an HTTP server.
ClassFileServer
Modifier and Type | Field and Description |
---|---|
private java.util.function.Consumer<java.lang.String> |
errMsgStrategy
Strategy to display an error message.
|
private java.util.function.Consumer<java.lang.String> |
infoMsgStrategy
Strategy to display an informational message.
|
private boolean |
isStop
Flag to used to stop the server
|
private java.net.ServerSocket |
server
The socket object the server uses
|
Modifier | Constructor and Description |
---|---|
protected |
ClassServer(int port)
Constructs a ClassServer that listens on port and
obtains a class's bytecodes using the method getBytes.
|
protected |
ClassServer(int port,
java.util.function.Consumer<java.lang.String> infoMsgStrategy,
java.util.function.Consumer<java.lang.String> errMsgStrategy)
Constructs a ClassServer that listens on port and
obtains a class's bytecodes using the method getBytes.
|
Modifier and Type | Method and Description |
---|---|
abstract byte[] |
getBytes(java.lang.String path)
Returns an array of bytes containing the bytecodes for
the class represented by the argument path.
|
private static java.lang.String |
getPath(java.io.BufferedReader in)
Returns the path to the class file obtained from
parsing the HTML header.
|
private void |
newListener()
Create a new thread to listen.
|
void |
run()
The "listen" thread that accepts a connection to the
server, parses the header to obtain the class file name
and sends back the bytecodes for the class (or error
if the class is not found or the response was malformed).
|
protected void |
showErrorMsg(java.lang.String msg)
Encapsulated behavior to show an error message
|
protected void |
showInfoMsg(java.lang.String msg)
Encapsulated behavior to show an informational message
|
void |
stop()
Stops the server
|
private java.net.ServerSocket server
private volatile boolean isStop
private java.util.function.Consumer<java.lang.String> infoMsgStrategy
private java.util.function.Consumer<java.lang.String> errMsgStrategy
protected ClassServer(int port) throws java.io.IOException
port
- the port numberjava.io.IOException
- if the ClassServer could not listen
on port.protected ClassServer(int port, java.util.function.Consumer<java.lang.String> infoMsgStrategy, java.util.function.Consumer<java.lang.String> errMsgStrategy) throws java.io.IOException
port
- the port numberinfoMsgStrategy
- Strategy to display an info message.
Recommended: For increased system robustness, this strategy
should first write to System.out.println()errMsgStrategy
- Strategy to display an error message.
Recommended: For increased system robustness, this strategy
should first write to System.err.println()java.io.IOException
- if the ClassServer could not listen on port.public abstract byte[] getBytes(java.lang.String path) throws java.io.IOException, java.lang.ClassNotFoundException
path
- fully qualified classname of the desired classjava.lang.ClassNotFoundException
- if the class corresponding
to path could not be loaded.java.io.IOException
- if error occurs reading the classpublic void stop()
public void run()
run
in interface java.lang.Runnable
private void newListener()
private static java.lang.String getPath(java.io.BufferedReader in) throws java.io.IOException
in
- stream from which the header is read.java.io.IOException
- if the stream cannot be correctly readprotected void showInfoMsg(java.lang.String msg)
msg
- Message to show.protected void showErrorMsg(java.lang.String msg)
msg
- Message to show.