org.gradle.launcher.daemon.registry.EmbeddedDaemonRegistry Maven / Gradle / Ivy
Show all versions of gradle-api Show documentation
/*
* Copyright 2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.gradle.launcher.daemon.registry;
import com.google.common.collect.Lists;
import org.gradle.api.specs.Spec;
import org.gradle.api.specs.Specs;
import org.gradle.internal.remote.Address;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import static org.gradle.launcher.daemon.server.api.DaemonStateControl.*;
import static org.gradle.launcher.daemon.server.api.DaemonStateControl.State.*;
/**
* A daemon registry for daemons running in the same JVM.
*
* This implementation is thread safe in that its getAll(), getIdle() and getBusy() methods are expected to be called from “consumer” threads,
* while the newEntry() method is expected to be called by “producer” threads.
*
* The collections returned by the consumer methods do not return live collections so may not reflect the precise state of the registry
* by the time they are returned to the caller. Clients must therefore be prepared for this and expect connection failures, either through
* the endpoint disappearing or becoming busy between asking for idle daemons and trying to connect.
*/
public class EmbeddedDaemonRegistry implements DaemonRegistry {
private final List stopEvents = Lists.newCopyOnWriteArrayList();
private final Map daemonInfos = new ConcurrentHashMap();
private final Spec allSpec = new Spec() {
public boolean isSatisfiedBy(DaemonInfo entry) {
return true;
}
};
@SuppressWarnings("unchecked")
private final Spec idleSpec = Specs.intersect(allSpec, new Spec() {
public boolean isSatisfiedBy(DaemonInfo daemonInfo) {
return daemonInfo.getState() == Idle;
}
});
@SuppressWarnings("unchecked")
private final Spec busySpec = Specs.intersect(allSpec, new Spec() {
public boolean isSatisfiedBy(DaemonInfo daemonInfo) {
return daemonInfo.getState() != Idle;
}
});
@SuppressWarnings("unchecked")
private final Spec canceledSpec = Specs.intersect(allSpec, new Spec() {
public boolean isSatisfiedBy(DaemonInfo daemonInfo) {
return daemonInfo.getState() == Canceled;
}
});
public List getAll() {
return daemonInfosOfEntriesMatching(allSpec);
}
public List getIdle() {
return daemonInfosOfEntriesMatching(idleSpec);
}
public List getNotIdle() {
return daemonInfosOfEntriesMatching(busySpec);
}
public List getCanceled() {
return daemonInfosOfEntriesMatching(canceledSpec);
}
@Override
public void store(DaemonInfo info) {
daemonInfos.put(info.getAddress(), info);
}
@Override
public void remove(Address address) {
daemonInfos.remove(address);
}
public void markState(Address address, State state) {
synchronized (daemonInfos) {
daemonInfos.get(address).setState(state);
}
}
@Override
public void storeStopEvent(DaemonStopEvent stopEvent) {
stopEvents.add(stopEvent);
}
@Override
public List getStopEvents() {
return stopEvents;
}
@Override
public void removeStopEvents(final Collection events) {
stopEvents.removeAll(events);
}
private List daemonInfosOfEntriesMatching(Spec spec) {
List matches = new ArrayList();
for (DaemonInfo daemonInfo : daemonInfos.values()) {
if (spec.isSatisfiedBy(daemonInfo)) {
matches.add(daemonInfo);
}
}
return matches;
}
}