All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.ehcache.management.registry.DefaultSharedManagementService Maven / Gradle / Ivy

There is a newer version: 3.10.8
Show newest version
/*
 * Copyright Terracotta, 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 org.ehcache.management.registry;

import org.ehcache.Cache;
import org.ehcache.Status;
import org.ehcache.core.events.CacheManagerListener;
import org.ehcache.core.spi.store.InternalCacheManager;
import org.ehcache.impl.internal.concurrent.ConcurrentHashMap;
import org.ehcache.management.ManagementRegistryService;
import org.ehcache.management.SharedManagementService;
import org.ehcache.spi.service.ServiceProvider;
import org.ehcache.core.spi.service.CacheManagerProviderService;
import org.ehcache.spi.service.Service;
import org.ehcache.spi.service.ServiceDependencies;
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.concurrent.ConcurrentMap;

/**
 * This service can be registered across several cache managers and provides a way to access per-cache manager management registry
 */
@ServiceDependencies({CacheManagerProviderService.class, ManagementRegistryService.class})
public class DefaultSharedManagementService implements SharedManagementService {

  private final ConcurrentMap delegates = new ConcurrentHashMap();

  @Override
  public void start(final ServiceProvider 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:
            // in case we need management capabilities in maintenance mode
            break;

          default:
            throw new AssertionError("Unsupported state: " + to);
        }
      }
    });
  }

  @Override
  public void stop() {
  }

  @Override
  public Map getContextContainers() {
    Map contexts = new HashMap();
    for (Map.Entry entry : delegates.entrySet()) {
      contexts.put(entry.getKey(), entry.getValue().getContextContainer());
    }
    return contexts;
  }

  @Override
  public Map> getCapabilities() {
    Map> capabilities = new HashMap>();
    for (Map.Entry entry : delegates.entrySet()) {
      capabilities.put(entry.getKey(), entry.getValue().getCapabilities());
    }
    return capabilities;
  }

  @Override
  public Collection> getManagementProvidersByCapability(String capabilityName) {
    List> 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);
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy