示例#1
0
        internal Namespace(INamespaceParent parent, string name, string[] enabledEvents, string[] mixedEvents, Interner interner)
            :       base(parent, name)
        {
            // Check.

            const string method = ".ctor";

            if (parent == null)
            {
                throw new NullParameterException(typeof(Namespace), method, "parent");
            }
            if (!CatalogueName.IsName(name))
            {
                throw new InvalidParameterFormatException(typeof(Namespace), method, "name", name, Constants.Validation.CompleteNamePattern);
            }

            // Collections.

            InitialiseCollections(2);

            _enabledEvents = Util.InternStringArray(interner, enabledEvents);
            _mixedEvents   = Util.InternStringArray(interner, mixedEvents);

            CheckEventOverlap();
        }
示例#2
0
        private static ObjectQuery GetChildQuery(string className, ManagementBaseObject config)
        {
            var parent = (string)WmiUtil.GetPropertyValue(config, Constants.Wmi.ParentProperty);
            var name   = (string)WmiUtil.GetPropertyValue(config, Constants.Wmi.NameProperty);

            return(GetChildQuery(className, CatalogueName.GetFullName(parent, name)));
        }
示例#3
0
        internal ISourceParent EnsureSourceParent(string fullName)
        {
            // Check for the top.

            if (fullName.Length == 0)
            {
                return(this);
            }

            // Extract the top name and look for it.

            string parentName;
            string relativeName;

            CatalogueName.SplitRootName(fullName, out parentName, out relativeName);

            // Ensure the container exists.

            Namespace ns = Namespaces[parentName];

            if (ns == null)
            {
                // Not there so create one.

                ns = Catalogue.CreateNamespace(this, parentName);
                Namespaces.Add(ns);
            }

            // Keep going.

            return(ns.EnsureSourceParent(relativeName));
        }
示例#4
0
        public INamespaceParent EnsureNamespaceParent(string fullName)
        {
            // Check for the top.

            if (fullName.Length == 0)
            {
                return(this);
            }

            // Extract the top name and look for it.

            string namespaceName;
            string relativeName;

            CatalogueName.SplitRootName(fullName, out namespaceName, out relativeName);

            // Ensure the top category exists.

            Namespace ns = Namespaces[namespaceName];

            if (ns == null)
            {
                // Not there so create one.

                ns           = CreateNamespace(this, namespaceName);
                ns.IsEnsured = true;
                Namespaces.Add(ns);
            }

            // Keep going.

            return(ns.EnsureNamespaceParent(relativeName));
        }
示例#5
0
        private static string GetSourceFullyQualifiedReference(EventArrivedEventArgs e)
        {
            var config = (ManagementBaseObject)WmiUtil.GetPropertyValue(e.NewEvent, "TargetInstance");

            Debug.Assert(config != null, "config != null");

            var name   = (string)WmiUtil.GetPropertyValue(config, Constants.Wmi.NameProperty);
            var parent = (string)WmiUtil.GetPropertyValue(config, Constants.Wmi.ParentProperty);

            return(CatalogueName.GetQualifiedReferenceUnchecked(parent, name));
        }
示例#6
0
        protected override void Execute(Catalogue catalogue, string sourceName, string[] allEventNames, string[] eventNames)
        {
            var sourceType = catalogue.GetSearcher().GetSource(sourceName);

            if (sourceType == null)
            {
                var catalogueName = new CatalogueName(sourceName);
                var parent        = catalogue.EnsureSourceParent(catalogueName.Namespace);
                parent.Add(catalogue.CreateSource(parent, catalogueName.Name, allEventNames.Except(eventNames).ToArray()), true);
            }
            else
            {
                sourceType.DisableEvents(eventNames);
            }

            catalogue.Commit();
        }
示例#7
0
文件: Source.cs 项目: formist/LinkMe
        internal Source(IQualifiedElement parent, string name, string[] enabledEvents, Interner interner)
            :       base(parent, name)
        {
            // Check.

            const string method = ".ctor";

            if (parent == null)
            {
                throw new NullParameterException(typeof(Source), method, "parent");
            }
            if (!CatalogueName.IsName(name))
            {
                throw new InvalidParameterFormatException(typeof(Source), method, "name", name, Constants.Validation.CompleteNamePattern);
            }

            // Assign.

            _enabledEvents = Util.InternStringArray(interner, enabledEvents);
        }
示例#8
0
        internal EventType(Catalogue catalogue, string name, bool isEnabled)
            :       base(catalogue, name)
        {
            // Check.

            const string method = ".ctor";

            if (catalogue == null)
            {
                throw new NullParameterException(typeof(EventType), method, "catalogue");
            }
            if (!CatalogueName.IsName(name))
            {
                throw new InvalidParameterFormatException(typeof(EventType), method, "name", name, Constants.Validation.CompleteNamePattern);
            }

            // Assign.

            _isEnabled = isEnabled;
        }
示例#9
0
        public Source GetSource(string relativeReference)
        {
            const string method = "GetSource";

            if (relativeReference == null)
            {
                throw new NullParameterException(GetType(), method, "relativeReference");
            }

            var catalogueName = new CatalogueName(relativeReference);

            Namespace ns = (catalogueName.Namespace.Length == 0 ? _searchRoot as Namespace :
                            GetNamespace(catalogueName.Namespace));

            if (ns == null)
            {
                return(null);
            }

            ICatalogueSearch search = GetCatalogueSearch();

            if (search == null)
            {
                // Repository search not available, so load the Sources collection if necessary.

                return(ns.Sources.GetItem(catalogueName.RelativeQualifiedReference, Options.IgnoreCase, true));
            }

            // Search in memory first, without loading the Sources collection.

            Source result = ns.Sources.GetItem(catalogueName.RelativeQualifiedReference, Options.IgnoreCase,
                                               false);

            // If the search failed, but the collection was not loaded then search the repository.

            if (result == null && !ns.Sources.IsLoaded)
            {
                return(search.GetSource(ns, catalogueName.Name, Options.IgnoreCase));
            }
            return(result);
        }
示例#10
0
        protected override bool Rename(string newName)
        {
            try
            {
                // Extract the name and version.

                CatalogueName catalogueName = new CatalogueName(newName);

                // Create the new source.

                Source newSource = m_source.Clone(m_source.Parent, catalogueName.Name, catalogueName.Version);
                // Don't update parent state in this case, since only the name is being changed.
                m_source.Parent.Replace(m_source, newSource, false);
                m_source.Parent.Commit();

                m_source = newSource;
                return(true);
            }
            catch (System.Exception e)
            {
                new ExceptionDialog(e, "Cannot rename the source to '" + newName + "'.").ShowDialog();
                return(false);
            }
        }
示例#11
0
        public Namespace GetNamespace(INamespaceParent parent, string relativeName, bool ignoreCase)
        {
            // WMI is very slow, so do everything possible to minimise WMI access. We need to load all the
            // namespaces in the hierarchy (from the parent to the one being sought), because any of them
            // may have enabled events. Get them all in one query.

            // Build a query that gets all the namespaces we need (if they exist).

            string[] nsParts        = relativeName.Split('.');
            string   parentFullName = parent.FullName;
            string   queryString    = BuildQueryStringForNamespaces(nsParts, parentFullName);

            // WQL is case-insensitive, so if ignoreCase is false use the Hashtable created below to filter.

            Hashtable namespaces = ignoreCase ? new Hashtable(StringComparer.InvariantCultureIgnoreCase) : new Hashtable();

            var searcher = new ManagementObjectSearcher(_scope, new ObjectQuery(queryString));

            // Read the namespaces returned and store them temporarily in a Hasthable. We cannot create
            // Namespace objects as we go, because they may be returned out of order (ie. children before parents).

            using (ManagementObjectCollection configs = searcher.Get())
            {
                foreach (ManagementObject config in configs)
                {
                    var details = new NamespaceDetails();

                    var nsName        = (string)WmiUtil.GetPropertyValue(config, Constants.Wmi.NameProperty);
                    var nsParentName  = (string)WmiUtil.GetPropertyValue(config, Constants.Wmi.ParentProperty);
                    var enabledEvents = (string)WmiUtil.GetPropertyValue(config, Constants.Wmi.Namespace.EnabledEventsProperty);
                    var mixedEvents   = (string)WmiUtil.GetPropertyValue(config, Constants.Wmi.Namespace.MixedEventsProperty);

                    details._enabledEvents = SplitString(enabledEvents, Constants.Module.ListSeparator);
                    details._mixedEvents   = SplitString(mixedEvents, Constants.Module.ListSeparator);

                    namespaces.Add(CatalogueName.GetFullNameUnchecked(nsParentName, nsName), details);
                }
            }

            // Now that we have all the details create the hierarchy of Namespace objects.

            Catalogue catalogue = parent.Catalogue;
            Namespace ns        = null;

            foreach (string nsName in nsParts)
            {
                string nsFullName = CatalogueName.GetFullNameUnchecked(parentFullName, nsName);
                var    details    = (NamespaceDetails)namespaces[nsFullName];
                if (details == null)
                {
                    return(null);                    // One of the namespaces in the path was not found in WMI.
                }
                ns = catalogue.CreateNamespace(parent, nsName, details._enabledEvents, details._mixedEvents);
                parent.AddIfNotLoaded(ns);

                parentFullName = nsFullName;
                parent         = ns;
            }

            return(ns);
        }
示例#12
0
        internal static void UpdateNamespace(string fullName)
        {
            Debug.Assert(!string.IsNullOrEmpty(fullName), "fullName != null && fullName.Length > 0");

            // Find the SourceStates for all Sources in the namespace (recursively).

            string prefix         = fullName + ".";
            var    statesToUpdate = new ArrayList();
            var    nsCount        = new Hashtable();   // Count of sources to update per namespace < string, int >

            Lock.AcquireReaderLock(Timeout.Infinite);
            try
            {
                foreach (SourceState sourceState in SourceStates.Values)
                {
                    if (sourceState.FullyQualifiedReference.StartsWith(prefix))
                    {
                        statesToUpdate.Add(sourceState);

                        // Maintain the count of states in this namespace that are being updated.

                        string nsName = CatalogueName.CreateUnchecked(sourceState.FullyQualifiedReference).Namespace;
                        object count  = nsCount[nsName];
                        nsCount[nsName] = (count == null ? 1 : (int)count + 1);
                    }
                }
            }
            finally
            {
                Lock.ReleaseReaderLock();
            }

            if (statesToUpdate.Count == 0)
            {
                return;                 // No Sources from this namespace have been created - nothing to update.
            }
            // Get the catalogue and find the namespace on which event status was changed.

            var rootNs = _catalogue.GetSearcher().GetNamespace(fullName);

            if (rootNs == null)
            {
                return;                 // The namespace must have been deleted after it was updated.
            }
            // Update each source.

            foreach (SourceState sourceState in statesToUpdate)
            {
                CatalogueName catalogueName = CatalogueName.CreateUnchecked(sourceState.FullyQualifiedReference);
                var           sourceCount   = (int)nsCount[catalogueName.Namespace];

                // To minimise access to WMI use two different ways to search for the source. If there are only
                // 1 or 2 sources that need to be updated in the namespace then find them using the searcher -
                // if the repository is WMI it will read only those sources. If there are 3 or more then just
                // use the Sources collection, which will load all the sources in the namespace in one go.

                Source source;
                if (sourceCount < 3)
                {
                    string relativeName = sourceState.FullyQualifiedReference.Substring(prefix.Length);
                    source = rootNs.GetSearcher().GetSource(relativeName);
                }
                else
                {
                    if (catalogueName.Namespace == fullName)
                    {
                        source = rootNs.Sources[catalogueName.RelativeQualifiedReference];
                    }
                    else
                    {
                        Namespace leafNs = rootNs.GetSearcher().GetNamespace(catalogueName.Namespace.Substring(prefix.Length));
                        source = leafNs == null ? null : leafNs.Sources[catalogueName.RelativeQualifiedReference];
                    }
                }

                if (source == null)
                {
                    sourceState.SetEventStatus(_defaultEventStatus);
                }
                else
                {
                    sourceState.SetEventStatus(CreateEventStatus(source.GetEffectiveEnabledEvents()));
                }
            }
        }
示例#13
0
 internal static SourceState GetSourceState(string ns, string name)
 {
     return(GetSourceState(CatalogueName.GetQualifiedReference(ns, name)));
 }
示例#14
0
文件: Source.cs 项目: formist/LinkMe
        protected override Element CreateElement(Element parent, string key)
        {
            var catalogueName = new CatalogueName(key);

            return(((ICatalogueElement)parent).Catalogue.CreateSource((ISourceParent)parent, catalogueName.Name));
        }