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 */ 018package org.apache.hadoop.hbase.util; 019 020import org.apache.yetus.audience.InterfaceAudience; 021 022/** 023 * This class provides ShutdownHookManager shims for HBase to interact with the Hadoop 1.0.x and the 024 * Hadoop 2.0+ series. 025 * 026 * NOTE: No testing done against 0.22.x, or 0.21.x. 027 */ 028@InterfaceAudience.Private 029abstract public class ShutdownHookManager { 030 private static ShutdownHookManager instance; 031 032 static Class shutdownHookManagerClass = null; 033 static { 034 try { 035 // This class exists in hadoop 2.0+ but not in Hadoop 20.x/1.x 036 shutdownHookManagerClass = Class.forName("org.apache.hadoop.util.ShutdownHookManager"); 037 instance = new ShutdownHookManagerV2(); 038 } catch (Exception e) { 039 instance = new ShutdownHookManagerV1(); 040 } 041 } 042 043 abstract public void addShutdownHook(Thread shutdownHook, int priority); 044 045 abstract public boolean removeShutdownHook(Runnable shutdownHook); 046 047 public static void affixShutdownHook(Thread shutdownHook, int priority) { 048 instance.addShutdownHook(shutdownHook, priority); 049 } 050 051 public static boolean deleteShutdownHook(Runnable shutdownHook) { 052 return instance.removeShutdownHook(shutdownHook); 053 } 054 055 private static class ShutdownHookManagerV1 extends ShutdownHookManager { 056 // priority is ignored in hadoop versions earlier than 2.0 057 @Override 058 public void addShutdownHook(Thread shutdownHookThread, int priority) { 059 Runtime.getRuntime().addShutdownHook(shutdownHookThread); 060 } 061 062 @Override 063 public boolean removeShutdownHook(Runnable shutdownHook) { 064 Thread shutdownHookThread = null; 065 if (!(shutdownHook instanceof Thread)) { 066 shutdownHookThread = new Thread(shutdownHook); 067 } else shutdownHookThread = (Thread) shutdownHook; 068 069 return Runtime.getRuntime().removeShutdownHook(shutdownHookThread); 070 } 071 }; 072 073 private static class ShutdownHookManagerV2 extends ShutdownHookManager { 074 @Override 075 public void addShutdownHook(Thread shutdownHookThread, int priority) { 076 try { 077 Methods.call(shutdownHookManagerClass, 078 Methods.call(shutdownHookManagerClass, null, "get", null, null), 079 "addShutdownHook", 080 new Class[] { Runnable.class, int.class }, 081 new Object[] { shutdownHookThread, priority }); 082 } catch (Exception ex) { 083 throw new RuntimeException("we could not use ShutdownHookManager.addShutdownHook", ex); 084 } 085 } 086 087 @Override 088 public boolean removeShutdownHook(Runnable shutdownHook) { 089 try { 090 return (Boolean) 091 Methods.call(shutdownHookManagerClass, 092 Methods.call(shutdownHookManagerClass, null, "get", null, null), 093 "removeShutdownHook", 094 new Class[] { Runnable.class }, 095 new Object[] { shutdownHook }); 096 } catch (Exception ex) { 097 throw new RuntimeException("we could not use ShutdownHookManager", ex); 098 } 099 } 100 }; 101 102}