001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to you under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.hadoop.hbase.quotas;
018
019import java.util.Objects;
020
021import org.apache.hadoop.conf.Configuration;
022import org.apache.yetus.audience.InterfaceAudience;
023
024/**
025 * Factory for creating {@link SpaceQuotaSnapshotNotifier} implementations. Implementations
026 * must have a no-args constructor.
027 */
028@InterfaceAudience.Private
029public class SpaceQuotaSnapshotNotifierFactory {
030  private static final SpaceQuotaSnapshotNotifierFactory INSTANCE =
031      new SpaceQuotaSnapshotNotifierFactory();
032
033  public static final String SNAPSHOT_NOTIFIER_KEY = "hbase.master.quota.snapshot.notifier.impl";
034  public static final Class<? extends SpaceQuotaSnapshotNotifier> SNAPSHOT_NOTIFIER_DEFAULT =
035      TableSpaceQuotaSnapshotNotifier.class;
036
037  // Private
038  private SpaceQuotaSnapshotNotifierFactory() {}
039
040  public static SpaceQuotaSnapshotNotifierFactory getInstance() {
041    return INSTANCE;
042  }
043
044  /**
045   * Instantiates the {@link SpaceQuotaSnapshotNotifier} implementation as defined in the
046   * configuration provided.
047   *
048   * @param conf Configuration object
049   * @return The SpaceQuotaSnapshotNotifier implementation
050   * @throws IllegalArgumentException if the class could not be instantiated
051   */
052  public SpaceQuotaSnapshotNotifier create(Configuration conf) {
053    Class<? extends SpaceQuotaSnapshotNotifier> clz = Objects.requireNonNull(conf)
054        .getClass(SNAPSHOT_NOTIFIER_KEY, SNAPSHOT_NOTIFIER_DEFAULT,
055            SpaceQuotaSnapshotNotifier.class);
056    try {
057      return clz.getDeclaredConstructor().newInstance();
058    } catch (Exception e) {
059      throw new IllegalArgumentException("Failed to instantiate the implementation", e);
060    }
061  }
062}