Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Breadcrumbs/HeuristicLab.Clients.Hive/3.3/Util/PluginUtil.cs @ 11297

Last change on this file since 11297 was 9456, checked in by swagner, 12 years ago

Updated copyright year and added some missing license headers (#1889)

File size: 5.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.IO;
25using System.Linq;
26using System.Security.Cryptography;
27using System.ServiceModel;
28using HeuristicLab.PluginInfrastructure;
29
30namespace HeuristicLab.Clients.Hive {
31  public static class PluginUtil {
32
33    /// <summary>
34    /// Checks if plugins are available on Hive Server. If not they are uploaded. Ids are returned.
35    /// </summary>
36    /// <param name="service">An active service-proxy</param>
37    /// <param name="onlinePlugins">List of plugins which are available online</param>
38    /// <param name="alreadyUploadedPlugins">List of plugins which have been uploaded from this Task</param>
39    /// <param name="neededPlugins">List of plugins which need to be uploaded</param>
40    /// <returns></returns>
41    public static List<Guid> GetPluginDependencies(IHiveService service, List<Plugin> onlinePlugins, List<Plugin> alreadyUploadedPlugins, IEnumerable<IPluginDescription> neededPlugins) {
42      var pluginIds = new List<Guid>();
43      Dictionary<IPluginDescription, byte[]> checksumsNeededPlugins = CalcChecksumsForPlugins(neededPlugins);
44
45      foreach (var neededPlugin in checksumsNeededPlugins) {
46        Plugin foundPlugin = alreadyUploadedPlugins.FirstOrDefault(p => p.Hash.SequenceEqual(neededPlugin.Value));
47        if (foundPlugin == null) {
48          foundPlugin = onlinePlugins.FirstOrDefault(p => {
49            if (p.Hash != null) {
50              return p.Hash.SequenceEqual(neededPlugin.Value);
51            } else {
52              return false;
53            }
54          });
55
56          if (foundPlugin == null) {
57            Plugin p = CreatePlugin(neededPlugin.Key, neededPlugin.Value);
58            List<PluginData> pd = CreatePluginDatas(neededPlugin.Key);
59            try {
60              p.Id = service.AddPlugin(p, pd);
61              alreadyUploadedPlugins.Add(p);
62              pluginIds.Add(p.Id);
63            }
64            catch (FaultException<PluginAlreadyExistsFault> fault) {
65              onlinePlugins.Add(service.GetPlugin(fault.Detail.Id));
66            }
67          } else {
68            pluginIds.Add(foundPlugin.Id);
69          }
70        } else {
71          pluginIds.Add(foundPlugin.Id);
72        }
73      }
74      return pluginIds;
75    }
76
77    private static Plugin CreatePlugin(IPluginDescription plugin, byte[] hash) {
78      return new Plugin() { Name = plugin.Name, Version = plugin.Version, Hash = hash };
79    }
80
81    public static Plugin CreatePlugin(IPluginDescription plugin) {
82      return new Plugin() { Name = plugin.Name, Version = plugin.Version };
83    }
84
85    public static List<PluginData> CreatePluginDatas(IPluginDescription plugin) {
86      List<PluginData> pluginDatas = new List<PluginData>();
87
88      foreach (IPluginFile pf in plugin.Files) {
89        PluginData pluginData = new PluginData();
90
91        pluginData.Data = File.ReadAllBytes(pf.Name);
92        pluginData.FileName = Path.GetFileName(pf.Name);
93        pluginDatas.Add(pluginData);
94      }
95      return pluginDatas;
96    }
97
98    public static void CollectDeclaringPlugins(List<IPluginDescription> plugins, IEnumerable<Type> usedTypes) {
99      foreach (Type type in usedTypes) {
100        var plugin = ApplicationManager.Manager.GetDeclaringPlugin(type);
101        if (plugin != null && !plugins.Contains(plugin)) {
102          plugins.Add(plugin);
103          CollectPluginDependencies(plugins, plugin);
104        }
105      }
106    }
107
108    public static void CollectPluginDependencies(List<IPluginDescription> plugins, IPluginDescription plugin) {
109      if (plugin == null) return;
110      foreach (var dependency in plugin.Dependencies) {
111        if (!plugins.Contains(dependency)) {
112          plugins.Add(dependency);
113          CollectPluginDependencies(plugins, dependency);
114        }
115      }
116    }
117
118    private static Dictionary<IPluginDescription, byte[]> CalcChecksumsForPlugins(IEnumerable<IPluginDescription> neededPlugins) {
119      Dictionary<IPluginDescription, byte[]> pluginChecksums = new Dictionary<IPluginDescription, byte[]>();
120
121      foreach (IPluginDescription desc in neededPlugins) {
122        byte[] hash;
123        byte[] buffer = new byte[0];
124
125        //calculate checksum over all files belonging to a plugin
126        foreach (IPluginFile pf in desc.Files) {
127          byte[] tmpBuffer = File.ReadAllBytes(pf.Name);
128          byte[] newBuffer = new byte[buffer.Length + tmpBuffer.Length];
129          Array.Copy(buffer, newBuffer, buffer.Length);
130          Array.Copy(tmpBuffer, 0, newBuffer, buffer.Length, tmpBuffer.Length);
131          buffer = newBuffer;
132        }
133
134        using (SHA1 sha1 = SHA1.Create()) {
135          hash = sha1.ComputeHash(buffer);
136        }
137        pluginChecksums.Add(desc, hash);
138      }
139      return pluginChecksums;
140    }
141  }
142}
Note: See TracBrowser for help on using the repository browser.