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

com.gemstone.gemfire.admin.jmx.internal.SystemMemberCacheJmxImpl 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.admin.jmx.internal;

import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import javax.management.MalformedObjectNameException;
import javax.management.ObjectName;
import javax.management.modelmbean.ModelMBean;

import org.apache.commons.modeler.ManagedBean;

import com.gemstone.gemfire.SystemFailure;
import com.gemstone.gemfire.admin.AdminException;
import com.gemstone.gemfire.admin.SystemMemberCacheServer;
import com.gemstone.gemfire.admin.SystemMemberRegion;
import com.gemstone.gemfire.admin.internal.SystemMemberBridgeServerImpl;
import com.gemstone.gemfire.cache.Region;
import com.gemstone.gemfire.internal.LogWriterImpl;
import com.gemstone.gemfire.internal.admin.AdminBridgeServer;
import com.gemstone.gemfire.internal.admin.GemFireVM;
import com.gemstone.gemfire.internal.i18n.LocalizedStrings;

/**
 * MBean representation of {@link com.gemstone.gemfire.admin.SystemMemberCache}.
 *
 * @author    Darrel Schneider
 * @author    Kirk Lund
 * @since     3.5
 */
public class SystemMemberCacheJmxImpl 
extends com.gemstone.gemfire.admin.internal.SystemMemberCacheImpl
implements com.gemstone.gemfire.admin.jmx.internal.ManagedResource {

  /** The object name of this managed resource */
  private ObjectName objectName;
  
  /** collection to collect all the resources created for this member */
  private final Map managedRegionResourcesMap = new HashMap();
  private final Map managedCacheServerResourcesMap = new HashMap();

  // -------------------------------------------------------------------------
  //   Constructor(s)
  // -------------------------------------------------------------------------
  
  /** 
   * Constructs an instance of SystemMemberCacheJmxImpl.
   *
   * @param vm
   *        The vm owning the cache this object will manage
   */
  public SystemMemberCacheJmxImpl(GemFireVM vm)
  throws com.gemstone.gemfire.admin.AdminException { 
    super(vm);
    initializeMBean();
  }

  /** Create and register the MBean to manage this resource */
  private void initializeMBean() 
  throws com.gemstone.gemfire.admin.AdminException {
    this.mbeanName = new StringBuffer("GemFire.Cache:")
        .append("name=")
        .append(MBeanUtil.makeCompliantMBeanNameProperty(getName()))
        .append(",id=")
        .append(getId())
        .append(",owner=")
        .append(MBeanUtil.makeCompliantMBeanNameProperty(vm.getId().toString()))
        .append(",type=Cache").toString();
      
    this.objectName = 
      MBeanUtil.createMBean(this, 
                            addDynamicAttributes(MBeanUtil.lookupManagedBean(this)));
  }
  
  // -------------------------------------------------------------------------
  //   Template methods overriden from superclass...
  // -------------------------------------------------------------------------

  /**
   * Override createSystemMemberRegion by instantiating
   * SystemMemberRegionJmxImpl. This instance is also added to the
   * managedResources collection.
   * 
   * @param r
   *          reference to Region instance for which this JMX resource is to be
   *          created
   * @return SystemMemberRegionJmxImpl - JMX Implementation of
   *         SystemMemberRegion
   * @throws AdminException
   *           if constructing SystemMemberRegionJmxImpl instance fails
   */
  @Override  
  protected SystemMemberRegion createSystemMemberRegion(Region r)
    throws com.gemstone.gemfire.admin.AdminException {
    SystemMemberRegionJmxImpl managedSystemMemberRegion = null;
    boolean needsRefresh = false;
    synchronized (this.managedRegionResourcesMap) {
      /* 
       * Ensuring that a single instance of System Member Region is created 
       * per Region.
       */
      SystemMemberRegionJmxImpl managedResource = managedRegionResourcesMap.get(r.getFullPath());
      if (managedResource != null) {
        managedSystemMemberRegion = managedResource;
      } else {
        managedSystemMemberRegion = new SystemMemberRegionJmxImpl(this, r);
        managedRegionResourcesMap.put(r.getFullPath(), managedSystemMemberRegion);
        needsRefresh = true;
      }
    }
    if (needsRefresh) {
      managedSystemMemberRegion.refresh();
    }
    return managedSystemMemberRegion;
  }

  /**
   * Creates a SystemMemberBridgeServerJmxImpl instance. This instance is also
   * added to the managedResources collection.
   * 
   * @param bridge
   *          reference to AdminBridgeServer for which this JMX resource is to
   *          be created
   * @return SystemMemberBridgeServerJmxImpl - JMX Implementation of
   *         SystemMemberBridgeServerImpl
   * @throws AdminException
   *           if constructing SystemMemberBridgeServerJmxImpl instance fails
   */
  @Override  
  protected SystemMemberBridgeServerImpl
    createSystemMemberBridgeServer(AdminBridgeServer bridge) 
    throws AdminException {
    SystemMemberBridgeServerJmxImpl managedSystemMemberBridgeServer = null;
    synchronized (this.managedCacheServerResourcesMap) {
      /* 
       * Ensuring that a single instance of SystemMember BridgeServer is 
       * created per AdminBridgeServer.
       */
      SystemMemberBridgeServerJmxImpl managedCacheServerResource = managedCacheServerResourcesMap.get(bridge.getId());
      if (managedCacheServerResource != null) {
        managedSystemMemberBridgeServer = managedCacheServerResource;
      } else {
        managedSystemMemberBridgeServer = new SystemMemberBridgeServerJmxImpl(this, bridge);
        managedCacheServerResourcesMap.put(bridge.getId(), managedSystemMemberBridgeServer);
      }
    }
    return managedSystemMemberBridgeServer;
  }

  // -------------------------------------------------------------------------
  //   Create MBean attributes for each Statistic
  // -------------------------------------------------------------------------
  
  /**
   * Add MBean attribute definitions for each Statistic.
   *
   * @param managed   the mbean definition to add attributes to
   * @return a new instance of ManagedBean copied from managed but 
   *         with the new attributes added
   */
  ManagedBean addDynamicAttributes(ManagedBean managed)
  throws com.gemstone.gemfire.admin.AdminException {
    if (managed == null) {
      throw new IllegalArgumentException(LocalizedStrings.SystemMemberCacheJmxImpl_MANAGEDBEAN_IS_NULL.toLocalizedString());
    }
    
    refresh(); // to get the stats...
    
    // need to create a new instance of ManagedBean to clean the "slate"...
    ManagedBean newManagedBean = new DynamicManagedBean(managed);
    for (int i = 0; i < this.statistics.length; i++) {
      StatisticAttributeInfo attrInfo = new StatisticAttributeInfo();

      attrInfo.setName(this.statistics[i].getName());
      attrInfo.setDisplayName(this.statistics[i].getName());
      attrInfo.setDescription(this.statistics[i].getDescription());
      attrInfo.setType("java.lang.Number");

      attrInfo.setIs(false);
      attrInfo.setReadable(true);
      attrInfo.setWriteable(false);

      attrInfo.setStat(this.statistics[i]);
      
      newManagedBean.addAttribute(attrInfo);
    }

    return newManagedBean;
  }

  // -------------------------------------------------------------------------
  //   MBean Operations
  // -------------------------------------------------------------------------

  /** 
   * Returns the ObjectName of the Region for the specified path.
   *
   * @throws AdminException
   *         If no region with path path exists
   */
  public ObjectName manageRegion(String path)
  throws AdminException, MalformedObjectNameException {
    try {
      SystemMemberRegionJmxImpl region = null;

      try {
        region = (SystemMemberRegionJmxImpl) getRegion(path);

      } catch (AdminException e) {
        MBeanUtil.logStackTrace(LogWriterImpl.WARNING_LEVEL, e);
        throw e;
      }

      if (region == null) {
        throw new AdminException(LocalizedStrings.SystemMemberCacheJmxImpl_THIS_CACHE_DOES_NOT_CONTAIN_REGION_0.toLocalizedString(path));
          
      } else {
        return ObjectName.getInstance(region.getMBeanName());
      }
    } catch (RuntimeException e) { 
      MBeanUtil.logStackTrace(LogWriterImpl.WARNING_LEVEL, e);
      throw e; 
    } catch (Error e) { 
      if (SystemFailure.isJVMFailureError(e)) {
        SystemFailure.initiateFailure(e);
        // If this ever returns, rethrow the error. We're poisoned
        // now, so don't let this thread continue.
        throw e;
      }
      // Whenever you catch Error or Throwable, you must also
      // check for fatal JVM error (see above).  However, there is
      // _still_ a possibility that you are dealing with a cascading
      // error condition, so you also need to check to see if the JVM
      // is still usable:
      SystemFailure.checkFailure();
      MBeanUtil.logStackTrace(LogWriterImpl.ERROR_LEVEL, e);
      throw e; 
    }
  }

  /**
   * Creates a new cache server MBean and returns its
   * ObjectName.
   *
   * @since 5.7
   */
  public ObjectName manageCacheServer()
    throws AdminException, MalformedObjectNameException {

    try {
      SystemMemberBridgeServerJmxImpl bridge =
        (SystemMemberBridgeServerJmxImpl) addCacheServer();
      return ObjectName.getInstance(bridge.getMBeanName());
    } catch (AdminException e) { 
 	  MBeanUtil.logStackTrace(LogWriterImpl.WARNING_LEVEL, e);
 	  throw e; 
    } catch (RuntimeException e) { 
	  MBeanUtil.logStackTrace(LogWriterImpl.WARNING_LEVEL, e); 
	  throw e;
    } catch (Error e) { 
      if (SystemFailure.isJVMFailureError(e)) {
        SystemFailure.initiateFailure(e);
        // If this ever returns, rethrow the error. We're poisoned
        // now, so don't let this thread continue.
        throw e;
      }
      // Whenever you catch Error or Throwable, you must also
      // check for fatal JVM error (see above).  However, there is
      // _still_ a possibility that you are dealing with a cascading
      // error condition, so you also need to check to see if the JVM
      // is still usable:
      SystemFailure.checkFailure();
      MBeanUtil.logStackTrace(LogWriterImpl.ERROR_LEVEL, e); 
      throw e; 
    }
  }

  /**
   * Creates a new bridge server MBean and returns its
   * ObjectName.
   *
   * @since 4.0
   * @deprecated as of 5.7
   */
  @Deprecated
  public ObjectName manageBridgeServer()
    throws AdminException, MalformedObjectNameException {
    return manageCacheServer();
  }
  
  /**
   * Returns the MBean ObjectNames for all cache servers
   * that serve this cache to clients.
   *
   * @since 4.0
   */
  public ObjectName[] manageCacheServers()
    throws AdminException, MalformedObjectNameException {

    try {
      SystemMemberCacheServer[] bridges = getCacheServers();
      ObjectName[] names = new ObjectName[bridges.length];
      for (int i = 0; i < bridges.length; i++) {
        SystemMemberBridgeServerJmxImpl bridge =
          (SystemMemberBridgeServerJmxImpl) bridges[i];
        names[i] = ObjectName.getInstance(bridge.getMBeanName());
      }

      return names;
    } catch (AdminException e) { 
  	  MBeanUtil.logStackTrace(LogWriterImpl.WARNING_LEVEL, e); 
  	  throw e;
    } catch (RuntimeException e) { 
      MBeanUtil.logStackTrace(LogWriterImpl.WARNING_LEVEL, e); 
      throw e; 
    } catch (Error e) { 
      if (SystemFailure.isJVMFailureError(e)) {
        SystemFailure.initiateFailure(e);
        // If this ever returns, rethrow the error. We're poisoned
        // now, so don't let this thread continue.
        throw e;
      }
      // Whenever you catch Error or Throwable, you must also
      // check for fatal JVM error (see above).  However, there is
      // _still_ a possibility that you are dealing with a cascading
      // error condition, so you also need to check to see if the JVM
      // is still usable:
      SystemFailure.checkFailure();
      MBeanUtil.logStackTrace(LogWriterImpl.ERROR_LEVEL, e); 
      throw e;
    }
  }

  /**
   * Returns the MBean ObjectNames for all bridge servers
   * that serve this cache.
   *
   * @since 4.0
   * @deprecated as of 5.7
   */
  @Deprecated
  public ObjectName[] manageBridgeServers()
    throws AdminException, MalformedObjectNameException {
    return manageCacheServers();
  }

  // -------------------------------------------------------------------------
  //   ManagedResource implementation
  // -------------------------------------------------------------------------
  
  /** The name of the MBean that will manage this resource */
  private String mbeanName;

  /** The ModelMBean that is configured to manage this resource */
  private ModelMBean modelMBean;
  
	public String getMBeanName() {
		return this.mbeanName;
	}
  
	public ModelMBean getModelMBean() {
		return this.modelMBean;
	}
	public void setModelMBean(ModelMBean modelMBean) {
		this.modelMBean = modelMBean;
	}
  
  public ObjectName getObjectName() {
    return this.objectName;
  }

  public ManagedResourceType getManagedResourceType() {
    return ManagedResourceType.SYSTEM_MEMBER_CACHE;
  }

  
  /**
   * Un-registers all the statistics & cache managed resource created for this 
   * member. After un-registering the resource MBean instances, clears 
   * this.memberResources collection.
   * 
   * Creates ConfigurationParameterJmxImpl, StatisticResourceJmxImpl and 
   * SystemMemberCacheJmxImpl. But cleans up only StatisticResourceJmxImpl and 
   * SystemMemberCacheJmxImpl which are of type ManagedResource.  
   */
  public void cleanupResource() {
    synchronized (this.managedRegionResourcesMap) {
      Collection values = managedRegionResourcesMap.values();
      
      for (SystemMemberRegionJmxImpl systemMemberRegionJmxImpl : values) {
        MBeanUtil.unregisterMBean(systemMemberRegionJmxImpl);
      }
      
      this.managedRegionResourcesMap.clear();
    }
    
    synchronized (this.managedCacheServerResourcesMap) {
      Collection values = managedCacheServerResourcesMap.values();
      
      for (SystemMemberBridgeServerJmxImpl SystemMemberBridgeServerJmxImpl : values) {
        MBeanUtil.unregisterMBean(SystemMemberBridgeServerJmxImpl);
      }
      
      this.managedCacheServerResourcesMap.clear();
    }
  }

  /**
   * Cleans up managed resources created for the region that was (created and)
   * destroyed in a cache represented by this Managed Resource.
   * 
   * @param regionPath
   *          path of the region that got destroyed
   * @return a managed resource related to this region path
   */
  public ManagedResource cleanupRegionResources(String regionPath) {
    ManagedResource cleaned = null;
    
    synchronized (this.managedRegionResourcesMap) {
      Set> entries = managedRegionResourcesMap.entrySet();
      for (Iterator> it = entries.iterator(); it.hasNext();) {
        Entry entry = it.next();
        SystemMemberRegionJmxImpl managedResource = entry.getValue();
        ObjectName                objName         = managedResource.getObjectName();
        
        String pathProp = objName.getKeyProperty("path");
        if (pathProp != null && pathProp.equals(regionPath)) {
          cleaned = managedResource;
          it.remove();
          
          break;
        }
      }
    }

    return cleaned;
  }
  
  /**
   * Checks equality of the given object with this based on the
   * type (Class) and the MBean Name returned by getMBeanName()
   * methods.
   * 
   * @param obj
   *          object to check equality with
   * @return true if the given object is if the same type and its MBean Name is
   *         same as this object's MBean Name, false otherwise
   */
  @Override
  public boolean equals(Object obj) {
    if ( !(obj instanceof SystemMemberCacheJmxImpl) ) {
      return false;
    }
    
    SystemMemberCacheJmxImpl other = (SystemMemberCacheJmxImpl) obj;

    return this.getMBeanName().equals(other.getMBeanName());
  }
  
  /**
   * Returns hash code for this object which is based on the MBean 
   * Name generated. 
   * 
   * @return hash code for this object
   */
  @Override
  public int hashCode() {
    return this.getMBeanName().hashCode();
  }
}





© 2015 - 2024 Weber Informatics LLC | Privacy Policy