- Timestamp:
- 05/29/19 12:16:15 (5 years ago)
- Location:
- branches/2924_DotNetCoreMigration/HeuristicLab.PluginInfrastructure/3.3
- Files:
-
- 1 added
- 12 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/2924_DotNetCoreMigration/HeuristicLab.PluginInfrastructure/3.3/ApplicationBase.cs
r16984 r16993 36 36 private ApplicationAttribute ApplicationAttribute { 37 37 get { 38 object[] appAttributes = this.GetType().GetCustomAttributes(typeof(ApplicationAttribute), false);38 object[] appAttributes = GetType().GetCustomAttributes(typeof(ApplicationAttribute), false); 39 39 40 40 // exactly one attribute of the type ClassInfoAttribute must be given 41 41 if (appAttributes.Length == 0) { 42 throw new InvalidPluginException("ApplicationAttribute on type " + this.GetType() + " is missing.");42 throw new InvalidPluginException("ApplicationAttribute on type " + GetType() + " is missing."); 43 43 } else if (appAttributes.Length > 1) { 44 throw new InvalidPluginException("Found multiple ApplicationAttributes on type " + this.GetType());44 throw new InvalidPluginException("Found multiple ApplicationAttributes on type " + GetType()); 45 45 } 46 46 … … 50 50 51 51 #region IApplication Members 52 53 52 /// <summary> 54 53 /// Gets the name of the application. 55 54 /// </summary> 56 public string Name { 57 get { return ApplicationAttribute.Name; } 58 } 55 public string Name => ApplicationAttribute.Name; 59 56 60 57 /// <summary> 61 58 /// Gets the description of the application. 62 59 /// </summary> 63 public string Description { 64 get { 65 return ApplicationAttribute.Description; 66 } 67 } 60 public string Description => ApplicationAttribute.Description; 68 61 69 62 /// <summary> … … 72 65 public abstract void Run(ICommandLineArgument[] args); 73 66 74 public virtual void OnCancel() { Console.WriteLine("OnCancellation"); } 75 public virtual void OnPause() { Console.WriteLine("OnPause"); } 76 public virtual void OnResume() { Console.WriteLine("OnResume"); } 77 67 public virtual void OnCancel() { } 68 public virtual void OnPause() { } 69 public virtual void OnResume() { } 78 70 #endregion 79 71 } -
branches/2924_DotNetCoreMigration/HeuristicLab.PluginInfrastructure/3.3/HeuristicLab.PluginInfrastructure-3.3.csproj
r16984 r16993 21 21 <PackageReference Include="System.Runtime.Loader" Version="4.3.0" /> 22 22 <PackageReference Include="System.Security.Cryptography.ProtectedData" Version="4.5.0" /> 23 <PackageReference Include="System.ServiceModel.Primitives" Version="4.5.3" />24 23 <PackageReference Include="System.Configuration.ConfigurationManager" Version="4.5.0" /> 25 24 <PackageReference Include="System.Security.Cryptography.Primitives" Version="4.3.0" /> 26 25 </ItemGroup> 27 <ItemGroup>28 <Folder Include="Exceptions\" />29 </ItemGroup>30 26 </Project> -
branches/2924_DotNetCoreMigration/HeuristicLab.PluginInfrastructure/3.3/Interfaces/IApplicationManager.cs
r16984 r16993 47 47 48 48 /// <summary> 49 /// Discovers and creates instances of <typeparamref name="T"/> and all types implementing or inheriting <typeparamref name="T"/> (directly and indirectly). 50 /// </summary> 51 /// <typeparam name="T">The type or super-type to discover.</typeparam> 52 /// <param name="args">Constructor arguments.</param> 53 /// <returns>An enumerable of instances of the discovered types.</returns> 54 IEnumerable<T> GetInstances<T>(params object[] args) where T : class; 55 56 /// <summary> 49 57 /// Discovers and creates instances of <paramref name="type"/> and all types implementing or inheriting <paramref name="type"/> (directly and indirectly). 50 58 /// </summary> … … 52 60 /// <returns>An enumerable of instances of the discovered types.</returns> 53 61 IEnumerable<object> GetInstances(Type type); 62 63 /// <summary> 64 /// Discovers and creates instances of <paramref name="type"/> and all types implementing or inheriting <paramref name="type"/> (directly and indirectly). 65 /// </summary> 66 /// <param name="type">The type or super-type to discover.</param> 67 /// <param name="args">Constructor arguments.</param> 68 /// <returns>An enumerable of instances of the discovered types.</returns> 69 IEnumerable<object> GetInstances(Type type, params object[] args); 54 70 55 71 /// <summary> -
branches/2924_DotNetCoreMigration/HeuristicLab.PluginInfrastructure/3.3/Interfaces/IRunner.cs
r16984 r16993 1 using System; 2 using System.Collections.Generic; 3 using System.IO; 4 using System.Text; 1 using System.Collections.Generic; 5 2 6 3 namespace HeuristicLab.PluginInfrastructure { -
branches/2924_DotNetCoreMigration/HeuristicLab.PluginInfrastructure/3.3/Interfaces/IRunnerHost.cs
r16985 r16993 8 8 /// </summary> 9 9 public interface IRunnerHost { 10 /// <summary> 11 /// The runner state. 12 /// </summary> 13 RunnerState State { get; } 10 14 11 15 /// <summary> … … 32 36 /// </summary> 33 37 void Resume(); 34 35 38 } 36 39 } -
branches/2924_DotNetCoreMigration/HeuristicLab.PluginInfrastructure/3.3/Isolation/ApplicationRunner.cs
r16984 r16993 1 1 using System; 2 using System.Collections.Generic;3 2 using System.IO; 4 using System.Text;5 3 6 4 namespace HeuristicLab.PluginInfrastructure { … … 17 15 public IApplication StartApplication { 18 16 get { 19 using (var memStream = new MemoryStream(serializedStartApplication)) { 20 return (IApplication)formatter.Deserialize(memStream); 17 if (application == null) { 18 using (var memStream = new MemoryStream(serializedStartApplication)) { 19 application = (IApplication)formatter.Deserialize(memStream); 20 } 21 21 } 22 return application; 22 23 } 23 24 set { … … 31 32 // instantly deserialize the application, before all assemblies are loaded. 32 33 private byte[] serializedStartApplication = new byte[0]; 34 // cache application to prevent new instances every get call of StartApplication 35 private IApplication application; 33 36 34 37 protected override void Execute() { … … 39 42 switch (runnerJob) { 40 43 case RunnerJob.Cancel: StartApplication.OnCancel(); break; 41 case RunnerJob.Pause: StartApplication.OnPause(); break;44 case RunnerJob.Pause: StartApplication.OnPause(); break; 42 45 case RunnerJob.Resume: StartApplication.OnResume(); break; 43 46 } -
branches/2924_DotNetCoreMigration/HeuristicLab.PluginInfrastructure/3.3/Isolation/AssemblyLoader.cs
r16984 r16993 120 120 #endregion 121 121 122 123 122 private void LoadTypes(IEnumerable<Assembly> assemblies) { 124 123 foreach (Assembly asm in assemblies) { … … 136 135 } 137 136 } catch (BadImageFormatException) { } 138 } catch (Exception e) { // to catch every other exception137 } catch (Exception) { // to catch every other exception 139 138 //Tracing.Logger.Error( 140 139 // $"Exception occured while loading types of assembly {asm.FullName}! \n " + … … 147 146 foreach (string path in GetAssembliesFromBasePath(basePath)) 148 147 LoadAssemblyFromPath(path); 149 148 150 149 LoadTypes(this.Assemblies); 151 150 return this.Assemblies; … … 155 154 foreach (var info in assemblyInfos) 156 155 LoadAssemblyFromPath(info.Path.ToString()); 157 156 158 157 LoadTypes(this.Assemblies); 159 158 return this.Assemblies; … … 167 166 } // else 168 167 //Tracing.Logger.Error($"Unnable to load assembly with path {path}!"); 169 } catch (Exception e) { // to catch every exception occured by assembly loading.168 } catch (Exception) { // to catch every exception occured by assembly loading. 170 169 //Tracing.Logger.Error( 171 170 // $"Exception occured while loading assembly from path {path}! \n " + -
branches/2924_DotNetCoreMigration/HeuristicLab.PluginInfrastructure/3.3/Isolation/DockerRunnerHost.cs
r16985 r16993 1 namespace HeuristicLab.PluginInfrastructure { 1 using System.Diagnostics; 2 using HeuristicLab.PluginInfrastructure.Exceptions; 3 4 namespace HeuristicLab.PluginInfrastructure { 2 5 /// <summary> 3 6 /// IRunner for isolation with docker. 4 7 /// </summary> 5 8 public class DockerRunnerHost : RunnerHost { 6 public DockerRunnerHost() 7 : base("docker", "run -i --rm --mount type=bind,source=/c/Users/,target=/Users heuristiclab33", null, null, null) { 9 #region Constants 10 private const string Docker = "docker"; 11 private const string ContainerStartup = "container run -i --rm "; 12 private const string Mounting = @"--mount type=bind,source=/c/Users/,target=/Users "; 13 private const string Image = "heuristiclab33:latest"; 14 private const string DockerExceptionMessage = "Docker is not running or image '" + Image + "' does not exists!"; 15 #endregion 16 17 #region Constructors 18 public DockerRunnerHost(bool doDockerAvailableCheck = true) 19 : base(Docker, ContainerStartup + Mounting + Image, null, null, null) { 20 if (doDockerAvailableCheck && !IsDockerAvailable()) 21 throw new DockerException(DockerExceptionMessage); 8 22 } 23 #endregion 24 25 #region Helper 26 private bool IsDockerAvailable() { 27 var process = new Process { 28 StartInfo = new ProcessStartInfo { 29 FileName = Docker, 30 Arguments = "image inspect " + Image, 31 UseShellExecute = false, 32 RedirectStandardOutput = true, 33 RedirectStandardInput = false, 34 RedirectStandardError = true, 35 CreateNoWindow = false 36 }, 37 EnableRaisingEvents = false 38 }; 39 40 process.Start(); 41 process.BeginOutputReadLine(); 42 process.BeginErrorReadLine(); 43 process.WaitForExit(); 44 return process.ExitCode == 0; 45 } 46 #endregion 9 47 } 10 48 } -
branches/2924_DotNetCoreMigration/HeuristicLab.PluginInfrastructure/3.3/Isolation/NativeRunnerHost.cs
r16985 r16993 6 6 /// </summary> 7 7 public class NativeRunnerHost : RunnerHost { 8 public NativeRunnerHost()9 : base($"{Directory.GetCurrentDirectory()}{Path.DirectorySeparatorChar}HeuristicLab-3.3.exe", "",null, null, null) {8 #region Constructors 9 public NativeRunnerHost() : this(null, null, null) { 10 10 } 11 12 public NativeRunnerHost(string userName, string password, string domain) 13 : base($"{Directory.GetCurrentDirectory()}{Path.DirectorySeparatorChar}HeuristicLab-3.3.exe", "", userName, password, domain) { 14 } 15 #endregion 11 16 } 12 17 } -
branches/2924_DotNetCoreMigration/HeuristicLab.PluginInfrastructure/3.3/Isolation/Runner.cs
r16984 r16993 4 4 using System.Runtime.Serialization; 5 5 using System.Runtime.Serialization.Formatters.Binary; 6 using System.Text;7 6 using System.Threading; 8 7 … … 11 10 public abstract class Runner : IRunner { 12 11 12 #region Vars 13 13 [NonSerialized] 14 14 private Thread listener; … … 19 19 [NonSerialized] 20 20 protected static IFormatter formatter = new BinaryFormatter(); 21 21 #endregion 22 23 #region Properties 22 24 public bool QuietMode { get; set; } 23 25 24 26 public IEnumerable<AssemblyInfo> AssembliesToLoad { get; set; } 27 #endregion 28 25 29 26 30 public static void Serialize(IRunner runner, Stream stream) => formatter.Serialize(stream, runner); … … 41 45 protected abstract void OnRunnerJob(RunnerJob runnerJob); 42 46 47 #region Helper 43 48 private void StartExecutor() { 44 49 executor = new Thread(Execute); … … 46 51 executor.Start(); 47 52 } 53 48 54 private void StartListener() { 49 55 listener = new Thread(() => { 50 56 Stream stdin = Console.OpenStandardInput(); 51 while (executor.IsAlive) 57 while (executor.IsAlive) 52 58 OnRunnerJob((RunnerJob)formatter.Deserialize(stdin)); 53 59 }); … … 55 61 listener.Start(); 56 62 } 57 63 #endregion 58 64 } 59 65 } -
branches/2924_DotNetCoreMigration/HeuristicLab.PluginInfrastructure/3.3/Isolation/RunnerHost.cs
r16984 r16993 74 74 UserName = string.IsNullOrEmpty(UserName) ? null : UserName, 75 75 PasswordInClearText = string.IsNullOrEmpty(Password) ? null : Password, 76 Domain = string.IsNullOrEmpty(Domain) ? null : Domain,76 Domain = string.IsNullOrEmpty(Domain) ? null : Domain, 77 77 WorkingDirectory = Directory.GetCurrentDirectory() 78 78 }, … … 86 86 Runner.Serialize(runner, process.StandardInput.BaseStream); 87 87 88 process.BeginOutputReadLine(); 89 process.BeginErrorReadLine(); 90 88 91 if (!runner.QuietMode) { 89 process.BeginOutputReadLine();90 process.BeginErrorReadLine();91 92 process.OutputDataReceived += (s, e) => Console.WriteLine(e.Data); 92 93 process.ErrorDataReceived += (s, e) => Console.WriteLine(e.Data); … … 124 125 /// When the child process gets finished without requested cancellation, the linked token gets cancelled and a result set. 125 126 /// </summary> 126 /// <param name="token"></param>127 /// <returns></returns>128 127 private Task<bool> RegisterCancellation(CancellationToken token) { 129 128 if (process != null && State == RunnerState.Starting) { -
branches/2924_DotNetCoreMigration/HeuristicLab.PluginInfrastructure/3.3/LightweightApplicationManager.cs
r16984 r16993 37 37 } 38 38 39 Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args) { 40 return null; 41 } 39 Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args) => null; 42 40 43 41 … … 46 44 /// Gets an empty list of plugins. (LightweightApplicationManager doesn't support plugin discovery) 47 45 /// </summary> 48 public IEnumerable<IPluginDescription> Plugins { 49 get { return new IPluginDescription[0]; } 50 } 46 public IEnumerable<IPluginDescription> Plugins => new IPluginDescription[0]; 51 47 52 48 /// <summary> 53 49 /// Gets an empty list of applications. (LightweightApplicationManager doesn't support application discovery) 54 50 /// </summary> 55 public IEnumerable<IApplicationDescription> Applications { 56 get { return new IApplicationDescription[0]; } 57 } 51 public IEnumerable<IApplicationDescription> Applications => new IApplicationDescription[0]; 58 52 59 53 /// <summary> … … 62 56 /// <typeparam name="T">Most general type.</typeparam> 63 57 /// <returns>Enumerable of the created instances.</returns> 64 public IEnumerable<T> GetInstances<T>() where T : class { 65 return GetInstances(typeof(T)).Cast<T>(); 66 } 58 public IEnumerable<T> GetInstances<T>() where T : class => GetInstances(typeof(T)).Cast<T>(); 59 60 /// <summary> 61 /// Creates an instance of all types that are subtypes or the same type of the specified type 62 /// </summary> 63 /// <typeparam name="T">Most general type.</typeparam> 64 /// <param name="args">Constructor arguments.</param> 65 /// <returns>Enumerable of the created instances.</returns> 66 public IEnumerable<T> GetInstances<T>(params object[] args) where T : class => GetInstances(typeof(T), args).Cast<T>(); 67 67 68 68 /// <summary> … … 71 71 /// <param name="type">Most general type.</param> 72 72 /// <returns>Enumerable of the created instances.</returns> 73 public IEnumerable<object> GetInstances(Type type) { 73 public IEnumerable<object> GetInstances(Type type) => GetInstances(type, null); 74 75 /// <summary> 76 /// Creates an instance of all types that are subtypes or the same type of the specified type 77 /// </summary> 78 /// <param name="type">Most general type.</param> 79 /// <param name="args">Constructor arguments.</param> 80 /// <returns>Enumerable of the created instances.</returns> 81 public IEnumerable<object> GetInstances(Type type, params object[] args) { 74 82 List<object> instances = new List<object>(); 75 83 foreach (Type t in GetTypes(type)) { 76 84 object instance = null; 77 try { instance = Activator.CreateInstance(t ); } catch { }85 try { instance = Activator.CreateInstance(t, args); } catch { } 78 86 if (instance != null) instances.Add(instance); 79 87 } … … 81 89 } 82 90 91 /// <summary> 92 /// Discovers a specific type by its name. 93 /// </summary> 94 /// <param name="typeName">Full name of the type.</param> 95 /// <returns>A type or null, if nothing was found.</returns> 83 96 public Type GetType(string typeName) { 84 97 foreach (Assembly asm in AppDomain.CurrentDomain.GetAssemblies()) { … … 217 230 throw new NotSupportedException("LightweightApplicationManager doesn't support type discovery for plugins."); 218 231 } 219 220 232 #endregion 221 233 }
Note: See TracChangeset
for help on using the changeset viewer.