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.master.http.jersey; 019 020import java.util.function.Supplier; 021import javax.inject.Singleton; 022import org.apache.hadoop.hbase.http.jersey.SupplierFactoryAdapter; 023import org.apache.hadoop.hbase.master.HMaster; 024import org.apache.hadoop.hbase.master.MasterServices; 025import org.apache.yetus.audience.InterfaceAudience; 026 027import org.apache.hbase.thirdparty.javax.ws.rs.core.Feature; 028import org.apache.hbase.thirdparty.javax.ws.rs.core.FeatureContext; 029import org.apache.hbase.thirdparty.org.glassfish.hk2.utilities.binding.AbstractBinder; 030import org.apache.hbase.thirdparty.org.glassfish.hk2.utilities.binding.ServiceBindingBuilder; 031 032/** 033 * Implements a Singleton binding to the provided instance of {@link HMaster} for both 034 * {@link HMaster} and {@link MasterServices} injections. 035 */ 036@InterfaceAudience.Private 037public class MasterFeature implements Feature { 038 039 private final Supplier<HMaster> supplier; 040 041 public MasterFeature(HMaster master) { 042 this.supplier = () -> master; 043 } 044 045 @Override 046 public boolean configure(FeatureContext context) { 047 context.register(new Binder()); 048 return true; 049 } 050 051 /** 052 * Register this feature's provided functionality and defines their lifetime scopes. 053 */ 054 private class Binder extends AbstractBinder { 055 056 @Override 057 protected void configure() { 058 bindFactory(supplier).to(HMaster.class).in(Singleton.class); 059 bindFactory(supplier).to(MasterServices.class).in(Singleton.class); 060 } 061 062 /** 063 * Helper method for smoothing over use of {@link SupplierFactoryAdapter}. Inspired by internal 064 * implementation details of jersey itself. 065 */ 066 private <T> ServiceBindingBuilder<T> bindFactory(Supplier<T> supplier) { 067 return bindFactory(new SupplierFactoryAdapter<>(supplier)); 068 } 069 } 070}