Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3.3-HiveMigration/sources/HeuristicLab.Hive/HeuristicLab.Hive.Client.Core/3.3/PluginCache.cs @ 4171

Last change on this file since 4171 was 4141, checked in by cneumuel, 14 years ago

merged with changes from Hive-3.2

File size: 4.6 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.IO;
4using System.Linq;
5using System.Runtime.CompilerServices;
6using HeuristicLab.Hive.Client.Communication;
7using HeuristicLab.Hive.Contracts.BusinessObjects;
8using HeuristicLab.PluginInfrastructure;
9using HeuristicLab.PluginInfrastructure.Manager;
10using HeuristicLab.Tracing;
11
12namespace HeuristicLab.Hive.Client.Core {
13  public class PluginCache {
14
15    private static PluginCache instance = null;
16
17    public const string PLUGIN_REPO = @"plugins\";
18
19    private List<PluginDescription> cachedPlugins = new List<PluginDescription>();
20
21    private PluginManager pm = new PluginManager(PLUGIN_REPO);
22
23    public static PluginCache Instance {
24      get {
25        if (instance == null)
26          instance = new PluginCache();
27        return instance;
28      }
29    }
30    public PluginCache() {
31      DoUpdateRun();
32    }
33
34    private void DoUpdateRun() {
35      pm.DiscoverAndCheckPlugins();
36      cachedPlugins = new List<PluginDescription>(pm.Plugins);
37    }
38
39
40    [MethodImpl(MethodImplOptions.Synchronized)]
41    public bool CopyPluginsForJob(List<HivePluginInfoDto> requests, Guid jobId) {
42
43      String targetDir = PLUGIN_REPO + jobId.ToString() + "\\";
44
45      if (Directory.Exists(targetDir)) {
46        Directory.Delete(targetDir, true);
47      }
48
49      DirectoryInfo di = Directory.CreateDirectory(targetDir);
50
51      foreach (HivePluginInfoDto requestedPlugin in requests) {
52        PluginDescription pd =
53          cachedPlugins.Where(
54            cp =>
55            cp.Name == requestedPlugin.Name &&
56            cp.Version.Major == requestedPlugin.Version.Major &&
57            cp.Version.Minor == requestedPlugin.Version.Minor &&
58            cp.Version.Revision >= requestedPlugin.Version.Revision).
59            SingleOrDefault();
60        if (pd == null) {
61          return false;
62        }
63
64        foreach (IPluginFile ipf in pd.Files) {
65          File.Copy(ipf.Name, targetDir + ipf.Name.Split('\\').Last());
66        }
67      }
68      return true;
69    }
70
71    [MethodImpl(MethodImplOptions.Synchronized)]
72    internal void PreparePlugins(List<HivePluginInfoDto> requiredPlugins) {
73      Logger.Debug("Fetching plugins for job");
74      List<HivePluginInfoDto> localPlugins = new List<HivePluginInfoDto>();
75      List<HivePluginInfoDto> neededPlugins = new List<HivePluginInfoDto>();
76      List<HivePluginInfoDto> missingPlugins = new List<HivePluginInfoDto>();
77      bool found = false;
78
79      foreach (HivePluginInfoDto info in requiredPlugins) {
80        //we MAY run in problems here - if there is a plugin twice in requests, there may be added two different versions of the plugin
81        foreach (PluginDescription cachedPlugin in cachedPlugins) {
82          if (info.Name.Equals(cachedPlugin.Name)) {
83            Logger.Debug("Found plugin " + info.Name + ", " + info.Version);
84            localPlugins.Add(new HivePluginInfoDto() { Id = new Guid(), Name = info.Name, Version = info.Version, Update = true });
85            neededPlugins.Add(info);
86            found = true;
87
88            break;
89          }
90        }
91        if (!found) {
92          Logger.Debug("Plugin NOT found " + info.Name + ", " + info.Version);
93          missingPlugins.Add(info);
94        }
95        found = false;
96      }
97
98      Logger.Debug("First run - Update the plugins in the cache");
99
100      localPlugins.AddRange(missingPlugins);
101
102      List<CachedHivePluginInfoDto> updateablePlugins = WcfService.Instance.RequestPlugins(localPlugins);
103
104      foreach (CachedHivePluginInfoDto updateablePlugin in updateablePlugins) {
105        PluginDescription pd =
106          cachedPlugins.Where(cachedPlugin => cachedPlugin.Name.Equals(updateablePlugin.Name)).SingleOrDefault();
107
108        if (pd != null) {
109          Logger.Debug("deleting old files");
110          foreach (IPluginFile ipf in pd.Files) {
111            File.Delete(ipf.Name);
112          }
113        }
114
115        Logger.Debug("deleted old files");
116        Logger.Debug("creating new files");
117
118
119
120        foreach (HivePluginFile pf in updateablePlugin.PluginFiles) {
121          File.WriteAllBytes(PLUGIN_REPO + pf.Name.Split('\\').Last(), pf.BinaryFile);
122        }
123
124        Logger.Debug("created new files");
125        DoUpdateRun();
126      }
127    }
128
129    internal void DeletePluginsForJob(Guid id) {
130      try {
131        Logger.Debug("unloading...");
132        Directory.Delete(Path.Combine(PLUGIN_REPO, id.ToString()), true);
133      } catch (Exception ex) {
134        Logger.Debug("failed while unloading " + id + " with exception " + ex);
135      }
136    }
137  }
138}
Note: See TracBrowser for help on using the repository browser.