001/*
002 * JPPF.
003 * Copyright (C) 2005-2018 JPPF Team.
004 * http://www.jppf.org
005 *
006 * Licensed under the Apache License, Version 2.0 (the "License");
007 * you may not use this file except in compliance with the License.
008 * You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018
019package org.jppf.client.monitoring.topology;
020
021import org.jppf.client.monitoring.AbstractComponent;
022import org.jppf.management.JPPFManagementInfo;
023import org.jppf.management.diagnostics.HealthSnapshot;
024
025/**
026 * Base superclass for components of a JPPF grid topology.
027 * This class proivdes an API to navigate the topology tree and attributes common to all ellements in te tree.
028 * @author Laurent Cohen
029 * @since 5.0
030 */
031public abstract class AbstractTopologyComponent extends AbstractComponent<AbstractTopologyComponent> {
032  /**
033   * Explicit serialVersionUID.
034   */
035  private static final long serialVersionUID = 1L;
036  /**
037   * Object describing the current health snapshot of a node or driver.
038   */
039  protected transient HealthSnapshot healthSnapshot = new HealthSnapshot();
040  /**
041   * The management informtation on this topology component.
042   */
043  protected transient JPPFManagementInfo managementInfo;
044
045  /**
046   * Initialize this component witht he specified uuid.
047   * @param uuid the uuid assigned to this component.
048   */
049  AbstractTopologyComponent(final String uuid) {
050    super(uuid);
051  }
052
053  /**
054   * Determine whether this object represents a driver.
055   * @return {@code true} if this object represents a driver, {@code false} otherwise.
056   */
057  public boolean isDriver() {
058    return false;
059  }
060
061  /**
062   * Determine whether this object represents a node.
063   * @return {@code true} if this object represents a node, {@code false} otherwise.
064   */
065  public boolean isNode() {
066    return false;
067  }
068
069  /**
070   * Determine whether this object represents a peer driver.
071   * @return {@code true} if this object represnets a peer driver, {@code false} otherwise.
072   */
073  public boolean isPeer() {
074    return false;
075  }
076
077  /**
078   * Get the object describing the health of a node or driver.
079   * @return a {@link HealthSnapshot} instance.
080   */
081  public synchronized HealthSnapshot getHealthSnapshot() {
082    return healthSnapshot;
083  }
084
085  /**
086   * Refresh the health snapshot state of the driver or node represented by this topology data.
087   * @param newSnapshot the new health snapshot fetched from the grid.
088   */
089  synchronized void refreshHealthSnapshot(final HealthSnapshot newSnapshot) {
090    if (isPeer()) return;
091    this.healthSnapshot = newSnapshot;
092  }
093
094  /**
095   * Get the management informtation on this topology component.
096   * @return a {@link JPPFManagementInfo} instance.
097   */
098  public synchronized JPPFManagementInfo getManagementInfo() {
099    return managementInfo;
100  }
101
102  @Override
103  public String toString() {
104    return (getManagementInfo() == null) ? "unknown" : getManagementInfo().getHost() + ':' + getManagementInfo().getPort();
105  }
106}