예제 #1
0
        /// <summary>
        /// Serialize a ClassHierarchy into a file 
        /// </summary>
        /// <param name="c"></param>
        /// <param name="fileName"></param>
        public void ToFile(IClassHierarchy c, string fileName)
        {
            var avroNodeData = ToAvroNode(c);
            using (var buffer = new MemoryStream())
            {
                using (var w = AvroContainer.CreateWriter<AvroNode>(buffer, Codec.Null))
                {
                    using (var writer = new SequentialWriter<AvroNode>(w, 24))
                    {
                        writer.Write(avroNodeData);
                    }
                }

                if (!WriteFile(buffer, fileName))
                {
                    var e = new TangApplicationException("Error during file operation. Quitting method: " + fileName);
                    Utilities.Diagnostics.Exceptions.Throw(e, LOGGER);
                }
            }
        }
예제 #2
0
        private AvroNode AvroDeserializeFromFile(string fileName)
        {
            AvroNode avroNode = null;
            try
            {
                using (var buffer = new MemoryStream())
                {
                    if (!ReadFile(buffer, fileName))
                    {
                        var e =
                            new TangApplicationException("Error during file operation. Quitting method : " + fileName);
                        Utilities.Diagnostics.Exceptions.Throw(e, LOGGER);
                    }

                    buffer.Seek(0, SeekOrigin.Begin);
                    using (
                        var reader =
                            new SequentialReader<AvroNode>(AvroContainer.CreateReader<AvroNode>(buffer, true)))
                    {
                        var results = reader.Objects;

                        if (results != null)
                        {
                            avroNode = (AvroNode)results.First();
                        }
                    }
                }
            }
            catch (SerializationException ex)
            {
                Utilities.Diagnostics.Exceptions.Caught(ex, Level.Error, LOGGER);
                var e = new TangApplicationException("Cannot deserialize the file: " + fileName, ex);
                Utilities.Diagnostics.Exceptions.Throw(e, LOGGER);
            }

            return avroNode;
        }
예제 #3
0
        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);
                }
            }

            if (!string.IsNullOrEmpty(np.GetAlias()))
            {
                IDictionary<string, string> mapping = null;
                _aliasLookupTable.TryGetValue(np.GetAliasLanguage().ToString(), out mapping);
                if (null == mapping)
                {
                    mapping = new Dictionary<string, string>();
                    _aliasLookupTable.Add(np.GetAliasLanguage().ToString(), mapping);
                }
                try
                {
                    mapping.Add(np.GetAlias(), np.GetFullName());
                }
                catch (Exception)
                {
                    var e = new TangApplicationException(string.Format(CultureInfo.CurrentCulture, "Duplicated alias {0} on named parameter {1}.", np.GetAlias(), np.GetFullName()));
                    Org.Apache.REEF.Utilities.Diagnostics.Exceptions.Throw(e, LOGGER);
                }
            }

            string shortName = np.GetShortName();
            if (shortName != null && !shortName.Equals(string.Empty))
            {
                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;            
        }
예제 #4
0
 private void AddAlias(INamedParameterNode np)
 {
     if (!string.IsNullOrEmpty(np.GetAlias()))
     {
         IDictionary<string, string> mapping = null;
         _aliasLookupTable.TryGetValue(np.GetAliasLanguage().ToString(), out mapping);
         if (mapping == null)
         {
             mapping = new Dictionary<string, string>();
             _aliasLookupTable.Add(np.GetAliasLanguage().ToString(), mapping);
         }
         try
         {
             mapping.Add(np.GetAlias(), np.GetFullName());
         }
         catch (Exception)
         {
             var e = new TangApplicationException(string.Format(CultureInfo.CurrentCulture, "Duplicated alias {0} on named parameter {1}.", np.GetAlias(), np.GetFullName()));
             Utilities.Diagnostics.Exceptions.Throw(e, LOGGER);
         }
     }
 }
예제 #5
0
        private static void AddConfiguration(IConfigurationBuilder conf, StreamReader reader)
        {
            IList<KeyValuePair<string, string>> settings = new List<KeyValuePair<string, string>>();

            while (!reader.EndOfStream)
            {
                string line = reader.ReadLine();
                string[] p = line.Split('=');
                if (p.Length == 2)
                {
                    settings.Add(new KeyValuePair<string, string>(GetAssemblyName(p[0]), GetAssemblyName(p[1])));
                } 
                else if (p.Length > 2)
                {
                    string v = line.Substring(p[0].Length + 1, line.Length - p[0].Length - 1);
                    settings.Add(new KeyValuePair<string, string>(GetAssemblyName(p[0]), GetAssemblyName(v)));
                }
                else
                {
                    var e = new TangApplicationException("Config data is not in format of KeyValuePair: " + line);
                    Org.Apache.REEF.Utilities.Diagnostics.Exceptions.Throw(e, LOGGER);
                }  
            }
            ProcessConfigData(conf, settings);
        }
예제 #6
0
        public void ToFile(IConfiguration c, string fileName)
        {
            var avronConfigurationData = ToAvroConfiguration(c);
            using (var buffer = new MemoryStream())
            {
                using (var w = AvroContainer.CreateWriter<AvroConfiguration>(buffer, Codec.Null))
                {
                    using (var writer = new SequentialWriter<AvroConfiguration>(w, 24))
                    {
                        // Serialize the data to stream using the sequential writer
                        writer.Write(avronConfigurationData);
                    }
                }

                if (!WriteFile(buffer, fileName))
                {
                    var e = new TangApplicationException("Error during file operation. Quitting method: " + fileName);
                    Org.Apache.REEF.Utilities.Diagnostics.Exceptions.Throw(e, LOGGER);
                }
            }          
        }
예제 #7
0
 /// <summary>
 /// Determines whether [is generic typeof] [the specified iface].
 /// </summary>
 /// <param name="iface">The iface.</param>
 /// <param name="type">The type.</param>
 /// <returns>
 ///   <c>true</c> if [is generic typeof] [the specified iface]; otherwise, <c>false</c>.
 /// </returns>
 /// <exception cref="TangApplicationException">The type passed in IsGenericTypeof is null: iface : + iface + type: + type + .</exception>
 public static bool IsGenericTypeof(Type iface, Type type)
 {
     if (iface == null || type == null)
     {
         var ex = new TangApplicationException(string.Format(CultureInfo.CurrentCulture,
                                                      "The type passed in IsGenericTypeof is null: iface : {0} type: {1}. ",
                                                      iface, type));
         Utilities.Diagnostics.Exceptions.Throw(ex, LOGGER);
     }
     if (type.IsGenericType)
     {
         if (GetAssemblyQualifiedName(iface).Equals(GetAssemblyQualifiedNameForGeneric(type)))
         {
             return true;
         }
     }
     return false;
 }