Class UtilServer


  • public class UtilServer
    extends java.lang.Object
    Utility class for use with HttpServer.

    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).

    Since:
    22 Jul 2009
    Author:
    Mark Taylor
    • Constructor Summary

      Constructors 
      Constructor Description
      UtilServer​(HttpServer server)
      Constructor.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method 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.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • baseSet_

        private final java.util.Set baseSet_
      • PORT_PROP

        public static final java.lang.String PORT_PROP
        System Property key giving a preferred port number for the server. If unset, or 0, or the chosen port is occupied, a system-chosen value will be used. The property name is "jsamp.server.port".
        See Also:
        Constant Field Values
      • BUFSIZ

        private static int BUFSIZ
        Buffer size for copy data from input to output stream.
      • instance_

        private static UtilServer instance_
        Default instance of this class.
      • SLASH_REGEX

        private static final java.util.regex.Pattern SLASH_REGEX
      • NUMBER_REGEX

        private static final java.util.regex.Pattern NUMBER_REGEX
      • logger_

        private static final java.util.logging.Logger logger_
    • Constructor Detail

      • UtilServer

        public UtilServer​(HttpServer server)
                   throws java.io.IOException
        Constructor. Note, it may be more appropriate to use the getInstance() method.
        Parameters:
        server - HTTP server providing base services
        Throws:
        java.io.IOException
    • Method Detail

      • getServer

        public HttpServer getServer()
        Returns the HttpServer associated with this object.
        Returns:
        a running server instance
      • getMapperHandler

        public MultiURLMapperHandler getMapperHandler()
        Returns a handler for mapping local to external URLs associated with this server.
        Returns:
        url mapping handler
      • getResourceHandler

        public ResourceHandler getResourceHandler()
        Returns a handler for general purpose resource serving associated with this server.
        Returns:
        resource serving handler
      • exportResource

        public java.net.URL exportResource​(java.lang.String resource)
                                    throws java.io.IOException
        Exposes a resource from the JVM's classpath as a publicly visible URL. The classloader of this class is used.
        Parameters:
        resource - fully qualified path to a resource in the current classpath; separators are "/" characters
        Returns:
        URL for external reference to the resource
        Throws:
        java.io.IOException
      • exportFile

        public java.net.URL exportFile​(java.io.File file)
                                throws java.io.IOException
        Exposes a file in the local filesystem as a publicly visible URL.
        Parameters:
        file - a file on a filesystem visible from the local host
        Returns:
        URL for external reference to the resource
        Throws:
        java.io.IOException
      • getBasePath

        public java.lang.String getBasePath​(java.lang.String txt)
        May be used to return a unique base path for use with this class's HttpServer. If all users of this server use this method to get base paths for use with different handlers, nameclash avoidance is guaranteed.
        Parameters:
        txt - basic text for base path
        Returns:
        base path; will likely bear some resemblance to txt, but may be adjusted to ensure uniqueness
      • getInstance

        public static UtilServer getInstance()
                                      throws java.io.IOException
        Returns the default instance of this class. The first time this method is called a new daemon UtilServer is (lazily) created, and started. Any subsequent calls will return the same object, unless getInstance() is called.
        Returns:
        default instance of this class
        Throws:
        java.io.IOException
      • setInstance

        public static void setInstance​(UtilServer server)
        Sets the default instance of this class.
        Parameters:
        server - default instance to be returned by getInstance()
      • copy

        static void copy​(java.io.InputStream in,
                         java.io.OutputStream out)
                  throws java.io.IOException
        Copies the content of an input stream to an output stream. The input stream is always closed on exit; the output stream is not.
        Parameters:
        in - input stream
        out - output stream
        Throws:
        java.io.IOException