Package com.helger.photon.jetty
Class JettyStarter
- java.lang.Object
-
- com.helger.photon.jetty.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 Summary
Fields Modifier and Type Field Description static StringCONTAINER_INCLUDE_JAR_PATTERN_ALLstatic StringCONTAINER_INCLUDE_JAR_PATTERN_CLASSESstatic StringCONTAINER_INCLUDE_JAR_PATTERN_JARstatic booleanDEFAULT_ALLOW_ANNOTATION_BASED_CONFIGstatic booleanDEFAULT_ALLOW_DIRECTORY_LISTINGstatic StringDEFAULT_CONTAINER_INCLUDE_JAR_PATTERNstatic StringDEFAULT_CONTEXT_PATHstatic intDEFAULT_PORTstatic StringDEFAULT_SESSION_COOKIE_NAMEstatic StringDEFAULT_STOP_KEYstatic intDEFAULT_STOP_PORTstatic StringDEFAULT_WEB_INF_INCLUDE_JAR_PATTERN
-
Constructor Summary
Constructors Constructor Description JettyStarter(Class<?> aAppClass)JettyStarter(String sAppName)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description org.eclipse.jetty.webapp.WebAppContextcreateWebAppContext(String sContextPath)Create a newWebAppContextbased on the settings of this class.protected voidcustomizeHandlerList(org.eclipse.jetty.server.handler.HandlerList aHandlerList)Customize theHandlerListprotected voidcustomizeHttpConfiguration(org.eclipse.jetty.server.HttpConfiguration aHttpConfiguration)Customizeprotected voidcustomizeHttpConnectionFactory(org.eclipse.jetty.server.HttpConnectionFactory aHttpConnectionFactory)Customizeprotected voidcustomizeServer(org.eclipse.jetty.server.Server aServer)Customizeprotected voidcustomizeServerConnector(org.eclipse.jetty.server.ServerConnector aServerConnector)Customizeprotected voidcustomizeWebAppCtx(org.eclipse.jetty.webapp.WebAppContext aWebAppCtx)CustomizeStringgetContainerIncludeJarPattern()StringgetContextPath()intgetPort()org.eclipse.jetty.util.resource.ResourcegetResourceBase()StringgetSessionCookieName()StringgetStopKey()intgetStopPort()org.eclipse.jetty.util.thread.ThreadPoolgetThreadPool()StringgetWebInfIncludeJarPattern()StringgetWebXmlResource()booleanisAllowAnnotationBasedConfig()booleanisAllowDirectoryListing()booleanisRunStopMonitor()booleanisSpecialSessionMgr()protected voidonServerStarted(org.eclipse.jetty.server.Server aServer)Callback to be invoked when server successfully finished startup.protected voidonServerStartFailure(org.eclipse.jetty.server.Server aServer, Throwable t)Callback to be invoked when server failed startup.voidrun()Run Jetty with the provided settings.JettyStartersetAllowAnnotationBasedConfig(boolean bAllowAnnotationBasedConfig)Enable or disable annotation based scanning.JettyStartersetAllowDirectoryListing(boolean bAllowDirectoryListing)Enable or disable the listing of Directories.JettyStartersetContainerIncludeJarPattern(String sContainerIncludeJarPattern)Set the container JAR pattern to be scanned for annotations.JettyStartersetContextPath(String sContextPath)Set the context path in which the web application should run.JettyStartersetPort(int nPort)Set the port to be used to run the application.JettyStartersetResourceBase(String sResourceBase)Set the common resource base (directory) from which all web application resources will be loaded (servlet context root).JettyStartersetResourceBase(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).JettyStartersetRunStopMonitor(boolean bRunStopMonitor)Enable or disable the "stop monitor" that listens for the graceful shutdown.JettyStartersetSessionCookieName(String sSessionCookieName)Set the session cookie name.JettyStartersetSpecialSessionMgr(boolean bSpecialSessionMgr)JettyStartersetStopKey(String sStopKey)Set the hidden "stop key" that must be submitted to stop the server.JettyStartersetStopPort(int nStopPort)Set the port on which the "stop monitor" should be running.JettyStartersetStopPort(IntUnaryOperator aStopPort)Set the port on which the "stop monitor" should be running.JettyStartersetThreadPool(org.eclipse.jetty.util.thread.ThreadPool aThreadPool)Set the thread pool to use.JettyStartersetWebInfIncludeJarPattern(String sWebInfIncludeJarPattern)JettyStartersetWebXmlResource(String sWebXmlResource)Set the path to WEB-INF/web.xml.
-
-
-
Field Detail
-
CONTAINER_INCLUDE_JAR_PATTERN_JAR
public static final String CONTAINER_INCLUDE_JAR_PATTERN_JAR
- See Also:
- Constant Field Values
-
CONTAINER_INCLUDE_JAR_PATTERN_CLASSES
public static final String CONTAINER_INCLUDE_JAR_PATTERN_CLASSES
- See Also:
- Constant Field Values
-
CONTAINER_INCLUDE_JAR_PATTERN_ALL
public static final String CONTAINER_INCLUDE_JAR_PATTERN_ALL
- See Also:
- Constant Field Values
-
DEFAULT_PORT
public static final int DEFAULT_PORT
- See Also:
- Constant Field Values
-
DEFAULT_STOP_KEY
public static final String DEFAULT_STOP_KEY
- See Also:
- Constant Field Values
-
DEFAULT_STOP_PORT
public static final int DEFAULT_STOP_PORT
- See Also:
- Constant Field Values
-
DEFAULT_CONTEXT_PATH
public static final String DEFAULT_CONTEXT_PATH
- 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_SESSION_COOKIE_NAME
public static final String DEFAULT_SESSION_COOKIE_NAME
- See Also:
- Constant Field Values
-
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
-
-
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-trueto enable it,falseto 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 toDEFAULT_STOP_KEY. If set here, it must also be set inJettyStopper.- Parameters:
sStopKey- The stop key to be used. May not benull.- 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 toDEFAULT_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 inJettyStopper.- 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 toDEFAULT_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 inJettyStopper.
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 benull.- Returns:
- this for chaining
- Since:
- 8.3.2
-
isSpecialSessionMgr
public boolean isSpecialSessionMgr()
-
setSpecialSessionMgr
@Nonnull public final JettyStarter setSpecialSessionMgr(boolean bSpecialSessionMgr)
- Parameters:
bSpecialSessionMgr-trueto 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 benullnor 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 benullnor 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 benull.- 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 thisDEFAULT_CONTEXT_PATH- Parameters:
sContextPath- The new context path. May neither benullnor empty and must start with a slash.- Returns:
- this for chaining
-
setContainerIncludeJarPattern
@Nonnull public final JettyStarter setContainerIncludeJarPattern(@Nullable String sContainerIncludeJarPattern)
Set the container JAR pattern to be scanned for annotations. By default thisDEFAULT_CONTAINER_INCLUDE_JAR_PATTERN- Parameters:
sContainerIncludeJarPattern- The new container JAR pattern. May benullto use the default.- Returns:
- this for chaining
-
setWebInfIncludeJarPattern
@Nonnull public final JettyStarter setWebInfIncludeJarPattern(@Nullable String sWebInfIncludeJarPattern)
-
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 benullto 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-falseto 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-trueto enable it.- Returns:
- this
- Since:
- 8.3.7
-
getSessionCookieName
@Nullable public String getSessionCookieName()
- Returns:
- The name of the session cookie or null to use Jetty default. The
default values is
DEFAULT_SESSION_COOKIE_NAME.
-
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 ornullto 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 newWebAppContextbased on the settings of this class.- Parameters:
sContextPath- The context path to be used. May neither benullnor 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 theHandlerList- Parameters:
aHandlerList- TheHandlerList. Nevernull.- 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. Nevernull.- 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. Nevernull.t- The exception that occurred- Throws:
Exception- in case of error- Since:
- 7.0.2
-
-