Class JettyStarter

  • Direct Known Subclasses:
    JettyRunner

    @NotThreadSafe
    public class JettyStarter
    extends Object
    Run a standalone web application in Jetty on port 8080.
    http://localhost:8080/
    Author:
    Philip Helger
    • Field Detail

      • CONTAINER_INCLUDE_JAR_PATTERN_CLASSES

        public static final String CONTAINER_INCLUDE_JAR_PATTERN_CLASSES
        See Also:
        Constant Field Values
      • DEFAULT_CONTAINER_INCLUDE_JAR_PATTERN

        public static final String DEFAULT_CONTAINER_INCLUDE_JAR_PATTERN
      • DEFAULT_WEB_INF_INCLUDE_JAR_PATTERN

        public static final String DEFAULT_WEB_INF_INCLUDE_JAR_PATTERN
      • DEFAULT_ALLOW_ANNOTATION_BASED_CONFIG

        public static final boolean DEFAULT_ALLOW_ANNOTATION_BASED_CONFIG
        See Also:
        Constant Field Values
      • DEFAULT_ALLOW_DIRECTORY_LISTING

        public static final boolean DEFAULT_ALLOW_DIRECTORY_LISTING
        See Also:
        Constant Field Values
    • Constructor Detail

      • JettyStarter

        public JettyStarter​(@Nonnull
                            Class<?> aAppClass)
      • JettyStarter

        public JettyStarter​(@Nonnull @Nonempty
                            String sAppName)
    • Method Detail

      • getPort

        @Nonnegative
        public int getPort()
        Returns:
        Port to run on.
      • setPort

        @Nonnull
        public final JettyStarter setPort​(@Nonnegative
                                          int nPort)
        Set the port to be used to run the application. Defaults to 8080
        Parameters:
        nPort - The port to be used. Must be > 0.
        Returns:
        this for chaining
      • isRunStopMonitor

        public boolean isRunStopMonitor()
      • setRunStopMonitor

        @Nonnull
        public final JettyStarter setRunStopMonitor​(boolean bRunStopMonitor)
        Enable or disable the "stop monitor" that listens for the graceful shutdown. By default this is enabled.
        Parameters:
        bRunStopMonitor - true to enable it, false to disable it.
        Returns:
        this for chaining
      • setStopKey

        @Nonnull
        public final JettyStarter setStopKey​(@Nonnull
                                             String sStopKey)
        Set the hidden "stop key" that must be submitted to stop the server. Defaults to DEFAULT_STOP_KEY. If set here, it must also be set in JettyStopper.
        Parameters:
        sStopKey - The stop key to be used. May not be null.
        Returns:
        this for chaining
      • getStopPort

        public int getStopPort()
      • setStopPort

        @Nonnull
        public final JettyStarter setStopPort​(@Nonnegative
                                              int nStopPort)
        Set the port on which the "stop monitor" should be running. Defaults to DEFAULT_STOP_PORT. When running multiple Jettys at once, each instance must use it's own stop port. If this is set here, it must also be set in JettyStopper.
        Parameters:
        nStopPort - The stop port to be used. Must be > 0.
        Returns:
        this for chaining
      • setStopPort

        @Nonnull
        public final JettyStarter setStopPort​(@Nonnull
                                              IntUnaryOperator aStopPort)
        Set the port on which the "stop monitor" should be running. Defaults to DEFAULT_STOP_PORT. When running multiple Jettys at once, each instance must use it's own stop port. If this is set here, it must also be set in JettyStopper.
        This overload lets you set a function that takes as input the default port and you can calculate the stop port from it.
        Parameters:
        aStopPort - The stop port to be used. May not be null.
        Returns:
        this for chaining
        Since:
        8.3.2
      • isSpecialSessionMgr

        public boolean isSpecialSessionMgr()
      • setSpecialSessionMgr

        @Nonnull
        public final JettyStarter setSpecialSessionMgr​(boolean bSpecialSessionMgr)
        Parameters:
        bSpecialSessionMgr - true to set a session manager that allows for persistent activation and passivation of sessions.
        Returns:
        this for chaining
      • getResourceBase

        @Nonnull
        public org.eclipse.jetty.util.resource.Resource getResourceBase()
      • setResourceBase

        @Nonnull
        public final JettyStarter setResourceBase​(@Nonnull @Nonempty
                                                  String sResourceBase)
        Set the common resource base (directory) from which all web application resources will be loaded (servlet context root).
        Parameters:
        sResourceBase - The path. May neither be null nor empty.
        Returns:
        this for chaining
      • setResourceBase

        @Nonnull
        public final JettyStarter setResourceBase​(@Nonnull
                                                  org.eclipse.jetty.util.resource.Resource aResourceBase)
        Set the common resource base (directory) from which all web application resources will be loaded (servlet context root).
        Parameters:
        aResourceBase - The resource. May neither be null nor empty.
        Returns:
        this for chaining
      • setWebXmlResource

        @Nonnull
        public final JettyStarter setWebXmlResource​(@Nullable
                                                    String sWebXmlResource)
        Set the path to WEB-INF/web.xml. If unspecified, the default relative to the resource base is used.
        Parameters:
        sWebXmlResource - web.xml resource. May be null.
        Returns:
        this for chaining.
      • setContextPath

        @Nonnull
        public final JettyStarter setContextPath​(@Nonnull @Nonempty
                                                 String sContextPath)
        Set the context path in which the web application should run. By default this DEFAULT_CONTEXT_PATH
        Parameters:
        sContextPath - The new context path. May neither be null nor empty and must start with a slash.
        Returns:
        this for chaining
      • getContainerIncludeJarPattern

        @Nullable
        public String getContainerIncludeJarPattern()
      • setContainerIncludeJarPattern

        @Nonnull
        public final JettyStarter setContainerIncludeJarPattern​(@Nullable
                                                                String sContainerIncludeJarPattern)
        Set the container JAR pattern to be scanned for annotations. By default this DEFAULT_CONTAINER_INCLUDE_JAR_PATTERN
        Parameters:
        sContainerIncludeJarPattern - The new container JAR pattern. May be null to use the default.
        Returns:
        this for chaining
      • getWebInfIncludeJarPattern

        @Nullable
        public String getWebInfIncludeJarPattern()
      • getThreadPool

        @Nullable
        public org.eclipse.jetty.util.thread.ThreadPool getThreadPool()
      • setThreadPool

        @Nonnull
        public final JettyStarter setThreadPool​(@Nullable
                                                org.eclipse.jetty.util.thread.ThreadPool aThreadPool)
        Set the thread pool to use.
        Parameters:
        aThreadPool - Thread pool. May be null to use the default thread pool.
        Returns:
        this
        Since:
        7.0.6
      • isAllowAnnotationBasedConfig

        public boolean isAllowAnnotationBasedConfig()
      • setAllowAnnotationBasedConfig

        @Nonnull
        public final JettyStarter setAllowAnnotationBasedConfig​(boolean bAllowAnnotationBasedConfig)
        Enable or disable annotation based scanning. By default it is enabled. Disable it for better performance.
        Parameters:
        bAllowAnnotationBasedConfig - false to disable it.
        Returns:
        this
        Since:
        8.0.0
      • isAllowDirectoryListing

        public boolean isAllowDirectoryListing()
      • setAllowDirectoryListing

        @Nonnull
        public final JettyStarter setAllowDirectoryListing​(boolean bAllowDirectoryListing)
        Enable or disable the listing of Directories. Turned off by default for security reasons.
        Parameters:
        bAllowDirectoryListing - true to enable it.
        Returns:
        this
        Since:
        8.3.7
      • setSessionCookieName

        @Nonnull
        public final JettyStarter setSessionCookieName​(@Nullable
                                                       String sSessionCookieName)
        Set the session cookie name. Default is "PHOTONSESSIONID". When running different applications ensure to use different names to ensure you can test them in the same browser in the same session.
        Parameters:
        sSessionCookieName - New name or null to use Jetty default.
        Returns:
        this for chaining
        Since:
        8.1.0
      • customizeHttpConfiguration

        @OverrideOnDemand
        protected void customizeHttpConfiguration​(@Nonnull
                                                  org.eclipse.jetty.server.HttpConfiguration aHttpConfiguration)
                                           throws Exception
        Customize
        Parameters:
        aHttpConfiguration - HTTP configuration
        Throws:
        Exception - in case of error
        Since:
        8.4.0
      • customizeHttpConnectionFactory

        @OverrideOnDemand
        protected void customizeHttpConnectionFactory​(@Nonnull
                                                      org.eclipse.jetty.server.HttpConnectionFactory aHttpConnectionFactory)
                                               throws Exception
        Customize
        Parameters:
        aHttpConnectionFactory - HTTP connection factory
        Throws:
        Exception - in case of error
        Since:
        8.4.0
      • customizeServerConnector

        @OverrideOnDemand
        protected void customizeServerConnector​(@Nonnull
                                                org.eclipse.jetty.server.ServerConnector aServerConnector)
                                         throws Exception
        Customize
        Parameters:
        aServerConnector - Server connector
        Throws:
        Exception - in case of error
      • customizeServer

        @OverrideOnDemand
        protected void customizeServer​(@Nonnull
                                       org.eclipse.jetty.server.Server aServer)
                                throws Exception
        Customize
        Parameters:
        aServer - Server
        Throws:
        Exception - in case of error
      • customizeWebAppCtx

        @OverrideOnDemand
        protected void customizeWebAppCtx​(@Nonnull
                                          org.eclipse.jetty.webapp.WebAppContext aWebAppCtx)
                                   throws Exception
        Customize
        Parameters:
        aWebAppCtx - Web application context
        Throws:
        Exception - in case of error
      • createWebAppContext

        @Nonnull
        public org.eclipse.jetty.webapp.WebAppContext createWebAppContext​(@Nonnull @Nonempty
                                                                          String sContextPath)
                                                                   throws Exception
        Create a new WebAppContext based on the settings of this class.
        Parameters:
        sContextPath - The context path to be used. May neither be null nor empty.
        Returns:
        The created object. Never null.
        Throws:
        Exception - In case of error
      • customizeHandlerList

        protected void customizeHandlerList​(@Nonnull
                                            org.eclipse.jetty.server.handler.HandlerList aHandlerList)
                                     throws Exception
        Customize the HandlerList
        Parameters:
        aHandlerList - The HandlerList. Never null.
        Throws:
        Exception - in case of error
      • onServerStarted

        @OverrideOnDemand
        protected void onServerStarted​(@Nonnull
                                       org.eclipse.jetty.server.Server aServer)
                                throws Exception
        Callback to be invoked when server successfully finished startup.
        Parameters:
        aServer - The server that was started. Never null.
        Throws:
        Exception - in case of error
        Since:
        7.0.2
      • onServerStartFailure

        @OverrideOnDemand
        protected void onServerStartFailure​(@Nonnull
                                            org.eclipse.jetty.server.Server aServer,
                                            @Nonnull
                                            Throwable t)
                                     throws Exception
        Callback to be invoked when server failed startup.
        Parameters:
        aServer - The server that was started. Never null.
        t - The exception that occurred
        Throws:
        Exception - in case of error
        Since:
        7.0.2
      • run

        public void run()
                 throws Exception
        Run Jetty with the provided settings.
        Throws:
        Exception - In case something goes wrong