1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.hadoop.hbase.mapreduce;
19  
20  import java.io.IOException;
21  import java.lang.reflect.Constructor;
22  import java.lang.reflect.Method;
23  
24  import org.apache.hadoop.conf.Configuration;
25  import org.apache.hadoop.mapreduce.Job;
26  import org.apache.hadoop.mapreduce.JobContext;
27  import org.apache.hadoop.mapreduce.JobID;
28  
29  /**
30   * This class provides shims for HBase to interact with the Hadoop 1.0.x and the
31   * Hadoop 0.23.x series.
32   * 
33   * NOTE: No testing done against 0.22.x, or 0.21.x.
34   */
35  abstract public class MapreduceTestingShim {
36    private static MapreduceTestingShim instance;
37  
38    static {
39      try {
40        // This class exists in hadoop 0.22+ but not in Hadoop 20.x/1.x
41        Class c = Class
42            .forName("org.apache.hadoop.mapreduce.task.TaskAttemptContextImpl");
43        instance = new MapreduceV2Shim();
44      } catch (Exception e) {
45        instance = new MapreduceV1Shim();
46      }
47    }
48  
49    abstract public JobContext newJobContext(Configuration jobConf)
50        throws IOException;
51  
52    public static JobContext createJobContext(Configuration jobConf)
53        throws IOException {
54      return instance.newJobContext(jobConf);
55    }
56  
57    private static class MapreduceV1Shim extends MapreduceTestingShim {
58      public JobContext newJobContext(Configuration jobConf) throws IOException {
59        // Implementing:
60        // return new JobContext(jobConf, new JobID());
61        JobID jobId = new JobID();
62        Constructor<JobContext> c;
63        try {
64          c = JobContext.class.getConstructor(Configuration.class, JobID.class);
65          return c.newInstance(jobConf, jobId);
66        } catch (Exception e) {
67          throw new IllegalStateException(
68              "Failed to instantiate new JobContext(jobConf, new JobID())", e);
69        }
70      }
71    };
72  
73    private static class MapreduceV2Shim extends MapreduceTestingShim {
74      public JobContext newJobContext(Configuration jobConf) {
75        // Implementing:
76        // return Job.getInstance(jobConf);
77        try {
78          Method m = Job.class.getMethod("getInstance", Configuration.class);
79          return (JobContext) m.invoke(null, jobConf); // static method, then arg
80        } catch (Exception e) {
81          e.printStackTrace();
82          throw new IllegalStateException(
83              "Failed to return from Job.getInstance(jobConf)");
84        }
85      }
86    };
87  
88  }