public class UtilServer
extends java.lang.Object
This class performs two functions. Firstly it provides a static
getInstance()
method which allows its use in a singleton-like way.
The constructor is public, so singleton use is not enforced, but if
you need a server but don't need exclusive control over it, obtaining
one in this way will ensure that you don't start a new server
(which requires a new socket and other resources) if a suitable one
is already available.
Secondly, it provides some utility methods,
exportResource(java.lang.String)
and exportFile(java.io.File)
,
useful for turning files or classpath resources into
publicly viewable URLs, which is sometimes useful within a SAMP
context (for instance when providing an Icon URL in metadata).
Modifier and Type | Field and Description |
---|---|
private java.util.Set |
baseSet_ |
private static int |
BUFSIZ
Buffer size for copy data from input to output stream.
|
private static UtilServer |
instance_
Default instance of this class.
|
private static java.util.logging.Logger |
logger_ |
private MultiURLMapperHandler |
mapperHandler_ |
private static java.util.regex.Pattern |
NUMBER_REGEX |
static java.lang.String |
PORT_PROP
System Property key giving a preferred port number for the server.
|
private ResourceHandler |
resourceHandler_ |
private HttpServer |
server_ |
private static java.util.regex.Pattern |
SLASH_REGEX |
Constructor and Description |
---|
UtilServer(HttpServer server)
Constructor.
|
Modifier and Type | Method and Description |
---|---|
(package private) static void |
copy(java.io.InputStream in,
java.io.OutputStream out)
Copies the content of an input stream to an output stream.
|
java.net.URL |
exportFile(java.io.File file)
Exposes a file in the local filesystem as a publicly visible URL.
|
java.net.URL |
exportResource(java.lang.String resource)
Exposes a resource from the JVM's classpath as a publicly visible URL.
|
java.lang.String |
getBasePath(java.lang.String txt)
May be used to return a unique base path for use with this class's
HttpServer.
|
static UtilServer |
getInstance()
Returns the default instance of this class.
|
MultiURLMapperHandler |
getMapperHandler()
Returns a handler for mapping local to external URLs associated with
this server.
|
ResourceHandler |
getResourceHandler()
Returns a handler for general purpose resource serving associated with
this server.
|
HttpServer |
getServer()
Returns the HttpServer associated with this object.
|
static void |
setInstance(UtilServer server)
Sets the default instance of this class.
|
private final HttpServer server_
private final java.util.Set baseSet_
private MultiURLMapperHandler mapperHandler_
private ResourceHandler resourceHandler_
public static final java.lang.String PORT_PROP
private static int BUFSIZ
private static UtilServer instance_
private static final java.util.regex.Pattern SLASH_REGEX
private static final java.util.regex.Pattern NUMBER_REGEX
private static final java.util.logging.Logger logger_
public UtilServer(HttpServer server) throws java.io.IOException
getInstance()
method.server
- HTTP server providing base servicesjava.io.IOException
public HttpServer getServer()
public MultiURLMapperHandler getMapperHandler()
public ResourceHandler getResourceHandler()
public java.net.URL exportResource(java.lang.String resource) throws java.io.IOException
resource
- fully qualified path to a resource in the current
classpath; separators are "/" charactersjava.io.IOException
public java.net.URL exportFile(java.io.File file) throws java.io.IOException
file
- a file on a filesystem visible from the local hostjava.io.IOException
public java.lang.String getBasePath(java.lang.String txt)
txt
- basic text for base pathtxt
, but may be adjusted to ensure uniquenesspublic static UtilServer getInstance() throws java.io.IOException
getInstance()
is called.java.io.IOException
public static void setInstance(UtilServer server)
server
- default instance to be returned by getInstance()
static void copy(java.io.InputStream in, java.io.OutputStream out) throws java.io.IOException
in
- input streamout
- output streamjava.io.IOException