package org.ehcache.management.registry;
import org.ehcache.Cache;
import org.ehcache.Status;
import org.ehcache.core.events.CacheManagerListener;
import org.ehcache.core.spi.service.CacheManagerProviderService;
import org.ehcache.core.spi.store.InternalCacheManager;
import org.ehcache.management.ManagementRegistryService;
import org.ehcache.management.SharedManagementService;
import org.ehcache.spi.service.Service;
import org.ehcache.spi.service.ServiceDependencies;
import org.ehcache.spi.service.ServiceProvider;
import org.terracotta.management.model.capabilities.Capability;
import org.terracotta.management.model.context.Context;
import org.terracotta.management.model.context.ContextContainer;
import org.terracotta.management.registry.CapabilityManagement;
import org.terracotta.management.registry.DefaultCapabilityManagement;
import org.terracotta.management.registry.ManagementProvider;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeSet;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
@ServiceDependencies({CacheManagerProviderService.class, ManagementRegistryService.class})
public class DefaultSharedManagementService implements SharedManagementService {
private final ConcurrentMap<Context, ManagementRegistryService> delegates = new ConcurrentHashMap<>();
@Override
public void start(final ServiceProvider<Service> serviceProvider) {
final ManagementRegistryService managementRegistry = serviceProvider.getService(ManagementRegistryService.class);
final Context cmContext = managementRegistry.getConfiguration().getContext();
final InternalCacheManager cacheManager =
serviceProvider.getService(CacheManagerProviderService.class).getCacheManager();
cacheManager.registerListener(new CacheManagerListener() {
@Override
public void cacheAdded(String alias, Cache<?, ?> cache) {
}
@Override
public void cacheRemoved(String alias, Cache<?, ?> cache) {
}
@Override
public void stateTransition(Status from, Status to) {
switch (to) {
case AVAILABLE:
delegates.put(cmContext, managementRegistry);
break;
case UNINITIALIZED:
delegates.remove(cmContext);
cacheManager.deregisterListener(this);
break;
case MAINTENANCE:
break;
default:
throw new AssertionError("Unsupported state: " + to);
}
}
});
}
@Override
public void stop() {
}
@Override
public Map<Context, ContextContainer> getContextContainers() {
Map<Context, ContextContainer> contexts = new HashMap<>();
for (Map.Entry<Context, ManagementRegistryService> entry : delegates.entrySet()) {
contexts.put(entry.getKey(), entry.getValue().getContextContainer());
}
return contexts;
}
@Override
public Collection<? extends Capability> getCapabilities() {
Collection<Capability> capabilities = new ArrayList<>();
for (ManagementRegistryService registryService : delegates.values()) {
capabilities.addAll(registryService.getCapabilities());
}
return capabilities;
}
@Override
public Collection<String> getCapabilityNames() {
Collection<String> names = new TreeSet<>();
for (ManagementRegistryService registryService : delegates.values()) {
names.addAll(registryService.getCapabilityNames());
}
return names;
}
@Override
public Map<Context, Collection<? extends Capability>> getCapabilitiesByContext() {
Map<Context, Collection<? extends Capability>> capabilities = new HashMap<>();
for (Map.Entry<Context, ManagementRegistryService> entry : delegates.entrySet()) {
capabilities.put(entry.getKey(), entry.getValue().getCapabilities());
}
return capabilities;
}
@Override
public Collection<ManagementProvider<?>> getManagementProvidersByCapability(String capabilityName) {
List<ManagementProvider<?>> allProviders = new ArrayList<>();
for (ManagementRegistryService managementRegistry : delegates.values()) {
allProviders.addAll(managementRegistry.getManagementProvidersByCapability(capabilityName));
}
return allProviders;
}
@Override
public CapabilityManagement withCapability(String capabilityName) {
return new DefaultCapabilityManagement(this, capabilityName);
}
}