#region License Information
/* HeuristicLab
* Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HeuristicLab. If not, see .
*/
#endregion
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.ServiceModel;
using HeuristicLab.PluginInfrastructure;
namespace HeuristicLab.Clients.Hive {
public static class PluginUtil {
///
/// Checks if plugins are available on Hive Server. If not they are uploaded. Ids are returned.
///
/// An active service-proxy
/// List of plugins which are available online
/// List of plugins which have been uploaded from this HiveExperiment
/// List of plugins which need to be uploaded
/// If true, the plugins which are already online are ignored. All local plugins are uploaded, but only once.
///
public static List GetPluginDependencies(IHiveService service, List onlinePlugins, List alreadyUploadedPlugins, IEnumerable neededPlugins, bool useLocalPlugins) {
var pluginIds = new List();
foreach (var neededPlugin in neededPlugins) {
Plugin foundPlugin = alreadyUploadedPlugins.FirstOrDefault(p => p.Name == neededPlugin.Name && p.Version == neededPlugin.Version);
if (foundPlugin == null) {
foundPlugin = onlinePlugins.FirstOrDefault(p => p.Name == neededPlugin.Name && p.Version == neededPlugin.Version);
if (useLocalPlugins || foundPlugin == null) {
Plugin p = CreatePlugin(neededPlugin, useLocalPlugins);
List pd = CreatePluginDatas(neededPlugin);
try {
p.Id = service.AddPlugin(p, pd);
alreadyUploadedPlugins.Add(p);
pluginIds.Add(p.Id);
}
catch (FaultException fault) {
onlinePlugins.Add(service.GetPlugin(fault.Detail.Id));
}
} else {
pluginIds.Add(foundPlugin.Id);
}
} else {
pluginIds.Add(foundPlugin.Id);
}
}
return pluginIds;
}
public static Plugin CreatePlugin(IPluginDescription plugin, bool useLocalPlugins) {
return new Plugin() { Name = plugin.Name, Version = plugin.Version, IsLocal = useLocalPlugins };
}
public static List CreatePluginDatas(IPluginDescription plugin) {
List pluginDatas = new List();
foreach (IPluginFile pf in plugin.Files) {
PluginData pluginData = new PluginData();
pluginData.Data = File.ReadAllBytes(pf.Name);
pluginData.FileName = Path.GetFileName(pf.Name);
pluginDatas.Add(pluginData);
}
return pluginDatas;
}
public static void CollectDeclaringPlugins(List plugins, IEnumerable usedTypes) {
foreach (Type type in usedTypes) {
var plugin = ApplicationManager.Manager.GetDeclaringPlugin(type);
if (plugin != null && !plugins.Contains(plugin)) {
plugins.Add(plugin);
CollectPluginDependencies(plugins, plugin);
}
}
}
public static void CollectPluginDependencies(List plugins, IPluginDescription plugin) {
if (plugin == null) return;
foreach (var dependency in plugin.Dependencies) {
if (!plugins.Contains(dependency)) {
plugins.Add(dependency);
CollectPluginDependencies(plugins, dependency);
}
}
}
}
}