Class InternalWebServer

All Implemented Interfaces:
org.osgi.framework.BundleActivator

public class InternalWebServer extends AbstractUIPlugin
The activator class controls the plug-in life cycle for the Internal Web Server using Jetty.
Author:
Christopher Mindus
  • Field Details

  • Constructor Details

    • InternalWebServer

      public InternalWebServer()
      The constructor
  • Method Details

    • start

      public void start(org.osgi.framework.BundleContext context) throws Exception
      Specified by:
      start in interface org.osgi.framework.BundleActivator
      Overrides:
      start in class AbstractUIPlugin
      Throws:
      Exception
    • getInstance

      public static InternalWebServer getInstance()
      Returns the shared instance.
      Returns:
      the shared instance.
    • startWebServer

      public int startWebServer(File parentDir, IExternalBrowserPages extPages, IWSFileProviderServer fileProvider, WebSocketAcceptor acceptor)
      Starts the web server.
      Parameters:
      parentDir - The directory to use for the QUIC work directories, null for current directory.
      extPages - External pages implementor, null for none.
      fileProvider - File provider interface.
      acceptor - The acceptor for engine requests.
      Returns:
      The port used, 0 for failure.
    • startWebServer

      public int startWebServer(File parentDir, IExternalBrowserPages extPages, IWSFileProviderServer fileProvider, WebSocketAcceptor acceptor, Consumer<IntegratedWebServer> startedListener)
      Starts the web server.
      Parameters:
      parentDir - The directory to use for "quic-work" directory, null for current directory.
      extPages - External pages implementor, null for none.
      fileProvider - File provider interface.
      acceptor - The acceptor for engine requests.
      startedListener - Called with the server each time it has started, here or later from the Preferences page (P-D-6); null for none.
      Returns:
      The port used, 0 for failure.
    • restartWebServer

      public String restartWebServer()
      Starts the web server again from the Preferences page, with the preferences as they are now (the identity included, read from the saved preferences): stops it first when it runs, and starts one that never started (P-D-6: until CC-FIX-PORT-RANGE-A "Apply and Restart" refused a server that had failed to start, and only a restart of iiziGo recovered it).
      Returns:
      null for success, otherwise what failed.
    • getWorkspacePath

      public static String getWorkspacePath()
      Gets the path of this workspace.
      Returns:
      The path, empty when unknown.
    • getStartFailure

      public String getStartFailure()
      Gets what made the last start fail.
      Returns:
      The text, null when the last start did not fail (or none was made).
    • getBoundOffset

      public int getBoundOffset()
      Gets the offset of the port ranges the server bound.
      Returns:
      The offset, -1 when not started.
    • getHttp3Port

      public int getHttp3Port()
      Gets the UDP port of HTTP/3.
      Returns:
      The port, zero when not started or without HTTP/3.
    • getInstances

      public IiziGoInstances getInstances()
      Gets the registry of the live iiziGo and the port claims.
      Returns:
      The registry.
    • forgetWorkspacePort

      public void forgetWorkspacePort()
      Forgets this workspace's sticky port and claim ("Forget" on the Preferences page): its next start begins at the first offset no other workspace claims.
    • getClaimedByOthers

      public Set<Integer> getClaimedByOthers()
      Gets the other workspaces' claimed offsets, for the Preferences page.
      Returns:
      The offsets.
    • addStateListener

      public void addStateListener(Runnable listener)
      Adds a listener told when the web server started or stopped; called on the thread that did it.
      Parameters:
      listener - The listener.
    • removeStateListener

      public void removeStateListener(Runnable listener)
      Removes a listener.
      Parameters:
      listener - The listener.
    • stopWebServer

      public boolean stopWebServer(long maxTime)
      Stops the web server.

      No web server running is not a failure: there is nothing to stop, and true is returned. That is the case at the plug-in's stop when iiziGo's own stop has stopped the server already (WebSocketServer.dispose()).

      Parameters:
      maxTime - Max time to wait for server to stop, <=0 is forever.
      Returns:
      true when no web server runs any more (stopped now, or none was running), false for a timeout: the running server did not stop within maxTime.
    • getWebServerPort

      public int getWebServerPort()
      Gets the started web server port.
      Returns:
      The port number, zero for not started.
    • getSecureWebServerPort

      public int getSecureWebServerPort()
      Gets the started secure (HTTPS) web server port.
      Returns:
      The port number, zero for not started.
    • stop

      public void stop(org.osgi.framework.BundleContext context) throws Exception
      Specified by:
      stop in interface org.osgi.framework.BundleActivator
      Overrides:
      stop in class AbstractUIPlugin
      Throws:
      Exception
    • getRootDirectory

      public File getRootDirectory()
      Gets the root directory.
      Returns:
      The root directory, null if not started.
    • getHTTPFileUrl

      public String getHTTPFileUrl(String fileName)
      Gets the URL string for a file in the root using the HTTP protocol.
      Parameters:
      fileName - File name relative the root.
      Returns:
      A String like "http://127.0.0.1:port/name".
    • loadHTML

      public String loadHTML(String fileName)
      Loads the HTML file contents encoded in UTF-8 and returns it.
      Parameters:
      fileName - File name relative the root.
      Returns:
      The text or HTML file contents. If there is an error, a HTML document will be returned showing the error.
    • getFileUrl

      public String getFileUrl(String fileName)
      Gets the URL string for a file in the root using the File protocol.
      Parameters:
      fileName - File name relative the root.
      Returns:
      A String like "file:...";
    • getServer

      public IntegratedWebServer getServer()
      Gets the Integrated Jetty Server instance.
      Returns:
      The integrated web server instance, or null if not yet started.
    • getWebServerMapping

      public File getWebServerMapping(String mappedFile)
      Gets the map of web server mappings for files.
      Parameters:
      mappedFile - The file to map.
      Returns:
      The mapping from String (such as "dojo.root" or "iiziRun.root") to File Returns null if not found.