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