Free cookie consent management tool by TermsFeed Policy Generator

Changeset 1229


Ignore:
Timestamp:
02/26/09 13:44:15 (16 years ago)
Author:
gkronber
Message:

Merged implementation of #471 (OnLoad hook for plugins) (r1228) from CEDMA branch into the trunk.

Location:
trunk/sources/HeuristicLab.PluginInfrastructure
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.PluginInfrastructure/BaseClasses/PluginBase.cs

    r1189 r1229  
    3030  /// Default implementation of the IPlugin interface.
    3131  /// </summary>
    32   public class PluginBase : IPlugin {
     32  public abstract class PluginBase : IPlugin {
    3333    private string name;
    3434    private Version version;
     
    4747
    4848      // exactly one attribute of the type ClassInfoAttribute must be given
    49       if(pluginAttributes.Length != 1) {
     49      if (pluginAttributes.Length != 1) {
    5050        throw new InvalidPluginException();
    5151      }
     
    5555
    5656      // if the plugin name is not explicitly set in the attribute then the default plugin name is the FullName of the type
    57       if(pluginAttribute != null && pluginAttribute.Name != null) {
     57      if (pluginAttribute != null && pluginAttribute.Name != null) {
    5858        this.name = pluginAttribute.Name;
    5959      } else {
     
    6262
    6363      // if the version is not explicitly set in the attribute then the version of the assembly is used as default
    64       if(pluginAttribute != null && pluginAttribute.Version != null) {
     64      if (pluginAttribute != null && pluginAttribute.Version != null) {
    6565        this.version = new Version(pluginAttribute.Version);
    6666      } else {
     
    6969
    7070      // if the description is not explicitly set in the attribute then the name of name of the plugin is used as default
    71       if(pluginAttribute != null && pluginAttribute.Description != null) {
     71      if (pluginAttribute != null && pluginAttribute.Description != null) {
    7272        this.description = pluginAttribute.Description;
    7373      } else {
     
    8181      this.files = new string[fileAttributes.Length];
    8282      int i = 0;
    83       foreach(PluginFileAttribute fileAttr in fileAttributes) {
     83      foreach (PluginFileAttribute fileAttr in fileAttributes) {
    8484        files[i++] = fileAttr.Filename;
    8585      }
     
    121121    }
    122122
    123     /// <inheritdoc/>
    124     public virtual void OnInstall() {
    125     }
    126 
    127     /// <inheritdoc/>
    128     public virtual void OnDelete() {
    129     }
    130 
    131     /// <inheritdoc/>
    132     public virtual void OnPreUpdate() {
    133     }
    134 
    135     /// <inheritdoc/>
    136     public virtual void OnPostUpdate() {
    137     }
     123    /// <inhertidoc>
     124    public virtual void OnLoad() { }
     125    /// <inhertidoc>
     126    public virtual void OnInstall() { }
     127    /// <inhertidoc>
     128    public virtual void OnDelete() { }
     129    /// <inhertidoc>
     130    public virtual void OnPreUpdate() { }
     131    /// <inhertidoc>
     132    public virtual void OnPostUpdate() { }
    138133
    139134    #endregion
  • trunk/sources/HeuristicLab.PluginInfrastructure/Interfaces/IPlugin.cs

    r2 r1229  
    3030  /// assemblies of the plugin. Plugin developers can use the properties of this interface to store
    3131  /// plugin data (name, version, files, update location ...).
    32   /// The methods OnInstall(), OnDelete(), OnPreUpdate(), OnPostUpdate() are called by the framework
     32  /// The methods OnLoad(), OnInstall(), OnDelete(), OnPreUpdate(), OnPostUpdate() are called by the framework
    3333  /// when the corresponding actions are executed. This mechanism allows that the plugin reacts to such
    3434  /// events. For instance to store plugin specific settings.
     
    4242    string[] Files { get; }
    4343
     44
     45    /// <summary>
     46    /// Called by the framework whenever the plugin is loaded.
     47    /// Plugins are loaded once at startup and then each time a new application is started from the starter.
     48    /// </summary>
     49    void OnLoad();
    4450    /// <summary>
    4551    /// called by the framework after the plugin was successfully installed
  • trunk/sources/HeuristicLab.PluginInfrastructure/Loader.cs

    r1189 r1229  
    5151      get {
    5252        List<PluginInfo> list = new List<PluginInfo>();
    53         foreach(PluginInfo info in allPlugins.Keys) {
    54           if(!disabledPlugins.Exists(delegate(PluginInfo disabledInfo) { return info.Name == disabledInfo.Name; })) {
     53        foreach (PluginInfo info in allPlugins.Keys) {
     54          if (!disabledPlugins.Exists(delegate(PluginInfo disabledInfo) { return info.Name == disabledInfo.Name; })) {
    5555            list.Add(info);
    5656          }
     
    8080
    8181    private IPlugin FindPlugin(PluginInfo plugin) {
    82       if(allPlugins.ContainsKey(plugin)) {
     82      if (allPlugins.ContainsKey(plugin)) {
    8383        return allPlugins[plugin];
    8484      } else return null;
     
    101101        try {
    102102          return Assembly.ReflectionOnlyLoad(args.Name);
    103         } catch(FileLoadException ex) {
     103        }
     104        catch (FileLoadException ex) {
    104105          return null;
    105106        }
    106         };
     107      };
    107108      allPlugins.Clear();
    108109      disabledPlugins.Clear();
     
    121122      applications = new List<ApplicationInfo>();
    122123
    123       foreach(IApplication application in apps) {
     124      foreach (IApplication application in apps) {
    124125        ApplicationInfo info = new ApplicationInfo();
    125126        info.Name = application.Name;
     
    137138      List<Assembly> assemblies = new List<Assembly>();
    138139      // load all installed plugins into the reflection only context
    139       foreach(String filename in Directory.GetFiles(pluginDir, "*.dll")) {
     140      foreach (String filename in Directory.GetFiles(pluginDir, "*.dll")) {
    140141        try {
    141142          assemblies.Add(ReflectionOnlyLoadDll(filename));
    142         } catch(BadImageFormatException) { } // just ignore the case that the .dll file is not actually a CLR dll
     143        }
     144        catch (BadImageFormatException) { } // just ignore the case that the .dll file is not actually a CLR dll
    143145      }
    144146      return assemblies;
     
    150152
    151153    private void CheckAssemblyDependencies(List<Assembly> assemblies) {
    152       foreach(Assembly assembly in assemblies) {
     154      foreach (Assembly assembly in assemblies) {
    153155        // GetExportedTypes throws FileNotFoundException when a referenced assembly
    154156        // of the current assembly is missing.
     
    156158          Type[] exported = assembly.GetExportedTypes();
    157159
    158           foreach(Type t in exported) {
     160          foreach (Type t in exported) {
    159161            // if there is a type that implements IPlugin
    160             if(Array.Exists<Type>(t.GetInterfaces(), delegate(Type iface) {
     162            if (Array.Exists<Type>(t.GetInterfaces(), delegate(Type iface) {
    161163              // use AssemblyQualifiedName to compare the types because we can't directly
    162164              // compare ReflectionOnly types and Execution types
     
    167169            }
    168170          }
    169         } catch(FileNotFoundException ex) {
     171        }
     172        catch (FileNotFoundException ex) {
    170173          PluginInfo info = new PluginInfo();
    171174          AssemblyName name = assembly.GetName();
     
    176179          info.Message = "File not found: " + ex.FileName;
    177180          disabledPlugins.Add(info);
    178         } catch(FileLoadException ex) {
     181        }
     182        catch (FileLoadException ex) {
    179183          PluginInfo info = new PluginInfo();
    180184          AssemblyName name = assembly.GetName();
     
    203207      string pluginName = "";
    204208      // iterate through all custom attributes and search for named arguments that we are interested in
    205       foreach(CustomAttributeData attributeData in attributes) {
     209      foreach (CustomAttributeData attributeData in attributes) {
    206210        List<CustomAttributeNamedArgument> namedArguments = new List<CustomAttributeNamedArgument>(attributeData.NamedArguments);
    207211        // if the current attribute contains a named argument with the name "Name" then extract the plugin name
     
    209213          return arg.MemberInfo.Name == "Name";
    210214        });
    211         if(pluginNameArgument.MemberInfo != null) {
     215        if (pluginNameArgument.MemberInfo != null) {
    212216          pluginName = (string)pluginNameArgument.TypedValue.Value;
    213217        }
     
    217221          return arg.MemberInfo.Name == "Dependency";
    218222        });
    219         if(dependencyNameArgument.MemberInfo != null) {
     223        if (dependencyNameArgument.MemberInfo != null) {
    220224          pluginDependencies.Add((string)dependencyNameArgument.TypedValue.Value);
    221225        }
     
    228232          return arg.MemberInfo.Name == "Filetype";
    229233        });
    230         if(filenameArg.MemberInfo != null && filetypeArg.MemberInfo != null) {
     234        if (filenameArg.MemberInfo != null && filetypeArg.MemberInfo != null) {
    231235          pluginFiles.Add(pluginDir + "/" + (string)filenameArg.TypedValue.Value);
    232           if((PluginFileType)filetypeArg.TypedValue.Value == PluginFileType.Assembly) {
     236          if ((PluginFileType)filetypeArg.TypedValue.Value == PluginFileType.Assembly) {
    233237            pluginAssemblies.Add(pluginDir + "/" + (string)filenameArg.TypedValue.Value);
    234238          }
     
    237241
    238242      // minimal sanity check of the attribute values
    239       if(pluginName != "" && pluginAssemblies.Count > 0) {
     243      if (pluginName != "" && pluginAssemblies.Count > 0) {
    240244        // create a temporary PluginInfo that contains the attribute values
    241245        PluginInfo info = new PluginInfo();
     
    253257
    254258    private void CheckPluginDependencies() {
    255       foreach(PluginInfo pluginInfo in preloadedPluginInfos) {
     259      foreach (PluginInfo pluginInfo in preloadedPluginInfos) {
    256260        // don't need to check plugins that are already disabled
    257         if(disabledPlugins.Contains(pluginInfo)) {
     261        if (disabledPlugins.Contains(pluginInfo)) {
    258262          continue;
    259263        }
    260264        visitedDependencies.Clear();
    261         if(!CheckPluginDependencies(pluginInfo.Name)) {
     265        if (!CheckPluginDependencies(pluginInfo.Name)) {
    262266          PluginInfo matchingInfo = preloadedPluginInfos.Find(delegate(PluginInfo info) { return info.Name == pluginInfo.Name; });
    263           if(matchingInfo == null) throw new InvalidProgramException(); // shouldn't happen
    264           foreach(string dependency in pluginDependencies[matchingInfo]) {
     267          if (matchingInfo == null) throw new InvalidProgramException(); // shouldn't happen
     268          foreach (string dependency in pluginDependencies[matchingInfo]) {
    265269            PluginInfo dependencyInfo = new PluginInfo();
    266270            dependencyInfo.Name = dependency;
     
    276280    private List<string> visitedDependencies = new List<string>();
    277281    private bool CheckPluginDependencies(string pluginName) {
    278       if(!preloadedPluginInfos.Exists(delegate(PluginInfo info) { return pluginName == info.Name; }) ||
     282      if (!preloadedPluginInfos.Exists(delegate(PluginInfo info) { return pluginName == info.Name; }) ||
    279283        disabledPlugins.Exists(delegate(PluginInfo info) { return pluginName == info.Name; }) ||
    280284        visitedDependencies.Contains(pluginName)) {
     
    286290
    287291        PluginInfo matchingInfo = preloadedPluginInfos.Find(delegate(PluginInfo info) { return info.Name == pluginName; });
    288         if(matchingInfo == null) throw new InvalidProgramException(); // shouldn't happen
    289         foreach(string dependency in pluginDependencies[matchingInfo]) {
     292        if (matchingInfo == null) throw new InvalidProgramException(); // shouldn't happen
     293        foreach (string dependency in pluginDependencies[matchingInfo]) {
    290294          visitedDependencies.Add(pluginName);
    291           if(CheckPluginDependencies(dependency) == false) {
     295          if (CheckPluginDependencies(dependency) == false) {
    292296            // if only one dependency is not available that means that the current plugin also is unloadable
    293297            return false;
     
    304308    private void LoadPlugins() {
    305309      // load all loadable plugins (all dependencies available) into the execution context
    306       foreach(PluginInfo pluginInfo in preloadedPluginInfos) {
    307         if(!disabledPlugins.Contains(pluginInfo)) {
    308           foreach(string assembly in pluginInfo.Assemblies) {
     310      foreach (PluginInfo pluginInfo in preloadedPluginInfos) {
     311        if (!disabledPlugins.Contains(pluginInfo)) {
     312          foreach (string assembly in pluginInfo.Assemblies) {
    309313            Assembly.LoadFrom(assembly);
    310314          }
     
    314318      DiscoveryService service = new DiscoveryService();
    315319      // now search and instantiate an IPlugin type in each loaded assembly
    316       foreach(Assembly assembly in AppDomain.CurrentDomain.GetAssemblies()) {
     320      foreach (Assembly assembly in AppDomain.CurrentDomain.GetAssemblies()) {
    317321        // don't search for plugins in the PluginInfrastructure
    318         if(assembly == this.GetType().Assembly)
     322        if (assembly == this.GetType().Assembly)
    319323          continue;
    320324        Type[] availablePluginTypes = service.GetTypes(typeof(IPlugin), assembly);
    321         foreach(Type pluginType in availablePluginTypes) {
    322           if(!pluginType.IsAbstract && !pluginType.IsInterface && !pluginType.HasElementType) {
     325        foreach (Type pluginType in availablePluginTypes) {
     326          if (!pluginType.IsAbstract && !pluginType.IsInterface && !pluginType.HasElementType) {
    323327            IPlugin plugin = (IPlugin)Activator.CreateInstance(pluginType);
    324328            PluginAction(this, new PluginManagerActionEventArgs(plugin.Name, PluginManagerAction.InitializingPlugin));
     329            plugin.OnLoad();
    325330            pluginsByName.Add(plugin.Name, plugin);
    326331          }
     
    328333      }
    329334
    330       foreach(IPlugin plugin in pluginsByName.Values) {
     335      foreach (IPlugin plugin in pluginsByName.Values) {
    331336        PluginInfo pluginInfo = GetPluginInfo(plugin);
    332337        allPlugins.Add(pluginInfo, plugin);
     
    335340    }
    336341    private PluginInfo GetPluginInfo(IPlugin plugin) {
    337       if(pluginInfos.ContainsKey(plugin)) {
     342      if (pluginInfos.ContainsKey(plugin)) {
    338343        return pluginInfos[plugin];
    339344      }
     
    344349
    345350      object[] customAttributes = plugin.GetType().Assembly.GetCustomAttributes(typeof(AssemblyBuildDateAttribute), false);
    346       if(customAttributes.Length > 0) {
     351      if (customAttributes.Length > 0) {
    347352        pluginInfo.BuildDate = ((AssemblyBuildDateAttribute)customAttributes[0]).BuildDate;
    348353      }
     
    357362
    358363      PluginInfo preloadedInfo = preloadedPluginInfos.Find(delegate(PluginInfo info) { return info.Name == plugin.Name; });
    359       foreach(string assembly in preloadedInfo.Assemblies) {
     364      foreach (string assembly in preloadedInfo.Assemblies) {
    360365        // always use \ as directory separator (this is necessary for discovery of types in
    361366        // plugins see DiscoveryService.GetTypes()
    362367        pluginInfo.Assemblies.Add(assembly.Replace('/', '\\'));
    363368      }
    364       foreach(string dependency in pluginDependencies[preloadedInfo]) {
     369      foreach (string dependency in pluginDependencies[preloadedInfo]) {
    365370        // accumulate the dependencies of each assembly into the dependencies of the whole plugin
    366371        PluginInfo dependencyInfo = GetPluginInfo(pluginsByName[dependency]);
     
    372377
    373378    private void CheckPluginFiles() {
    374       foreach(PluginInfo plugin in preloadedPluginInfos) {
    375         if(!CheckPluginFiles(plugin)) {
     379      foreach (PluginInfo plugin in preloadedPluginInfos) {
     380        if (!CheckPluginFiles(plugin)) {
    376381          plugin.Message = "Disabled: missing plugin file.";
    377382          disabledPlugins.Add(plugin);
     
    381386
    382387    private bool CheckPluginFiles(PluginInfo pluginInfo) {
    383       foreach(string filename in pluginInfo.Files) {
    384         if(!File.Exists(filename)) {
    385           if(MissingPluginFile != null) {
     388      foreach (string filename in pluginInfo.Files) {
     389        if (!File.Exists(filename)) {
     390          if (MissingPluginFile != null) {
    386391            MissingPluginFile(pluginInfo.Name, filename);
    387392          }
     
    402407    internal void OnDelete(PluginInfo pluginInfo) {
    403408      IPlugin plugin = FindPlugin(pluginInfo);
    404       if(plugin!=null) plugin.OnDelete();
     409      if (plugin != null) plugin.OnDelete();
    405410    }
    406411
    407412    internal void OnInstall(PluginInfo pluginInfo) {
    408413      IPlugin plugin = FindPlugin(pluginInfo);
    409       if(plugin != null) plugin.OnInstall();
     414      if (plugin != null) plugin.OnInstall();
    410415    }
    411416
    412417    internal void OnPreUpdate(PluginInfo pluginInfo) {
    413418      IPlugin plugin = FindPlugin(pluginInfo);
    414       if(plugin != null) plugin.OnPreUpdate();
     419      if (plugin != null) plugin.OnPreUpdate();
    415420    }
    416421
    417422    internal void OnPostUpdate(PluginInfo pluginInfo) {
    418423      IPlugin plugin = FindPlugin(pluginInfo);
    419       if(plugin != null) plugin.OnPostUpdate();
     424      if (plugin != null) plugin.OnPostUpdate();
    420425    }
    421426  }
  • trunk/sources/HeuristicLab.PluginInfrastructure/Runner.cs

    r1189 r1229  
    3939      }
    4040      //CodeAccessPermission.RevertAssert();
     41      FireOnLoad();
    4142      PluginManager.Manager.LoadedPlugins = plugins;
    4243    }
     
    4546      IApplication runnablePlugin = (IApplication)Activator.CreateInstance(appInfo.PluginAssembly, appInfo.PluginType).Unwrap();
    4647      runnablePlugin.Run();
     48    }
     49
     50    private void FireOnLoad() {
     51      DiscoveryService service = new DiscoveryService();
     52      Array.ForEach(service.GetInstances<IPlugin>(), p => p.OnLoad());
    4753    }
    4854
Note: See TracChangeset for help on using the changeset viewer.