Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3612 was 3508, checked in by gkronber, 14 years ago

Worked on review comments by swagner. #989 (Implement review comments in plugin infrastructure)

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