
com.nesscomputing.service.discovery.testing.client.MemoryDiscoveryClient Maven / Gradle / Ivy
/**
* Copyright (C) 2012 Ness Computing, Inc.
*
* 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 com.nesscomputing.service.discovery.testing.client;
import java.util.Collection;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;
import com.google.common.base.Function;
import com.google.common.collect.Maps;
import com.google.common.collect.Multimaps;
import com.google.inject.Singleton;
import com.nesscomputing.logging.Log;
import com.nesscomputing.service.discovery.client.DiscoveryClient;
import com.nesscomputing.service.discovery.client.ServiceInformation;
import com.nesscomputing.service.discovery.client.internal.AbstractDiscoveryClient;
import com.nesscomputing.service.discovery.client.internal.ConsistentRingGroup;
@Singleton
public class MemoryDiscoveryClient extends AbstractDiscoveryClient implements DiscoveryClient {
private static final Log LOG = Log.findLog();
public MemoryDiscoveryClient() {
super(true);
updateStateOfTheWorld();
}
private final Set announcements = new CopyOnWriteArraySet();
@Override
public void announce(ServiceInformation serviceInformation) {
boolean announced = announcements.add(serviceInformation);
LOG.debug("Announcing new service %s %s", serviceInformation, announced ? "succeeded" : "failed, already announced");
updateStateOfTheWorld();
}
@Override
public void unannounce(ServiceInformation serviceInformation) {
boolean unannounced = announcements.remove(serviceInformation);
LOG.debug("Unannouncing service %s %s", serviceInformation, unannounced ? "succeeded" : "failed, no such announcement");
updateStateOfTheWorld();
}
@Override
public void unannounceAll() {
announcements.clear();
LOG.debug("Cleared all announcements");
updateStateOfTheWorld();
}
private void updateStateOfTheWorld() {
Map newWorldOrder =
Maps.transformValues(
Multimaps.index(announcements, GET_SERVICE_NAME).asMap(),
BUILD_RING_GROUP);
getStateOfTheWorldHolder().setState(newWorldOrder);
LOG.trace("Pushed out discovery update, new world order = %s", newWorldOrder);
}
private static final Function GET_SERVICE_NAME = new Function() {
@Override
public String apply(ServiceInformation input) {
if (input == null) {
return null;
}
return input.getServiceName();
}
};
private static final Function, ConsistentRingGroup> BUILD_RING_GROUP =
new Function, ConsistentRingGroup>() {
@Override
public ConsistentRingGroup apply(Collection servers) {
if (servers == null) {
return null;
}
return new ConsistentRingGroup(servers);
}
};
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy