Package net.sourceforge.javautil.web.server.impl

Source Code of net.sourceforge.javautil.web.server.impl.WebServerHostAbstract

package net.sourceforge.javautil.web.server.impl;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import net.sourceforge.javautil.common.exception.ThrowableManagerRegistry;
import net.sourceforge.javautil.web.server.IWebServer;
import net.sourceforge.javautil.web.server.WebServerConnector;
import net.sourceforge.javautil.web.server.IWebServerHost;
import net.sourceforge.javautil.web.server.WebServerHostValve;
import net.sourceforge.javautil.web.server.application.IWebApplication;
import net.sourceforge.javautil.web.server.application.WebApplicationDeploymentContext;

/**
* A basis for most {@link IWebServerHost} implementations providing
* common functionality.
*
* @author elponderador
* @author $Author: ponderator $
* @version $Id: WebServerHostAbstract.java 2287 2010-06-14 04:25:09Z ponderator $
*/
public abstract class WebServerHostAbstract<S extends IWebServer> implements IWebServerHost {
 
  /**
   * The server unique name of this host
   */
  protected final String name;
 
  /**
   * The server that generated this host
   */
  protected final S server;

  /**
   * The store of currently deployed applications
   */
  protected final Map<String, WebApplicationDeploymentContext> applications = new LinkedHashMap<String, WebApplicationDeploymentContext>();
 
  /**
   * The valves for this application
   */
  protected final List<WebServerHostValve> valves = new ArrayList<WebServerHostValve>();
 
  protected final boolean defaultHost;
 
  protected final Logger log = LoggerFactory.getLogger(getClass());
 
  public WebServerHostAbstract(S server, boolean defaultHost, String name) {
    this.server = server;
    this.name = name;
    this.defaultHost = defaultHost;
  }

  public boolean isDefault() { return this.defaultHost; }

  public List<WebApplicationDeploymentContext> getCurrentlyDeployed() {
    return new ArrayList<WebApplicationDeploymentContext>( applications.values() );
  }

  public String getName() { return name; }

  public S getServer() { return server; }

  public void registerValve(WebServerHostValve valve) { this.valves.add(valve); }

  public void unregisterValve(WebServerHostValve valve) { this.valves.remove(valve); }
 
  public URL getExternalURLFor (String name) {
    WebApplicationDeploymentContext wadc = this.applications.get(name);
    if (wadc == null)
      throw new IllegalArgumentException("No such application found: " + name);
   
    if (this.server.getConnectors().size() == 0)
      throw new IllegalStateException("No connectors have been defined for the server");
   
    WebServerConnector connector = this.server.getConnectors().get(0);
    if (connector instanceof WebServerConnectorHTTP) {
      WebServerConnectorHTTP http = (WebServerConnectorHTTP) connector;
      try {
        return new URL("http", http.getHostname(), http.getPort(), wadc.getApplication().getContextPath());
      } catch (MalformedURLException e) {
        throw ThrowableManagerRegistry.caught(e);
      }
    }
   
    throw new IllegalStateException("No HTTP connectors have been defined for the server");
  }
 
  public String toString () { return getClass().getSimpleName() + "[ " + name + " ]"; }
 
  /**
   * @param contextPath The context path of the application
   * @return The application context that corresponds to the path, otherwise null if no such application exists
   */
  public WebApplicationDeploymentContext getApplicationContext (String contextPath) {
    for (WebApplicationDeploymentContext application : this.getCurrentlyDeployed()) {
      if (contextPath.equals( application.getServletContext().getContextPath() ))
          return application;
    }
    return null;
  }
 
}
TOP

Related Classes of net.sourceforge.javautil.web.server.impl.WebServerHostAbstract

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.