Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Clients.Hive/3.4/Util/PluginUtil.cs @ 6373

Last change on this file since 6373 was 6373, checked in by cneumuel, 13 years ago

#1233

  • moved ExperimentManager into separate plugin
  • moved Administration into separate plugin
File size: 4.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 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.ServiceModel;
27using HeuristicLab.PluginInfrastructure;
28
29namespace HeuristicLab.Clients.Hive {
30  public static class PluginUtil {
31
32    /// <summary>
33    /// Checks if plugins are available on Hive Server. If not they are uploaded. Ids are returned.
34    /// </summary>
35    /// <param name="service">An active service-proxy</param>
36    /// <param name="onlinePlugins">List of plugins which are available online</param>
37    /// <param name="alreadyUploadedPlugins">List of plugins which have been uploaded from this HiveExperiment</param>
38    /// <param name="neededPlugins">List of plugins which need to be uploaded</param>
39    /// <param name="useLocalPlugins">If true, the plugins which are already online are ignored. All local plugins are uploaded, but only once.</param>
40    /// <returns></returns>
41    public static List<Guid> GetPluginDependencies(IHiveService service, List<Plugin> onlinePlugins, List<Plugin> alreadyUploadedPlugins, IEnumerable<IPluginDescription> neededPlugins, bool useLocalPlugins) {
42      var pluginIds = new List<Guid>();
43      foreach (var neededPlugin in neededPlugins) {
44        Plugin foundPlugin = alreadyUploadedPlugins.FirstOrDefault(p => p.Name == neededPlugin.Name && p.Version == neededPlugin.Version);
45        if (foundPlugin == null) {
46          foundPlugin = onlinePlugins.FirstOrDefault(p => p.Name == neededPlugin.Name && p.Version == neededPlugin.Version);
47          if (useLocalPlugins || foundPlugin == null) {
48            Plugin p = CreatePlugin(neededPlugin, useLocalPlugins);
49            List<PluginData> pd = CreatePluginDatas(neededPlugin);
50            try {
51              p.Id = service.AddPlugin(p, pd);
52              alreadyUploadedPlugins.Add(p);
53              pluginIds.Add(p.Id);
54            }
55            catch (FaultException<PluginAlreadyExistsFault> fault) {
56              onlinePlugins.Add(service.GetPlugin(fault.Detail.Id));
57            }
58          } else {
59            pluginIds.Add(foundPlugin.Id);
60          }
61        } else {
62          pluginIds.Add(foundPlugin.Id);
63        }
64      }
65      return pluginIds;
66    }
67
68    public static Plugin CreatePlugin(IPluginDescription plugin, bool useLocalPlugins) {
69      return new Plugin() { Name = plugin.Name, Version = plugin.Version, IsLocal = useLocalPlugins };
70    }
71
72    public static List<PluginData> CreatePluginDatas(IPluginDescription plugin) {
73      List<PluginData> pluginDatas = new List<PluginData>();
74
75      foreach (IPluginFile pf in plugin.Files) {
76        PluginData pluginData = new PluginData();
77
78        pluginData.Data = File.ReadAllBytes(pf.Name);
79        pluginData.FileName = Path.GetFileName(pf.Name);
80        pluginDatas.Add(pluginData);
81      }
82      return pluginDatas;
83    }
84
85    public static void CollectDeclaringPlugins(List<IPluginDescription> plugins, IEnumerable<Type> usedTypes) {
86      foreach (Type type in usedTypes) {
87        var plugin = ApplicationManager.Manager.GetDeclaringPlugin(type);
88        if (plugin != null && !plugins.Contains(plugin)) {
89          plugins.Add(plugin);
90          CollectPluginDependencies(plugins, plugin);
91        }
92      }
93    }
94
95    public static void CollectPluginDependencies(List<IPluginDescription> plugins, IPluginDescription plugin) {
96      if (plugin == null) return;
97      foreach (var dependency in plugin.Dependencies) {
98        if (!plugins.Contains(dependency)) {
99          plugins.Add(dependency);
100          CollectPluginDependencies(plugins, dependency);
101        }
102      }
103    }
104  }
105}
Note: See TracBrowser for help on using the repository browser.