public interface WebContainer
// Create and start GlassFish
GlassFish glassfish = GlassFishRuntime.bootstrap().newGlassFish();
glassfish.start();
// Access WebContainer
WebContainer container = glassfish.getService(WebContainer.class);
// Create and add WebListener
// By default, when GlassFish Embedded Server starts, no web listener is enabled
WebListener listener = container.createWebListener("listener-1", HttpListener.class);
listener.setPort(8080);
container.addWebListener(listener);
// Create and register web resources Context.
File docroot = new File(path_to_web_resources);
Context context = container.createContext(docroot);
container.addContext(context, "contextroot_to_register");
// Create and add VirtualServer
// By default, when GlassFish Embedded Server starts,
// a virtual server named server starts automatically.
VirtualServer virtualServer = (VirtualServer)
container.createVirtualServer("embedded-server", new File(docroot_of_VirtualServer));
VirtualServerConfig config = new VirtualServerConfig();
config.setHostNames("localhost");
virtualServer.setConfig(config);
container.addVirtualServer(virtualServer);
| Modifier and Type | Method and Description |
|---|---|
void |
addContext(Context context,
String contextRoot)
Registers the given Context with all VirtualServer
at the given context root.
|
void |
addVirtualServer(VirtualServer virtualServer)
Adds the given VirtualServer to this
WebContainer.
|
void |
addWebListener(WebListener webListener)
Adds the given WebListener to this
WebContainer.
|
Context |
createContext(File docRoot)
Creates a Context and configures it with the given
docroot and classloader.
|
Context |
createContext(File docRoot,
ClassLoader classLoader)
Creates a Context and configures it with the given
docroot and classloader.
|
Context |
createContext(File docRoot,
String contextRoot,
ClassLoader classLoader)
Creates a Context, configures it with the given
docroot and classloader, and registers it with all
VirtualServer.
|
VirtualServer |
createVirtualServer(String id,
File docRoot)
Creates a VirtualServer with the given id and docroot, and
maps it to all WebListener instances.
|
VirtualServer |
createVirtualServer(String id,
File docRoot,
WebListener... webListeners)
Creates a VirtualServer with the given id and docroot, and
maps it to the given WebListener instances.
|
<T extends WebListener> |
createWebListener(String id,
Class<T> c)
Creates a WebListener from the given class type and
assigns the given id to it.
|
VirtualServer |
getVirtualServer(String id)
Finds the VirtualServer with the given id.
|
Collection<VirtualServer> |
getVirtualServers()
Gets the collection of VirtualServer instances registered
with this WebContainer.
|
WebListener |
getWebListener(String id)
Finds the WebListener with the given id.
|
Collection<WebListener> |
getWebListeners()
Gets the collection of WebListener instances registered
with this WebContainer.
|
void |
removeContext(Context context)
Stops the given Context and removes it from all
VirtualServer.
|
void |
removeVirtualServer(VirtualServer virtualServer)
Stops the given virtualServer and removes it from this
WebContainer.
|
void |
removeWebListener(WebListener webListener)
Stops the given webListener and removes it from this
WebContainer.
|
void |
setConfiguration(WebContainerConfig config)
Sets the embedded configuration for this embedded instance.
|
void |
setLogLevel(Level level)
Sets log level
|
void setConfiguration(WebContainerConfig config)
config - the embedded instance configurationContext createContext(File docRoot)
The classloader of the class on which this method is called will be used.
In order to access the new Context or any of its resources, the Context must be registered with a VirtualServer that has been started using either WebContainer#addContext or VirtualServer#addContext method.
docRoot - the docroot of the ContextVirtualServer.addContext(org.glassfish.embeddable.web.Context, java.lang.String)Context createContext(File docRoot, ClassLoader classLoader)
The given classloader will be set as the thread's context classloader whenever the new Context or any of its resources are asked to process a request. If a null classloader is passed, the classloader of the class on which this method is called will be used.
In order to access the new Context or any of its resources, the Context must be registered with a VirtualServer that has been started using either WebContainer#addContext or VirtualServer#addContext method.
docRoot - the docroot of the ContextclassLoader - the classloader of the ContextVirtualServer.addContext(org.glassfish.embeddable.web.Context, java.lang.String)Context createContext(File docRoot, String contextRoot, ClassLoader classLoader)
The given classloader will be set as the thread's context classloader whenever the new Context or any of its resources are asked to process a request. If a null classloader is passed, the classloader of the class on which this method is called will be used.
docRoot - the docroot of the ContextcontextRoot - the contextroot at which to registerclassLoader - the classloader of the Contextvoid addContext(Context context, String contextRoot) throws ConfigException, GlassFishException
If VirtualServer has already been started, the given context will be started as well.
context - the Context to registercontextRoot - the context root at which to registerConfigException - if a Context already exists
at the given context root on VirtualServerGlassFishException - if the given context fails
to be startedvoid removeContext(Context context) throws ConfigException, GlassFishException
context - the Context to be stopped and removedGlassFishException - if an error occurs during the stopping
or removal of the given contextConfigException<T extends WebListener> T createWebListener(String id, Class<T> c) throws InstantiationException, IllegalAccessException
id - the id of the new WebListenerc - the class from which to instantiate the
WebListenerIllegalAccessException - if the given Class or
its nullary constructor is not accessible.InstantiationException - if the given Class
represents an abstract class, an interface, an array class,
a primitive type, or void; or if the class has no nullary
constructor; or if the instantiation fails for some other reason.ExceptionInInitializerError - if the initialization
failsSecurityException - if a security manager, s, is
present and any of the following conditions is met:
s.checkMemberAccess(this, Member.PUBLIC) denies
creation of new instances of the given Class
s.checkPackageAccess() denies access to the package
of this class
void addWebListener(WebListener webListener) throws ConfigException, GlassFishException
If this WebContainer has already been started, the given webListener will be started as well.
webListener - the WebListener to addConfigException - if a WebListener with the
same id has already been registered with this
WebContainerGlassFishException - if the given webListener fails
to be startedWebListener getWebListener(String id)
id - the id of the WebListener to findCollection<WebListener> getWebListeners()
void removeWebListener(WebListener webListener) throws GlassFishException
webListener - the WebListener to be stopped
and removedGlassFishException - if an error occurs during the stopping
or removal of the given webListenerVirtualServer createVirtualServer(String id, File docRoot, WebListener... webListeners)
id - the id of the VirtualServerdocRoot - the docroot of the VirtualServerwebListeners - the list of WebListener instances from
which the VirtualServer will receive requestsVirtualServer createVirtualServer(String id, File docRoot)
id - the id of the VirtualServerdocRoot - the docroot of the VirtualServervoid addVirtualServer(VirtualServer virtualServer) throws ConfigException, GlassFishException
If this WebContainer has already been started, the given virtualServer will be started as well.
virtualServer - the VirtualServer to addConfigException - if a VirtualServer with the
same id has already been registered with this
WebContainerGlassFishException - if the given virtualServer fails
to be startedVirtualServer getVirtualServer(String id)
id - the id of the VirtualServer to findCollection<VirtualServer> getVirtualServers()
void removeVirtualServer(VirtualServer virtualServer) throws GlassFishException
virtualServer - the VirtualServer to be stopped
and removedGlassFishException - if an error occurs during the stopping
or removal of the given virtualServervoid setLogLevel(Level level)
level - log levelCopyright © 2019. All rights reserved.