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.net.URL) 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
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static java.lang.String PORT_PROP
      System Property key giving a preferred port number for the server.
    • Constructor Summary

      Constructors 
      Constructor Description
      UtilServer​(HttpServer server)
      Constructor.
    • Field Detail

      • 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
    • 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.net.URL localUrl)
                                    throws java.io.IOException
        Exposes a resource from a given URL as a publicly visible URL. This is typically used if the given localUrl is only visible locally, for instance on this JVM's classpath.
        Parameters:
        localUrl - URL visible at least within this JVM
        Returns:
        URL for external reference to the resource
        Throws:
        java.io.IOException
      • exportResource

        public java.net.URL exportResource​(java.lang.String resource)
                                    throws java.io.IOException
        Deprecated.
        since after 1.3.5, use instead exportResource(java.net.URL)
        Exposes a resource from the JVM's classpath as a publicly visible URL. The classloader of this class is used, which may not be the right one for the resource in question. For that reason, this utility method is deprecated in favour of the overloaded method that takes a URL.
        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

        public 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