http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/c1b5200f/lang/cs/Source/TANG/Tang/Formats/ConfigurationModule.cs ---------------------------------------------------------------------- diff --git a/lang/cs/Source/TANG/Tang/Formats/ConfigurationModule.cs b/lang/cs/Source/TANG/Tang/Formats/ConfigurationModule.cs deleted file mode 100644 index a75428b..0000000 --- a/lang/cs/Source/TANG/Tang/Formats/ConfigurationModule.cs +++ /dev/null @@ -1,302 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -using System; -using System.Collections.Generic; -using System.Linq; -using System.Reflection; -using System.Text; -using System.Threading.Tasks; -using Org.Apache.Reef.Utilities.Logging; -using Org.Apache.Reef.Tang.Exceptions; -using Org.Apache.Reef.Tang.Interface; -using Org.Apache.Reef.Tang.Util; - -namespace Org.Apache.Reef.Tang.Formats -{ - /** - * Allows applications to bundle sets of configuration options together into - * discrete packages. Unlike more conventional approaches, - * ConfigurationModules store such information in static data structures that - * can be statically discovered and sanity-checked. - * - * @see Org.Apache.Reef.Tang.Formats.TestConfigurationModule for more information and examples. - * - */ - public class ConfigurationModule - { - public readonly ConfigurationModuleBuilder Builder; - private static readonly Logger LOGGER = Logger.GetLogger(typeof(ConfigurationModule)); - private readonly MonotonicHashSet<FieldInfo> reqSet = new MonotonicHashSet<FieldInfo>(); - private readonly MonotonicHashMap<object, Type> setImpls = new MonotonicHashMap<object, Type>(); - private readonly MonotonicMultiHashMap<object, Type> setImplSets = new MonotonicMultiHashMap<object, Type>(); - private readonly MonotonicMultiHashMap<object, string> setLateImplSets = new MonotonicMultiHashMap<object, string>(); - private readonly MonotonicMultiHashMap<object, string> setParamSets = new MonotonicMultiHashMap<object, string>(); - private readonly MonotonicHashMap<object, string> setLateImpls = new MonotonicHashMap<object, string>(); - private readonly MonotonicHashMap<object, string> setParams = new MonotonicHashMap<object, string>(); - - private readonly MonotonicHashMap<object, IList<Type>> setImplLists = new MonotonicHashMap<object, IList<Type>>(); - private readonly MonotonicHashMap<object, IList<string>> setParamLists = new MonotonicHashMap<object, IList<string>>(); - private readonly MonotonicHashMap<object, IList<string>> setLateImplLists = new MonotonicHashMap<object, IList<string>>(); - - public ConfigurationModule(ConfigurationModuleBuilder builder) - { - this.Builder = builder.DeepCopy(); - } - - //public final <T> ConfigurationModule set(Impl<T> opt, Class<? extends T> impl) - public ConfigurationModule Set<T, U>(IImpl<T> opt, GenericType<U> impl) - where U : T - { - Type implType = typeof(U); - - ConfigurationModule c = DeepCopy(); - c.ProcessSet(opt); - if (c.Builder.SetOpts.Contains(opt)) - { - c.setImplSets.Add(opt, implType); - } - else - { - c.setImpls.Add(opt, implType); - } - return c; - } - - public ConfigurationModule Set<T>(IImpl<T> opt, string impl) - { - ConfigurationModule c = DeepCopy(); - c.ProcessSet(opt); - if (c.Builder.SetOpts.Contains(opt)) - { - c.setLateImplSets.Add(opt, impl); - } - else - { - c.setLateImpls.Add(opt, impl); - } - return c; - } - - public ConfigurationModule Set<T, U>(IParam<T> opt, GenericType<U> val) - where U : T - { - Type t = typeof(U); - string n = ReflectionUtilities.GetAssemblyQualifiedName(t); - return Set(opt, n); - } - - public ConfigurationModule Set(IParam<bool> opt, bool val) - { - return Set(opt, val); - } - - ////TODO - ////public readonly ConfigurationModule set(Param<? extends Number> opt, Number val) - ////{ - //// return set(opt, val); - ////} - - public ConfigurationModule Set<T>(IParam<T> opt, string val) - { - //var o = (IParam<object>)opt; - ConfigurationModule c = DeepCopy(); - c.ProcessSet(opt); - if (c.Builder.SetOpts.Contains(opt)) - { - c.setParamSets.Add(opt, val); - } - else - { - c.setParams.Add(opt, val); - } - return c; - } - - public ConfigurationModule Set<T>(IImpl<IList<T>> opt, IList<string> impl) - { - ConfigurationModule c = DeepCopy(); - c.ProcessSet(opt); - c.setLateImplLists.Add(opt, impl); - return c; - } - - public ConfigurationModule Set<T>(IParam<IList<T>> opt, IList<string> impl) - { - ConfigurationModule c = DeepCopy(); - c.ProcessSet(opt); - c.setParamLists.Add(opt, impl); - return c; - } - - public ConfigurationModule Set<T>(IImpl<IList<T>> opt, IList<Type> impl) - { - ConfigurationModule c = DeepCopy(); - c.ProcessSet(opt); - c.setImplLists.Add(opt, impl); - return c; - } - - public IConfiguration Build() - { - ConfigurationModule c = DeepCopy(); - - if (!c.reqSet.ContainsAll(c.Builder.ReqDecl)) - { - ISet<FieldInfo> missingSet = new MonotonicHashSet<FieldInfo>(); - foreach (FieldInfo f in c.Builder.ReqDecl) - { - if (!c.reqSet.Contains(f)) - { - missingSet.Add(f); - } - } - var e = new BindException( - "Attempt to build configuration before setting required option(s): " - + Builder.ToString(missingSet)); - Org.Apache.Reef.Utilities.Diagnostics.Exceptions.Throw(e, LOGGER); - } - - foreach (Type clazz in c.Builder.FreeImpls.Keys) - { - object i = c.Builder.FreeImpls.Get(clazz); - if (c.setImpls.ContainsKey(i)) - { - var cb = (ICsInternalConfigurationBuilder)c.Builder.B; - cb.Bind(clazz, c.setImpls.Get(i)); - } - else if (c.setLateImpls.ContainsKey(i)) - { - c.Builder.B.Bind(ReflectionUtilities.GetAssemblyQualifiedName(clazz), c.setLateImpls.Get(i)); - } - else if (c.setImplSets.ContainsKey(i) || c.setLateImplSets.ContainsKey(i)) - { - foreach (Type clz in c.setImplSets.GetValuesForKey(i)) - { - ICsInternalConfigurationBuilder b = (ICsInternalConfigurationBuilder) c.Builder.B; - b.BindSetEntry(clazz, clz); - } - foreach (string s in c.setLateImplSets.GetValuesForKey(i)) - { - ICsInternalConfigurationBuilder b = (ICsInternalConfigurationBuilder) c.Builder.B; - b.BindSetEntry(clazz, s); - } - } - else if (c.setImplLists.ContainsKey(i)) - { - ICsConfigurationBuilder b = (ICsConfigurationBuilder) c.Builder.B; - b.BindList(clazz, setImplLists.Get(i)); - } - else if (c.setLateImplLists.ContainsKey(i)) - { - ICsInternalConfigurationBuilder b = (ICsInternalConfigurationBuilder)c.Builder.B; - b.BindList(clazz, setLateImplLists.Get(i)); - } - } - - //for (Class<? extends Name<?>> clazz : c.builder.freeParams.Keys) { - foreach (Type clazz in c.Builder.FreeParams.Keys) - { - object p = c.Builder.FreeParams.Get(clazz); - string s = c.setParams.Get(p); - bool foundOne = false; - if (s != null) - { - ICsConfigurationBuilder cb = c.Builder.B; - cb.BindNamedParameter(clazz, s); - foundOne = true; - } - - IList<string> paramListStr = c.setParamLists.Get(p); - if (paramListStr != null) - { - ICsInternalConfigurationBuilder b = (ICsInternalConfigurationBuilder)c.Builder.B; - b.BindList(clazz, paramListStr); - foundOne = true; - } - - foreach (string paramStr in c.setParamSets.GetValuesForKey(p)) - { - ICsInternalConfigurationBuilder b = (ICsInternalConfigurationBuilder)c.Builder.B; - b.BindSetEntry(clazz, paramStr); - foundOne = true; - } - - if (!foundOne) - { - //if (!(p is OptionalParameter<object>)) //p: OptionalParameter<int>, "is" doesn't work here for generic type object - if (!ReflectionUtilities.IsInstanceOfGeneric(p, typeof(OptionalParameter<>))) - { - Org.Apache.Reef.Utilities.Diagnostics.Exceptions.Throw(new IllegalStateException(), LOGGER); - } - } - } - return c.Builder.B.Build(); - } - - public void AssertStaticClean() - { - if (!( - setImpls.IsEmpty() && - setParams.IsEmpty()) && - setImplSets.IsEmpty() && - setLateImplSets.IsEmpty() && - setParamSets.IsEmpty() && - setImplLists.IsEmpty() && - setLateImplLists.IsEmpty() && - setParamLists.IsEmpty() && - setLateImpls.IsEmpty()) - { - var e = new ClassHierarchyException("Detected statically set ConfigurationModule Parameter / Implementation. set() should only be used dynamically. Use bind...() instead."); - Org.Apache.Reef.Utilities.Diagnostics.Exceptions.Throw(e, LOGGER); - } - } - - private ConfigurationModule DeepCopy() - { - ConfigurationModule cm = new ConfigurationModule(Builder.DeepCopy()); - cm.setImpls.AddAll(setImpls); - cm.setParams.AddAll(setParams); - cm.setImplSets.AddAll(setImplSets); - cm.setParamSets.AddAll(setParamSets); - cm.setLateImplSets.AddAll(setLateImplSets); - cm.setImplLists.AddAll(setImplLists); - cm.setParamLists.AddAll(setParamLists); - cm.setLateImplLists.AddAll(setLateImplLists); - cm.setLateImpls.AddAll(setLateImpls); - cm.reqSet.AddAll(reqSet); - return cm; - } - - private void ProcessSet(object impl) - { - FieldInfo f; - Builder.Map.TryGetValue(impl, out f); - if (f == null) - { - var e = new ClassHierarchyException("Unknown Impl/Param when setting " + impl.GetType().Name + ". Did you pass in a field from some other module?"); - Org.Apache.Reef.Utilities.Diagnostics.Exceptions.Throw(e, LOGGER); - } - if (!reqSet.Contains(f)) - { - reqSet.Add(f); - } - } - } -}
http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/c1b5200f/lang/cs/Source/TANG/Tang/Formats/ConfigurationModuleBuilder.cs ---------------------------------------------------------------------- diff --git a/lang/cs/Source/TANG/Tang/Formats/ConfigurationModuleBuilder.cs b/lang/cs/Source/TANG/Tang/Formats/ConfigurationModuleBuilder.cs deleted file mode 100644 index e9640d5..0000000 --- a/lang/cs/Source/TANG/Tang/Formats/ConfigurationModuleBuilder.cs +++ /dev/null @@ -1,519 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -using System; -using System.Collections.Generic; -using System.Reflection; -using System.Text; -using Org.Apache.Reef.Utilities.Logging; -using Org.Apache.Reef.Tang.Annotations; -using Org.Apache.Reef.Tang.Exceptions; -using Org.Apache.Reef.Tang.Implementations; -using Org.Apache.Reef.Tang.Interface; -using Org.Apache.Reef.Tang.Util; - -namespace Org.Apache.Reef.Tang.Formats -{ - public class ConfigurationModuleBuilder - { - public readonly ICsConfigurationBuilder B = TangFactory.GetTang().NewConfigurationBuilder(); - public readonly MonotonicHashSet<FieldInfo> ReqDecl = new MonotonicHashSet<FieldInfo>(); - public readonly MonotonicHashSet<FieldInfo> OptDecl = new MonotonicHashSet<FieldInfo>(); - public readonly MonotonicHashSet<object> SetOpts = new MonotonicHashSet<object>(); - public readonly MonotonicHashMap<object, FieldInfo> Map = new MonotonicHashMap<object, FieldInfo>(); - public readonly MonotonicHashMap<Type, object> FreeImpls = new MonotonicHashMap<Type, object>(); - public readonly MonotonicHashMap<Type, object> FreeParams = new MonotonicHashMap<Type, object>(); //Type must extends from Name<> - - private static readonly Logger LOGGER = Logger.GetLogger(typeof(ConfigurationModuleBuilder)); - private static readonly ISet<Type> ParamBlacklist = new MonotonicHashSet<Type>(new Type[] { typeof(IParam<>), typeof(IImpl<>) }); - private static readonly ISet<string> ParamTypes = - new MonotonicHashSet<string>(new string[] { typeof(RequiredImpl<>).Name, typeof(OptionalImpl<>).Name, typeof(RequiredParameter<>).Name, typeof(OptionalParameter<>).Name }); - - private readonly MonotonicHashSet<FieldInfo> reqUsed = new MonotonicHashSet<FieldInfo>(); - private readonly MonotonicHashSet<FieldInfo> optUsed = new MonotonicHashSet<FieldInfo>(); - private readonly MonotonicHashMap<Type, string> lateBindClazz = new MonotonicHashMap<Type, string>(); - - public ConfigurationModuleBuilder() - { - foreach (FieldInfo f in GetType().GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static)) - { - Type t = f.FieldType; - if (ParamBlacklist.Contains(t)) - { - var e = new ClassHierarchyException( - "Found a field of type " + t + " which should be a Required/Optional Parameter/Implementation instead"); - Org.Apache.Reef.Utilities.Diagnostics.Exceptions.Throw(e, LOGGER); - } - if (ParamTypes.Contains(t.Name)) - { - if (!f.IsPublic) - { - var e = new ClassHierarchyException("Found a non-public configuration option in " + GetType() + ": " + f); - Org.Apache.Reef.Utilities.Diagnostics.Exceptions.Throw(e, LOGGER); - } - - if (!f.IsStatic) - { - var e = new ClassHierarchyException("Found a non-static configuration option in " + GetType() + ": " + f); - Org.Apache.Reef.Utilities.Diagnostics.Exceptions.Throw(e, LOGGER); - } - if (!f.IsInitOnly) - { - var e = new ClassHierarchyException("Found a non-readonly configuration option in " + GetType() + ": " + f); - Org.Apache.Reef.Utilities.Diagnostics.Exceptions.Throw(e, LOGGER); - } - object o = null; - try - { - o = f.GetValue(null); - } - catch (ArgumentException e) - { - Org.Apache.Reef.Utilities.Diagnostics.Exceptions.Caught(e, Level.Error, LOGGER); - var ex = new ClassHierarchyException("Could not look up field instance in " + GetType() + " field: " + f, e); - Org.Apache.Reef.Utilities.Diagnostics.Exceptions.Throw(ex, LOGGER); - } - catch (FieldAccessException e) - { - Org.Apache.Reef.Utilities.Diagnostics.Exceptions.Caught(e, Level.Error, LOGGER); - var ex = new ClassHierarchyException("Could not look up field instance in " + GetType() + " field: " + f, e); - Org.Apache.Reef.Utilities.Diagnostics.Exceptions.Throw(ex, LOGGER); - } - - if (Map.ContainsKey(o)) - { - FieldInfo fi; - Map.TryGetValue(o, out fi); - var e = new ClassHierarchyException("Detected aliased instances in class " + GetType() + " for fields " + fi + " and " + f); - Org.Apache.Reef.Utilities.Diagnostics.Exceptions.Throw(e, LOGGER); - } - if (ReflectionUtilities.IsGenericTypeof(typeof(RequiredImpl<>), t) || ReflectionUtilities.IsGenericTypeof(typeof(RequiredParameter<>), t)) - { - ReqDecl.Add(f); - } - else - { - OptDecl.Add(f); - } - Map.Add(o, f); - } - } - } - - private ConfigurationModuleBuilder(ConfigurationModuleBuilder c) - { - try - { - B.AddConfiguration(c.B.Build()); - } - catch (BindException e) - { - Org.Apache.Reef.Utilities.Diagnostics.Exceptions.Caught(e, Level.Error, LOGGER); - var ex = new ClassHierarchyException("Build error in ConfigurationModuleBuilder: " + e); - Org.Apache.Reef.Utilities.Diagnostics.Exceptions.Throw(ex, LOGGER); - } - ReqDecl.UnionWith(c.ReqDecl); - OptDecl.UnionWith(c.OptDecl); - reqUsed.UnionWith(c.reqUsed); - optUsed.UnionWith(c.optUsed); - SetOpts.UnionWith(c.SetOpts); - Map.AddAll(c.Map); - FreeImpls.AddAll(c.FreeImpls); - FreeParams.AddAll(c.FreeParams); - lateBindClazz.AddAll(c.lateBindClazz); - } - - public ConfigurationModuleBuilder Merge(ConfigurationModule d) - { - if (d == null) - { - var e = new NullReferenceException("If merge() was passed a static final field that is initialized to non-null, then this is almost certainly caused by a circular class dependency."); - Org.Apache.Reef.Utilities.Diagnostics.Exceptions.Throw(e, LOGGER); - } - try - { - d.AssertStaticClean(); - } - catch (ClassHierarchyException ex) - { - Org.Apache.Reef.Utilities.Diagnostics.Exceptions.Caught(ex, Level.Error, LOGGER); - var e = new ClassHierarchyException(ReflectionUtilities.GetAssemblyQualifiedName(GetType()) + ": detected attempt to merge with ConfigurationModule that has had set() called on it", ex); - Org.Apache.Reef.Utilities.Diagnostics.Exceptions.Throw(e, LOGGER); - } - ConfigurationModuleBuilder c = DeepCopy(); - try - { - c.B.AddConfiguration(d.Builder.B.Build()); - } - catch (BindException ex) - { - Org.Apache.Reef.Utilities.Diagnostics.Exceptions.Caught(ex, Level.Error, LOGGER); - var e = new ClassHierarchyException("Error in AddConfiguration in Merge: " + ex); - Org.Apache.Reef.Utilities.Diagnostics.Exceptions.Throw(e, LOGGER); - } - c.ReqDecl.AddAll(d.Builder.ReqDecl); - c.OptDecl.AddAll(d.Builder.OptDecl); - c.reqUsed.AddAll(d.Builder.reqUsed); - c.optUsed.AddAll(d.Builder.optUsed); - c.SetOpts.AddAll(d.Builder.SetOpts); - //c.ListOpts.AddAll(d.Builder.ListOpts); - c.Map.AddAll(d.Builder.Map); - c.FreeImpls.AddAll(d.Builder.FreeImpls); - c.FreeParams.AddAll(d.Builder.FreeParams); - c.lateBindClazz.AddAll(d.Builder.lateBindClazz); - - return c; - } - - public ConfigurationModuleBuilder BindSetEntry<U, T>(GenericType<U> iface, string impl) - where U : Name<ISet<T>> - { - ConfigurationModuleBuilder c = DeepCopy(); - try - { - ICsInternalConfigurationBuilder b = (ICsInternalConfigurationBuilder)c.B; - b.BindSetEntry(typeof(U), impl); - } - catch (BindException ex) - { - Org.Apache.Reef.Utilities.Diagnostics.Exceptions.Caught(ex, Level.Error, LOGGER); - var e = new ClassHierarchyException("Error in BindSetEntry: " + ex); - Org.Apache.Reef.Utilities.Diagnostics.Exceptions.Throw(e, LOGGER); - } - return c; - } - - public ConfigurationModuleBuilder BindSetEntry<U, V, T>(GenericType<U> iface, GenericType<V> impl) - where U : Name<ISet<T>> - where V : T - { - ConfigurationModuleBuilder c = DeepCopy(); - try - { - c.B.BindSetEntry<U, V, T>(iface, impl); - } - catch (BindException ex) - { - Org.Apache.Reef.Utilities.Diagnostics.Exceptions.Caught(ex, Level.Error, LOGGER); - var e = new ClassHierarchyException("Error in BindSetEntry: " + ex); - Org.Apache.Reef.Utilities.Diagnostics.Exceptions.Throw(e, LOGGER); - } - return c; - } - - public ConfigurationModuleBuilder BindSetEntry<U, T>(GenericType<U> iface, IImpl<T> opt) - where U : Name<ISet<T>> - { - ConfigurationModuleBuilder c = DeepCopy(); - Type ifaceType = typeof(U); - - c.ProcessUse(opt); - c.FreeImpls.Add(ifaceType, opt); - - if (!SetOpts.Contains(opt)) - { - c.SetOpts.Add(opt); - } - return c; - } - - public ConfigurationModuleBuilder BindSetEntry<U, T>(GenericType<U> iface, IParam<T> opt) - where U : Name<ISet<T>> - { - ConfigurationModuleBuilder c = DeepCopy(); - Type ifaceType = typeof(U); - c.ProcessUse(opt); - - c.FreeParams.Add(ifaceType, opt); - if (!SetOpts.Contains(opt)) - { - c.SetOpts.Add(opt); - } - return c; - } - - public ConfigurationModuleBuilder BindList<U, T>(GenericType<U> iface, IList<string> impl) - where U : Name<IList<T>> - { - ConfigurationModuleBuilder c = DeepCopy(); - try - { - ICsInternalConfigurationBuilder b = (ICsInternalConfigurationBuilder)c.B; - b.BindList(typeof(U), impl); - } - catch (BindException ex) - { - Org.Apache.Reef.Utilities.Diagnostics.Exceptions.CaughtAndThrow(new ClassHierarchyException("Error in BindList: " + ex), Level.Error, LOGGER); - } - return c; - } - - public ConfigurationModuleBuilder BindList<U, T>(GenericType<U> iface, IImpl<IList<T>> opt) - where U : Name<IList<T>> - { - ConfigurationModuleBuilder c = DeepCopy(); - Type ifaceType = typeof(U); - - c.ProcessUse(opt); - c.FreeImpls.Add(ifaceType, opt); - return c; - } - - public ConfigurationModuleBuilder BindList<U, T>(GenericType<U> iface, IParam<IList<T>> opt) - where U : Name<IList<T>> - { - ConfigurationModuleBuilder c = DeepCopy(); - Type ifaceType = typeof(U); - c.ProcessUse(opt); - - c.FreeParams.Add(ifaceType, opt); - return c; - } - - public ConfigurationModuleBuilder BindImplementation<U, T>(GenericType<U> iface, GenericType<T> impl) - where T : U - { - ConfigurationModuleBuilder c = DeepCopy(); - try - { - c.B.BindImplementation(iface, impl); - } - catch (BindException e) - { - Org.Apache.Reef.Utilities.Diagnostics.Exceptions.Caught(e, Level.Error, LOGGER); - Org.Apache.Reef.Utilities.Diagnostics.Exceptions.Throw(new ClassHierarchyException("Error in BindImplementation: ", e), LOGGER); - } - return c; - } - - public ConfigurationModuleBuilder BindImplementation<T>(GenericType<T> iface, string impl) - { - ConfigurationModuleBuilder c = DeepCopy(); - - c.lateBindClazz.Add(typeof(T), impl); - return c; - } - - public ConfigurationModuleBuilder BindImplementation<U, T>(GenericType<T> iface, IImpl<U> opt) - where U : T - { - ConfigurationModuleBuilder c = DeepCopy(); - c.ProcessUse(opt); - Type ifaceType = typeof(T); - c.FreeImpls.Add(ifaceType, opt); - return c; - } - - public ConfigurationModuleBuilder BindNamedParameter<U, T>(GenericType<U> name, string value) - where U : Name<T> - { - ConfigurationModuleBuilder c = DeepCopy(); - try - { - c.B.BindNamedParameter<U, T>(name, value); - } - catch (BindException e) - { - Org.Apache.Reef.Utilities.Diagnostics.Exceptions.Caught(e, Level.Error, LOGGER); - Org.Apache.Reef.Utilities.Diagnostics.Exceptions.Throw(new ClassHierarchyException("Error in BindNamedParameter: ", e), LOGGER); - } - return c; - } - - public ConfigurationModuleBuilder BindNamedParameter<U, T>(GenericType<U> name, IParam<T> opt) - where U : Name<T> - { - ConfigurationModuleBuilder c = DeepCopy(); - c.ProcessUse(opt); - Type nameType = typeof(U); - c.FreeParams.Add(nameType, opt); - return c; - } - - //public final <T> ConfigurationModuleBuilder bindNamedParameter(Class<? extends Name<T>> iface, Class<? extends T> impl) - //if V is T, you'd better to use public ConfigurationModuleBuilder BindNamedParameter<U, T>(GenericType<U> iface, GenericType<T> impl) defined below - public ConfigurationModuleBuilder BindNamedParameter<U, V, T>(GenericType<U> iface, GenericType<V> impl) - where U : Name<T> - where V : T - { - ConfigurationModuleBuilder c = DeepCopy(); - try - { - c.B.BindNamedParameter<U, V, T>(iface, impl); - } - catch (BindException e) - { - Org.Apache.Reef.Utilities.Diagnostics.Exceptions.Caught(e, Level.Error, LOGGER); - Org.Apache.Reef.Utilities.Diagnostics.Exceptions.Throw(new ClassHierarchyException("Error in BindNamedParameter: ", e), LOGGER); - } - return c; - } - - public ConfigurationModuleBuilder BindNamedParameter<U, T>(GenericType<U> iface, GenericType<T> impl) - where U : Name<T> - { - ConfigurationModuleBuilder c = DeepCopy(); - try - { - c.B.BindNamedParameter<U, T, T>(iface, impl); - } - catch (BindException e) - { - Org.Apache.Reef.Utilities.Diagnostics.Exceptions.Caught(e, Level.Error, LOGGER); - Org.Apache.Reef.Utilities.Diagnostics.Exceptions.Throw(new ClassHierarchyException("Error in BindNamedParameter: ", e), LOGGER); - } - return c; - } - - // public final <T> ConfigurationModuleBuilder bindNamedParameter(Class<? extends Name<T>> iface, Impl<? extends T> opt) - //if ValueType is T, you would better to use public ConfigurationModuleBuilder BindNamedParameter<U, T>(GenericType<U> iface, IImpl<T> opt) - public ConfigurationModuleBuilder BindNamedParameter<U, V, T>(GenericType<U> iface, IImpl<V> opt) - where U : Name<T> - where V : T - { - ConfigurationModuleBuilder c = DeepCopy(); - c.ProcessUse(opt); - Type ifaceType = typeof(U); - c.FreeImpls.Add(ifaceType, opt); - return c; - } - - public ConfigurationModuleBuilder BindNamedParameter<U, T>(GenericType<U> iface, IImpl<T> opt) - where U : Name<T> - { - ConfigurationModuleBuilder c = DeepCopy(); - c.ProcessUse(opt); - Type ifaceType = typeof(U); - c.FreeImpls.Add(ifaceType, opt); - return c; - } - - public ConfigurationModuleBuilder BindConstructor<T, U>(GenericType<T> clazz, GenericType<U> constructor) - where U : IExternalConstructor<T> - { - ConfigurationModuleBuilder c = DeepCopy(); - try - { - c.B.BindConstructor<T, U>(clazz, constructor); - } - catch (BindException e) - { - Org.Apache.Reef.Utilities.Diagnostics.Exceptions.Caught(e, Level.Error, LOGGER); - Org.Apache.Reef.Utilities.Diagnostics.Exceptions.Throw(new ClassHierarchyException("Error in BindConstructor: ", e), LOGGER); - } - return c; - } - - public ConfigurationModuleBuilder BindConstructor<T, U>(GenericType<T> cons, IImpl<U> v) - where U : IExternalConstructor<T> - { - ConfigurationModuleBuilder c = DeepCopy(); - c.ProcessUse(v); - Type consType = typeof(T); - var i = (IImpl<object>)v; - c.FreeImpls.Add(consType, i); - return c; - } - - public ConfigurationModule Build() - { - ConfigurationModuleBuilder c = DeepCopy(); - - if (!(c.reqUsed.ContainsAll(c.ReqDecl) && c.optUsed.ContainsAll(c.OptDecl))) - { - ISet<FieldInfo> fset = new MonotonicHashSet<FieldInfo>(); - foreach (FieldInfo f in c.ReqDecl) - { - if (!c.reqUsed.Contains(f)) - { - fset.Add(f); - } - } - foreach (FieldInfo f in c.OptDecl) - { - if (!c.optUsed.Contains(f)) - { - fset.Add(f); - } - } - var e = new ClassHierarchyException( - "Found declared options that were not used in binds: " - + ToString(fset)); - Org.Apache.Reef.Utilities.Diagnostics.Exceptions.Throw(e, LOGGER); - } - foreach (Type clz in c.lateBindClazz.Keys) - { - try - { - c.B.Bind(ReflectionUtilities.GetAssemblyQualifiedName(clz), c.lateBindClazz.Get(clz)); - } - catch (NameResolutionException e) - { - Org.Apache.Reef.Utilities.Diagnostics.Exceptions.Caught(e, Level.Error, LOGGER); - var ex = new ClassHierarchyException("ConfigurationModule refers to unknown class: " + c.lateBindClazz.Get(clz), e); - Org.Apache.Reef.Utilities.Diagnostics.Exceptions.Throw(ex, LOGGER); - } - catch (BindException e) - { - Org.Apache.Reef.Utilities.Diagnostics.Exceptions.Caught(e, Level.Error, LOGGER); - var ex = new ClassHierarchyException("bind failed while initializing ConfigurationModuleBuilder", e); - Org.Apache.Reef.Utilities.Diagnostics.Exceptions.Throw(ex, LOGGER); - } - } - return new ConfigurationModule(c); - } - - public ConfigurationModuleBuilder DeepCopy() - { - return new ConfigurationModuleBuilder(this); - } - - public string ToString(ISet<FieldInfo> s) - { - StringBuilder sb = new StringBuilder("{"); - bool first = true; - foreach (FieldInfo f in s) - { - sb.Append((first ? " " : ", ") + f.Name); - first = false; - } - sb.Append(" }"); - return sb.ToString(); - } - - private void ProcessUse(object impl) - { - FieldInfo f; - Map.TryGetValue(impl, out f); - if (f == null) - { - var e = new ClassHierarchyException("Unknown Impl/Param when binding " + impl.GetType().Name + ". Did you pass in a field from some other module?"); - Org.Apache.Reef.Utilities.Diagnostics.Exceptions.Throw(e, LOGGER); - } - if (!reqUsed.Contains(f)) - { - reqUsed.Add(f); - } - if (!optUsed.Contains(f)) - { - optUsed.Add(f); - } - } - } -} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/c1b5200f/lang/cs/Source/TANG/Tang/Formats/IConfigurationSerializer.cs ---------------------------------------------------------------------- diff --git a/lang/cs/Source/TANG/Tang/Formats/IConfigurationSerializer.cs b/lang/cs/Source/TANG/Tang/Formats/IConfigurationSerializer.cs deleted file mode 100644 index ec3baac..0000000 --- a/lang/cs/Source/TANG/Tang/Formats/IConfigurationSerializer.cs +++ /dev/null @@ -1,49 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -using Org.Apache.Reef.Tang.Annotations; -using Org.Apache.Reef.Tang.Formats; -using Org.Apache.Reef.Tang.Interface; - -namespace Org.Apache.Reef.Tang.Formats -{ - [DefaultImplementation(typeof(AvroConfigurationSerializer), "default")] - public interface IConfigurationSerializer - { - void ToFileStream(IConfiguration c, string fileName); - - void ToFile(IConfiguration c, string fileName); - - byte[] ToByteArray(IConfiguration c); - - string ToBase64String(IConfiguration c); - - string ToString(IConfiguration c); - - IConfiguration FromFileStream(string fileName); - - IConfiguration FromFile(string fileName); - - IConfiguration FromByteArray(byte[] bytes); - - IConfiguration FromBase64String(string serializedConfig); - - IConfiguration FromString(string josonString); - } -} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/c1b5200f/lang/cs/Source/TANG/Tang/Formats/IImpl.cs ---------------------------------------------------------------------- diff --git a/lang/cs/Source/TANG/Tang/Formats/IImpl.cs b/lang/cs/Source/TANG/Tang/Formats/IImpl.cs deleted file mode 100644 index 70a454d..0000000 --- a/lang/cs/Source/TANG/Tang/Formats/IImpl.cs +++ /dev/null @@ -1,31 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace Org.Apache.Reef.Tang.Formats -{ - public interface IImpl<T> - { - } -} http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/c1b5200f/lang/cs/Source/TANG/Tang/Formats/IParam.cs ---------------------------------------------------------------------- diff --git a/lang/cs/Source/TANG/Tang/Formats/IParam.cs b/lang/cs/Source/TANG/Tang/Formats/IParam.cs deleted file mode 100644 index fa2f93b..0000000 --- a/lang/cs/Source/TANG/Tang/Formats/IParam.cs +++ /dev/null @@ -1,31 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace Org.Apache.Reef.Tang.Formats -{ - public interface IParam<T> - { - } -} http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/c1b5200f/lang/cs/Source/TANG/Tang/Formats/OptionalImpl.cs ---------------------------------------------------------------------- diff --git a/lang/cs/Source/TANG/Tang/Formats/OptionalImpl.cs b/lang/cs/Source/TANG/Tang/Formats/OptionalImpl.cs deleted file mode 100644 index 5ba77aa..0000000 --- a/lang/cs/Source/TANG/Tang/Formats/OptionalImpl.cs +++ /dev/null @@ -1,31 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace Org.Apache.Reef.Tang.Formats -{ - public sealed class OptionalImpl<T> : IImpl<T> - { - } -} http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/c1b5200f/lang/cs/Source/TANG/Tang/Formats/OptionalParameter.cs ---------------------------------------------------------------------- diff --git a/lang/cs/Source/TANG/Tang/Formats/OptionalParameter.cs b/lang/cs/Source/TANG/Tang/Formats/OptionalParameter.cs deleted file mode 100644 index 35f0993..0000000 --- a/lang/cs/Source/TANG/Tang/Formats/OptionalParameter.cs +++ /dev/null @@ -1,31 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace Org.Apache.Reef.Tang.Formats -{ - public sealed class OptionalParameter<T> : IParam<T> - { - } -} http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/c1b5200f/lang/cs/Source/TANG/Tang/Formats/Provides.cs ---------------------------------------------------------------------- diff --git a/lang/cs/Source/TANG/Tang/Formats/Provides.cs b/lang/cs/Source/TANG/Tang/Formats/Provides.cs deleted file mode 100644 index aa5b99b..0000000 --- a/lang/cs/Source/TANG/Tang/Formats/Provides.cs +++ /dev/null @@ -1,31 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace Org.Apache.Reef.Tang.Formats -{ - public sealed class Provides<T> - { - } -} http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/c1b5200f/lang/cs/Source/TANG/Tang/Formats/RequiredImpl.cs ---------------------------------------------------------------------- diff --git a/lang/cs/Source/TANG/Tang/Formats/RequiredImpl.cs b/lang/cs/Source/TANG/Tang/Formats/RequiredImpl.cs deleted file mode 100644 index 4181dce..0000000 --- a/lang/cs/Source/TANG/Tang/Formats/RequiredImpl.cs +++ /dev/null @@ -1,31 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace Org.Apache.Reef.Tang.Formats -{ - public sealed class RequiredImpl<T> : IImpl<T> - { - } -} http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/c1b5200f/lang/cs/Source/TANG/Tang/Formats/RequiredParameter.cs ---------------------------------------------------------------------- diff --git a/lang/cs/Source/TANG/Tang/Formats/RequiredParameter.cs b/lang/cs/Source/TANG/Tang/Formats/RequiredParameter.cs deleted file mode 100644 index 7c13dd3..0000000 --- a/lang/cs/Source/TANG/Tang/Formats/RequiredParameter.cs +++ /dev/null @@ -1,31 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace Org.Apache.Reef.Tang.Formats -{ - public sealed class RequiredParameter<T> : IParam<T> - { - } -} http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/c1b5200f/lang/cs/Source/TANG/Tang/Implementations/ClassHierarchy/AbstractNode.cs ---------------------------------------------------------------------- diff --git a/lang/cs/Source/TANG/Tang/Implementations/ClassHierarchy/AbstractNode.cs b/lang/cs/Source/TANG/Tang/Implementations/ClassHierarchy/AbstractNode.cs deleted file mode 100644 index a88e7d2..0000000 --- a/lang/cs/Source/TANG/Tang/Implementations/ClassHierarchy/AbstractNode.cs +++ /dev/null @@ -1,131 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -using System; -using System.Collections.Generic; -using Org.Apache.Reef.Tang.Types; -using Org.Apache.Reef.Tang.Util; - -namespace Org.Apache.Reef.Tang.Implementations -{ - public class AbstractNode : INode - { - /// It is from Type.FullName. This name is used as Name in a Node. - /// It is not unique for a generic type with different type of arguments. - /// It is used for toString or debug info as AssemblyQualifiedName is really long - private String name; - - /// It is from Type.AssemblyQualifiedName. THis name is used as full name in a Node - /// It is unique for a generic type with different type of arguments. - private String fullName; //it comes from - - //parent node in the class hierarchy - private INode parent; - - //children in the class hierarchy - protected IDictionary<String, INode> children = new MonotonicTreeMap<string, INode>(); - - public AbstractNode(INode parent, String name, String fullName) - { - this.parent = parent; - this.name = name; - this.fullName = fullName; - if (parent != null) - { - parent.Add(this); - } - } - - public ICollection<INode> GetChildren() - { - return children.Values; - } - - public bool Contains(String key) - { - return children.ContainsKey(key); - } - - public INode Get(String key) - { - INode val; - if (children.TryGetValue(key, out val)) - { - return val; - } - return null; - } - - public virtual void Add(INode n) - { - children.Add(n.GetFullName(), n); - } - - public string GetFullName() - { - return fullName; - } - - public string GetName() - { - return name; - } - - public INode GetParent() - { - return parent; - } - - public override bool Equals(Object o) - { - if(o == null) return false; - if(o == this) return true; - - AbstractNode n = (AbstractNode) o; - bool parentsEqual; - if (n.parent == this.parent) { - parentsEqual = true; - } else if (n.parent == null) { - parentsEqual = false; - } else if (this.parent == null) { - parentsEqual = false; - } else { - parentsEqual = n.parent.Equals(this.parent); - } - if (!parentsEqual) { - return false; - } - return fullName.Equals(n.fullName); - } - - public override int GetHashCode() - { - return fullName.GetHashCode(); - } - - public override String ToString() - { - return "[" + this.GetType().FullName + " '" + fullName + "']"; - } - - public int CompareTo(INode n) - { - return fullName.CompareTo(n.GetFullName()); - } - } -} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/c1b5200f/lang/cs/Source/TANG/Tang/Implementations/ClassHierarchy/ClassHierarchyImpl.cs ---------------------------------------------------------------------- diff --git a/lang/cs/Source/TANG/Tang/Implementations/ClassHierarchy/ClassHierarchyImpl.cs b/lang/cs/Source/TANG/Tang/Implementations/ClassHierarchy/ClassHierarchyImpl.cs deleted file mode 100644 index 3f9f16e..0000000 --- a/lang/cs/Source/TANG/Tang/Implementations/ClassHierarchy/ClassHierarchyImpl.cs +++ /dev/null @@ -1,526 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Reflection; -using System.Text; -using Org.Apache.Reef.Utilities.Logging; -using Org.Apache.Reef.Tang.Annotations; -using Org.Apache.Reef.Tang.Exceptions; -using Org.Apache.Reef.Tang.Interface; -using Org.Apache.Reef.Tang.Types; -using Org.Apache.Reef.Tang.Util; - -namespace Org.Apache.Reef.Tang.Implementations -{ - public class ClassHierarchyImpl : ICsClassHierarchy - { - private static readonly Logger LOGGER = Logger.GetLogger(typeof (ClassHierarchyImpl)); - private INode rootNode; - private MonotonicTreeMap<String, INamedParameterNode> shortNames = new MonotonicTreeMap<String, INamedParameterNode>(); - private IList<string> assemblies; - private AssemblyLoader loader = null; - - public ParameterParser Parameterparser = new ParameterParser(); - - public ClassHierarchyImpl(String file) : this(new string[] { file }, new Type[0]) - { - } - - public ClassHierarchyImpl(string[] assemblies) : this(assemblies, new Type[0]) - { - } - - //parameterParsers are classes that extends from IExternalConstructor - public ClassHierarchyImpl(string[] assemblies, Type[] parameterParsers) - { - this.assemblies = assemblies; - rootNode = NodeFactory.CreateRootPackageNode(); - loader = new AssemblyLoader(assemblies); - - foreach (Type p in parameterParsers) //p must be extend from IExternalConstructor - { - try - { - Parameterparser.AddParser(p); - } - catch (BindException e) - { - Org.Apache.Reef.Utilities.Diagnostics.Exceptions.Caught(e, Level.Error, LOGGER); - Org.Apache.Reef.Utilities.Diagnostics.Exceptions.Throw(new ArgumentException("Could not register parameter parsers", e), LOGGER); - } - } - - foreach (var a in loader.Assemblies) - { - foreach (var t in a.GetTypes()) - { - RegisterType(t); - } - } - } - - public INode RegisterType(string assemblyQualifiedName) - { - Type type = this.loader.GetType(assemblyQualifiedName); - if (type != null) - { - return RegisterType(type); - } - return null; - } - - public INode RegisterType(Type type) - { - if (ReflectionUtilities.IsAnnonymousType(type)) - { - // DevNote: Kinda hacky way to indicate the no-op case. - return rootNode; - } - - INode n = GetAlreadyBoundNode(type); - if (n != null) - { - return n; - } - - if (type.BaseType != null) - { - RegisterType(type.BaseType); - } - - foreach (Type interf in type.GetInterfaces()) - { - RegisterType(ReflectionUtilities.EnsureInterfaceType(interf)); - } - - Type enclosingClass = type.DeclaringType; // this.GetEnclosingClass(type); - if (enclosingClass != null) - { - RegisterType(enclosingClass); - } - - INode node = RegisterClass(type); - - foreach (Type inner in type.GetNestedTypes()) - { - RegisterType(inner); - } - - IClassNode classNode = node as ClassNodeImpl; - if (classNode != null) - { - foreach (IConstructorDef constructorDef in classNode.GetInjectableConstructors()) - { - foreach (IConstructorArg constructorArg in constructorDef.GetArgs()) - { - if (constructorArg.Gettype() == null) - { - Org.Apache.Reef.Utilities.Diagnostics.Exceptions.Throw(new ArgumentException("not type in arg"), LOGGER); - } - RegisterType(constructorArg.Gettype()); //Gettype returns param's Type.fullname - if (constructorArg.GetNamedParameterName() != null) - { - INamedParameterNode np = (INamedParameterNode)RegisterType(constructorArg.GetNamedParameterName()); - try - { - if (np.IsSet() || np.IsList()) - { - //throw new NotImplementedException(); - } - else - { - if (!ReflectionUtilities.IsCoercable(ClassForName(constructorArg.Gettype()), ClassForName(np.GetFullArgName()))) - { - var e = new ClassHierarchyException( - "Named parameter type mismatch in " + classNode.GetFullName() + ". Constructor expects a " - + constructorArg.Gettype() + " but " + np.GetName() + " is a " - + np.GetFullArgName()); - Org.Apache.Reef.Utilities.Diagnostics.Exceptions.Throw(e, LOGGER); - } - } - } - catch (TypeLoadException e) - { - Org.Apache.Reef.Utilities.Diagnostics.Exceptions.Caught(e, Level.Error, LOGGER); - var ex = new ClassHierarchyException("Constructor refers to unknown class " - + constructorArg.GetType(), e); - Org.Apache.Reef.Utilities.Diagnostics.Exceptions.Throw(ex, LOGGER); - } - } - } - } - } - else - { - INamedParameterNode npNode = node as INamedParameterNode; - if (npNode != null) - { - RegisterType(npNode.GetFullArgName()); - } - } - - return node; - } - - private INode RegisterClass(Type type) - { - INode node = GetAlreadyBoundNode(type); - if (node != null) - { - return node; - } - - node = BuildPathToNode(type); - IClassNode classNode = node as IClassNode; - if (classNode != null) - { - Type baseType = type.BaseType; - if (baseType != null) - { - IClassNode n = (IClassNode) GetAlreadyBoundNode(baseType); - if (n != null) - { - n.PutImpl(classNode); - } - else - { - Org.Apache.Reef.Utilities.Diagnostics.Exceptions.Throw(new IllegalStateException("Error in finding Node for BaseType"), LOGGER); - } - } - - foreach (Type interf in ReflectionUtilities.GetInterfaces(type, false)) - { - IClassNode n = (IClassNode)GetAlreadyBoundNode(ReflectionUtilities.EnsureInterfaceType(interf)); - if (n != null) - { - n.PutImpl(classNode); - } - else - { - Org.Apache.Reef.Utilities.Diagnostics.Exceptions.Throw(new IllegalStateException("Error in finding Node for Interface"), LOGGER); - } - } - } - return node; - } - - public INode BuildPathToNode(Type type) - { - INode parent = GetParentNode(type); - - Type argType = ReflectionUtilities.GetNamedParameterTargetOrNull(type); - - if (argType == null) - { - return NodeFactory.CreateClassNode(parent, type); - } - INamedParameterNode np = NodeFactory.CreateNamedParameterNode(parent, type, argType); - - if(Parameterparser.CanParse(ReflectionUtilities.GetAssemblyQualifiedName(argType))) { - if(type.GetCustomAttribute<NamedParameterAttribute>().DefaultClass != null) - { - var e = new ClassHierarchyException("Named parameter " + ReflectionUtilities.GetAssemblyQualifiedName(type) + " defines default implementation for parsable type " + ReflectionUtilities.GetAssemblyQualifiedName(argType)); - Org.Apache.Reef.Utilities.Diagnostics.Exceptions.Throw(e, LOGGER); - } - } - - string shortName = np.GetShortName(); - if (shortName != null && !shortName.Equals("")) - { - INamedParameterNode oldNode = null; - shortNames.TryGetValue(shortName, out oldNode); - if (oldNode != null) - { - if (oldNode.GetFullName().Equals(np.GetFullName())) - { - var ex = new IllegalStateException("Tried to double bind " - + oldNode.GetFullName() + " to short name " + shortName); - Org.Apache.Reef.Utilities.Diagnostics.Exceptions.Throw(ex, LOGGER); - } - var e = new ClassHierarchyException("Named parameters " + oldNode.GetFullName() - + " and " + np.GetFullName() + " have the same short name: " - + shortName); - Org.Apache.Reef.Utilities.Diagnostics.Exceptions.Throw(e, LOGGER); - } - shortNames.Add(shortName, np); - - } - return np; - } - - //return Type T if type implements Name<T>, null otherwise - //e.g. [NamedParameter(typeof(System.String), "Number of seconds to sleep", "10", "sec")] - //class Seconds : Name<Int32> { } - //return Int32 - - //TODO add error handlings - public Type GetNamedParameterTargetOrNull(Type type) - { - var npAnnotation = type.GetCustomAttribute<NamedParameterAttribute>(); - if (npAnnotation != null) - { - Type[] intfs = type.GetInterfaces(); - if (intfs.Length == 1) - { - if (intfs[0].Name.Equals(GetNameOfNameInterface())) - { - Type[] args = intfs[0].GetGenericArguments(); - if (args.Length == 1) - { - return args[0]; - } - } - } - - } - return null; - } - - private INode GetAlreadyBoundNode(Type t) - { - //get outclass names including itsself - string[] outerClassNames = ReflectionUtilities.GetEnclosingClassNames(t); - - INode current = rootNode; - for (int i = 0; i < outerClassNames.Length; i++) - { - current = current.Get(outerClassNames[i]); - if (current == null) - { - StringBuilder sb = new StringBuilder(); - for (int j = 0; j <= i; j++) - { - sb.Append(outerClassNames[j]); - if (j != i) - { - sb.Append("."); - } - } - return null; - //throw new NameResolutionException(t.FullName, sb.ToString()); - } - - } - return current; - } - - //starting from the root, get child for each eclosing class excluding the type itsself - //all enclosing classes should be already in the hierarchy - //Type B2 = asm.GetType(@"Org.Apache.Reef.Tang.Examples.B+B1+B2"); - //string[] pathB2 = ClassNameParser.GetEnclosingClassShortNames(B2); - //Assert.AreEqual(pathB2[0], "B"); - //Assert.AreEqual(pathB2[1], "B1"); - //Assert.AreEqual(pathB2[2], "B2"); - //return INode for B1 - private INode GetParentNode(Type type) - { - INode current = rootNode; - string[] enclosingPath = ReflectionUtilities.GetEnclosingClassNames(type); - for (int i = 0; i < enclosingPath.Length - 1; i++) - { - current = current.Get(enclosingPath[i]); - } - return current; - } - - private string GetNameOfNameInterface() - { - var tn = typeof(Name<int>); - return tn.Name; - } - - public INode GetNode(string fullName) - { - Type t = loader.GetType(fullName); - - if (t == null) - { - Org.Apache.Reef.Utilities.Diagnostics.Exceptions.Throw(new NameResolutionException(fullName, fullName), LOGGER); - } - - return this.GetNode(t); - } - - public INode GetNode(Type type) - { - this.RegisterType(type); - return GetAlreadyBoundNode(type); - } - - public INode GetNamespace() - { - return rootNode; - } - - public bool IsImplementation(IClassNode inter, IClassNode impl) - { - return impl.IsImplementationOf(inter); - } - - public IClassHierarchy Merge(IClassHierarchy ch) - { - if (this == ch) { return this; } - - if (!(ch is ClassHierarchyImpl)) - { - Org.Apache.Reef.Utilities.Diagnostics.Exceptions.Throw(new NotSupportedException("Can't merge java and non-java class hierarchies yet!"), LOGGER); - } - - if(this.assemblies.Count == 0) - { - return ch; - } - - ClassHierarchyImpl chi = (ClassHierarchyImpl)ch; - MonotonicHashSet<string> otherJars = new MonotonicHashSet<string>(); - otherJars.AddAll(chi.assemblies); - MonotonicHashSet<string> myJars = new MonotonicHashSet<string>(); - myJars.AddAll(this.assemblies); - if(myJars.ContainsAll(otherJars)) - { - return this; - } - if (otherJars.ContainsAll(myJars)) - { - return ch; - } - myJars.AddAll(otherJars); - return new ClassHierarchyImpl(myJars.ToArray()); - } - - public object Parse(INamedParameterNode np, string value) - { - IClassNode iface = null; - try - { - iface = (IClassNode)GetNode(np.GetFullArgName()); - } - catch(NameResolutionException e) - { - Org.Apache.Reef.Utilities.Diagnostics.Exceptions.Caught(e, Level.Error, LOGGER); - var ex = new IllegalStateException("Could not parse validated named parameter argument type. NamedParameter is " + np.GetFullName() + " argument type is " + np.GetFullArgName(), e); - Org.Apache.Reef.Utilities.Diagnostics.Exceptions.Throw(ex, LOGGER); - } - Type clazz; - String fullName; - try - { - clazz = (Type)ClassForName(iface.GetFullName()); - fullName = null; - } - catch(TypeLoadException e) - { - Org.Apache.Reef.Utilities.Diagnostics.Exceptions.Caught(e, Level.Warning, LOGGER); - clazz = null; - fullName = iface.GetFullName(); - } - - object result = null; - if (clazz != null) - { - result = Parameterparser.Parse(clazz, value); - } - else - { - result = Parameterparser.Parse(fullName, value); - } - - if (result == null) - { - try - { - INode impl = GetNode(value); - if (impl is IClassNode) - { - if (IsImplementation(iface, (IClassNode) impl)) - { - return impl; - } - } - var ex = - new ParseException( - "Name<" + iface.GetFullName() + "> " + np.GetFullName() + " cannot take non-subclass " + - impl.GetFullName()); - Org.Apache.Reef.Utilities.Diagnostics.Exceptions.Throw(ex, LOGGER); - } - catch (NameResolutionException ec) - { - Org.Apache.Reef.Utilities.Diagnostics.Exceptions.Caught(ec, Level.Error, LOGGER); - var ex = - new ParseException( - "Name<" + iface.GetFullName() + "> " + np.GetFullName() + " cannot take non-class " + value, - ec); - Org.Apache.Reef.Utilities.Diagnostics.Exceptions.Throw(ex, LOGGER); - } - } - return result; - } - - public object ParseDefaultValue(INamedParameterNode name) - { - string[] vals = name.GetDefaultInstanceAsStrings(); - object[] ret = new Object[vals.Length]; - for (int i = 0; i < vals.Length; i++) - { - string val = vals[i]; - try - { - ret[i] = Parse(name, val); - } - catch (ParseException e) - { - Org.Apache.Reef.Utilities.Diagnostics.Exceptions.Caught(e, Level.Error, LOGGER); - var ex = new ClassHierarchyException("Could not parse default value " + val, e); - Org.Apache.Reef.Utilities.Diagnostics.Exceptions.Throw(ex, LOGGER); - } - } - if (name.IsSet()) - { - return new HashSet<object>(ret.ToList<object>()); - } - if (name.IsList()) - { - return new List<object>(ret.ToList<object>()); - } - if (ret.Length == 0) - { - return null; - } - if (ret.Length == 1) - { - return ret[0]; - } - var ec = new IllegalStateException("Multiple defaults for non-set named parameter! " + name.GetFullName()); - Org.Apache.Reef.Utilities.Diagnostics.Exceptions.Throw(ec, LOGGER); - return null; //this line would be never reached as Throw will throw an exception - } - - public Type ClassForName(string name) - { - return this.GetType(name); - } - - public Type GetType(string name) - { - return this.loader.GetType(name); - } - } -} http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/c1b5200f/lang/cs/Source/TANG/Tang/Implementations/ClassHierarchy/ClassNodeImpl.cs ---------------------------------------------------------------------- diff --git a/lang/cs/Source/TANG/Tang/Implementations/ClassHierarchy/ClassNodeImpl.cs b/lang/cs/Source/TANG/Tang/Implementations/ClassHierarchy/ClassNodeImpl.cs deleted file mode 100644 index 27a57b9..0000000 --- a/lang/cs/Source/TANG/Tang/Implementations/ClassHierarchy/ClassNodeImpl.cs +++ /dev/null @@ -1,157 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using Org.Apache.Reef.Utilities.Logging; -using Org.Apache.Reef.Tang.Exceptions; -using Org.Apache.Reef.Tang.Types; -using Org.Apache.Reef.Tang.Util; - -namespace Org.Apache.Reef.Tang.Implementations -{ - public class ClassNodeImpl : AbstractNode, IClassNode - { - private static readonly Logger LOGGER = Logger.GetLogger(typeof(ClassNodeImpl)); - - private readonly bool injectable; - private readonly bool unit; - private readonly bool externalConstructor; - private readonly IList<IConstructorDef> injectableConstructors; - private readonly IList<IConstructorDef> allConstructors; - private readonly MonotonicSet<IClassNode> knownImpls; - private readonly String defaultImpl; - - public ClassNodeImpl(INode parent, String simpleName, String fullName, - bool unit, bool injectable, bool externalConstructor, - IList<IConstructorDef> injectableConstructors, - IList<IConstructorDef> allConstructors, - String defaultImplementation) - : base(parent, simpleName, fullName) - { - - this.unit = unit; - this.injectable = injectable; - this.externalConstructor = externalConstructor; - this.injectableConstructors = injectableConstructors; - this.allConstructors = allConstructors; - this.knownImpls = new MonotonicSet<IClassNode>(); - this.defaultImpl = defaultImplementation; - } - - public IList<IConstructorDef> GetInjectableConstructors() - { - return injectableConstructors; - } - - public IList<IConstructorDef> GetAllConstructors() - { - return allConstructors; - } - - public bool IsInjectionCandidate() - { - return injectable; - } - - public bool IsExternalConstructor() - { - return externalConstructor; - } - - public override String ToString() - { - StringBuilder sb = new StringBuilder(base.ToString() + ": "); - if (GetInjectableConstructors() != null) - { - foreach (IConstructorDef c in GetInjectableConstructors()) - { - sb.Append(c.ToString() + ", "); - } - } - else - { - sb.Append("OBJECT BUILD IN PROGRESS! BAD NEWS!"); - } - return sb.ToString(); - } - - public IConstructorDef GetConstructorDef(IList<IClassNode> paramTypes) - { - if (!IsInjectionCandidate()) - { - var e = new BindException("Cannot @Inject non-static member/local class: " - + GetFullName()); - Org.Apache.Reef.Utilities.Diagnostics.Exceptions.Throw(e, LOGGER); - } - foreach (IConstructorDef c in GetAllConstructors()) - { - if (c.TakesParameters(paramTypes)) - { - return c; - } - } - Org.Apache.Reef.Utilities.Diagnostics.Exceptions.Throw(new BindException("Could not find requested constructor for class " + GetFullName()), LOGGER); - return null; // this line would not be reached as Thrwo throws an exception - } - - public void PutImpl(IClassNode impl) - { - knownImpls.Add(impl); - } - - public ISet<IClassNode> GetKnownImplementations() - { - return new MonotonicSet<IClassNode>(knownImpls); - } - - public bool IsUnit() - { - return unit; - } - - public bool IsImplementationOf(IClassNode inter) - { - List<IClassNode> worklist = new List<IClassNode>(); - if (this.Equals(inter)) - { - return true; - } - worklist.Add(inter); - while (worklist.Count != 0) - { - IClassNode cn = worklist[worklist.Count - 1]; - worklist.RemoveAt(worklist.Count - 1); - ISet<IClassNode> impls = cn.GetKnownImplementations(); - if (impls.Contains(this)) - { - return true; - } - worklist.AddRange(impls); - } - return false; - } - - public String GetDefaultImplementation() - { - return defaultImpl; - } - } -} http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/c1b5200f/lang/cs/Source/TANG/Tang/Implementations/ClassHierarchy/ConstructorArgImpl.cs ---------------------------------------------------------------------- diff --git a/lang/cs/Source/TANG/Tang/Implementations/ClassHierarchy/ConstructorArgImpl.cs b/lang/cs/Source/TANG/Tang/Implementations/ClassHierarchy/ConstructorArgImpl.cs deleted file mode 100644 index 31e4103..0000000 --- a/lang/cs/Source/TANG/Tang/Implementations/ClassHierarchy/ConstructorArgImpl.cs +++ /dev/null @@ -1,97 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -using System; -using Org.Apache.Reef.Tang.Types; - -namespace Org.Apache.Reef.Tang.Implementations -{ - public class ConstructorArgImpl : IConstructorArg - { - private readonly String type; - private readonly String name; - private readonly bool isInjectionFuture; - - public ConstructorArgImpl(String type, String namedParameterName, bool isInjectionFuture) - { - if (type == null) - { - ; - } - this.type = type; - this.name = namedParameterName; - - //if (name != null && name.Contains(',')) - // throw new ApplicationException("Name contains comma : " + name); - this.isInjectionFuture = isInjectionFuture; - } - - public string GetName() - { - return name == null ? type : name; - } - - public string GetNamedParameterName() - { - return name; - } - - public string Gettype() - { - return type; - } - - public bool IsInjectionFuture() - { - return isInjectionFuture; - } - - public override String ToString() - { - return name == null ? type : type + " " + name; - } - - public override int GetHashCode() - { - return 0; - } - - public override bool Equals(Object o) - { - ConstructorArgImpl arg = (ConstructorArgImpl)o; - if (!type.Equals(arg.type)) - { - return false; - } - if (name == null && arg.name == null) - { - return true; - } - if (name == null && arg.name != null) - { - return false; - } - if (name != null && arg.name == null) - { - return false; - } - return name.Equals(arg.name); - - } - } -}
