001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  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.apache.hadoop.mapred.jobcontrol;
020
021import java.util.ArrayList;
022import java.util.Collection;
023import java.util.List;
024
025import org.apache.hadoop.classification.InterfaceAudience;
026import org.apache.hadoop.classification.InterfaceStability;
027import org.apache.hadoop.mapreduce.lib.jobcontrol.ControlledJob;
028
029@InterfaceAudience.Public
030@InterfaceStability.Stable
031public class JobControl extends 
032    org.apache.hadoop.mapreduce.lib.jobcontrol.JobControl {
033
034  /** 
035   * Construct a job control for a group of jobs.
036   * @param groupName a name identifying this group
037   */
038  public JobControl(String groupName) {
039    super(groupName);
040  }
041  
042  static ArrayList<Job> castToJobList(List<ControlledJob> cjobs) {
043    ArrayList<Job> ret = new ArrayList<Job>();
044    for (ControlledJob job : cjobs) {
045      ret.add((Job)job);
046    }
047    return ret;
048  }
049  
050  /**
051   * @return the jobs in the waiting state
052   */
053  public ArrayList<Job> getWaitingJobs() {
054    return castToJobList(super.getWaitingJobList());
055  }
056        
057  /**
058   * @return the jobs in the running state
059   */
060  public ArrayList<Job> getRunningJobs() {
061    return castToJobList(super.getRunningJobList());
062  }
063        
064  /**
065   * @return the jobs in the ready state
066   */
067  public ArrayList<Job> getReadyJobs() {
068    return castToJobList(super.getReadyJobsList());
069  }
070        
071  /**
072   * @return the jobs in the success state
073   */
074  public ArrayList<Job> getSuccessfulJobs() {
075    return castToJobList(super.getSuccessfulJobList());
076  }
077        
078  public ArrayList<Job> getFailedJobs() {
079    return castToJobList(super.getFailedJobList());
080  }
081
082  /**
083   * Add a collection of jobs
084   * 
085   * @param jobs
086   */
087  public void addJobs(Collection <Job> jobs) {
088    for (Job job : jobs) {
089      addJob(job);
090    }
091  }
092
093  /**
094   * @return the thread state
095   */
096  public int getState() {
097    ThreadState state = super.getThreadState();
098    if (state == ThreadState.RUNNING) {
099      return 0;
100    } 
101    if (state == ThreadState.SUSPENDED) {
102      return 1;
103    }
104    if (state == ThreadState.STOPPED) {
105      return 2;
106    }
107    if (state == ThreadState.STOPPING) {
108      return 3;
109    }
110    if (state == ThreadState.READY ) {
111      return 4;
112    }
113    return -1;
114  }
115
116}