// // RuleSet.cs // // Author: // Jacob Johnston (jacobj@inchoatethoughts.com) // // Copyright (C) 2009 Jacob Johnston // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU Lesser General Public License as // published by the Free Software Foundation, either version 3 of // the License, or (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU Lesser General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program. If not, see . using System; using System.Collections.Generic; using System.Linq; using System.IO; using System.Reflection; namespace ZenLibrary.RuleBase { /// /// The RuleSet is a singleton that instantiates all the rules contained /// in the assembly. There is no need to register the rules. As soon as /// the RuleSet is instantiated, all of the rules be instantiated and /// placed in their corresponding collection. /// public class RuleSet { static RuleSet instance = null; private List fileRules; private List directoryRules; /// /// Gets the singleton instance of RuleSet. /// public static RuleSet Instance { get { if (instance == null) instance = new RuleSet(); return instance; } } /// /// Constructor is private because this is run as a singleton. /// private RuleSet() { fileRules = new List(); directoryRules = new List(); FindAndInstantiateRules(); } /// /// Gets a list of all rules that are run at the file level. /// public List FileRules { get { return fileRules; } } /// /// Gets a list of all rules that are run at the directory level. /// public List DirectoryRules { get { return directoryRules; } } /// /// Finds all of the rules in the ZenLibrary directory and instantiates them. /// private void FindAndInstantiateRules() { Type ruleType = typeof(Rule); List directoryAssemblies = new List(); Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies(); directoryAssemblies.AddRange(assemblies); DirectoryInfo currentDirectory = new DirectoryInfo(System.Environment.CurrentDirectory); FileInfo[] dllFiles = currentDirectory.GetFiles("*.dll"); foreach (FileInfo file in dllFiles) { if (file.Name.CompareTo("ZenLibrary.RuleBase.dll") != 0) { Assembly dynamicAssembly = Assembly.LoadFile(file.FullName); directoryAssemblies.Add(dynamicAssembly); } } // This code finds all types that inherit from the Rule type. var types = directoryAssemblies.SelectMany(matchingTypes => matchingTypes.GetTypes()).Where(type => ruleType.IsAssignableFrom(type)); foreach (Type type in types) { // We don't want to instantiate the abstract Rule types itself, so we check to make sure we're not doing that. if (type.Name.CompareTo("Rule") != 0 && type.Name.CompareTo("ConfigurableRule") != 0) { Rule rule = (Rule)Activator.CreateInstance(type); switch (rule.TestType) { case TestType.DirectoryScan: directoryRules.Add(rule); break; case TestType.FileScan: fileRules.Add(rule); break; } } } } } }