Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
   /*
    * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
    *
    * Copyright (c) 1997-2012 Oracle and/or its affiliates. All rights reserved.
    *
    * The contents of this file are subject to the terms of either the GNU
    * General Public License Version 2 only ("GPL") or the Common Development
    * and Distribution License("CDDL") (collectively, the "License").  You
    * may not use this file except in compliance with the License.  You can
   * obtain a copy of the License at
   * https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
   * or packager/legal/LICENSE.txt.  See the License for the specific
   * language governing permissions and limitations under the License.
   *
   * When distributing the software, include this License Header Notice in each
   * file and include the License file at packager/legal/LICENSE.txt.
   *
   * GPL Classpath Exception:
   * Oracle designates this particular file as subject to the "Classpath"
   * exception as provided by Oracle in the GPL Version 2 section of the License
   * file that accompanied this code.
   *
   * Modifications:
   * If applicable, add the following below the License Header, with the fields
   * enclosed by brackets [] replaced by your own identifying information:
   * "Portions Copyright [year] [name of copyright owner]"
   *
   * Contributor(s):
   * If you wish your version of this file to be governed by only the CDDL or
   * only the GPL Version 2, indicate your decision by adding "[Contributor]
   * elects to include this software in this distribution under the [CDDL or GPL
   * Version 2] license."  If you don't indicate a single choice of license, a
   * recipient has the option to distribute your version of this file under
   * either the CDDL, the GPL Version 2 or to extend the choice of license to
   * its licensees as provided above.  However, if you add GPL Version 2 code
   * and therefore, elected the GPL Version 2 license, then the option applies
   * only if the new code is made subject to such option by the copyright
   * holder.
   */
  
  package org.glassfish.web.embed.impl;
  
  import java.io.File;
  
  import java.lang.System;
  import java.util.*;
  
  
  
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.

Author(s):
Amy Roh
 
 public class WebContainerImpl implements WebContainer {
 
 
 
     private ServiceHandle<?> embeddedInhabitant;
 
     @Inject
     private ServiceLocator habitat;
 
     private HttpService httpService;
 
     private static Logger log =
             Logger.getLogger(WebContainerImpl.class.getName());
 
     private NetworkConfig networkConfig;
 
     @Inject
     private ServerContext serverContext;
 
 
     // ----------------------------------------------------- Instance Variables
 
 
     private WebContainerConfig config;
     
     private EmbeddedWebContainer embedded;
     
     private Engine engine = null;
 
     private boolean initialized = false;
 
     private String listenerName = "embedded-listener";
 
     private List<WebListenerlisteners = new ArrayList<WebListener>();
 
     private String securityEnabled = "false";
 
 
 
     // --------------------------------------------------------- Private Methods
 
 
     private void init() {
 
         if () {
             return;
         }
 
         if ( == null) {
             // use default settings
              = new WebContainerConfig();
         }
 
                 "com.sun.enterprise.web.WebContainer");
         if ( == null) {
             .severe("Cannot find webcontainer implementation");
             return;
         }
 
         ActiveDescriptor<?> activeDescriptor = .getBestDescriptor(
                 BuilderHelper.createContractFilter("com.sun.enterprise.web.EmbeddedWebContainer"));
         if (activeDescriptor == null) {
             .severe("Cannot find embedded implementation");
             return;
         }
          = .getServiceHandle(activeDescriptor);
 
         try {
 
 
             if (( == null) || ( == null)) {
                 .severe("Cannot find webcontainer implementation");
                 return;
             }
 
              = .getEngine();
             if ( == null) {
                 .severe("Cannot find engine implementation");
                 return;
             }
 
              = true;
 
         } catch (Exception e) {
             .severe("Init exception " + e.getMessage());
         }
         
     }
 
     private void bind(Port portWebListener webListenerString vsId) {
 
         String protocol = .;
         final int portNumber = port.getPortNumber();
         final String defaultVS = vsId;
         final WebListener listener = webListener;
 
         if (webListener == null) {
              = getListenerName();
             webListener = new HttpListener();
             webListener.setId();
             webListener.setPort(portNumber);
         } else {
              = webListener.getId();
             protocol = webListener.getProtocol();
         }
         .add(webListener);
 
         if (protocol.equals(.)) {
              = "false";
         } else if (protocol.equals(.)) {
              = "true";
         }
 
         try {
 
             ConfigSupport.apply(new SingleConfigCode<Protocols>() {
                 public Object run(Protocols paramthrows TransactionFailure {
                     final Protocol protocol = param.createChild(Protocol.class);
                     protocol.setName();
                     protocol.setSecurityEnabled();
                     param.getProtocol().add(protocol);
                     final Http http = protocol.createChild(Http.class);
                     http.setDefaultVirtualServer(defaultVS);
                     http.setFileCache(http.createChild(FileCache.class));
                     protocol.setHttp(http);
                     return protocol;
                 }
             }, .getProtocols());
 
             ConfigSupport.apply(new ConfigCode() {
                 public Object run(ConfigBeanProxy... paramsthrows TransactionFailure {
                     NetworkListeners nls = (NetworkListenersparams[0];
                     Transports transports = (Transportsparams[1];
                     final NetworkListener listener = nls.createChild(NetworkListener.class);
                     listener.setName();
                     listener.setPort(Integer.toString(portNumber));
                     listener.setProtocol();
                     listener.setThreadPool("http-thread-pool");
                     if (listener.findThreadPool() == null) {
                         final ThreadPool pool = nls.createChild(ThreadPool.class);
                         pool.setName();
                         listener.setThreadPool();
                     }
                     listener.setTransport("tcp");
                     if (listener.findTransport() == null) {
                         final Transport transport = transports.createChild(Transport.class);
                         transport.setName();
                         listener.setTransport();
                     }
                     nls.getNetworkListener().add(listener);
                     return listener;
                 }
             }, .getNetworkListeners(), .getTransports());
             
             if (webListener.getProtocol().equals("https")) {
                 NetworkListener networkListener = .getNetworkListener();
                 Protocol httpProtocol = networkListener.findHttpProtocol();
                 ConfigSupport.apply(new SingleConfigCode<Protocol>() {
                     public Object run(Protocol paramthrows TransactionFailure {
                         Ssl newSsl = param.createChild(Ssl.class);
                         populateSslElement(newSsllistener);
                         ..println("SSL "+newSsl.getKeyStore()+" "+newSsl.getKeyStorePassword()+" "+newSsl.getTrustStore()+" "+newSsl.getTrustStorePassword());
                         param.setSsl(newSsl);
                         return newSsl;
                     }
                 }, httpProtocol);
             }
 
             com.sun.enterprise.config.serverbeans.VirtualServer vs =
                     .getVirtualServerByName(.getVirtualServerId());
             ConfigSupport.apply(new SingleConfigCode<com.sun.enterprise.config.serverbeans.VirtualServer>() {
                 public Object run(com.sun.enterprise.config.serverbeans.VirtualServer avs)
                         throws PropertyVetoException {
                     avs.addNetworkListener();
                     return avs;
                 }
             }, vs);
 
         } catch (Exception e) {
             if (.contains(webListener)) {
                 .remove(webListener);
             }
             e.printStackTrace();
         }
 
     }
 
     private void populateSslElement(Ssl newSslWebListener webListener) {
 
         if (webListener instanceof HttpsListener) {
 
             HttpsListener listener = (HttpsListenerwebListener;
             SslConfig sslConfig = listener.getSslConfig();
             if (sslConfig == null) {
                 return;
             }
 
             if (sslConfig.getKeyStore() != null) {
                 newSsl.setKeyStore(sslConfig.getKeyStore());
             }
             if (sslConfig.getKeyPassword() != null) {
                 newSsl.setKeyStorePassword(new String(sslConfig.getKeyPassword()));
             }
             if (sslConfig.getTrustStore() != null) {
                 newSsl.setTrustStore(sslConfig.getTrustStore());
             }
             if (sslConfig.getTrustPassword() != null) {
                 newSsl.setTrustStorePassword(new String(sslConfig.getTrustPassword()));
             }
 
             if (sslConfig.getAlgorithms() != null) {
                 for (SslType sslType : sslConfig.getAlgorithms()) {
                     if (sslType.equals(.)) {
                         newSsl.setSsl2Enabled("true");
                     }
                     if (sslType.equals(.)) {
                         newSsl.setSsl3Enabled("true");
                     }
                     if (sslType.equals(.)) {
                         newSsl.setSsl3TlsCiphers("true");
                     }
                 }
             }   
             if (sslConfig.getHandshakeTimeout() > 0) {
                 newSsl.setSSLInactivityTimeout(sslConfig.getHandshakeTimeout());
             }
             if (sslConfig.getCertNickname() != null) {
                 newSsl.setCertNickname(sslConfig.getCertNickname());
             }
 
         } else {
             .severe("HttpsListener required for https protocol");
         }
 
     }
 
     private void addWebListener(WebListener webListenerString vsId)
             throws ConfigExceptionGlassFishException {
 
         if (!) {
             init();
         }
 
         if (getWebListener(webListener.getId()) != null) {
             throw new ConfigException("Connector with name '" +
                     webListener.getId() + "' already exsits");
         }
 
          = webListener.getId();
 
         try {
             Ports ports = .getService(Ports.class);
             Port port = ports.createPort(webListener.getPort());
             bind(portwebListenervsId);
         } catch (java.io.IOException ex) {
             throw new ConfigException(ex);
         }
 
         webListener.setWebContainer(this);
 
         if (.isLoggable(.)) {
             .info("Added connector " + webListener.getId() +
                     " port " + webListener.getPort() + " to virtual server " + vsId);
         }
 
     }
 
     private String getListenerName() {
 
         int i = 1;
 
         // use listenerName set via addWebListener
         if (!existsListener()) {
             return ;
         }
 
         // use default listener name
         String name = "embedded-listener";
         while (existsListener(name)) {
             name = "embedded-listener-" + i++;
         }
 
         return name;
 
     }
 
     private boolean existsListener(String lName) {
 
         for (NetworkListener nl :
                 .getNetworkListeners().getNetworkListener()) {
             if (nl.getName().equals(lName)) {
                 return true;
             }
         }
 
         return false;
 
     }
 
     private void removeListener(String name) {
 
         try {
 
             NetworkListeners networkListeners = .getNetworkListeners();
             final NetworkListener listenerToBeRemoved =
                     .getNetworkListener(name);
 
             final Protocols protocols = .getProtocols();
             final Protocol protocol = .findProtocol(name);
 
             if (listenerToBeRemoved == null) {
                 .severe("Network Listener " + name + " doesn't exist");
             } else {
                 final com.sun.enterprise.config.serverbeans.VirtualServer virtualServer =
                         .getVirtualServerByName(
                         listenerToBeRemoved.findHttpProtocol().getHttp().getDefaultVirtualServer());
                 ConfigSupport.apply(new ConfigCode() {
                     public Object run(ConfigBeanProxy... paramsthrows PropertyVetoException {
                         final NetworkListeners listeners = (NetworkListenersparams[0];
                         final com.sun.enterprise.config.serverbeans.VirtualServer server =
                                 (com.sun.enterprise.config.serverbeans.VirtualServerparams[1];
                         listeners.getNetworkListener().remove(listenerToBeRemoved);
                         server.removeNetworkListener(listenerToBeRemoved.getName());
                         return listenerToBeRemoved;
                     }
                 }, networkListenersvirtualServer);
 
                 ConfigSupport.apply(new ConfigCode() {
                     public Object run(ConfigBeanProxy... paramsthrows PropertyVetoException {
                         final Protocols protocols = (Protocolsparams[0];
                         final Protocol protocol = (Protocolparams[1];
                         protocols.getProtocol().remove(protocol);
                         return protocol;
                     }
                 }, protocolsprotocol);
 
             }
             
         } catch (TransactionFailure e) {
             .severe("Remove listener " + name + " failed " +e.getMessage());
         }
 
     }
 
 
     // --------------------------------------------------------- Public Methods
 
 
     public void setConfiguration(WebContainerConfig config) {
 
         if (!) {
             init();
         }
 
         this. = config;
 
         final WebContainerConfig webConfig = config;
 
         try {
 
             VirtualServer vs = getVirtualServer(config.getVirtualServerId());
             if (vs != null) {
                 ((StandardHost)vs).setDefaultWebXmlLocation(config.getDefaultWebXml().getPath());
             }
 
             com.sun.enterprise.config.serverbeans.VirtualServer vsBean =
                 .getVirtualServerByName(config.getVirtualServerId());
 
             if (vsBean != null) {
 
                 ConfigSupport.apply(new SingleConfigCode<com.sun.enterprise.config.serverbeans.VirtualServer>() {
                     public Object run(com.sun.enterprise.config.serverbeans.VirtualServer avs)
                             throws PropertyVetoExceptionTransactionFailure {
                         avs.setId(webConfig.getVirtualServerId());
                         if (webConfig.getDocRootDir() != null) {
                             avs.setDocroot(webConfig.getDocRootDir().getAbsolutePath());
                         }
                         avs.setHosts(webConfig.getHostNames());
                         avs.setNetworkListeners(webConfig.getListenerName());
                         Property property = avs.createChild(Property.class);
                         property.setName("default-web-xml");
                         property.setValue(webConfig.getDefaultWebXml().getPath());
                         avs.getProperty().add(property); 
                         return avs;
                     }
                 }, vsBean);
 
             } else {
 
                 vs = createVirtualServer(config.getVirtualServerId(), config.getDocRootDir());
                 addVirtualServer(vs);
 
             }
 
             EmbeddedWebArchivist archivist = .<EmbeddedWebArchivist>getService(EmbeddedWebArchivist.class);
             archivist.setDefaultWebXml(config.getDefaultWebXml());
 
             .setDirectoryListing(config.getListings());
 
             WebListener listener = getWebListener(config.getListenerName());
             if (listener == null) {
                 listener = getWebListener(config.getPort());
                 if (listener == null) {
                     boolean found = false;
                     for (Map.Entry entry : .getConnectorMap().entrySet()) {
                         if (((WebConnector)entry.getValue()).getPort() == config.getPort()) {
                             found = true;
                             .info("Port "+config.getPort()+" is already configured");
                         }
                     }
                     if (!found) {
                         listener = createWebListener(config.getListenerName(), HttpListener.class);
                         listener.setPort(config.getPort());
                         addWebListener(listenerconfig.getVirtualServerId());
                     }
                 }
             } else {
                 if (listener.getPort() != config.getPort()) {
                     listener.setPort(config.getPort());
                 }
             }
 
         }  catch (Exception ex) {
             ex.printStackTrace();
         }
 
     }

    
Returns the list of sniffers associated with this embedded container

Returns:
list of sniffers
 
     public List<SniffergetSniffers() {
 
         List<Sniffersniffers = new ArrayList<Sniffer>();
         sniffers.add(.<Sniffer>getService(Sniffer.class"web"));
         sniffers.add(.<Sniffer>getService(Sniffer.class"weld"));
         Sniffer security = .getService(Sniffer.class"Security");
         if (security!=null) {
             sniffers.add(security);
         }
         return sniffers;
 
     }

    
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.

Parameters:
docRoot the docroot of the Context
Returns:
the new Context
See also:
org.glassfish.embeddable.web.VirtualServer.addContext(org.glassfish.embeddable.web.Context,java.lang.String)
 
     public Context createContext(File docRoot) {
 
         return createContext(docRootnull);
 
     }

    
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.

Parameters:
docRoot the docroot of the Context
classLoader the classloader of the Context
Returns:
the new Context
See also:
org.glassfish.embeddable.web.VirtualServer.addContext(org.glassfish.embeddable.web.Context,java.lang.String)
 
     public Context createContext(File docRootClassLoader classLoader) {
 
         if (docRoot == null) {
             .severe("Cannot create context with NULL docroot");
             return null;
         }
 
         if (!) {
             init();
         }
 
         if (.isLoggable(.)) {
             .info("Creating context '" + docRoot.getName() + "' with docBase '" +
                     docRoot.getAbsolutePath() + "'");
         }
 
         return new ContextFacade(docRootnullclassLoader);
 
     }
    
    
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
 
     public Context createContext(File docRootString contextRoot
             ClassLoader classLoader) {
 
         Context context = createContext(docRootclassLoader);
 
         try {
             addContext(contextcontextRoot);
         } catch (Exception ex) {
             .severe("Couldn't add context " + context + " using " + contextRoot);
             ex.printStackTrace();
         }
         
         return context;
         
     }

    
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:
org.glassfish.embeddable.web.ConfigException if a Context already exists at the given context root on VirtualServer
org.glassfish.embeddable.GlassFishException if the given context fails to be started
 
     public void addContext(Context contextString contextRoot)
             throws ConfigExceptionGlassFishException {
 
         if (contextRoot==null) {
             throw new ConfigException("Context root cannot be NULL");
         }
         for (VirtualServer vs : getVirtualServers()) {
             if (vs.getContext(contextRoot)!=null) {
                 throw new ConfigException("Context with contextRoot "+
                         contextRoot+" is already registered");
             }
 
             if (!......equals(vs.getID())) {
                 vs.addContext(contextcontextRoot);
                 if (.isLoggable(.)) {
                     .info("Added context with path " + contextRoot +
                             " from virtual server " + vs.getID());
                 }
             }
         }
 
     }

    
Stops the given Context and removes it from all VirtualServer.

Parameters:
context the Context to be stopped and removed
Throws:
org.glassfish.embeddable.GlassFishException if an error occurs during the stopping or removal of the given context
 
     public void removeContext(Context context)
             throws ConfigExceptionGlassFishException {
 
         String contextRoot = context.getPath();
         for (VirtualServer vs : getVirtualServers()) {
             if (!......equals(vs.getID())) {
                 if (vs.getContext(contextRoot)!=null) {
                     vs.removeContext(context);
                     if (.isLoggable(.)) {
                         .info("Removed context with path " + contextRoot +
                                 " from virtual server " + vs.getID());
                     }
                 } else {
                     throw new GlassFishException("Context with context path " +
                             context.getPath() + " does not exist on virtual server " + vs.getID());
                 }
             }
         }
 
     }

    
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:
java.lang.IllegalAccessException if the given Class or its nullary constructor is not accessible.
java.lang.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.
java.lang.ExceptionInInitializerError if the initialization fails
java.lang.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
 
     public <T extends WebListener> T createWebListener(String idClass<T> c
             throws InstantiationExceptionIllegalAccessException {
         
         T webListener = null;
 
         if (.isLoggable(.)) {
             .info("Creating connector " + id);
         }
 
         try {
             webListener = c.newInstance();
             webListener.setId(id);
         } catch (Exception e) {
             .severe("Couldn't create connector " + e.getMessage());
         }
 
         return webListener;
 
     }

    
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:
org.glassfish.embeddable.web.ConfigException if a WebListener with the same id has already been registered with this WebContainer
org.glassfish.embeddable.GlassFishException if the given webListener fails to be started
 
     public void addWebListener(WebListener webListener)
             throws ConfigExceptionGlassFishException {
 
         if (!) {
             init();
         }
         
         addWebListener(webListener.getVirtualServerId());
         
     }

    
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
 
     public WebListener getWebListener(String id) {
 
         if (!) {
             init();
         }
 
         for (WebListener listener : ) {
             if (listener.getId().equals(id)) {
                 return listener;
             }
         }
 
         return null;
 
     }
 
     private WebListener getWebListener(int port) {
 
         for (WebListener listener : ) {
             if (listener.getPort() == port) {
                 return listener;
             }
         }
 
         return null;
 
     }

    
Gets the collection of WebListener instances registered with this WebContainer.

Returns:
the (possibly empty) collection of WebListener instances registered with this WebContainer
 
     public Collection<WebListenergetWebListeners() {
 
         return ;
 
     }

    
Stops the given webListener and removes it from this WebContainer.

Parameters:
webListener the WebListener to be stopped and removed
Throws:
org.glassfish.embeddable.GlassFishException if an error occurs during the stopping or removal of the given webListener
 
     public void removeWebListener(WebListener webListener)
         throws GlassFishException {
 
         if (.contains(webListener)) {
             .remove(webListener);
         } else {
             throw new GlassFishException(new ConfigException(
                     "Connector with name '" + webListener.getId()+"' does not exsits"));
         }
 
         removeListener(webListener.getId());
 
         if (.isLoggable(.)) {
             .info("Removed connector " + webListener.getId());
         }
     }


    
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
 
         File docRootWebListener...  webListeners) {
 
         return new VirtualServerFacade(iddocRootwebListeners);
 
     }

    
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
     
     public VirtualServer createVirtualServer(String idFile docRoot) {
 
         return new VirtualServerFacade(iddocRoot, (WebListener[]) null);
 
     }

    
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:
org.glassfish.embeddable.web.ConfigException if a VirtualServer with the same id has already been registered with this WebContainer
org.glassfish.embeddable.GlassFishException if the given virtualServer fails to be started
 
     public void addVirtualServer(VirtualServer virtualServer)
         throws ConfigExceptionGlassFishException {
 
         if (!) {
             init();
         }
 
         if (.isLoggable(.)) {
             .info("Adding virtual server " + virtualServer.getID());
         }
 
         com.sun.enterprise.web.VirtualServer vs =
                 (com.sun.enterprise.web.VirtualServer.findChild(virtualServer.getID());
         if (vs != null) {
                 throw new ConfigException("VirtualServer with id "+
                         virtualServer.getID()+" is already registered");
         }
 
         Collection<WebListenerwebListeners = virtualServer.getWebListeners();
 
         List<Stringnames = new ArrayList<String>();
         if ((webListeners != null) && (!webListeners.isEmpty())) {
             for (WebListener listener : webListeners) {
                 names.add(listener.getId());
             }
         } else {
             for (NetworkListener networkListener :
                 .getNetworkListeners().getNetworkListener()) {
                 names.add(networkListener.getName());
             }
             webListeners = ;
         }
 
         StringBuffer networkListeners = new StringBuffer("");
         if (names.size()>0) {
             networkListeners.append(names.get(0));
         }
         for (int i=1; i<names.size(); i++) {
             networkListeners.append(",");
             networkListeners.append(names.get(i));
         }
 
         String docRoot = null;
         if (virtualServer.getDocRoot() != null) {
             docRoot = virtualServer.getDocRoot().getAbsolutePath();
         }
 
         String hostName = null;
         if (virtualServer.getConfig() != null) {
             hostName = virtualServer.getConfig().getHostNames();
         }
         final String root = docRoot;
         final String nl = networkListeners.toString();
         final String id = virtualServer.getID();
         final String hosts = hostName;
 
         try {
             ConfigSupport.apply(new SingleConfigCode<HttpService>() {
                 public Object run(HttpService paramthrows PropertyVetoExceptionTransactionFailure {
                     com.sun.enterprise.config.serverbeans.VirtualServer newVirtualServer =
                             param.createChild(com.sun.enterprise.config.serverbeans.VirtualServer.class);
                     newVirtualServer.setId(id);
                     newVirtualServer.setNetworkListeners(nl);
                     if (hosts != null) {
                         newVirtualServer.setHosts(hosts);
                     }
                     Property property = newVirtualServer.createChild(Property.class);
                     property.setName("docroot");
                     property.setValue(root);
                     newVirtualServer.getProperty().add(property);
                    param.getVirtualServer().add(newVirtualServer);
                    return newVirtualServer;
                }
            }, );
        } catch (Exception ex) {
            throw new GlassFishException(ex);
        }
        if ((webListeners != null) && (!webListeners.isEmpty())) {
            for (WebListener listener : webListeners) {
                if (getWebListener(listener.getId())==null) {
                    addWebListener(listenervirtualServer.getID());
                }
            }
        }
        vs = (com.sun.enterprise.web.VirtualServer.findChild(id);
        if (vs != null) {
            if (.isLoggable(.)) {
                .info("Added virtual server " + id +
                        " docroot " + docRoot + " networklisteners " + nl);
            }
            if (virtualServer instanceof VirtualServerFacade) {
                ((VirtualServerFacade)virtualServer).setVirtualServer(vs);
            }
            vs.setNetworkListenerNames(names.toArray(new String[names.size()]));
        } else {
            .severe("Could not add virtual server "+id);
            throw new GlassFishException(
                    new Exception("Cannot add virtual server " + 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
    public VirtualServer getVirtualServer(String id) {
        if (!) {
            init();
        }
        return (VirtualServer).findChild(id);
        
    }

    
Gets the collection of VirtualServer instances registered with this WebContainer.

Returns:
the (possibly empty) collection of VirtualServer instances registered with this WebContainer
        if (!) {
            init();
        }
        List<VirtualServervirtualServers = new ArrayList<VirtualServer>();
        for (Container child : .findChildren()) {
            if (child instanceof VirtualServer) {
                virtualServers.add((VirtualServer)child);
            }
        }
        return virtualServers;
    }

    
Stops the given virtualServer and removes it from this WebContainer.

Parameters:
virtualServer the VirtualServer to be stopped and removed
Throws:
org.glassfish.embeddable.GlassFishException if an error occurs during the stopping or removal of the given virtualServer
    public void removeVirtualServer(VirtualServer virtualServer
            throws GlassFishException {
        if (!) {
            init();
        }
        if (virtualServer instanceof Container) {
            .removeChild((Container)virtualServer);
        } else if (virtualServer instanceof VirtualServerFacade) {
            .removeChild(((VirtualServerFacade)virtualServer).getVirtualServer());
        }
    }
    
    
Sets log level

Parameters:
level
    public void setLogLevel(Level level) {
        .setLevel(level);
    }
New to GrepCode? Check out our FAQ X