Free cookie consent management tool by TermsFeed Policy Generator

source: branches/VNS/HeuristicLab.PluginInfrastructure/3.3/Advanced/InstallationManager.cs @ 5734

Last change on this file since 5734 was 5445, checked in by swagner, 14 years ago

Updated year of copyrights (#1406)

File size: 8.9 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.Manager;
28using ICSharpCode.SharpZipLib.Zip;
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      }
57      catch (TimeoutException e) {
58        client.Abort();
59        throw new InstallationManagerException("Time out while connecting to server.", e);
60      }
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      }
69    }
70
71    /// <summary>
72    /// Retrieves the list of products available at the remote server
73    /// </summary>
74    /// <returns></returns>
75    public IEnumerable<DeploymentService.ProductDescription> GetRemoteProductList() {
76      var client = DeploymentService.UpdateServiceClientFactory.CreateClient();
77      try {
78        List<DeploymentService.ProductDescription> products = new List<DeploymentService.ProductDescription>(client.GetProducts());
79        client.Close();
80        return products;
81      }
82      catch (TimeoutException e) {
83        client.Abort();
84        throw new InstallationManagerException("Time out while connecting to server.", e);
85      }
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      }
94    }
95
96    /// <summary>
97    ///  Installs plugins from remote server
98    /// </summary>
99    /// <param name="plugins"></param>
100    public void Install(IEnumerable<IPluginDescription> plugins) {
101      var args = new PluginInfrastructureCancelEventArgs(plugins);
102      OnPreInstall(args);
103      if (!args.Cancel) {
104        var client = DeploymentService.UpdateServiceClientFactory.CreateClient();
105        try {
106          foreach (DeploymentService.PluginDescription plugin in plugins) {
107            byte[] zippedPackage = client.GetPlugin(plugin);
108            Unpack(zippedPackage);
109            OnInstalled(new PluginInfrastructureEventArgs(plugin));
110          }
111          client.Close();
112        }
113        catch (TimeoutException e) {
114          client.Abort();
115          throw new InstallationManagerException("Time out while connecting to server.", e);
116        }
117        catch (FaultException e) {
118          client.Abort();
119          throw new InstallationManagerException("Fault in connection to server.", e);
120        }
121        catch (CommunicationException e) {
122          client.Abort();
123          throw new InstallationManagerException("General communication exception in connection to server.", e);
124        }
125      }
126    }
127
128    /// <summary>
129    /// Updates plugins from remote server
130    /// </summary>
131    /// <param name="plugins"></param>
132    public void Update(IEnumerable<IPluginDescription> plugins) {
133      PluginInfrastructureCancelEventArgs args = new PluginInfrastructureCancelEventArgs(plugins);
134      OnPreUpdate(args);
135      if (!args.Cancel) {
136        var client = DeploymentService.UpdateServiceClientFactory.CreateClient();
137        try {
138          foreach (DeploymentService.PluginDescription plugin in plugins) {
139            byte[] zippedPackage = client.GetPlugin(plugin);
140            Unpack(zippedPackage);
141            OnUpdated(new PluginInfrastructureEventArgs(plugin));
142          }
143          client.Close();
144        }
145        catch (TimeoutException e) {
146          client.Abort();
147          throw new InstallationManagerException("Time out while connecting to server.", e);
148        }
149        catch (FaultException e) {
150          client.Abort();
151          throw new InstallationManagerException("Fault in connection to server.", e);
152        }
153        catch (CommunicationException e) {
154          client.Abort();
155          throw new InstallationManagerException("General communication exception in connection to server.", e);
156        }
157      }
158    }
159
160    /// <summary>
161    /// Deletes all plugin files from local installation
162    /// </summary>
163    /// <param name="plugins"></param>
164    public void Remove(IEnumerable<IPluginDescription> plugins) {
165      var fileNames = from pluginToDelete in plugins
166                      from file in pluginToDelete.Files
167                      select Path.Combine(pluginDir, file.Name);
168      var args = new PluginInfrastructureCancelEventArgs(plugins);
169      OnPreDelete(args);
170      if (!args.Cancel) {
171        foreach (string fileName in fileNames) {
172          File.Delete(fileName);
173          OnDeleted(new PluginInfrastructureEventArgs(fileName));
174        }
175      }
176    }
177
178    private void Unpack(byte[] zippedPackage) {
179      using (ZipInputStream s = new ZipInputStream(new MemoryStream(zippedPackage))) {
180        ZipEntry theEntry;
181        while ((theEntry = s.GetNextEntry()) != null) {
182          string directoryName = pluginDir;
183          string fileName = Path.GetFileName(theEntry.Name);
184          // create directory
185          if (!string.IsNullOrEmpty(directoryName)) {
186            Directory.CreateDirectory(directoryName);
187          }
188          if (!string.IsNullOrEmpty(fileName)) {
189            string fullPath = Path.Combine(directoryName, fileName);
190            string fullDirPath = Path.GetDirectoryName(fullPath);
191            if (!Directory.Exists(fullDirPath)) Directory.CreateDirectory(fullDirPath);
192            using (FileStream streamWriter = File.Create(fullPath)) {
193              int size = 2048;
194              byte[] data = new byte[2048];
195              while (true) {
196                size = s.Read(data, 0, data.Length);
197                if (size > 0) {
198                  streamWriter.Write(data, 0, size);
199                } else {
200                  break;
201                }
202              }
203              streamWriter.Close();
204            }
205          }
206        }
207      }
208    }
209
210    private void OnPreUpdate(PluginInfrastructureCancelEventArgs args) {
211      if (PreUpdatePlugin != null) PreUpdatePlugin(this, args);
212    }
213
214    private void OnUpdated(PluginInfrastructureEventArgs args) {
215      if (PluginUpdated != null) PluginUpdated(this, args);
216    }
217
218    private void OnPreDelete(PluginInfrastructureCancelEventArgs args) {
219      if (PreRemovePlugin != null) PreRemovePlugin(this, args);
220    }
221
222    private void OnDeleted(PluginInfrastructureEventArgs args) {
223      if (PluginRemoved != null) PluginRemoved(this, args);
224    }
225
226    private void OnPreInstall(PluginInfrastructureCancelEventArgs args) {
227      if (PreInstallPlugin != null) PreInstallPlugin(this, args);
228    }
229
230    private void OnInstalled(PluginInfrastructureEventArgs args) {
231      if (PluginInstalled != null) PluginInstalled(this, args);
232    }
233  }
234}
Note: See TracBrowser for help on using the repository browser.