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.coprocessor;
019
020import java.util.Optional;
021import org.apache.hadoop.hbase.CoprocessorEnvironment;
022import org.apache.hadoop.hbase.ipc.RpcServer;
023import org.apache.hadoop.hbase.security.User;
024import org.apache.yetus.audience.InterfaceAudience;
025
026import org.apache.hbase.thirdparty.com.google.common.annotations.VisibleForTesting;
027
028/**
029 * This is the only implementation of {@link ObserverContext}, which serves as the interface for
030 * third-party Coprocessor developers.
031 */
032@InterfaceAudience.Private
033public class ObserverContextImpl<E extends CoprocessorEnvironment> implements ObserverContext<E> {
034  private E env;
035  private boolean bypass;
036  /**
037   * Is this operation bypassable?
038   */
039  private final boolean bypassable;
040  private final User caller;
041
042  public ObserverContextImpl(User caller) {
043    this(caller, false);
044  }
045
046  public ObserverContextImpl(User caller, boolean bypassable) {
047    this.caller = caller;
048    this.bypassable = bypassable;
049  }
050
051  @Override
052  public E getEnvironment() {
053    return env;
054  }
055
056  public void prepare(E env) {
057    this.env = env;
058  }
059
060  public boolean isBypassable() {
061    return this.bypassable;
062  };
063
064  @Override
065  public void bypass() {
066    if (!this.bypassable) {
067      throw new UnsupportedOperationException("This method does not support 'bypass'.");
068    }
069    bypass = true;
070  }
071
072  /**
073   * @return {@code true}, if {@link ObserverContext#bypass()} was called by one of the loaded
074   * coprocessors, {@code false} otherwise.
075   */
076  public boolean shouldBypass() {
077    if (!isBypassable()) {
078      return false;
079    }
080    if (bypass) {
081      bypass = false;
082      return true;
083    }
084    return false;
085  }
086
087  @Override
088  public Optional<User> getCaller() {
089    return Optional.ofNullable(caller);
090  }
091
092  /**
093   * Instantiates a new ObserverContext instance if the passed reference is <code>null</code> and
094   * sets the environment in the new or existing instance. This allows deferring the instantiation
095   * of a ObserverContext until it is actually needed.
096   * @param <E> The environment type for the context
097   * @param env The coprocessor environment to set
098   * @return An instance of <code>ObserverContext</code> with the environment set
099   */
100  @Deprecated
101  @VisibleForTesting
102  // TODO: Remove this method, ObserverContext should not depend on RpcServer
103  public static <E extends CoprocessorEnvironment> ObserverContext<E> createAndPrepare(E env) {
104    ObserverContextImpl<E> ctx = new ObserverContextImpl<>(RpcServer.getRequestUser().orElse(null));
105    ctx.prepare(env);
106    return ctx;
107  }
108}