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 java.util.EventObject;
022
023/**
024 * Instances of this class represent topology events, including addition/removal of a driver,
025 * addition/removal/update of a node and addition/removal of a peer driver connected as a node.
026 * @author Laurent Cohen
027 * @since 5.0
028 */
029public class TopologyEvent extends EventObject {
030  /**
031   * Explicit serialVersionUID.
032   */
033  private static final long serialVersionUID = 1L;
034  /**
035   * Data for the driver.
036   */
037  private final TopologyDriver driverData;
038  /**
039   * Data for the node, if any.
040   */
041  private final TopologyNode nodeData;
042  /**
043   * The type of update.
044   */
045  private final UpdateType updateType;
046
047  /**
048   * The possible types of events.
049   * @exclude
050   */
051  enum Type {
052    /**
053     * A driver was added.
054     */
055    DRIVER_ADDED,
056    /**
057     * A driver was removed.
058     */
059    DRIVER_REMOVED,
060    /**
061     * A driver was updated.
062     */
063    DRIVER_UPDATED,
064    /**
065     * A node was added.
066     */
067    NODE_ADDED,
068    /**
069     * A node was removed.
070     */
071    NODE_REMOVED,
072    /**
073     * A node was updated.
074     */
075    NODE_UPDATED
076  };
077
078  /**
079   * The types of updates for an event.
080   * @exclude
081   */
082  public enum UpdateType {
083    /**
084     * Topology update: driver or node has been added or removed.
085     */
086    TOPOLOGY,
087    /**
088     * JVM health snapshot was updated.
089     */
090    JVM_HEALTH,
091    /**
092     * Node state was updated.
093     */
094    NODE_STATE,
095  }
096
097  /**
098   * Initialize this event.
099   * @param source the source of this event.
100   * @param driverData the driver data.
101   * @param nodeData the node data.
102   * @param updateType the type of update.
103   * @exclude
104   */
105  public TopologyEvent(final TopologyManager source, final TopologyDriver driverData, final TopologyNode nodeData, final UpdateType updateType) {
106    super(source);
107    this.driverData = driverData;
108    this.nodeData = nodeData;
109    this.updateType = updateType;
110  }
111
112  /**
113   * Get the driver data.
114   * @return a {@link TopologyDriver} instance.
115   */
116  public TopologyDriver getDriver() {
117    return driverData;
118  }
119
120  /**
121   * Get the node data.
122   * @return a {@link TopologyNode} instance.
123   */
124  public TopologyNode getNodeOrPeer() {
125    return nodeData;
126  }
127
128  /**
129   * Get the topology manager which emitted this event.
130   * @return a {@link TopologyManager} instance.
131   */
132  public TopologyManager getTopologyManager() {
133    return (TopologyManager) getSource();
134  }
135
136  @Override
137  public String toString() {
138    final StringBuilder sb = new StringBuilder(getClass().getSimpleName()).append('[');
139    sb.append("source=").append(getTopologyManager());
140    sb.append(", driver=").append(getDriver());
141    sb.append(", node/peer=").append(getNodeOrPeer());
142    sb.append(']');
143    return sb.toString();
144  }
145
146  /**
147   * Get the type of update.
148   * @return an {@link UpdateType} enum value.
149   * @exclude
150   */
151  public UpdateType getUpdateType() {
152    return updateType;
153  }
154}