/// <summary>
        /// The <see cref="Configure"/> implementation method.
        /// </summary>
        /// <remarks>
        /// This method must only be called by a thread that has synchronized
        /// on this RemoteService.
        /// </remarks>
        /// <param name="xml">
        /// The <see cref="IXmlElement"/> containing the new configuration
        /// for this RemoteService.
        /// </param>
        protected virtual void DoConfigure(IXmlElement xml)
        {
            if (xml == null)
            {
                throw new ArgumentNullException("xml", "xml configuration must not be null");
            }

            Xml = xml;

            // find the configuration for the Initiator
            IXmlElement xmlInitiator = Tangosol.Util.Daemon.QueueProcessor.
                                       Service.Peer.Initiator.Initiator.FindInitiatorConfig(xml);

            // inject service configuration
            IXmlElement xmlHandler = XmlHelper.EnsureElement(xmlInitiator, "incoming-message-handler");

            IXmlElement xmlSub = XmlHelper.EnsureElement(xmlHandler, "request-timeout");

            if (xmlSub.Value == null)
            {
                xmlSub.SetString(xml.GetSafeElement("request-timeout").GetString());
            }

            // create the Initiator
            IConnectionInitiator initiator = Tangosol.Util.Daemon.QueueProcessor.
                                             Service.Peer.Initiator.Initiator.CreateInitiator(xmlInitiator, OperationalContext);

            if (initiator is Initiator)
            {
                var initiatorImpl = (Initiator)initiator;
                initiatorImpl.ServiceName   = ServiceName + ':' + initiatorImpl.ServiceName;
                initiatorImpl.ParentService = this;
            }
            Initiator = initiator;

            RemoteClusterName = xml.GetSafeElement("cluster-name").GetString();
            RemoteServiceName = xml.GetSafeElement("proxy-service-name").GetString();
            ScopeName         = xml.GetSafeElement("scope-name").GetString();

            if (initiator is TcpInitiator)
            {
                IsNameServiceAddressProvider = ((TcpInitiator)initiator).IsNameService;
            }
        }
        public void TestObjectHelpers()
        {
            Exception e = null;

            //EqualsValue
            SimpleValue sv1 = new SimpleValue("");
            SimpleValue sv2 = new SimpleValue(Binary.NO_BINARY);

            Assert.IsTrue(XmlHelper.EqualsValue(sv1, sv2)); //both are empty
            sv1.SetString("True");
            Assert.IsFalse(XmlHelper.EqualsValue(sv1, sv2));
            sv2.SetBoolean(true);
            Assert.IsTrue(XmlHelper.EqualsValue(sv1, sv2));
            try
            {
                XmlHelper.EqualsValue(null, sv2);
            }
            catch (Exception ex)
            {
                e = ex;
            }
            Assert.IsNotNull(e);
            Assert.IsInstanceOf(typeof(ArgumentException), e);
            sv2.SetString(null);
            try
            {
                XmlHelper.EqualsValue(sv1, sv2);
            }
            catch (Exception ex)
            {
                e = ex;
            }
            Assert.IsNotNull(e);
            Assert.IsInstanceOf(typeof(ArgumentException), e);
            sv2.SetBoolean(true);

            //EqualsElement
            SimpleElement se1 = new SimpleElement();
            SimpleElement se2 = new SimpleElement("name", "value");

            se2.Comment = "comment";
            Assert.IsFalse(XmlHelper.EqualsElement(se1, se2));
            se1.Name = se2.Name;
            Assert.IsFalse(XmlHelper.EqualsElement(se1, se2));
            se1.SetString(se2.GetString());
            Assert.IsFalse(XmlHelper.EqualsElement(se1, se2));
            se1.Comment = se2.Comment;
            Assert.IsTrue(XmlHelper.EqualsElement(se1, se2));
            se1.AddElement("el1");
            Assert.IsFalse(XmlHelper.EqualsElement(se1, se2));
            se2.AddElement("el1");
            Assert.IsTrue(XmlHelper.EqualsElement(se1, se2));
            se1.AddAttribute("attr1");
            Assert.IsFalse(XmlHelper.EqualsElement(se1, se2));
            se2.AddAttribute("attr1");
            Assert.IsTrue(XmlHelper.EqualsElement(se1, se2));
            try
            {
                XmlHelper.EqualsElement(null, se2);
            }
            catch (Exception ex)
            {
                e = ex;
            }
            Assert.IsNotNull(e);
            Assert.IsInstanceOf(typeof(ArgumentException), e);

            //HashValue
            SimpleValue sv = new SimpleValue();

            Assert.IsNull(sv.Value);
            Assert.AreEqual(0, XmlHelper.HashValue(sv));
            sv.SetString("test");
            Assert.AreEqual("test".GetHashCode(), XmlHelper.HashValue(sv));
            sv.SetDecimal(decimal.MinusOne);
            Assert.AreEqual(XmlHelper.Convert(decimal.MinusOne, XmlValueType.String).GetHashCode(),
                            XmlHelper.HashValue(sv));
            Assert.AreEqual(XmlHelper.HashValue(sv1), XmlHelper.HashValue(sv2));

            //HashElement
            Assert.AreEqual(XmlHelper.HashElement(se1), XmlHelper.HashElement(se2));
            IXmlElement el1 = se1.GetElement("el1");

            el1.SetString("new value");
            Assert.AreNotEqual(XmlHelper.HashElement(se1), XmlHelper.HashElement(se2));
        }
Esempio n. 3
0
        /// <summary>
        /// Preprocess the Coherence properties specified either in the
        /// application configuration or environment variables.
        /// When both are specified, environment varialbe takes the precedence.
        /// </summary>
        /// <param name="xmlElement">The XML element to process.</param>
        /// <since>coherence 12.2.1.0.1</since>
        public static void PreprocessProp(IXmlElement xmlElement)
        {
            IXmlValue attr = xmlElement.GetAttribute("system-property");

            if (attr != null)
            {
                string property = attr.GetString();
                string val      = ConfigurationUtils.GetProperty(property, null);
                if (val != null)
                {
                    if (xmlElement.Value is Int32)
                    {
                        xmlElement.SetInt(Int32.Parse(val));
                    }
                    else
                    {
                        xmlElement.SetString(val);
                    }
                }
                xmlElement.Attributes.Remove("system-property");
            }

            string value          = xmlElement.Value.ToString();
            string newValue       = null;
            int    next           = value.IndexOf("${");
            int    i              = next + 2;
            bool   processedParam = false;

            while (next >= 0)
            {
                processedParam = true;
                string   curParam = value.Substring(i, value.IndexOf('}', i) - i);
                string[] entry    = curParam.Split(' ');
                string   property = entry[0];

                string val = ConfigurationUtils.GetProperty(property, null);
                if (val == null)
                {
                    newValue += entry[1];
                }
                else
                {
                    newValue += val;
                }

                next = value.IndexOf("${", i);
                int start = value.IndexOf('}', i) + 1;
                if (next > 0)
                {
                    newValue += value.Substring(start, next - start);
                    i         = next + 2;
                }
                else
                {
                    i = start;
                }
            }
            if (processedParam)
            {
                if (i < value.Length)
                {
                    newValue += value.Substring(i);
                }
                xmlElement.SetString(newValue);
            }
        }
        public void TestElementHelpers()
        {
            Exception e = null;

            //GetAbsolutePath
            SimpleElement root = new SimpleElement();

            Assert.AreEqual(XmlHelper.GetAbsolutePath(root), "/");
            root.Name = "root";
            Assert.AreEqual(XmlHelper.GetAbsolutePath(root), "/root");
            SimpleElement se = new SimpleElement("child");

            se.Parent = root;
            Assert.AreEqual(XmlHelper.GetAbsolutePath(se), "/root/child");
            try
            {
                XmlHelper.GetAbsolutePath(null);
            }
            catch (Exception ex)
            {
                e = ex;
            }
            Assert.IsNotNull(e);
            Assert.IsInstanceOf(typeof(ArgumentNullException), e);

            //IsEmpty
            se = new SimpleElement();
            Assert.IsTrue(XmlHelper.IsEmpty(se));
            se.SetString("value");
            Assert.IsFalse(XmlHelper.IsEmpty(se));
            se.SetString(null);
            Assert.IsTrue(XmlHelper.IsEmpty(se));
            se.AddElement("name");
            Assert.IsFalse(XmlHelper.IsEmpty(se));
            se.ElementList.Clear();
            Assert.IsTrue(XmlHelper.IsEmpty(se));
            se.AddAttribute("attr");
            Assert.IsFalse(XmlHelper.IsEmpty(se));

            //GetElement
            se = new SimpleElement();
            Assert.IsNull(XmlHelper.GetElement(se, "child1"));
            se.AddElement("child1");
            se.AddElement("child2");
            Assert.IsNotNull(XmlHelper.GetElement(se, "child1"));
            e = null;
            try
            {
                XmlHelper.GetElement(se, "&invalid");
            }
            catch (Exception ex)
            {
                e = ex;
            }
            Assert.IsNotNull(e);
            Assert.IsInstanceOf(typeof(ArgumentException), e);

            //FindElement
            root = new SimpleElement("root");
            IXmlElement child1 = root.AddElement("child1");
            IXmlElement child2 = child1.AddElement("child2");
            string      path   = "/child1";

            Assert.AreEqual(child1, XmlHelper.FindElement(root, path));
            Assert.AreEqual(child1, XmlHelper.FindElement(child1, path));
            path = "child2";
            Assert.IsNull(XmlHelper.FindElement(root, path));
            Assert.AreEqual(child2, XmlHelper.FindElement(child1, path));
            path = "../child1";
            Assert.IsNull(XmlHelper.FindElement(child2, path));
            Assert.AreEqual(child1, XmlHelper.FindElement(child1, path));
            e = null;
            try
            {
                XmlHelper.FindElement(root, null);
            }
            catch (Exception ex)
            {
                e = ex;
            }
            Assert.IsNotNull(e);
            Assert.IsInstanceOf(typeof(ArgumentException), e);
            e = null;
            try
            {
                XmlHelper.FindElement(root, path);
            }
            catch (Exception ex)
            {
                e = ex;
            }
            Assert.IsNotNull(e);
            Assert.IsInstanceOf(typeof(ArgumentException), e);

            //FindElement with value
            root   = new SimpleElement();
            child1 = root.AddElement("child1");
            child1.SetString("value1");
            root.AddElement("child1").SetString("value2");
            child2 = child1.AddElement("child2");
            path   = "/child1";
            Assert.AreEqual(child1, XmlHelper.FindElement(root, path, "value1"));
            Assert.IsNull(XmlHelper.FindElement(root, path, "valueX"));
            path = "child2";
            Assert.IsNull(XmlHelper.FindElement(root, path, null));
            Assert.AreEqual(child2, XmlHelper.FindElement(child1, path, null));
            path = "../child1";
            Assert.IsNull(XmlHelper.FindElement(child2, path, null));
            Assert.AreNotEqual(child1, XmlHelper.FindElement(child1, path, "value2"));
            path = "child2/../child2";
            Assert.IsNull(XmlHelper.FindElement(child1, path, 5));
            Assert.AreEqual(child2, XmlHelper.FindElement(child1, path, null));
            e = null;
            try
            {
                XmlHelper.FindElement(null, path, null);
            }
            catch (Exception ex)
            {
                e = ex;
            }
            Assert.IsNotNull(e);
            Assert.IsInstanceOf(typeof(ArgumentException), e);

            //EnsureElement
            root   = new SimpleElement("root");
            child1 = root.AddElement("child1");
            child1.AddElement("child2");
            path = "/child1";
            Assert.AreEqual(root.ElementList.Count, 1);
            Assert.AreEqual(child1, XmlHelper.EnsureElement(root, path));
            path = "/child3";
            Assert.IsNotNull(XmlHelper.EnsureElement(root, path));
            Assert.AreEqual(root.ElementList.Count, 2);
            path = "/child3/../child4";
            Assert.IsNotNull(XmlHelper.EnsureElement(root, path));
            Assert.AreEqual(root.ElementList.Count, 3);
            e = null;
            try
            {
                XmlHelper.EnsureElement(null, path);
            }
            catch (Exception ex)
            {
                e = ex;
            }
            Assert.IsNotNull(e);
            Assert.IsInstanceOf(typeof(ArgumentException), e);
            e = null;
            try
            {
                XmlHelper.EnsureElement(root, "../child1");
            }
            catch (Exception ex)
            {
                e = ex;
            }
            Assert.IsNotNull(e);
            Assert.IsInstanceOf(typeof(ArgumentException), e);

            //AddElements
            root = new SimpleElement();
            Assert.AreEqual(root.ElementList.Count, 0);
            IList elements = new ArrayList();

            elements.Add(new SimpleElement("el1"));
            elements.Add(new SimpleElement("el2"));
            elements.Add(new SimpleElement("el3"));
            XmlHelper.AddElements(root, elements.GetEnumerator());
            Assert.AreEqual(root.ElementList.Count, 3);

            //RemoveElement
            root = new SimpleElement();
            Assert.AreEqual(root.ElementList.Count, 0);
            int result = XmlHelper.RemoveElement(root, "child");

            Assert.AreEqual(result, 0);
            root.AddElement("child");
            root.AddElement("child");
            root.AddElement("child2");
            Assert.AreEqual(root.ElementList.Count, 3);
            result = XmlHelper.RemoveElement(root, "child");
            Assert.AreEqual(result, 2);
            Assert.AreEqual(root.ElementList.Count, 1);

            //ReplaceElement
            root = new SimpleElement("root");
            root.AddElement("child1");
            root.AddElement("child2");
            Assert.AreEqual(root.ElementList.Count, 2);
            Assert.IsNull(root.GetElement("child1").Value);
            IXmlElement replaceEl = new SimpleElement("child1", "value");
            bool        replaced  = XmlHelper.ReplaceElement(root, replaceEl);

            Assert.IsTrue(replaced);
            Assert.AreEqual(root.GetElement("child1").GetString(), replaceEl.GetString());
            Assert.AreEqual(root.ElementList.Count, 2);

            replaceEl = new SimpleElement("child3");
            replaced  = XmlHelper.ReplaceElement(root, replaceEl);
            Assert.IsFalse(replaced);
            Assert.AreEqual(root.ElementList.Count, 3);
        }