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.jobs;
020
021import java.util.EventObject;
022
023import org.jppf.client.monitoring.AbstractComponent;
024
025/**
026 * This class represents an event emitteed by a {@link JobMonitor}.
027 * @author Laurent Cohen
028 * @since 5.1
029 */
030public class JobMonitoringEvent extends EventObject {
031  /**
032   * Explicit serialVersionUID.
033   */
034  private static final long serialVersionUID = 1L;
035  /**
036   * The job driver where this event originates from.
037   */
038  private final JobDriver jobDriver;
039  /**
040   * The related job, if any.
041   */
042  private final Job job;
043  /**
044   * The related job dispatch, if any.
045   */
046  private final JobDispatch jobDispatch;
047
048  /**
049   * The possible types of events.
050   * @exclude
051   */
052  enum Type {
053    /**
054     * A driver was added.
055     */
056    DRIVER_ADDED,
057    /**
058     * A driver was removed.
059     */
060    DRIVER_REMOVED,
061    /**
062     * A job was added.
063     */
064    JOB_ADDED,
065    /**
066     * A job was removed.
067     */
068    JOB_REMOVED,
069    /**
070     * A job was updated.
071     */
072    JOB_UPDATED,
073    /**
074     * A job dispatch was added.
075     */
076    DISPATCH_ADDED,
077    /**
078     * A job dispatch was removed.
079     */
080    DISPATCH_REMOVED
081  };
082
083  /**
084   * Initialize this job event.
085   * @param jobMonitor the job monitor which emitted this event.
086   * @param jobDriver the job driver from which this event originates.
087   * @param job the related job, if any.
088   * @param jobDuspatch the related job dispatch, if any.
089   */
090  JobMonitoringEvent(final JobMonitor jobMonitor, final JobDriver jobDriver, final Job job, final JobDispatch jobDuspatch) {
091    super(jobMonitor);
092    this.jobDriver = jobDriver;
093    this.job = job;
094    this.jobDispatch = jobDuspatch;
095  }
096
097  /**
098   * Get the job monitor which emitted this event.
099   * @return a {@link JobMonitor} instance.
100   */
101  public JobMonitor getJobMonitor() {
102    return (JobMonitor) getSource();
103  }
104
105  /**
106   * Get the job driver from which this event originates.
107   * @return a {@link JobDriver} instance.
108   */
109  public JobDriver getJobDriver() {
110    return jobDriver;
111  }
112
113  /**
114   * Get the related job, if any.
115   * @return a {@link Job} instance, or {@code null} if this event does not relate to a job.
116   */
117  public Job getJob() {
118    return job;
119  }
120
121  /**
122   * Get the related job dispatch, if any.
123   * @return a {@link JobDispatch} instance, or {@code null} if this event does not relate to a job dispatch.
124   */
125  public JobDispatch getJobDispatch() {
126    return jobDispatch;
127  }
128
129  @Override
130  public String toString() {
131    return "JobMonitoringEvent[jobDriver=" + name(jobDriver) + ", job=" + name(job) + ", jobDispatch=" + name(jobDispatch) + "]";
132  }
133
134  /**
135   * Get a string representing the specified component.
136   * @param comp the component whose name to get.
137   * @return a string representing the component.
138   */
139  private static String name(final AbstractComponent<?> comp) {
140    return (comp == null) ? "none" : comp.getDisplayName();
141  }
142}