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

com.gemstone.gemfire.management.internal.BaseManagementService Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (c) 2010-2015 Pivotal Software, Inc. All rights reserved.
 *
 * 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. See accompanying
 * LICENSE file.
 */
package com.gemstone.gemfire.management.internal;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.gemstone.gemfire.cache.Cache;
import com.gemstone.gemfire.distributed.DistributedSystemDisconnectedException;
import com.gemstone.gemfire.distributed.internal.InternalDistributedSystem;
import com.gemstone.gemfire.i18n.LogWriterI18n;
import com.gemstone.gemfire.management.ManagementService;

/**
 * Super class to all Management Service
 * 
 * @author rishim.
 * @since 7.0
 */
public abstract class BaseManagementService extends ManagementService {

  /**
   * The main mapping between different resources and Service instance Object
   * can be Cache
   */
  protected static final Map instances = new HashMap();

  /** List of connected DistributedSystems */
  private static final List systems = new ArrayList(
      1);

  /** Log writer */
  protected static LogWriterI18n logger;

  /** Protected constructor. */
  protected BaseManagementService() {
    logger = InternalDistributedSystem.getLoggerI18n();
  }

  // Static block to initialize the ConnectListener on the System

  static {
    initInternalDistributedSystem();

  }
  
  /**
   * This method will close the service. Any operation on the service instance
   * will throw exception
   */

  protected abstract void close();

  /**
   * This method will close the service. Any operation on the service instance
   * will throw exception
   */

  protected abstract boolean isClosed();

  /**
   * Returns a ManagementService to use for the specified Cache.
   * 
   * @param cache
   *          defines the scope of resources to be managed
   */
  public static ManagementService getManagementService(Cache cache) {
    synchronized (instances) {
      BaseManagementService service = (BaseManagementService) instances.get(cache);
      if (service == null) {
        service = SystemManagementService.newSystemManagementService(cache);
        instances.put(cache, service);

      }
      return service;
    }
  }
  public static ManagementService getExistingManagementService(Cache cache) {
    synchronized (instances) {
      BaseManagementService service = (BaseManagementService) instances.get(cache);
      return service;
    }
  }
  
 
  /**
   * Initialises the distributed system listener
   */
  private static void initInternalDistributedSystem() {
    synchronized (instances) {
      // Initialize our own list of distributed systems via a connect listener
      @SuppressWarnings("unchecked")
      List existingSystems = InternalDistributedSystem
          .addConnectListener(new InternalDistributedSystem.ConnectListener() {
            public void onConnect(InternalDistributedSystem sys) {
              addInternalDistributedSystem(sys);
            }
          });

      // While still holding the lock on systems, add all currently known
      // systems to our own list
      for (InternalDistributedSystem sys : existingSystems) {
        try {
          if (sys.isConnected()) {
            addInternalDistributedSystem(sys);
          }
        } catch (DistributedSystemDisconnectedException e) {
          if (logger.fineEnabled()) {
            logger.fine("DistributedSystemDisconnectedException"
                + e.getMessage());
          }
        }
      }
    }
  }

  /**
   * Add an Distributed System and adds a Discon Listener
   * 
   * @param sys
   */
  private static void addInternalDistributedSystem(InternalDistributedSystem sys) {
    synchronized (instances) {
      sys
          .addDisconnectListener(new InternalDistributedSystem.DisconnectListener() {
            @Override
            public String toString() {
              return "Disconnect listener for BaseManagementService";
            }

            public void onDisconnect(InternalDistributedSystem ss) {
              removeInternalDistributedSystem(ss);
            }
          });
      systems.add(sys);
    }
  }

  /**
   * Remove a Distributed System from the system lists. If list is empty it
   * closes down all the services if not closed
   * 
   * @param sys
   */
  private static void removeInternalDistributedSystem(
      InternalDistributedSystem sys) {
    synchronized (instances) {
      systems.remove(sys);
      if (systems.isEmpty()) {
        for (Object key : instances.keySet()) {
          BaseManagementService service = (BaseManagementService)instances.get(key);
          try {
            if (!service.isClosed()) {
              // Service close method should take care of the cleaning up
              // activities
              service.close();
            }

          } catch (Exception e) {
            if (logger.fineEnabled()) {
              logger
                  .fine("ManagementException while removing InternalDistributedSystem"
                      + e.getMessage());
            }
          }
        }
        instances.clear();
      }

    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy