// Loads all supported types, checks if they have a correct constructor and is enabled private void LoadSupportedTypes() { Assembly assembly = Assembly.GetCallingAssembly(); foreach (Type type in ReflectionFu.GetTypesFromAssemblyAttribute(assembly, typeof(ThunderbirdDefinedGeneratorsAttribute))) { foreach (ThunderbirdIndexableGeneratorAttribute attr in ReflectionFu.ScanTypeForAttribute(type, typeof(ThunderbirdIndexableGeneratorAttribute))) { foreach (ConstructorInfo constructor in type.GetConstructors()) { ParameterInfo[] parameters = constructor.GetParameters(); if (parameters.Length != 3) { continue; } if (parameters [0].ParameterType.Equals(typeof(ThunderbirdIndexer)) && parameters [1].ParameterType.Equals(typeof(TB.Account)) && parameters [2].ParameterType.Equals(typeof(string))) { // Make sure we should enable this type if (attr.Enabled) { supported_types [attr.Type] = type; } } else { Logger.Log.Debug("{0} has an invalid constructor!", type.ToString()); } } } } }
protected static Type[] GetTypes(Type parent_type, Type attr_type) { ArrayList types = new ArrayList(); foreach (Assembly a in AppDomain.CurrentDomain.GetAssemblies()) { types.AddRange(ReflectionFu.GetTypesFromAssemblyAttribute(a, attr_type)); } return((Type[])types.ToArray(typeof(Type))); }
static void PrintFilterDetails(Assembly assembly) { StringBuilder sb = new StringBuilder(); foreach (Type t in ReflectionFu.GetTypesFromAssemblyAttribute(assembly, typeof(FilterTypesAttribute))) { Filter filter = null; try { filter = (Filter)Activator.CreateInstance(t); } catch (Exception ex) { Logger.Log.Error(ex, "Caught exception while instantiating {0}", t); } if (filter == null) { continue; } string name; if (t.FullName.StartsWith(t.Namespace)) { name = t.FullName.Substring(t.Namespace.Length + 1); } else { name = t.FullName; } sb.Length = 0; sb.Append(name + " - Version " + filter.Version + " (" + assembly.Location + ")\n"); bool has_filter = false; foreach (FilterFlavor flavor in filter.SupportedFlavors) { sb.Append(" - "); sb.Append(flavor); sb.Append("\n"); has_filter = true; } if (has_filter) { Console.WriteLine(sb.ToString()); } } }
static FilterFactory() { FilterCache filter_cache = new FilterCache(filter_cache_dir); ReflectionFu.ScanEnvironmentForAssemblies("BEAGREP_FILTER_PATH", PathFinder.FilterDir, delegate(Assembly a) { int n = ScanAssemblyForFilters(a, filter_cache); Logger.Log.Debug("Loaded {0} filter{1} from {2}", n, n == 1 ? "" : "s", a.Location); }); // FIXME: Up external filter version if external-filters.xml is modified // Check if cache is dirty and also update the cache on the disk cache_dirty = filter_cache.UpdateCache(); }
static TileActivatorOrg() { activators = new ArrayList(); foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies()) { foreach (Type type in ReflectionFu.GetTypesFromAssemblyAttribute(assembly, typeof(TileActivatorTypesAttribute))) { try { activators.Add((TileActivator)Activator.CreateInstance(type)); } catch (Exception e) { Console.WriteLine("Caught exception while instantiating tile"); Console.WriteLine(e); } } } }
// (1) register themselves in AssemblyInfo.cs:IQueryableTypes and // (2) has a QueryableFlavor attribute attached // assemble a Queryable object and stick it into our list of queryables. static void ScanAssemblyForQueryables(Assembly assembly) { int count = 0; foreach (Type type in ReflectionFu.GetTypesFromAssemblyAttribute(assembly, typeof(IQueryableTypesAttribute))) { foreach (QueryableFlavor flavor in ReflectionFu.ScanTypeForAttribute(type, typeof(QueryableFlavor))) { if (!UseQueryable(flavor.Name)) { continue; } if (flavor.RequireInotify && !Inotify.Enabled) { Logger.Log.Warn("Can't start backend '{0}' without inotify", flavor.Name); continue; } if (flavor.RequireExtendedAttributes && !ExtendedAttribute.Supported) { Logger.Log.Warn("Can't start backend '{0}' without extended attributes", flavor.Name); continue; } IQueryable iq = null; try { iq = Activator.CreateInstance(type) as IQueryable; } catch (Exception e) { Logger.Log.Error(e, "Caught exception while instantiating {0} backend", flavor.Name); } if (iq != null) { Queryable q = new Queryable(flavor, iq); iqueryable_to_queryable [iq] = q; ++count; break; } } } Logger.Log.Debug("Found {0} backends in {1}", count, assembly.Location); }
//////////////////////////////////////////////////////// public static string ListBackends() { ArrayList assemblies = ReflectionFu.ScanEnvironmentForAssemblies("BEAGLE_BACKEND_PATH", PathFinder.BackendDir); // Only add the executing assembly if we haven't already loaded it. if (assemblies.IndexOf(Assembly.GetExecutingAssembly()) == -1) { assemblies.Add(Assembly.GetExecutingAssembly()); } string ret = "User:\n"; foreach (Assembly assembly in assemblies) { foreach (Type type in ReflectionFu.GetTypesFromAssemblyAttribute(assembly, typeof(IQueryableTypesAttribute))) { foreach (QueryableFlavor flavor in ReflectionFu.ScanTypeForAttribute(type, typeof(QueryableFlavor))) { ret += String.Format(" - {0}", flavor.Name); if (flavor.DependsOn != null) { ret += String.Format(" (depends on {0})", flavor.DependsOn); } ret += "\n"; } } } if (!Directory.Exists(PathFinder.SystemIndexesDir)) { return(ret); } ret += "System:\n"; foreach (DirectoryInfo index_dir in new DirectoryInfo(PathFinder.SystemIndexesDir).GetDirectories()) { ret += String.Format(" - {0}\n", index_dir.Name); } return(ret); }
///////////////////////////////////////////////////// static private void LoadAllPairs(Assembly assembly) { foreach (Type type in assembly.GetTypes()) // assembly.GetTypes() is very bad. { if (type.GetInterface(typeof(IHammer).ToString()) == null) { continue; } foreach (HammerAttribute attr in ReflectionFu.ScanTypeForAttribute(type, typeof(HammerAttribute))) { Pair pair; pair = new Pair(); pair.Attribute = attr; pair.Type = type; all_pairs.Add(pair); pairs_by_name [pair.Attribute.Name] = pair; break; } } }
static public void ScanAssemblyForExecutors(Assembly assembly) { if (scanned_assemblies.Contains(assembly)) { return; } scanned_assemblies [assembly] = assembly; foreach (Type t in ReflectionFu.GetTypesFromAssemblyAttribute(assembly, typeof(RequestMessageExecutorTypesAttribute))) { Attribute attr = Attribute.GetCustomAttribute(t, typeof(RequestMessageAttribute)); if (attr == null) { Logger.Log.Warn("No handler attribute for executor {0}", t); continue; } RequestMessageAttribute pra = (RequestMessageAttribute)attr; request_type_to_executor_type [pra.MessageType] = t; } }
static private int ScanAssemblyForFilters(Assembly assembly, FilterCache filter_cache) { int count = 0; foreach (Type t in ReflectionFu.GetTypesFromAssemblyAttribute(assembly, typeof(FilterTypesAttribute))) { Filter filter = null; try { filter = (Filter)Activator.CreateInstance(t); } catch (Exception ex) { Logger.Log.Error(ex, "Caught exception while instantiating {0}", t); } if (filter == null) { continue; } filter_versions_by_name [t.ToString()] = filter.Version; foreach (FilterFlavor flavor in filter.SupportedFlavors) { FilterFlavor.FilterTable [flavor] = t; } ++count; } if (count > 0) { DateTime last_mtime = File.GetLastWriteTimeUtc(assembly.Location); filter_cache.RegisterFilter(assembly.Location, last_mtime); } return(count); }
private static void PrintBackendInformation() { ArrayList assemblies = ReflectionFu.ScanEnvironmentForAssemblies("BEAGLE_BACKEND_PATH", PathFinder.BackendDir); // Add BeagleDaemonLib if it hasn't already been added. bool found_daemon_lib = false; foreach (Assembly assembly in assemblies) { if (assembly.GetName().Name == "BeagleDaemonLib") { found_daemon_lib = true; break; } } if (!found_daemon_lib) { try { assemblies.Add(Assembly.LoadFrom(Path.Combine(PathFinder.PkgLibDir, "BeagleDaemonLib.dll"))); } catch (FileNotFoundException) { Console.WriteLine("WARNING: Could not find backend list."); Environment.Exit(1); } } foreach (Assembly assembly in assemblies) { foreach (Type type in ReflectionFu.GetTypesFromAssemblyAttribute(assembly, typeof(IQueryableTypesAttribute))) { foreach (Beagle.Daemon.QueryableFlavor flavor in ReflectionFu.ScanTypeForAttribute(type, typeof(Beagle.Daemon.QueryableFlavor))) { Console.WriteLine("{0,-20} (" + assembly.Location + ")", flavor.Name); } } } }
// Perform expensive initialization steps all at once. // Should be done before SignalHandler comes into play. static public void Init() { ReadBackendsFromConf(); assemblies = ReflectionFu.ScanEnvironmentForAssemblies("BEAGLE_BACKEND_PATH", PathFinder.BackendDir); }
private static void PrintFilterInformation() { ReflectionFu.ScanEnvironmentForAssemblies("BEAGLE_FILTER_PATH", PathFinder.FilterDir, PrintFilterDetails); }
private static void ListBackends() { ArrayList backends = new ArrayList(); ArrayList assemblies = ReflectionFu.ScanEnvironmentForAssemblies("BEAGLE_BACKEND_PATH", PathFinder.BackendDir); // Add BeagleDaemonLib if it hasn't already been added. bool found_daemon_lib = false; foreach (Assembly assembly in assemblies) { if (assembly.GetName().Name == "BeagleDaemonLib") { found_daemon_lib = true; break; } } if (!found_daemon_lib) { try { assemblies.Add(Assembly.LoadFrom(Path.Combine(PathFinder.PkgLibDir, "BeagleDaemonLib.dll"))); } catch (FileNotFoundException) { Console.WriteLine("WARNING: Could not find backend list."); Environment.Exit(1); } } foreach (Assembly assembly in assemblies) { foreach (Type type in ReflectionFu.GetTypesFromAssemblyAttribute(assembly, typeof(IQueryableTypesAttribute))) { foreach (Beagle.Daemon.QueryableFlavor flavor in ReflectionFu.ScanTypeForAttribute(type, typeof(Beagle.Daemon.QueryableFlavor))) { backends.Add(flavor.Name); } } } if (Directory.Exists(PathFinder.SystemIndexesDir)) { foreach (DirectoryInfo index_dir in new DirectoryInfo(PathFinder.SystemIndexesDir).GetDirectories()) { backends.Add(index_dir.Name); } } bool found_any = false; Console.WriteLine("Allowed backends:"); Config config = Conf.Load("daemon"); Option opt; ArrayList denied_backends = new ArrayList(); if (config != null && (opt = config ["DeniedBackends"]) != null) { List <string[]> denied_backends_list = config.GetListOptionValues(opt.Name); if (denied_backends_list != null) { foreach (string[] val in denied_backends_list) { denied_backends.Add(val [0]); } } } foreach (string name in backends) { if (denied_backends.Contains(name)) { continue; } Console.WriteLine(" - {0}", name); found_any = true; } if (!found_any) { Console.WriteLine(" (none)"); } Console.WriteLine(); found_any = false; Console.WriteLine("Denied backends:"); foreach (string name in denied_backends) { Console.WriteLine(" - {0}", name); found_any = true; } if (!found_any) { Console.WriteLine(" (none)"); } }