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.exceptions.BindException; 022import org.apache.reef.tang.implementation.TangImpl; 023 024import java.net.URL; 025 026/** 027 * The root factory interface for Tang. This interface allows callers to 028 * instantiate Tang's core API, and is responsible for memoization and other 029 * runtime optimizations. 030 */ 031public interface Tang { 032 033 /** 034 * Returns an Injector for the given Configurations. 035 * 036 * @throws BindException If the confs conflict, a BindException will be thrown. 037 */ 038 public Injector newInjector(final Configuration... confs) 039 throws BindException; 040 041 /** 042 * Returns an Injector for the given Configuration. 043 */ 044 public Injector newInjector(final Configuration confs); 045 046 /** 047 * Returns an Injector based on an empty Configuration. 048 */ 049 public Injector newInjector(); 050 051 /** 052 * Return a new ConfigurationBuilder that is backed by the provided 053 * ClassHierarchy object. 054 * 055 * @param ch Any valid Tang ClassHierarchy, including ones derived from non-Java application binaries. 056 * @return an instance of ConfigurationBuilder. In Tang's default implementation this returns an instance or JavaConfigurationBuilder if ch is backed by a Java classloader. 057 */ 058 public ConfigurationBuilder newConfigurationBuilder(ClassHierarchy ch); 059 060 /** 061 * Create a new ConfigurationBuilder that is backed by the default 062 * classloader and the provided jars. Following standard Java's semantics, 063 * when looking up a class, Tang will consult the default classloader first, 064 * then the first classpath entry / jar passed to this method, then the 065 * second, and so on. 066 * 067 * @return a new JavaConfigurationBuilder 068 */ 069 public JavaConfigurationBuilder newConfigurationBuilder(URL... jars); 070 071 /** 072 * Merge a set of configurations into a new JavaConfiurationBuilder. If 073 * the configurations conflict, this method will throw a bind exception. 074 * <p/> 075 * The underlying ClassHierarchies and parameter parsers of the 076 * configurations will be checked for consistency. The returned 077 * configuration builder will be backed by a ClassHierachy that incorporates 078 * the classpath and parsers from all of the provided Configurations. 079 * 080 * @return a new ConfigurationBuilder 081 * @throws BindException if any of the configurations contain duplicated or 082 * conflicting bindings, or if the backing ClassHierarchy objects conflict 083 * in some way. 084 */ 085 public JavaConfigurationBuilder newConfigurationBuilder(Configuration... confs) 086 throws BindException; 087 088 /** 089 * Create an empty JavaConfigurationBuilder that is capable of parsing 090 * application-specific configuration values. The returned 091 * JavaConfigurationBuilder will be backed by the default classloader. 092 * 093 * @return a new ConfigurationBuilder 094 */ 095 public JavaConfigurationBuilder newConfigurationBuilder(@SuppressWarnings("unchecked") Class<? extends ExternalConstructor<?>>... parameterParsers) 096 throws BindException; 097 098 /** 099 * Create a new JavaConfiguration builder that has additional jars, 100 * incorporates existing configuration data and / or can parse 101 * application-specific types. 102 * 103 * @see The documentation for the other newConfigurationBuilder methods in 104 * this class for detailed information about each of the parameters to 105 * this method. 106 */ 107 public JavaConfigurationBuilder newConfigurationBuilder(URL[] jars, 108 Configuration[] confs, Class<? extends ExternalConstructor<?>>[] parameterParsers) throws BindException; 109 110 /** 111 * Create a new empty ConfigurationBuilder that is backed by the default 112 * classloader. 113 */ 114 public JavaConfigurationBuilder newConfigurationBuilder(); 115 116 /** 117 * @return an instance of JavaClassHierarchy that is backed by the default 118 * Java classloader. ClassHierarchy objects are immutable, so multiple 119 * invocations of this method may return the same instance. 120 */ 121 public JavaClassHierarchy getDefaultClassHierarchy(); 122 123 /** 124 * @return a custom instance of JavaClassHierarchy. ClassHierarchy objects 125 * are immutable, so multiple invocations of this method may return the 126 * same instance. 127 * @TODO Is the class hierarchy returned here backed by the default 128 * classloader or not? If not, then callers will need to merge it with 129 * getDefaultClassHierarchy(). If so, we should add a new method like 130 * getNonDefaultClassHiearchy() that takes the same options as this one. 131 */ 132 public JavaClassHierarchy getDefaultClassHierarchy(URL[] jars, Class<? extends ExternalConstructor<?>>[] parsers); 133 134 /** 135 * A factory that returns the default implementation of the Tang interface. 136 */ 137 public final class Factory { 138 /** 139 * Return an instance of the default implementation of Tang. 140 * 141 * @return 142 */ 143 public static Tang getTang() { 144 return new TangImpl(); 145 } 146 } 147 148 149}