Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package io.undertow.server.handlers.proxy;
  
  
  import java.util.Map;
  import java.util.Set;
Class that maintains a table of remote hosts that this proxy knows about. Basically this maps a virtual host + context path pair to a set of hosts. Note that this class does not have any knowledge of connection pooling

Author(s):
Stuart Douglas
 
 public class HostTable<H> {
 
     private final Map<H, Set<Target>> hosts = new CopyOnWriteMap<H, Set<Target>>();
     private final Map<StringPathMatcher<Set<H>>> targets = new CopyOnWriteMap<StringPathMatcher<Set<H>>>();
 
     public synchronized HostTable addHost(H host) {
         if(.containsKey(host)) {
             throw ..hostAlreadyRegistered(host);
         }
         .put(hostnew CopyOnWriteArraySet<Target>());
         return this;
     }
 
     public synchronized HostTable removeHost(H host) {
         Set<Targettargets = .remove(host);
         for(Target target : targets) {
             removeRoute(hosttarget.virtualHosttarget.contextPath);
         }
         return this;
     }
 
     public synchronized HostTable addRoute(H hostString virtualHostString contextPath) {
         Set<TargethostData = .get(host);
         if(hostData == null) {
             throw ..hostHasNotBeenRegistered(host);
         }
         hostData.add(new Target(virtualHostcontextPath));
         PathMatcher<Set<H>> paths = .get(virtualHost);
         if(paths == null) {
             paths = new PathMatcher<Set<H>>();
             .put(virtualHostpaths);
         }
         Set<H> hostSet = paths.getPrefixPath(contextPath);
         if(hostSet == null) {
             hostSet = new CopyOnWriteArraySet<H>();
             paths.addPrefixPath(contextPathhostSet);
         }
         hostSet.add(host);
         return this;
     }
 
     public synchronized HostTable removeRoute(H hostString virtualHostString contextPath) {
 
         Set<TargethostData = .get(host);
         if(hostData != null) {
             hostData.remove(new Target(virtualHostcontextPath));
         }
         PathMatcher<Set<H>> paths = .get(virtualHost);
         if(paths == null) {
             return this;
         }
         Set<H> hostSet = paths.getPrefixPath(contextPath);
         if(hostSet == null) {
             return this;
         }
         hostSet.remove(host);
         if(hostSet.isEmpty()) {
             paths.removePrefixPath(contextPath);
         }
         return this;
     }
 
     public Set<H> getHostsForTarget(final String hostNamefinal String path) {
         PathMatcher<Set<H>> matcher = .get(hostName);
         if(matcher == null) {
             return null;
         }
         return matcher.match(path).getValue();
     }
 
     private static final class Target {
         final String virtualHost;
         final String contextPath;
 
         private Target(String virtualHostString contextPath) {
             this. = virtualHost;
             this. = contextPath;
         }
 
         @Override
        public boolean equals(Object o) {
            if (this == oreturn true;
            if (o == null || getClass() != o.getClass()) return false;
            Target target = (Targeto;
            if ( != null ? !.equals(target.contextPath) : target.contextPath != null)
                return false;
            if ( != null ? !.equals(target.virtualHost) : target.virtualHost != null)
                return false;
            return true;
        }
        @Override
        public int hashCode() {
            int result =  != null ? .hashCode() : 0;
            result = 31 * result + ( != null ? .hashCode() : 0);
            return result;
        }
    }
New to GrepCode? Check out our FAQ X