[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 |
|
---|
| 22 | using System;
|
---|
[2513] | 23 | using System.Collections.Generic;
|
---|
[4068] | 24 | using System.IO;
|
---|
[2513] | 25 | using System.Linq;
|
---|
[4068] | 26 | using System.ServiceModel;
|
---|
[2513] | 27 | using HeuristicLab.PluginInfrastructure.Manager;
|
---|
[2922] | 28 | using ICSharpCode.SharpZipLib.Zip;
|
---|
[2513] | 29 |
|
---|
| 30 | namespace 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>
|
---|
[3006] | 100 | public void Install(IEnumerable<IPluginDescription> plugins) {
|
---|
| 101 | var args = new PluginInfrastructureCancelEventArgs(plugins);
|
---|
| 102 | OnPreInstall(args);
|
---|
| 103 | if (!args.Cancel) {
|
---|
[4495] | 104 | var client = DeploymentService.UpdateServiceClientFactory.CreateClient();
|
---|
[3006] | 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();
|
---|
[2922] | 112 | }
|
---|
[3508] | 113 | catch (TimeoutException e) {
|
---|
[3006] | 114 | client.Abort();
|
---|
[3508] | 115 | throw new InstallationManagerException("Time out while connecting to server.", e);
|
---|
[3006] | 116 | }
|
---|
[3508] | 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 | }
|
---|
[2922] | 125 | }
|
---|
[2517] | 126 | }
|
---|
| 127 |
|
---|
[2922] | 128 | /// <summary>
|
---|
| 129 | /// Updates plugins from remote server
|
---|
| 130 | /// </summary>
|
---|
[3092] | 131 | /// <param name="plugins"></param>
|
---|
[3006] | 132 | public void Update(IEnumerable<IPluginDescription> plugins) {
|
---|
| 133 | PluginInfrastructureCancelEventArgs args = new PluginInfrastructureCancelEventArgs(plugins);
|
---|
[2922] | 134 | OnPreUpdate(args);
|
---|
| 135 | if (!args.Cancel) {
|
---|
[4495] | 136 | var client = DeploymentService.UpdateServiceClientFactory.CreateClient();
|
---|
[3006] | 137 | try {
|
---|
[2922] | 138 | foreach (DeploymentService.PluginDescription plugin in plugins) {
|
---|
| 139 | byte[] zippedPackage = client.GetPlugin(plugin);
|
---|
| 140 | Unpack(zippedPackage);
|
---|
| 141 | OnUpdated(new PluginInfrastructureEventArgs(plugin));
|
---|
| 142 | }
|
---|
[3006] | 143 | client.Close();
|
---|
[2922] | 144 | }
|
---|
[3508] | 145 | catch (TimeoutException e) {
|
---|
[3006] | 146 | client.Abort();
|
---|
[3508] | 147 | throw new InstallationManagerException("Time out while connecting to server.", e);
|
---|
[3006] | 148 | }
|
---|
[3508] | 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 | }
|
---|
[2922] | 157 | }
|
---|
[2513] | 158 | }
|
---|
| 159 |
|
---|
[2922] | 160 | /// <summary>
|
---|
| 161 | /// Deletes all plugin files from local installation
|
---|
| 162 | /// </summary>
|
---|
[3092] | 163 | /// <param name="plugins"></param>
|
---|
[2922] | 164 | public void Remove(IEnumerable<IPluginDescription> plugins) {
|
---|
| 165 | var fileNames = from pluginToDelete in plugins
|
---|
[2688] | 166 | from file in pluginToDelete.Files
|
---|
| 167 | select Path.Combine(pluginDir, file.Name);
|
---|
[3006] | 168 | var args = new PluginInfrastructureCancelEventArgs(plugins);
|
---|
[2517] | 169 | OnPreDelete(args);
|
---|
| 170 | if (!args.Cancel) {
|
---|
| 171 | foreach (string fileName in fileNames) {
|
---|
[2922] | 172 | File.Delete(fileName);
|
---|
| 173 | OnDeleted(new PluginInfrastructureEventArgs(fileName));
|
---|
[2513] | 174 | }
|
---|
| 175 | }
|
---|
| 176 | }
|
---|
| 177 |
|
---|
[2922] | 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
|
---|
[4482] | 185 | if (!string.IsNullOrEmpty(directoryName)) {
|
---|
[2922] | 186 | Directory.CreateDirectory(directoryName);
|
---|
[2527] | 187 | }
|
---|
[4482] | 188 | if (!string.IsNullOrEmpty(fileName)) {
|
---|
[2922] | 189 | string fullPath = Path.Combine(directoryName, fileName);
|
---|
| 190 | string fullDirPath = Path.GetDirectoryName(fullPath);
|
---|
| 191 | if (!Directory.Exists(fullDirPath)) Directory.CreateDirectory(fullDirPath);
|
---|
[4482] | 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 | }
|
---|
[2922] | 202 | }
|
---|
[4482] | 203 | streamWriter.Close();
|
---|
[2527] | 204 | }
|
---|
| 205 | }
|
---|
| 206 | }
|
---|
| 207 | }
|
---|
[2513] | 208 | }
|
---|
[2517] | 209 |
|
---|
[2527] | 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 |
|
---|
[2517] | 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 | }
|
---|
[2513] | 233 | }
|
---|
| 234 | }
|
---|