Interface WebContainer

  • All Known Implementing Classes:
    WebContainerImpl

    public interface WebContainer
    Class representing an embedded web container, which supports the programmatic creation of different types of web protocol listeners and virtual servers, and the registration of static and dynamic web resources into the URI namespace. WebContainer service can be accessed using GlassFish instance.

    Usage example:

          // 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);
     
    • Method Detail

      • setConfiguration

        void setConfiguration​(WebContainerConfig config)
        Sets the embedded configuration for this embedded instance. Such configuration will always override any xml based configuration.
        Parameters:
        config - the embedded instance configuration
      • createContext

        Context createContext​(File docRoot)
        Creates a Context and configures it with the given docroot and classloader.

        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.

        Parameters:
        docRoot - the docroot of the Context
        Returns:
        the new Context
        See Also:
        VirtualServer.addContext(org.glassfish.embeddable.web.Context, java.lang.String)
      • createContext

        Context createContext​(File docRoot,
                              ClassLoader classLoader)
        Creates a Context and configures it with the given docroot and 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.

        Parameters:
        docRoot - the docroot of the Context
        classLoader - the classloader of the Context
        Returns:
        the new Context
        See Also:
        VirtualServer.addContext(org.glassfish.embeddable.web.Context, java.lang.String)
      • createContext

        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.

        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.

        Parameters:
        docRoot - the docroot of the Context
        contextRoot - the contextroot at which to register
        classLoader - the classloader of the Context
        Returns:
        the new Context
      • addContext

        void addContext​(Context context,
                        String contextRoot)
                 throws ConfigException,
                        GlassFishException
        Registers the given Context with all VirtualServer at the given context root.

        If VirtualServer has already been started, the given context will be started as well.

        Parameters:
        context - the Context to register
        contextRoot - the context root at which to register
        Throws:
        ConfigException - if a Context already exists at the given context root on VirtualServer
        GlassFishException - if the given context fails to be started
      • createWebListener

        <T extends WebListener> T createWebListener​(String id,
                                                    Class<T> c)
                                             throws InstantiationException,
                                                    IllegalAccessException
        Creates a WebListener from the given class type and assigns the given id to it.
        Parameters:
        id - the id of the new WebListener
        c - the class from which to instantiate the WebListener
        Returns:
        the new WebListener instance
        Throws:
        IllegalAccessException - 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 fails
        SecurityException - if a security manager, s, is present and any of the following conditions is met:
        • invocation of s.checkMemberAccess(this, Member.PUBLIC) denies creation of new instances of the given Class
        • the caller's class loader is not the same as or an ancestor of the class loader for the current class and invocation of s.checkPackageAccess() denies access to the package of this class
      • addWebListener

        void addWebListener​(WebListener webListener)
                     throws ConfigException,
                            GlassFishException
        Adds the given WebListener to this WebContainer.

        If this WebContainer has already been started, the given webListener will be started as well.

        Parameters:
        webListener - the WebListener to add
        Throws:
        ConfigException - if a WebListener with the same id has already been registered with this WebContainer
        GlassFishException - if the given webListener fails to be started
      • getWebListener

        WebListener getWebListener​(String id)
        Finds the WebListener with the given id.
        Parameters:
        id - the id of the WebListener to find
        Returns:
        the WebListener with the given id, or null if no WebListener with that id has been registered with this WebContainer
      • getWebListeners

        Collection<WebListener> getWebListeners()
        Gets the collection of WebListener instances registered with this WebContainer.
        Returns:
        the (possibly empty) collection of WebListener instances registered with this WebContainer
      • removeWebListener

        void removeWebListener​(WebListener webListener)
                        throws GlassFishException
        Stops the given webListener and removes it from this WebContainer.
        Parameters:
        webListener - the WebListener to be stopped and removed
        Throws:
        GlassFishException - if an error occurs during the stopping or removal of the given webListener
      • createVirtualServer

        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.
        Parameters:
        id - the id of the VirtualServer
        docRoot - the docroot of the VirtualServer
        webListeners - the list of WebListener instances from which the VirtualServer will receive requests
        Returns:
        the new VirtualServer instance
      • createVirtualServer

        VirtualServer createVirtualServer​(String id,
                                          File docRoot)
        Creates a VirtualServer with the given id and docroot, and maps it to all WebListener instances.
        Parameters:
        id - the id of the VirtualServer
        docRoot - the docroot of the VirtualServer
        Returns:
        the new VirtualServer instance
      • addVirtualServer

        void addVirtualServer​(VirtualServer virtualServer)
                       throws ConfigException,
                              GlassFishException
        Adds the given VirtualServer to this WebContainer.

        If this WebContainer has already been started, the given virtualServer will be started as well.

        Parameters:
        virtualServer - the VirtualServer to add
        Throws:
        ConfigException - if a VirtualServer with the same id has already been registered with this WebContainer
        GlassFishException - if the given virtualServer fails to be started
      • getVirtualServer

        VirtualServer getVirtualServer​(String id)
        Finds the VirtualServer with the given id.
        Parameters:
        id - the id of the VirtualServer to find
        Returns:
        the VirtualServer with the given id, or null if no VirtualServer with that id has been registered with this WebContainer
      • getVirtualServers

        Collection<VirtualServer> getVirtualServers()
        Gets the collection of VirtualServer instances registered with this WebContainer.
        Returns:
        the (possibly empty) collection of VirtualServer instances registered with this WebContainer
      • removeVirtualServer

        void removeVirtualServer​(VirtualServer virtualServer)
                          throws GlassFishException
        Stops the given virtualServer and removes it from this WebContainer.
        Parameters:
        virtualServer - the VirtualServer to be stopped and removed
        Throws:
        GlassFishException - if an error occurs during the stopping or removal of the given virtualServer
      • setLogLevel

        void setLogLevel​(Level level)
        Sets log level
        Parameters:
        level - log level