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

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

import java.net.InetAddress;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

import com.gemstone.gemfire.CancelException;
import com.gemstone.gemfire.SystemFailure;
import com.gemstone.gemfire.admin.AdminDistributedSystem;
import com.gemstone.gemfire.admin.AdminException;
import com.gemstone.gemfire.admin.CacheDoesNotExistException;
import com.gemstone.gemfire.admin.ConfigurationParameter;
import com.gemstone.gemfire.admin.RuntimeAdminException;
import com.gemstone.gemfire.admin.StatisticResource;
import com.gemstone.gemfire.admin.SystemMemberCache;
import com.gemstone.gemfire.admin.SystemMemberType;
import com.gemstone.gemfire.distributed.DistributedMember;
import com.gemstone.gemfire.distributed.Role;
import com.gemstone.gemfire.distributed.internal.DistributionConfig;
import com.gemstone.gemfire.distributed.internal.DistributionConfigImpl;
import com.gemstone.gemfire.distributed.internal.membership.InternalDistributedMember;
import com.gemstone.gemfire.i18n.LogWriterI18n;
import com.gemstone.gemfire.internal.Config;
import com.gemstone.gemfire.internal.ConfigSource;
import com.gemstone.gemfire.internal.admin.GemFireVM;
import com.gemstone.gemfire.internal.admin.StatResource;
import com.gemstone.gemfire.internal.i18n.LocalizedStrings;

/**
 * Member of a GemFire system.
 *
 * @author    Kirk Lund
 * @since     3.5
 */
public class SystemMemberImpl 
implements com.gemstone.gemfire.admin.SystemMember,
           com.gemstone.gemfire.admin.internal.ConfigurationParameterListener {

  /** Identifying name of this member.
   * Note that by default this is the string form of internalId but the
   * ManagedSystemMemberImpl subclass resets it to getNewId()
   */
  protected String id;

  /** Unique internal id that the system impl identifies this member with */
  protected InternalDistributedMember internalId;

  /** The name of this system member */
  protected String name;

  /** Host name of the machine this member resides on */
  protected String host;

  /** The internal configuration this impl delegates to for runtime config */
//  private Config config;

  /** The configuration parameters for this member.  Maps the name of
      the ConfigurationParameter to the ConfigurationParameter. */
  protected Map parms = new HashMap();

  /** The {@link AdminDistributedSystem} this is a member of */
  protected AdminDistributedSystem system;

  /** Internal GemFire vm to delegate to */
  private GemFireVM vm;
  
  // -------------------------------------------------------------------------
  //   Constructor(s)
  // -------------------------------------------------------------------------

  /** 
   * Constructs new SystemMemberImpl for a
   * ManagedEntity that has yet to be started.
   *
   * @param system  the distributed system this member belongs to
   */
  protected SystemMemberImpl(AdminDistributedSystem system) 
    throws AdminException {

    this.system = system;
    refreshConfig(getDefaultConfig());
  }

  /** 
   * Constructs new SystemMemberImpl from the given
   * GemFireVM.  This constructor is invoked when we
   * discover a new member of the distributed system.
   *
   * @param system      the distributed system this member belongs to
   * @param vm internal GemFire vm to delegate to
   */
  public SystemMemberImpl(AdminDistributedSystem system, 
                          GemFireVM vm)
    throws AdminException {

    this(system);
    setGemFireVM(vm);
  }

  /**
   * Constructs the instance of SystemMember using the corresponding
   * InternalDistributedMember instance of a DS member for the given
   * AdminDistributedSystem.
   * 
   * @param system
   *          Current AdminDistributedSystem instance
   * @param member
   *          InternalDistributedMember instance for which a SystemMember
   *          instance is to be constructed.
   * @throws AdminException
   *           if construction of SystemMember fails
   *           
   * @since 6.5
   */
  protected SystemMemberImpl(AdminDistributedSystem system,
                          InternalDistributedMember member) 
                          throws AdminException {
    this(system);
    updateByInternalDistributedMember(member);
  }

  // -------------------------------------------------------------------------
  //   Attribute accessors and mutators
  // -------------------------------------------------------------------------

  /**
   * Returns a Config object with the appropriate default
   * values for a newly-created system member.
   */
  protected Config getDefaultConfig() {
    Properties props = new Properties();
    return new DistributionConfigImpl(props);
  }

  public final AdminDistributedSystem getDistributedSystem() {
    return this.system;
  }
  
  public final InternalDistributedMember getInternalId() {
    return internalId;
  }

  public final String getId() {
    return this.id;
  }

  public final String getName() {
    return this.name;
  }
  
  public String getHost() {
    return this.host;
  }
  
  public final InetAddress getHostAddress() {
    return InetAddressUtil.toInetAddress(this.getHost());
  }

  // -------------------------------------------------------------------------
  //   Operations
  // -------------------------------------------------------------------------
  
  public final String getLog() {
    String childTail = null;
    String mainTail = null;
    GemFireVM vm = getGemFireVM();
    if (vm != null) {
      String[] log = vm.getSystemLogs();
      if (log != null && log.length > 0) mainTail = log[0];
      if (log != null && log.length > 1) childTail = log[1];
    }
    
    if (childTail == null && mainTail == null) {
      return LocalizedStrings.SystemMemberImpl_NO_LOG_FILE_CONFIGURED_LOG_MESSAGES_WILL_BE_DIRECTED_TO_STDOUT.toLocalizedString();
    } 
    else {
      StringBuffer result = new StringBuffer();
      if (mainTail != null) {
        result.append(mainTail);
      }
      if (childTail != null) {
        result.append("\n" + LocalizedStrings.SystemMemberImpl_TAIL_OF_CHILD_LOG.toLocalizedString() + "\n");
        result.append(childTail);
      }
      return result.toString();
    }
  }

  public final String getVersion() {
    GemFireVM vm = getGemFireVM();
    if (vm == null) return null;
    return vm.getVersionInfo();
  }

  public StatisticResource[] getStat(String statisticsTypeName) 
  throws com.gemstone.gemfire.admin.AdminException {
    StatisticResource[] res = new StatisticResource[0];
    if (this.vm != null) {
      res = getStatsImpl(this.vm.getStats(statisticsTypeName));
    }
    return res.length==0 ? null : res;
  }

  public StatisticResource[] getStats() 
  throws com.gemstone.gemfire.admin.AdminException {
    StatisticResource[] statsImpl = new StatisticResource[0];
    if (this.vm != null) {
      statsImpl = getStatsImpl(this.vm.getStats(null));
    }
    return statsImpl;
  }

  public final boolean hasCache() {
    GemFireVM member = getGemFireVM();
    if (member == null) {
      return false;

    } else {
      return member.getCacheInfo() != null;
    }
  }

  public final SystemMemberCache getCache()
    throws com.gemstone.gemfire.admin.AdminException
  {
    GemFireVM vm = getGemFireVM(); // fix for bug 33505
    if (vm == null) return null;
    try {
      return createSystemMemberCache(vm);

    } catch (CancelException ex) {
      return null;

    } catch (CacheDoesNotExistException ex) {
      return null;
    }
  }
  
  public void refreshConfig() 
  throws com.gemstone.gemfire.admin.AdminException {
    GemFireVM vm = getGemFireVM();
    if (vm == null) return;
    refreshConfig(vm.getConfig());
  }
  
  /**
   * Sets the value of this system member's distribution-related
   * configuration based on the given Config object.
   */
  public final void refreshConfig(Config config) 
  throws com.gemstone.gemfire.admin.AdminException {
    if (config == null) {
      throw new AdminException(LocalizedStrings.SystemMemberImpl_FAILED_TO_REFRESH_CONFIGURATION_PARAMETERS_FOR_0.toLocalizedString(new Object[] {getId()}));
    }
    
    String[] names = config.getAttributeNames();
    if (names == null || names.length < 1) {
      throw new AdminException(LocalizedStrings.SystemMemberImpl_FAILED_TO_REFRESH_CONFIGURATION_PARAMETERS_FOR_0.toLocalizedString(new Object[] {getId()}));
    }
    
    for (int i = 0; i < names.length; i++) {
      String name = names[i];
      ConfigurationParameter parm = createConfigurationParameter(
            name,                                 // name
            config.getAttributeDescription(name), // description
            config.getAttributeObject(name),      // value
            config.getAttributeType(name),        // valueType
            config.isAttributeModifiable(name) ); // isModifiable
      ((ConfigurationParameterImpl) parm).addConfigurationParameterListener(this);
      this.parms.put(name, parm);
    }
  }
  
  public final ConfigurationParameter[] getConfiguration() {
    ConfigurationParameter[] array =
      new ConfigurationParameter[this.parms.size()];
    this.parms.values().toArray(array);
    return array;
  }
  
  public ConfigurationParameter[]
    setConfiguration(ConfigurationParameter[] parms) 
    throws AdminException {

    for (int i = 0; i < parms.length; i++) {
      ConfigurationParameter parm = parms[i];
      this.parms.put(parm.getName(), parm);
    }

    GemFireVM vm = getGemFireVM();
    if (vm != null) {
      // update internal vm's config...    
      Config config = vm.getConfig();
      for (int i = 0; i < parms.length; i++) {
        config.setAttributeObject(parms[i].getName(), parms[i].getValue(), ConfigSource.runtime());
      }
      vm.setConfig(config);
    }

    return this.getConfiguration();
  }
  
  public SystemMemberType getType() {
    return SystemMemberType.APPLICATION;
  }
  
  // -------------------------------------------------------------------------
  //   Listener callbacks
  // -------------------------------------------------------------------------
  
  // -- com.gemstone.gemfire.admin.internal.ConfigurationParameterListener ---
  public void configurationParameterValueChanged(ConfigurationParameter parm) {
    LogWriterI18n logWriter = this.system.getLogWriter().convertToLogWriterI18n();
    try {
      setConfiguration(new ConfigurationParameter[] { parm });
    } catch (com.gemstone.gemfire.admin.AdminException e) {
      // this shouldn't occur since this is a config listener method...
      logWriter.warning(e);
      throw new RuntimeAdminException(e);
    } catch (java.lang.Exception e) {
      logWriter.warning(e);
//    }
//    catch (java.lang.RuntimeException e) {
//      logWriter.warning(e);
//      throw e;
//    }
    } catch (java.lang.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();
      logWriter.error(e);
      throw e;
    }
  }
  
  // -------------------------------------------------------------------------
  //   Overridden method(s) from java.lang.Object
  // -------------------------------------------------------------------------
  
  @Override
  public String toString() {
    return getName();
  }
  
  // -------------------------------------------------------------------------
  //   Template methods with default behavior impl'ed.  Override if needed.
  // -------------------------------------------------------------------------
  
  /**
   * Returns the GemFireVM that underlies this
   * SystemMember. 
   */
  protected final GemFireVM getGemFireVM() {
    return this.vm;
  }

  /**
   * Sets the GemFireVM that underlies this
   * SystemMember.  This method is used when a member,
   * such as a cache server, is started by the admin API.
   */
  void setGemFireVM(GemFireVM vm) throws AdminException {
    this.vm = vm;
    if (vm != null) {
      this.internalId = vm.getId();
      this.id = this.internalId.toString();
      this.name = vm.getName();
      this.host = InetAddressUtil.toString(vm.getHost());
    } else {
      this.internalId = null;
      this.id = null;
      // leave this.name set to what it is (how come?)
      this.host = this.getHost();
    }

    if (DistributionConfig.DEFAULT_NAME.equals(this.name)) {
      // Fix bug 32877
      this.name = this.id;
    }

    if (vm != null) {
      this.refreshConfig();
    }
  }

  /**
   * Updates this SystemMember instance using the corresponding
   * InternalDistributedMember
   * 
   * @param member
   *          InternalDistributedMember instance to update this SystemMember
   *          
   * @since 6.5
   */
  private void updateByInternalDistributedMember(
      InternalDistributedMember member) {
    if (member != null) {
      this.internalId = member;
      this.id         = this.internalId.toString();
      this.host       = this.internalId.getHost();
      this.name       = this.internalId.getName();      
      if (this.name == null || 
          DistributionConfig.DEFAULT_NAME.equals(this.name)) { 
        /*
         * name could be null & referring to description of a fix for 32877
         */
        this.name = this.id;
      }
    }
  }
  
  /**
   * Template method for creating {@link StatisticResource}.
   *
   * @param stat  the internal stat resource to wrap with {@link StatisticResource}
   * @return new impl instance of {@link StatisticResource}
   */
  protected StatisticResource createStatisticResource(StatResource stat)
  throws com.gemstone.gemfire.admin.AdminException {
    return new StatisticResourceImpl(stat, this);
  }
  
  /**
   * Template method for creating {@link ConfigurationParameter}.
   *
   * @param name            the name of this parameter which cannot change
   * @param description     full description to use
   * @param value           the value of this parameter
   * @param type            the class type of the value
   * @param userModifiable  true if this is modifiable; false if read-only
   * @return new impl instance of {@link ConfigurationParameter}
   */
  protected ConfigurationParameter createConfigurationParameter(String name,
                                                                String description,
                                                                Object value,
                                                                Class type,
                                                                boolean userModifiable) {
    return new ConfigurationParameterImpl(
        name, description, value, type, userModifiable);
  }
  
  /**
   * Template method for creating {@link SystemMemberCache}.
   *
   * @param vm  the GemFire vm to retrieve cache info from
   * @return new impl instance of {@link SystemMemberCache}
   */
  protected SystemMemberCache createSystemMemberCache(GemFireVM vm)
    throws com.gemstone.gemfire.admin.AdminException
  {
    return new SystemMemberCacheImpl(vm);
  }

  /** Wrap the internal stats with impls of {@link StatisticResource} */
  protected StatisticResource[] getStatsImpl(StatResource[] stats)
  throws com.gemstone.gemfire.admin.AdminException {
    List statList = new ArrayList();
    for (int i = 0; i < stats.length; i++) {
      statList.add(createStatisticResource(stats[i]));
    }
    return (StatisticResource[]) statList.toArray(new StatisticResource[0]);
  }

  public String[] getRoles() {
    Set roles = this.internalId.getRoles();
    String[] roleNames = new String[roles.size()];
    Iterator iter = roles.iterator();
    for (int i = 0; i < roleNames.length; i++) {
      Role role = (Role) iter.next();
      roleNames[i] = role.getName();
    }
    return roleNames;
  }
  
  public DistributedMember getDistributedMember() {
    return this.internalId;    
  }
}





© 2015 - 2024 Weber Informatics LLC | Privacy Policy