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.event;
020
021import java.util.*;
022
023import org.jppf.client.*;
024import org.jppf.client.balancer.ChannelWrapperRemote;
025import org.jppf.execute.ExecutorChannel;
026import org.jppf.node.protocol.Task;
027
028/**
029 * Event emitted by a job when its execution starts or completes.
030 * @author Laurent Cohen
031 */
032public class JobEvent extends EventObject {
033  /**
034   * Explicit serialVersionUID.
035   */
036  private static final long serialVersionUID = 1L;
037  /**
038   * The type of event.
039   * @exclude
040   */
041  public enum Type {
042    /**
043     * The job started.
044     */
045    JOB_START,
046    /**
047     * The job ended.
048     */
049    JOB_END,
050    /**
051     * The job was disatched to a channel.
052     */
053    JOB_DISPATCH,
054    /**
055     * The returnd from a channel.
056     */
057    JOB_RETURN
058  }
059
060  /**
061   * The channel to which a job is dispatched or from which it returns.
062   */
063  private final ExecutorChannel<?> channel;
064  /**
065   * The tasks that were dispatched or returned.
066   */
067  private final List<Task<?>> tasks;
068
069  /**
070   * Initialize this event with the specified job as its source.
071   * @param source the source of this event.
072   * @exclude
073   */
074  public JobEvent(final JPPFJob source) {
075    this(source, null, null);
076  }
077
078  /**
079   * Initialize this event with the specified job as its source.
080   * @param source the source of this event.
081   * @param channel the channel to which a job is dispatched or from which it returns.
082   * @param tasks the tasks that were dispatched or returned.
083   * @exclude
084   */
085  public JobEvent(final JPPFJob source, final ExecutorChannel<?> channel, final List<Task<?>> tasks) {
086    super(source);
087    this.channel = channel;
088    this.tasks = tasks;
089  }
090
091  /**
092   * Get the source of this event.
093   * @return the source as a {@link JPPFJob} object.
094   */
095  public JPPFJob getJob() {
096    return (JPPFJob) getSource();
097  }
098
099  /**
100   * Get the tasks that were dispatched or returned.
101   * <p>This method returns a non <code>null</code> value only for <code>jobDispatched()</code> and <code>jobReturned()</code> events.
102   * @return a list of {@link Task} instances.
103   * @since 4.0
104   */
105  public List<Task<?>> getJobTasks() {
106    return tasks;
107  }
108
109  /**
110   * Determine whether the current job dispatch is sent to a remote driver.
111   * Note that this method is only useful in the scope of the {@link JobListener#jobDispatched(JobEvent)} and {@link JobListener#jobReturned(JobEvent)} notifications.
112   * @return {@code true} if the current job dispatch is executed remotely or the job has not yet been dispatched, {@code false} if the job dispatch is executed locally.
113   * @since 4.2
114   */
115  public boolean isRemoteExecution() {
116    return channel == null ? false : !channel.isLocal();
117  }
118
119  /**
120   * Get the remote driver connection through which the job dispatch was sent.
121   * Note that this method is only useful in the scope of the {@link JobListener#jobDispatched(JobEvent)} and {@link JobListener#jobReturned(JobEvent)} notifications.
122   * @return a {@link JPPFClientConnection} instance, or {@code null} if {@link #isRemoteExecution()} returns {@code false}.
123   * @since 4.2
124   */
125  public JPPFClientConnection getConnection() {
126    return isRemoteExecution() ? ((ChannelWrapperRemote) channel).getChannel() : null;
127  }
128}