Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PersistenceReintegration/HeuristicLab.PluginInfrastructure/3.3/Advanced/InstallationManager.cs @ 16003

Last change on this file since 16003 was 14927, checked in by gkronber, 8 years ago

#2520: changed all usages of StorableClass to use StorableType with an auto-generated GUID (did not add StorableType to other type definitions yet)

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