Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.PluginInfrastructure/3.3/Advanced/InstallationManager.cs @ 7787

Last change on this file since 7787 was 7259, checked in by swagner, 13 years ago

Updated year of copyrights to 2012 (#1716)

File size: 9.1 KB
RevLine 
[2612]1#region License Information
2/* HeuristicLab
[7259]3 * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[2612]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;
[2513]23using System.Collections.Generic;
[4068]24using System.IO;
[2513]25using System.Linq;
[4068]26using System.ServiceModel;
[2513]27using HeuristicLab.PluginInfrastructure.Manager;
[2922]28using ICSharpCode.SharpZipLib.Zip;
[2513]29
30namespace HeuristicLab.PluginInfrastructure.Advanced {
[2517]31  internal class InstallationManager {
[2513]32
[2517]33    internal event EventHandler<PluginInfrastructureCancelEventArgs> PreUpdatePlugin;
34    internal event EventHandler<PluginInfrastructureCancelEventArgs> PreRemovePlugin;
35    internal event EventHandler<PluginInfrastructureCancelEventArgs> PreInstallPlugin;
36
37    internal event EventHandler<PluginInfrastructureEventArgs> PluginUpdated;
38    internal event EventHandler<PluginInfrastructureEventArgs> PluginRemoved;
39    internal event EventHandler<PluginInfrastructureEventArgs> PluginInstalled;
40
[2513]41    private string pluginDir;
[2517]42    public InstallationManager(string pluginDir) {
43      this.pluginDir = pluginDir;
[2513]44    }
45
[2922]46    /// <summary>
47    /// Retrieves a list of plugins available at the remote server
48    /// </summary>
49    /// <returns></returns>
[3006]50    public IEnumerable<IPluginDescription> GetRemotePluginList() {
[4495]51      var client = DeploymentService.UpdateServiceClientFactory.CreateClient();
[3006]52      try {
53        List<IPluginDescription> plugins = new List<IPluginDescription>(client.GetPlugins());
54        client.Close();
55        return plugins;
[2922]56      }
[3508]57      catch (TimeoutException e) {
[3006]58        client.Abort();
[3508]59        throw new InstallationManagerException("Time out while connecting to server.", e);
[3006]60      }
[3508]61      catch (FaultException e) {
62        client.Abort();
63        throw new InstallationManagerException("Fault in connection to server.", e);
64      }
65      catch (CommunicationException e) {
66        client.Abort();
67        throw new InstallationManagerException("General communication exception in connection to server.", e);
68      }
[2517]69    }
70
[2922]71    /// <summary>
72    /// Retrieves the list of products available at the remote server
73    /// </summary>
74    /// <returns></returns>
[3006]75    public IEnumerable<DeploymentService.ProductDescription> GetRemoteProductList() {
[4495]76      var client = DeploymentService.UpdateServiceClientFactory.CreateClient();
[3006]77      try {
78        List<DeploymentService.ProductDescription> products = new List<DeploymentService.ProductDescription>(client.GetProducts());
79        client.Close();
80        return products;
[2922]81      }
[3508]82      catch (TimeoutException e) {
[3006]83        client.Abort();
[3508]84        throw new InstallationManagerException("Time out while connecting to server.", e);
[3006]85      }
[3508]86      catch (FaultException e) {
87        client.Abort();
88        throw new InstallationManagerException("Fault in connection to server.", e);
89      }
90      catch (CommunicationException e) {
91        client.Abort();
92        throw new InstallationManagerException("General communication exception in connection to server.", e);
93      }
[2513]94    }
95
[2922]96    /// <summary>
97    ///  Installs plugins from remote server
98    /// </summary>
[3092]99    /// <param name="plugins"></param>
[6413]100    public void Install(IEnumerable<IPluginDescription> plugins, out bool cancelled) {
[3006]101      var args = new PluginInfrastructureCancelEventArgs(plugins);
102      OnPreInstall(args);
103      if (!args.Cancel) {
[6413]104        cancelled = false;
[4495]105        var client = DeploymentService.UpdateServiceClientFactory.CreateClient();
[3006]106        try {
107          foreach (DeploymentService.PluginDescription plugin in plugins) {
108            byte[] zippedPackage = client.GetPlugin(plugin);
109            Unpack(zippedPackage);
110            OnInstalled(new PluginInfrastructureEventArgs(plugin));
111          }
112          client.Close();
[2922]113        }
[3508]114        catch (TimeoutException e) {
[3006]115          client.Abort();
[3508]116          throw new InstallationManagerException("Time out while connecting to server.", e);
[3006]117        }
[3508]118        catch (FaultException e) {
119          client.Abort();
120          throw new InstallationManagerException("Fault in connection to server.", e);
121        }
122        catch (CommunicationException e) {
123          client.Abort();
124          throw new InstallationManagerException("General communication exception in connection to server.", e);
125        }
[6413]126      } else {
127        cancelled = true;
[2922]128      }
[2517]129    }
130
[2922]131    /// <summary>
132    /// Updates plugins from remote server
133    /// </summary>
[3092]134    /// <param name="plugins"></param>
[6413]135    public void Update(IEnumerable<IPluginDescription> plugins, out bool cancelled) {
[3006]136      PluginInfrastructureCancelEventArgs args = new PluginInfrastructureCancelEventArgs(plugins);
[2922]137      OnPreUpdate(args);
138      if (!args.Cancel) {
[6413]139        cancelled = false;
[4495]140        var client = DeploymentService.UpdateServiceClientFactory.CreateClient();
[3006]141        try {
[2922]142          foreach (DeploymentService.PluginDescription plugin in plugins) {
143            byte[] zippedPackage = client.GetPlugin(plugin);
144            Unpack(zippedPackage);
145            OnUpdated(new PluginInfrastructureEventArgs(plugin));
146          }
[3006]147          client.Close();
[2922]148        }
[3508]149        catch (TimeoutException e) {
[3006]150          client.Abort();
[3508]151          throw new InstallationManagerException("Time out while connecting to server.", e);
[3006]152        }
[3508]153        catch (FaultException e) {
154          client.Abort();
155          throw new InstallationManagerException("Fault in connection to server.", e);
156        }
157        catch (CommunicationException e) {
158          client.Abort();
159          throw new InstallationManagerException("General communication exception in connection to server.", e);
160        }
[6413]161      } else {
162        cancelled = true;
[2922]163      }
[2513]164    }
165
[2922]166    /// <summary>
167    /// Deletes all plugin files from local installation
168    /// </summary>
[3092]169    /// <param name="plugins"></param>
[2922]170    public void Remove(IEnumerable<IPluginDescription> plugins) {
171      var fileNames = from pluginToDelete in plugins
[2688]172                      from file in pluginToDelete.Files
173                      select Path.Combine(pluginDir, file.Name);
[3006]174      var args = new PluginInfrastructureCancelEventArgs(plugins);
[2517]175      OnPreDelete(args);
176      if (!args.Cancel) {
177        foreach (string fileName in fileNames) {
[2922]178          File.Delete(fileName);
179          OnDeleted(new PluginInfrastructureEventArgs(fileName));
[2513]180        }
181      }
182    }
183
[2922]184    private void Unpack(byte[] zippedPackage) {
185      using (ZipInputStream s = new ZipInputStream(new MemoryStream(zippedPackage))) {
186        ZipEntry theEntry;
187        while ((theEntry = s.GetNextEntry()) != null) {
188          string directoryName = pluginDir;
189          string fileName = Path.GetFileName(theEntry.Name);
190          // create directory
[4482]191          if (!string.IsNullOrEmpty(directoryName)) {
[2922]192            Directory.CreateDirectory(directoryName);
[2527]193          }
[4482]194          if (!string.IsNullOrEmpty(fileName)) {
[2922]195            string fullPath = Path.Combine(directoryName, fileName);
196            string fullDirPath = Path.GetDirectoryName(fullPath);
197            if (!Directory.Exists(fullDirPath)) Directory.CreateDirectory(fullDirPath);
[4482]198            using (FileStream streamWriter = File.Create(fullPath)) {
199              int size = 2048;
200              byte[] data = new byte[2048];
201              while (true) {
202                size = s.Read(data, 0, data.Length);
203                if (size > 0) {
204                  streamWriter.Write(data, 0, size);
205                } else {
206                  break;
207                }
[2922]208              }
[4482]209              streamWriter.Close();
[2527]210            }
211          }
212        }
213      }
[2513]214    }
[2517]215
[2527]216    private void OnPreUpdate(PluginInfrastructureCancelEventArgs args) {
217      if (PreUpdatePlugin != null) PreUpdatePlugin(this, args);
218    }
219
220    private void OnUpdated(PluginInfrastructureEventArgs args) {
221      if (PluginUpdated != null) PluginUpdated(this, args);
222    }
223
[2517]224    private void OnPreDelete(PluginInfrastructureCancelEventArgs args) {
225      if (PreRemovePlugin != null) PreRemovePlugin(this, args);
226    }
227
228    private void OnDeleted(PluginInfrastructureEventArgs args) {
229      if (PluginRemoved != null) PluginRemoved(this, args);
230    }
231
232    private void OnPreInstall(PluginInfrastructureCancelEventArgs args) {
233      if (PreInstallPlugin != null) PreInstallPlugin(this, args);
234    }
235
236    private void OnInstalled(PluginInfrastructureEventArgs args) {
237      if (PluginInstalled != null) PluginInstalled(this, args);
238    }
[2513]239  }
240}
Note: See TracBrowser for help on using the repository browser.