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, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019package org.apache.reef.tang; 020 021import org.apache.reef.tang.annotations.Name; 022import org.apache.reef.tang.exceptions.BindException; 023import org.apache.reef.tang.exceptions.InjectionException; 024import org.apache.reef.tang.exceptions.NameResolutionException; 025import org.apache.reef.tang.implementation.InjectionPlan; 026 027public interface Injector { 028 029 /** 030 * Gets an instance of iface, or the implementation that has been bound to it. 031 * If an instance has alread been created in this (or a parent) scope, then 032 * the existing instance will be returned. Otherwise, a new instance will be 033 * returned, and registered in the current scope. 034 * 035 * @param iface 036 * @return 037 * @throws NameResolutionException 038 * @throws ReflectiveOperationException 039 */ 040 <U> U getInstance(Class<U> iface) throws InjectionException; 041 042 <U> U getInstance(String iface) throws InjectionException, 043 NameResolutionException; 044 045 /** 046 * Gets the value stored for the given named parameter. 047 * 048 * @param <U> 049 * @param name 050 * @return an Instance of the class configured as the implementation for the 051 * given interface class. 052 * @throws InjectionException 053 */ 054 <U> U getNamedInstance(Class<? extends Name<U>> iface) 055 throws InjectionException; 056 057 /** 058 * Binds the given object to the class. Note that this only affects objects 059 * created by the returned Injector and its children. Also, like all 060 * Injectors, none of those objects can be serialized back to a configuration 061 * file). 062 * 063 * @param iface 064 * @param inst 065 * @return A copy of this injector that reflects the new binding. 066 * @throws BindException 067 */ 068 <T> void bindVolatileInstance(Class<T> iface, T inst) 069 throws BindException; 070 071 <T> void bindVolatileParameter(Class<? extends Name<T>> iface, T inst) 072 throws BindException; 073 074 /** 075 * Binds a TANG Aspect to this injector. Tang Aspects interpose on each 076 * injection performed by an injector, and return an instance of their choosing. 077 * <p/> 078 * A given aspect will be invoked once for each object that Tang injects, and aspects 079 * will be copied in a way that mirrors the scoping that Tang creates at runtime. 080 * 081 * @param a 082 * @throws BindException 083 */ 084 <T> void bindAspect(Aspect a) throws BindException; 085 086 /** 087 * Allows InjectionFuture to tell the aspect when get() is invoked. Package private. 088 * 089 * @return 090 */ 091 Aspect getAspect(); 092 093 /** 094 * Create a copy of this Injector that inherits the instances that were already 095 * created by this Injector, but reflects additional Configuration objects. 096 * This can be used to create trees of Injectors that obey hierarchical 097 * scoping rules. 098 * <p/> 099 * <p/> 100 * Except for the fact that the child Injector will have references to this 101 * injector's instances, the returned Injector is equivalent to the one you 102 * would get by using ConfigurationBuilder to build a merged Configuration, 103 * and then using the merged Configuration to create an Injector. Injectors 104 * returned by ConfigurationBuilders are always independent, and never 105 * share references to the same instances of injected objects. 106 * 107 * @throws BindException If any of the configurations conflict with each other, or the 108 * existing Injector's Configuration. 109 */ 110 Injector forkInjector(Configuration... configurations) throws BindException; 111 112 /** 113 * Returns true if this Injector is able to instantiate the object named by 114 * name. 115 * 116 * @param name 117 * @return 118 * @throws BindException 119 */ 120 boolean isInjectable(String name) throws BindException; 121 122 boolean isParameterSet(String name) throws BindException; 123 124 boolean isInjectable(Class<?> clazz) throws BindException; 125 126 boolean isParameterSet(Class<? extends Name<?>> name) throws BindException; 127 128 InjectionPlan<?> getInjectionPlan(String name) throws NameResolutionException; 129 130 <T> InjectionPlan<T> getInjectionPlan(Class<T> name); 131 132 Injector forkInjector(); 133}