Click here to Skip to main content
15,896,118 members
Articles / Programming Languages / C#

NetMX - a JMX port to the .NET world

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
10 Mar 2008LGPL34 min read 39.6K   268   11  
An introduction to NetMX - a lightweight .NET management solution.
using System;
using System.ServiceModel.Channels;
using System.Xml;
using System.ServiceModel;

namespace NetMX.Remote.WebServices.WSManagement
{
    public sealed class Selector
    {
        private readonly string _name;
        private readonly string _simpleValue;
        private readonly EndpointAddress _addressReferenceValue;

        public Selector(string name, string value) : this(name)
        {
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }
            _simpleValue = value;
        }        

        public Selector(string name, EndpointAddress value)
            : this(name)
        {
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }
            _addressReferenceValue = value;
        }

        private Selector(string name)
        {
            _name = name;
        }

        public string Name
        {
            get { return _name; }
        }

        public object Value
        {
            get
            {
                if (SimpleValue != null)
                {
                    return SimpleValue;
                }
                else
                {
                    return AddressReference;
                }
            }
        }

        public bool IsSimpleValue
        {
            get { return _simpleValue != null; }
        }

        public bool IsAddressReference
        {
            get { return _addressReferenceValue != null; }
        }

        public string SimpleValue
        {
            get
            {
                if (!IsSimpleValue)
                {
                    throw new InvalidOperationException("This selector contains address reference value.");
                }
                else
                {
                    return _simpleValue;
                }
            }
        }

        public EndpointAddress AddressReference
        {
            get
            {
                if (!IsAddressReference)
                {
                    throw new InvalidOperationException("This selctor contains simple value.");
                }
                else
                {
                    return _addressReferenceValue;
                }
            }
        }

        public static Selector ReadFrom(XmlDictionaryReader reader)
        {
            Selector result;
            reader.ReadStartElement("Selector",WSMan.WSManagementNamespace);
            string name = reader.GetAttribute("Name");
            if (reader.NodeType == XmlNodeType.Text)
            {                
                result = new Selector(name, reader.Value);
                reader.Read();
            }
            else
            {
                EndpointAddress addr = EndpointAddress.ReadFrom(AddressingVersion.WSAddressingAugust2004, reader);
                result = new Selector(name, addr);
            }
            reader.ReadEndElement();
            return result;
        }

        public void WriteTo(XmlDictionaryWriter writer)
        {
            writer.WriteStartElement("Selector", WSMan.WSManagementNamespace);
            writer.WriteAttributeString("Name", Name);
            if (_simpleValue != null)
            {
                writer.WriteValue(_simpleValue);
            }
            else if (_addressReferenceValue != null)
            {
                _addressReferenceValue.WriteTo(AddressingVersion.WSAddressingAugust2004, writer);
            }
            writer.WriteEndElement();
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The GNU Lesser General Public License (LGPLv3)


Written By
Software Developer (Senior) VSoft
Poland Poland
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions