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.implementation; 020 021import org.apache.reef.tang.*; 022import org.apache.reef.tang.exceptions.BindException; 023import org.apache.reef.tang.implementation.java.ClassHierarchyImpl; 024import org.apache.reef.tang.implementation.java.InjectorImpl; 025import org.apache.reef.tang.implementation.java.JavaConfigurationBuilderImpl; 026 027import java.net.URL; 028import java.util.*; 029 030public class TangImpl implements Tang { 031 032 private static Map<SetValuedKey, JavaClassHierarchy> defaultClassHierarchy = new HashMap<>(); 033 034 /** 035 * Only for testing. Deletes Tang's current database of known classes, forcing 036 * it to rebuild them over time. 037 */ 038 public static void reset() { 039 defaultClassHierarchy = new HashMap<>(); //new ClassHierarchyImpl(); 040 } 041 042 @Override 043 public Injector newInjector(Configuration... confs) throws BindException { 044 return new InjectorImpl(new JavaConfigurationBuilderImpl(confs).build()); 045 } 046 047 @SuppressWarnings("unchecked") 048 @Override 049 public JavaConfigurationBuilder newConfigurationBuilder() { 050 try { 051 return newConfigurationBuilder(new URL[0], new Configuration[0], new Class[0]); 052 } catch (BindException e) { 053 throw new IllegalStateException( 054 "Caught unexpeceted bind exception! Implementation bug.", e); 055 } 056 } 057 058 @Override 059 public ConfigurationBuilder newConfigurationBuilder(ClassHierarchy ch) { 060 return new ConfigurationBuilderImpl(ch); 061 } 062 063 @SuppressWarnings("unchecked") 064 @Override 065 public JavaConfigurationBuilder newConfigurationBuilder(URL... jars) { 066 try { 067 return newConfigurationBuilder(jars, new Configuration[0], new Class[0]); 068 } catch (BindException e) { 069 throw new IllegalStateException( 070 "Caught unexpeceted bind exception! Implementation bug.", e); 071 } 072 } 073 074 @SuppressWarnings("unchecked") 075 @Override 076 public JavaConfigurationBuilder newConfigurationBuilder( 077 Configuration... confs) throws BindException { 078 return newConfigurationBuilder(new URL[0], confs, new Class[0]); 079 } 080 081 @Override 082 public final JavaConfigurationBuilder newConfigurationBuilder( 083 @SuppressWarnings("unchecked") Class<? extends ExternalConstructor<?>>... parsers) throws BindException { 084 return newConfigurationBuilder(new URL[0], new Configuration[0], parsers); 085 } 086 087 @Override 088 public JavaConfigurationBuilder newConfigurationBuilder(URL[] jars, 089 Configuration[] confs, Class<? extends ExternalConstructor<?>>[] parameterParsers) throws BindException { 090 JavaConfigurationBuilder cb = new JavaConfigurationBuilderImpl(jars, confs, parameterParsers); 091// for (Configuration c : confs) { 092// cb.addConfiguration(c); 093// } 094 return cb; 095 } 096 097 @SuppressWarnings("unchecked") 098 @Override 099 public JavaClassHierarchy getDefaultClassHierarchy() { 100 return getDefaultClassHierarchy(new URL[0], new Class[0]); 101 } 102 103 @Override 104 public JavaClassHierarchy getDefaultClassHierarchy(URL[] jars, Class<? extends ExternalConstructor<?>>[] parameterParsers) { 105 SetValuedKey key = new SetValuedKey(jars, parameterParsers); 106 107 JavaClassHierarchy ret = defaultClassHierarchy.get(key); 108 if (ret == null) { 109 ret = new ClassHierarchyImpl(jars, parameterParsers); 110 defaultClassHierarchy.put(key, ret); 111 } 112 return ret; 113 } 114 115 @Override 116 public Injector newInjector(Configuration confs) { 117 try { 118 return newInjector(new Configuration[]{confs}); 119 } catch (BindException e) { 120 throw new IllegalStateException("Unexpected error cloning configuration", e); 121 } 122 } 123 124 @Override 125 public Injector newInjector() { 126 try { 127 return newInjector(new Configuration[]{}); 128 } catch (BindException e) { 129 throw new IllegalStateException("Unexpected error from empty configuration", e); 130 } 131 } 132 133 private class SetValuedKey { 134 public final Set<Object> key; 135 136 public SetValuedKey(Object[] ts, Object[] us) { 137 key = new HashSet<Object>(Arrays.asList(ts)); 138 key.addAll(Arrays.asList(us)); 139 } 140 141 @Override 142 public int hashCode() { 143 int i = 0; 144 for (Object t : key) { 145 i += t.hashCode(); 146 } 147 return i; 148 } 149 150 @Override 151 public boolean equals(Object o) { 152 SetValuedKey other = (SetValuedKey) o; 153 if (other.key.size() != this.key.size()) { 154 return false; 155 } 156 return key.containsAll(other.key); 157 } 158 } 159 160 161}