diff options
Diffstat (limited to 'config-model/src/main/java/com/yahoo/vespa/model/container/http/Http.java')
-rw-r--r-- | config-model/src/main/java/com/yahoo/vespa/model/container/http/Http.java | 116 |
1 files changed, 62 insertions, 54 deletions
diff --git a/config-model/src/main/java/com/yahoo/vespa/model/container/http/Http.java b/config-model/src/main/java/com/yahoo/vespa/model/container/http/Http.java index 400ddf80cf9..4742ebfcf89 100644 --- a/config-model/src/main/java/com/yahoo/vespa/model/container/http/Http.java +++ b/config-model/src/main/java/com/yahoo/vespa/model/container/http/Http.java @@ -8,104 +8,112 @@ import com.yahoo.vespa.model.container.component.chain.Chain; import com.yahoo.vespa.model.container.component.chain.ChainedComponent; import java.util.Collection; -import java.util.Collections; import java.util.List; import java.util.Optional; +import java.util.concurrent.CopyOnWriteArrayList; /** * Represents the http servers and filters of a container cluster. * * @author Tony Vaagenes + * @author bjorncs */ public class Http extends AbstractConfigProducer<AbstractConfigProducer<?>> implements ServerConfig.Producer { - private FilterChains filterChains; - private JettyHttpServer httpServer; - private List<Binding> bindings; - private final Optional<AccessControl> accessControl; - - public Http(List<Binding> bindings) { - this(bindings, null); - } + private final Object monitor = new Object(); - public Http(List<Binding> bindings, AccessControl accessControl) { - super( "http"); - this.bindings = Collections.unmodifiableList(bindings); - this.accessControl = Optional.ofNullable(accessControl); - } + private final FilterChains filterChains; + private final List<Binding> bindings = new CopyOnWriteArrayList<>(); + private JettyHttpServer httpServer; + private AccessControl accessControl; - public void setFilterChains(FilterChains filterChains) { - this.filterChains = filterChains; + public Http(FilterChains chains) { + super("http"); + this.filterChains = chains; } - public void setBindings(List<Binding> bindings) { - this.bindings = Collections.unmodifiableList(bindings); + public void setAccessControl(AccessControl accessControl) { + synchronized (monitor) { + if (this.accessControl != null) throw new IllegalStateException("Access control already assigned"); + this.accessControl = accessControl; + } } public FilterChains getFilterChains() { - return filterChains; + synchronized (monitor) { + return filterChains; + } } - public JettyHttpServer getHttpServer() { - return httpServer; + public Optional<JettyHttpServer> getHttpServer() { + synchronized (monitor) { + return Optional.ofNullable(httpServer); + } } public void setHttpServer(JettyHttpServer newServer) { - JettyHttpServer oldServer = this.httpServer; - this.httpServer = newServer; - - if (oldServer == null && newServer != null) { - addChild(newServer); - } else if (newServer == null && oldServer != null) { - removeChild(oldServer); - } else if (newServer == null && oldServer == null) { - //do nothing - } else { - //none of them are null - removeChild(oldServer); - addChild(newServer); + synchronized (monitor) { + JettyHttpServer oldServer = this.httpServer; + this.httpServer = newServer; + + if (oldServer == null && newServer != null) { + addChild(newServer); + } else if (newServer == null && oldServer != null) { + removeChild(oldServer); + } else if (newServer == null && oldServer == null) { + //do nothing + } else { + //none of them are null + removeChild(oldServer); + addChild(newServer); + } } } public void removeAllServers() { - setHttpServer(null); + synchronized (monitor) { + setHttpServer(null); + } } public List<Binding> getBindings() { - return bindings; + synchronized (monitor) { + return bindings; + } } public Optional<AccessControl> getAccessControl() { - return accessControl; + synchronized (monitor) { + return Optional.ofNullable(accessControl); + } } @Override public void getConfig(ServerConfig.Builder builder) { - for (Binding binding : bindings) { - builder.filter(new ServerConfig.Filter.Builder() - .id(binding.filterId().stringValue()) - .binding(binding.binding())); + synchronized (monitor) { + for (Binding binding : bindings) { + builder.filter(new ServerConfig.Filter.Builder() + .id(binding.filterId().stringValue()) + .binding(binding.binding())); + } } } @Override public void validate() { - validate(bindings); - } - - void validate(Collection<Binding> bindings) { - if (bindings.isEmpty()) return; + synchronized (monitor) { + if (((Collection<Binding>) bindings).isEmpty()) return; - if (filterChains == null) - throw new IllegalArgumentException("Null FilterChains are not allowed when there are filter bindings"); + if (filterChains == null) + throw new IllegalArgumentException("Null FilterChains are not allowed when there are filter bindings"); - ComponentRegistry<ChainedComponent<?>> filters = filterChains.componentsRegistry(); - ComponentRegistry<Chain<Filter>> chains = filterChains.allChains(); + ComponentRegistry<ChainedComponent<?>> filters = filterChains.componentsRegistry(); + ComponentRegistry<Chain<Filter>> chains = filterChains.allChains(); - for (Binding binding: bindings) { - if (filters.getComponent(binding.filterId()) == null && chains.getComponent(binding.filterId()) == null) - throw new RuntimeException("Can't find filter " + binding.filterId() + " for binding " + binding.binding()); + for (Binding binding: bindings) { + if (filters.getComponent(binding.filterId()) == null && chains.getComponent(binding.filterId()) == null) + throw new RuntimeException("Can't find filter " + binding.filterId() + " for binding " + binding.binding()); + } } } - } |