Exemplo n.º 1
0
        /// <summary>
        /// Serialize a ClassHierarchy into a Json string
        /// </summary>
        /// <param name="c"></param>
        /// <returns></returns>
        public string ToString(IClassHierarchy c)
        {
            AvroNode obj = ToAvroNode(c);
            string   s   = JsonConvert.SerializeObject(obj, Formatting.Indented);

            return(s);
        }
Exemplo n.º 2
0
        private byte[] AvroSerialize(AvroNode obj)
        {
            var serializer = AvroSerializer.Create <AvroNode>();

            using (MemoryStream stream = new MemoryStream())
            {
                serializer.Serialize(stream, obj);
                return(stream.ToArray());
            }
        }
Exemplo n.º 3
0
        private void WireUpInheritanceRelationships(AvroNode n)
        {
            if (n.classNode != null && !n.classNode.Equals(string.Empty))
            {
                AvroClassNode cn    = (AvroClassNode)n.classNode;
                IClassNode    iface = null;

                try
                {
                    iface = (IClassNode)GetNode(n.fullName);
                }
                catch (NameResolutionException e)
                {
                    Utilities.Diagnostics.Exceptions.Caught(e, Level.Error, LOGGER);
                    var ex = new IllegalStateException("When reading protocol buffer node "
                                                       + n.fullName + " does not exist.  Full record is " + n, e);
                    Utilities.Diagnostics.Exceptions.Throw(ex, LOGGER);
                }

                foreach (string impl in cn.implFullNames)
                {
                    try
                    {
                        iface.PutImpl((IClassNode)GetNode(impl));
                    }
                    catch (NameResolutionException e)
                    {
                        Utilities.Diagnostics.Exceptions.Caught(e, Level.Error, LOGGER);
                        var ex = new IllegalStateException("When reading protocol buffer node "
                                                           + n + " refers to non-existent implementation:" + impl);
                        Utilities.Diagnostics.Exceptions.Throw(ex, LOGGER);
                    }
                    catch (InvalidCastException e)
                    {
                        Utilities.Diagnostics.Exceptions.Caught(e, Level.Error, LOGGER);
                        try
                        {
                            var ex = new IllegalStateException(
                                "When reading protocol buffer node " + n
                                + " found implementation" + GetNode(impl)
                                + " which is not a ClassNode!");
                            Utilities.Diagnostics.Exceptions.Throw(ex, LOGGER);
                        }
                        catch (NameResolutionException ne)
                        {
                            Utilities.Diagnostics.Exceptions.Caught(ne, Level.Error, LOGGER);
                            var ex = new IllegalStateException(
                                "Got 'cant happen' exception when producing error message for " + e);
                            Utilities.Diagnostics.Exceptions.Throw(ex, LOGGER);
                        }
                    }
                }
            }
        }
Exemplo n.º 4
0
        private void ParseSubHierarchy(INode parent, AvroNode n)
        {
            INode parsed = null;

            if (n.packageNode != null && !n.packageNode.Equals(string.Empty))
            {
                parsed = new PackageNodeImpl(parent, n.name, n.fullName);
            }
            else if (n.namedParameterNode != null && !n.namedParameterNode.Equals(string.Empty))
            {
                AvroNamedParameterNode np = (AvroNamedParameterNode)n.namedParameterNode;
                parsed = new NamedParameterNodeImpl(parent, n.name,
                                                    n.fullName, np.fullArgClassName, np.simpleArgClassName,
                                                    np.isSet, np.isList, np.documentation, np.shortName,
                                                    np.instanceDefault.ToArray());
            }
            else if (n.classNode != null && !n.classNode.Equals(string.Empty))
            {
                AvroClassNode           cn = (AvroClassNode)n.classNode;
                IList <IConstructorDef> injectableConstructors = new List <IConstructorDef>();
                IList <IConstructorDef> allConstructors        = new List <IConstructorDef>();

                foreach (AvroConstructorDef injectable in cn.injectableConstructors)
                {
                    IConstructorDef def = ParseConstructorDef(injectable, true);
                    injectableConstructors.Add(def);
                    allConstructors.Add(def);
                }
                foreach (AvroConstructorDef other in cn.otherConstructors)
                {
                    IConstructorDef def = ParseConstructorDef(other, false);
                    allConstructors.Add(def);
                }

                parsed = new ClassNodeImpl(parent, n.name, n.fullName,
                                           cn.isUnit, cn.isInjectionCandidate,
                                           cn.isExternalConstructor, injectableConstructors,
                                           allConstructors, cn.defaultImplementation);
            }
            else
            {
                Utilities.Diagnostics.Exceptions.Throw(new IllegalStateException("Bad protocol buffer: got abstract node" + n), LOGGER);
            }

            foreach (AvroNode child in n.children)
            {
                ParseSubHierarchy(parsed, child);
            }
        }
Exemplo n.º 5
0
        private AvroNode NewPackageNode(string name,
                                        string fullName, IList <AvroNode> children)
        {
            AvroPackageNode packageNode = new AvroPackageNode();
            AvroNode        n           = new AvroNode();

            n.name        = name;
            n.fullName    = fullName;
            n.packageNode = packageNode;

            n.children = new List <AvroNode>();
            foreach (var c in children)
            {
                n.children.Add(c);
            }

            return(n);
        }
Exemplo n.º 6
0
        private AvroNode NewNamedParameterNode(string name,
                                               string fullName, string simpleArgClassName, string fullArgClassName,
                                               bool isSet, bool isList, string documentation, // can be null
                                               string shortName,                              // can be null
                                               string[] instanceDefault,                      // can be null
                                               IList <AvroNode> children)
        {
            AvroNamedParameterNode namedParameterNode = new AvroNamedParameterNode();

            namedParameterNode.simpleArgClassName = simpleArgClassName;
            namedParameterNode.fullArgClassName   = fullArgClassName;
            namedParameterNode.isSet  = isSet;
            namedParameterNode.isList = isList;

            if (documentation != null)
            {
                namedParameterNode.documentation = documentation;
            }

            if (shortName != null)
            {
                namedParameterNode.shortName = shortName;
            }

            namedParameterNode.instanceDefault = new List <string>();
            foreach (var id in instanceDefault)
            {
                namedParameterNode.instanceDefault.Add(id);
            }

            AvroNode n = new AvroNode();

            n.name               = name;
            n.fullName           = fullName;
            n.namedParameterNode = namedParameterNode;

            n.children = new List <AvroNode>();
            foreach (var c in children)
            {
                n.children.Add(c);
            }

            return(n);
        }
Exemplo n.º 7
0
        private AvroNode NewClassNode(string name,
                                      string fullName,
                                      bool isInjectionCandidate,
                                      bool isExternalConstructor, bool isUnit,
                                      IList <AvroConstructorDef> injectableConstructors,
                                      IList <AvroConstructorDef> otherConstructors,
                                      IList <string> implFullNames, IList <AvroNode> children)
        {
            AvroClassNode classNode = new AvroClassNode();

            classNode.isInjectionCandidate   = isInjectionCandidate;
            classNode.injectableConstructors = new List <AvroConstructorDef>();
            foreach (var ic in injectableConstructors)
            {
                classNode.injectableConstructors.Add(ic);
            }

            classNode.otherConstructors = new List <AvroConstructorDef>();
            foreach (var oc in otherConstructors)
            {
                classNode.otherConstructors.Add(oc);
            }

            classNode.implFullNames = new List <string>();
            foreach (var implFullName in implFullNames)
            {
                classNode.implFullNames.Add(implFullName);
            }

            AvroNode n = new AvroNode();

            n.name      = name;
            n.fullName  = fullName;
            n.classNode = classNode;

            n.children = new List <AvroNode>();
            foreach (var c in children)
            {
                n.children.Add(c);
            }

            return(n);
        }
Exemplo n.º 8
0
        /// <summary>
        /// Convert AvroNode into AvroClassHierarchy object
        /// </summary>
        /// <param name="root"></param>
        internal AvroClassHierarchy(AvroNode root)
        {
            _rootNode = new PackageNodeImpl();
            if (root.packageNode == null)
            {
                Utilities.Diagnostics.Exceptions.Throw(new ArgumentException("Expected a package node.  Got: " + root), LOGGER);
            }

            foreach (AvroNode child in root.children)
            {
                ParseSubHierarchy(_rootNode, child);
            }

            BuildHashTable(_rootNode);

            foreach (AvroNode child in root.children)
            {
                WireUpInheritanceRelationships(child);
            }
        }
Exemplo n.º 9
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);
        }
Exemplo n.º 10
0
        public void TestToFromAvroNode()
        {
            Type timerType             = typeof(Timer);
            Type secondType            = typeof(Timer.Seconds);
            Type simpleConstructorType = typeof(SimpleConstructors);

            IClassHierarchy ns = TangFactory.GetTang().GetClassHierarchy(
                new string[] { typeof(Timer).Assembly.GetName().Name, typeof(SimpleConstructors).Assembly.GetName().Name });
            IClassNode timerClassNode = (IClassNode)ns.GetNode(timerType.AssemblyQualifiedName);
            INode      secondNode     = ns.GetNode(secondType.AssemblyQualifiedName);
            IClassNode simpleConstructorsClassNode = (IClassNode)ns.GetNode(simpleConstructorType.AssemblyQualifiedName);

            AvroNode        n   = _serializer.ToAvroNode(ns);
            IClassHierarchy ns2 = _serializer.FromAvroNode(n);

            IClassNode timerClassNode2 = (IClassNode)ns2.GetNode(timerType.AssemblyQualifiedName);
            INode      secondNode2     = ns2.GetNode(secondType.AssemblyQualifiedName);
            IClassNode simpleConstructorsClassNode2 = (IClassNode)ns2.GetNode(simpleConstructorType.AssemblyQualifiedName);

            Assert.Equal(timerClassNode, timerClassNode2);
            Assert.Equal(secondNode, secondNode2);
            Assert.Equal(simpleConstructorsClassNode, simpleConstructorsClassNode2);
        }
Exemplo n.º 11
0
        public void TestAvroClassHierarchyMerge()
        {
            IClassHierarchy ns = TangFactory.GetTang().GetClassHierarchy(
                new string[] { typeof(Timer).Assembly.GetName().Name });
            IClassNode timerClassNode = (IClassNode)ns.GetNode(typeof(Timer).AssemblyQualifiedName);

            AvroNode        n   = _serializer.ToAvroNode(ns);
            IClassHierarchy ns2 = _serializer.FromAvroNode(n);

            IClassHierarchy ns3 = TangFactory.GetTang().GetClassHierarchy(
                new string[] { typeof(AvroNode).Assembly.GetName().Name });
            IClassNode avroNodeClassNode = (IClassNode)ns3.GetNode(typeof(AvroNode).AssemblyQualifiedName);

            AvroNode        n2  = _serializer.ToAvroNode(ns3);
            IClassHierarchy ns4 = _serializer.FromAvroNode(n2);

            var ns5 = ns2.Merge(ns4);

            IClassNode timerClassNode2    = (IClassNode)ns5.GetNode(typeof(Timer).AssemblyQualifiedName);
            IClassNode avroNodeClassNode2 = (IClassNode)ns5.GetNode(typeof(AvroNode).AssemblyQualifiedName);

            Assert.Equal(timerClassNode, timerClassNode2);
            Assert.Equal(avroNodeClassNode, avroNodeClassNode2);
        }
Exemplo n.º 12
0
 /// <summary>
 /// Deserialize ClassHierarchy from an AvroNode into AvroClassHierarchy object
 /// </summary>
 /// <param name="n"></param>
 /// <returns></returns>
 public IClassHierarchy FromAvroNode(AvroNode n)
 {
     return(new AvroClassHierarchy(n));
 }
Exemplo n.º 13
0
        /// <summary>
        /// Deserialize a ClassHierarchy from a Json string
        /// </summary>
        /// <param name="jsonString"></param>
        /// <returns></returns>
        public IClassHierarchy FromString(string jsonString)
        {
            AvroNode avroConf = JsonConvert.DeserializeObject <AvroNode>(jsonString);

            return(FromAvroNode(avroConf));
        }
Exemplo n.º 14
0
        /// <summary>
        /// Deserialize a ClassHierarchy from a byte array
        /// </summary>
        /// <param name="bytes"></param>
        /// <returns></returns>
        public IClassHierarchy FromByteArray(byte[] bytes)
        {
            AvroNode avroNode = AvroDeserialize(bytes);

            return(FromAvroNode(avroNode));
        }
Exemplo n.º 15
0
        /// <summary>
        /// Deserialize a ClassHierarchy from a file
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public IClassHierarchy FromFile(string fileName)
        {
            AvroNode avroNode = AvroDeserializeFromFile(fileName);

            return(FromAvroNode(avroNode));
        }
Exemplo n.º 16
0
        /// <summary>
        /// Serialize a ClassHierarchy into a byte array
        /// </summary>
        /// <param name="c"></param>
        /// <returns></returns>
        public byte[] ToByteArray(IClassHierarchy c)
        {
            AvroNode obj = ToAvroNode(c);

            return(AvroSerialize(obj));
        }