Replace use of monitor with volatile
Volatile is sufficient as there is no need for global synchronization
across all members of Http. There is only a single thread constructing
and mutating the object.
@@ -20,12 +20,10 @@ import java.util.concurrent.CopyOnWriteArrayList;
*/
public class Http extends AbstractConfigProducer<AbstractConfigProducer<?>> implements ServerConfig.Producer {
- private final Object monitor = new Object();
-
private final FilterChains filterChains;
private final List<Binding> bindings = new CopyOnWriteArrayList<>();
- private JettyHttpServer httpServer;
- private AccessControl accessControl;
+ private volatile JettyHttpServer httpServer;
+ private volatile AccessControl accessControl;
public Http(FilterChains chains) {
super("http");
@@ -33,26 +31,19 @@ public class Http extends AbstractConfigProducer<AbstractConfigProducer<?>> impl
}
public void setAccessControl(AccessControl accessControl) {
- synchronized (monitor) {
if (this.accessControl != null) throw new IllegalStateException("Access control already assigned");
this.accessControl = accessControl;
}
- }
public FilterChains getFilterChains() {
- synchronized (monitor) {
return filterChains;
}
- }
public Optional<JettyHttpServer> getHttpServer() {
- synchronized (monitor) {
return Optional.ofNullable(httpServer);
}
- }
public void setHttpServer(JettyHttpServer newServer) {
- synchronized (monitor) {
JettyHttpServer oldServer = this.httpServer;
this.httpServer = newServer;
@@ -68,40 +59,30 @@ public class Http extends AbstractConfigProducer<AbstractConfigProducer<?>> impl
addChild(newServer);
}
}
- }
public void removeAllServers() {
- synchronized (monitor) {
setHttpServer(null);
}
- }
public List<Binding> getBindings() {
- synchronized (monitor) {
return bindings;
}
- }
public Optional<AccessControl> getAccessControl() {
- synchronized (monitor) {
return Optional.ofNullable(accessControl);
}
- }
@Override
public void getConfig(ServerConfig.Builder builder) {
- synchronized (monitor) {
for (Binding binding : bindings) {
builder.filter(new ServerConfig.Filter.Builder()
.id(binding.filterId().stringValue())
.binding(binding.binding()));
}
}
- }
@Override
public void validate() {
- synchronized (monitor) {
if (((Collection<Binding>) bindings).isEmpty()) return;
if (filterChains == null)
@@ -116,4 +97,3 @@ public class Http extends AbstractConfigProducer<AbstractConfigProducer<?>> impl
}
}
}
-}
