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