Ejemplo n.º 1
0
  /**
   * Set the default Host used for resolving unknown host names.
   *
   * @param name Name of the default host
   */
  private void setDefaultHost(String name) {

    if (debug >= 3) engine.log("Setting default host '" + name + "'");

    if (name == null) defaultHost = null;
    else defaultHost = (Host) engine.findChild(name);
  }
Ejemplo n.º 2
0
  /**
   * Return the child Container that should be used to process this Request, based upon its
   * characteristics. If no such child Container can be identified, return <code>null</code>
   * instead.
   *
   * @param request Request being processed
   * @param update Update the Request to reflect the mapping selection?
   */
  public Container map(Request request, boolean update) {

    debug = engine.getDebug();

    // Extract the requested server name
    String server = request.getRequest().getServerName();
    if (server == null) {
      server = engine.getDefaultHost();
      if (update) request.setServerName(server);
    }
    if (server == null) return (null);
    if (debug >= 1) engine.log("Mapping server name '" + server + "'");

    // Find the specified host in our cache
    if (debug >= 2) engine.log(" Trying a cache match");
    Host host = (Host) cache.get(server);

    // Map to the default host if any
    if ((host == null) && (defaultHost != null)) {
      if (debug >= 2) engine.log(" Mapping to default host");
      host = defaultHost;
      addAlias(server, host);
    }

    // Update the Request if requested, and return the selected Host
    ; // No update to the Request is required
    return (host);
  }
Ejemplo n.º 3
0
  /**
   * Gracefully shut down active use of the public methods of this Component.
   *
   * @exception LifecycleException if this component detects a fatal error that needs to be reported
   */
  public synchronized void stop() throws LifecycleException {

    // Validate and update our current component state
    if (!started)
      throw new LifecycleException(sm.getString("fastEngineMapper.notStarted", engine.getName()));

    // Notify our interested LifecycleListeners
    lifecycle.fireLifecycleEvent(STOP_EVENT, null);
    started = false;

    // Deconfigure based on our associated Engine properties
    engine.removePropertyChangeListener(this);
    setDefaultHost(null);
    engine.removeContainerListener(this);

    // Clear our mapping cache
    cache.clear();
  }
Ejemplo n.º 4
0
  /**
   * Add a new child Host to our associated Engine.
   *
   * @param host Child host to add
   */
  private void addHost(Host host) {

    if (debug >= 3) engine.log("Adding host '" + host.getName() + "'");

    host.addContainerListener(this);

    // Register the host name
    addAlias(host.getName(), host);

    // Register all associated aliases
    String aliases[] = host.findAliases();
    for (int i = 0; i < aliases.length; i++) addAlias(aliases[i], host);
  }
Ejemplo n.º 5
0
  /**
   * Prepare for active use of the public methods of this Component.
   *
   * @exception LifecycleException if this component detects a fatal error that prevents it from
   *     being started
   */
  public synchronized void start() throws LifecycleException {

    // Validate and update our current component state
    if (started)
      throw new LifecycleException(
          sm.getString("fastEngineMapper.alreadyStarted", engine.getName()));
    started = true;

    // Configure based on our associated Engine properties
    engine.addContainerListener(this);
    engine.addPropertyChangeListener(this);
    setDefaultHost(engine.getDefaultHost());

    // Cache mappings for our child hosts
    Container children[] = engine.findChildren();
    for (int i = 0; i < children.length; i++) {
      addHost((Host) children[i]);
    }

    // Notify our interested LifecycleListeners
    lifecycle.fireLifecycleEvent(START_EVENT, null);
  }
Ejemplo n.º 6
0
  public EmbeddedTomcat(String contextPath, int port, String jvmRoute)
      throws MalformedURLException {
    this.contextPath = contextPath;
    this.port = port;

    // create server
    container = new Embedded();
    container.setCatalinaHome(catalinaHome);
    // Not really necessasry, but let's still do it...
    container.setRealm(new MemoryRealm());

    // create webapp loader
    WebappLoader loader = new WebappLoader(this.getClass().getClassLoader());
    if (classesDir != null) {
      loader.addRepository(new File(classesDir).toURI().toURL().toString());
    }

    rootContext = container.createContext("", webappDir);
    rootContext.setLoader(loader);
    rootContext.setReloadable(true);
    // Otherwise we get NPE when instantiating servlets
    rootContext.setIgnoreAnnotations(true);

    // create host
    Host localHost = container.createHost("127.0.0.1", new File("").getAbsolutePath());
    localHost.addChild(rootContext);

    localHost.setDeployOnStartup(true);

    // create engine
    engine = container.createEngine();
    engine.setName("localEngine");
    engine.addChild(localHost);
    engine.setDefaultHost(localHost.getName());
    engine.setJvmRoute(jvmRoute);
    engine.setService(new StandardService());
    container.addEngine(engine);

    // create http connector
    Connector httpConnector = container.createConnector((InetAddress) null, port, false);
    container.addConnector(httpConnector);
    container.setAwait(true);

    // Create the JVMRoute valve for session failover
    ValveBase valve = new JvmRouteBinderValve();
    ((StandardEngine) engine).addValve(valve);
  }
Ejemplo n.º 7
0
  /**
   * Remove an existing child Host from our associated Engine.
   *
   * @param host Host to be removed
   */
  private void removeHost(Host host) {

    if (debug >= 3) engine.log("Removing host '" + host.getName() + "'");

    host.removeContainerListener(this);

    // Identify all names mapped to this host
    ArrayList removes = new ArrayList();
    Iterator keys = cache.keySet().iterator();
    while (keys.hasNext()) {
      String key = (String) keys.next();
      if (host.equals((Host) cache.get(key))) removes.add(key);
    }

    // Remove the associated names
    keys = removes.iterator();
    while (keys.hasNext()) {
      removeAlias((String) keys.next());
    }
  }
Ejemplo n.º 8
0
 public void removeValve(Valve valve) {
   ((StandardEngine) engine).removeValve(valve);
 }
Ejemplo n.º 9
0
 public void addValve(Valve valve) {
   ((StandardEngine) engine).addValve(valve);
 }
Ejemplo n.º 10
0
  /**
   * Remove the specified alias from our cache.
   *
   * @param alias Alias to remove
   */
  private void removeAlias(String alias) {

    if (debug >= 3) engine.log("Removing alias '" + alias + "'");
    cache.remove(alias.toLowerCase());
  }
Ejemplo n.º 11
0
  /**
   * Add an alias for the specified host.
   *
   * @param alias New alias name
   * @param host Host to resolve to
   */
  private void addAlias(String alias, Host host) {

    if (debug >= 3) engine.log("Adding alias '" + alias + "' for host '" + host.getName() + "'");
    cache.put(alias.toLowerCase(), host);
  }