Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3.2/sources/HeuristicLab.PluginInfrastructure/Advanced/InstallationManager.cs @ 10159

Last change on this file since 10159 was 3508, checked in by gkronber, 15 years ago

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

File size: 8.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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.Linq;
25using System.Text;
26using HeuristicLab.PluginInfrastructure.Manager;
27using System.IO;
28using System.ComponentModel;
29using System.Reflection;
30using ICSharpCode.SharpZipLib.Zip;
31using System.ServiceModel;
32
33namespace HeuristicLab.PluginInfrastructure.Advanced {
34  internal class InstallationManager {
35
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
44    private string pluginDir;
45    public InstallationManager(string pluginDir) {
46      this.pluginDir = pluginDir;
47    }
48
49    /// <summary>
50    /// Retrieves a list of plugins available at the remote server
51    /// </summary>
52    /// <returns></returns>
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;
59      }
60      catch (TimeoutException e) {
61        client.Abort();
62        throw new InstallationManagerException("Time out while connecting to server.", e);
63      }
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      }
72    }
73
74    /// <summary>
75    /// Retrieves the list of products available at the remote server
76    /// </summary>
77    /// <returns></returns>
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;
84      }
85      catch (TimeoutException e) {
86        client.Abort();
87        throw new InstallationManagerException("Time out while connecting to server.", e);
88      }
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      }
97    }
98
99    /// <summary>
100    ///  Installs plugins from remote server
101    /// </summary>
102    /// <param name="plugins"></param>
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();
115        }
116        catch (TimeoutException e) {
117          client.Abort();
118          throw new InstallationManagerException("Time out while connecting to server.", e);
119        }
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        }
128      }
129    }
130
131    /// <summary>
132    /// Updates plugins from remote server
133    /// </summary>
134    /// <param name="plugins"></param>
135    public void Update(IEnumerable<IPluginDescription> plugins) {
136      PluginInfrastructureCancelEventArgs args = new PluginInfrastructureCancelEventArgs(plugins);
137      OnPreUpdate(args);
138      if (!args.Cancel) {
139        var client = DeploymentService.UpdateClientFactory.CreateClient();
140        try {
141          foreach (DeploymentService.PluginDescription plugin in plugins) {
142            byte[] zippedPackage = client.GetPlugin(plugin);
143            Unpack(zippedPackage);
144            OnUpdated(new PluginInfrastructureEventArgs(plugin));
145          }
146          client.Close();
147        }
148        catch (TimeoutException e) {
149          client.Abort();
150          throw new InstallationManagerException("Time out while connecting to server.", e);
151        }
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        }
160      }
161    }
162
163    /// <summary>
164    /// Deletes all plugin files from local installation
165    /// </summary>
166    /// <param name="plugins"></param>
167    public void Remove(IEnumerable<IPluginDescription> plugins) {
168      var fileNames = from pluginToDelete in plugins
169                      from file in pluginToDelete.Files
170                      select Path.Combine(pluginDir, file.Name);
171      var args = new PluginInfrastructureCancelEventArgs(plugins);
172      OnPreDelete(args);
173      if (!args.Cancel) {
174        foreach (string fileName in fileNames) {
175          File.Delete(fileName);
176          OnDeleted(new PluginInfrastructureEventArgs(fileName));
177        }
178      }
179    }
180
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);
191          }
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              }
206            }
207            streamWriter.Close();
208          }
209        }
210      }
211    }
212
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
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    }
236  }
237}
Note: See TracBrowser for help on using the repository browser.