View Javadoc

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.util;
19  
20  
21  /**
22   * This class provides ShutdownHookManager shims for HBase to interact with the Hadoop 1.0.x and the
23   * Hadoop 2.0+ series.
24   * 
25   * NOTE: No testing done against 0.22.x, or 0.21.x.
26   */
27  abstract public class ShutdownHookManager {
28    private static ShutdownHookManager instance;
29  
30    static Class shutdownHookManagerClass = null;
31    static {
32      try {
33        // This class exists in hadoop 2.0+ but not in Hadoop 20.x/1.x
34        shutdownHookManagerClass = Class.forName("org.apache.hadoop.util.ShutdownHookManager");
35        instance = new ShutdownHookManagerV2();
36      } catch (Exception e) {
37        instance = new ShutdownHookManagerV1();
38      }
39    }
40  
41    abstract public void addShutdownHook(Thread shutdownHook, int priority);
42    
43    abstract public boolean removeShutdownHook(Runnable shutdownHook);
44      
45    public static void affixShutdownHook(Thread shutdownHook, int priority) {
46      instance.addShutdownHook(shutdownHook, priority);
47    }
48    
49    public static boolean deleteShutdownHook(Runnable shutdownHook) {
50      return instance.removeShutdownHook(shutdownHook);
51    }
52  
53    private static class ShutdownHookManagerV1 extends ShutdownHookManager {
54      // priority is ignored in hadoop versions earlier than 2.0
55      public void addShutdownHook(Thread shutdownHookThread, int priority) {      
56        Runtime.getRuntime().addShutdownHook(shutdownHookThread);
57      }
58      
59      public boolean removeShutdownHook(Runnable shutdownHook) {
60        Thread shutdownHookThread = null;
61        if (!(shutdownHook instanceof Thread)) {
62          shutdownHookThread = new Thread(shutdownHook);
63        } else shutdownHookThread = (Thread) shutdownHook;
64        
65        return Runtime.getRuntime().removeShutdownHook(shutdownHookThread);
66      }
67    };
68  
69    private static class ShutdownHookManagerV2 extends ShutdownHookManager {
70      public void addShutdownHook(Thread shutdownHookThread, int priority) {
71        try {
72          Methods.call(shutdownHookManagerClass, 
73              Methods.call(shutdownHookManagerClass, null, "get", null, null),
74              "addShutdownHook",
75              new Class[] { Runnable.class, int.class },
76              new Object[] { shutdownHookThread, priority });
77        } catch (Exception ex) {
78          throw new RuntimeException("we could not use ShutdownHookManager.addShutdownHook", ex);
79        }
80      }
81      
82      public boolean removeShutdownHook(Runnable shutdownHook) {
83        try {
84          return (Boolean)
85          Methods.call(shutdownHookManagerClass, 
86              Methods.call(shutdownHookManagerClass, null, "get", null, null),
87              "removeShutdownHook",
88              new Class[] { Runnable.class },
89              new Object[] { shutdownHook });
90        } catch (Exception ex) {
91          throw new RuntimeException("we could not use ShutdownHookManager", ex);
92        }
93      }
94    };
95  
96  }