Changeset 2527 for branches/PluginInfrastructure Refactoring/HeuristicLab.PluginInfrastructure/Manager
- Timestamp:
- 11/23/09 20:27:43 (15 years ago)
- Location:
- branches/PluginInfrastructure Refactoring/HeuristicLab.PluginInfrastructure/Manager
- Files:
-
- 1 deleted
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/PluginInfrastructure Refactoring/HeuristicLab.PluginInfrastructure/Manager/ApplicationDescription.cs
r2517 r2527 53 53 /// Gets or sets the description of the application. 54 54 /// </summary> 55 internalstring Description {55 public string Description { 56 56 get { return description; } 57 set { description = value; }57 internal set { description = value; } 58 58 } 59 59 -
branches/PluginInfrastructure Refactoring/HeuristicLab.PluginInfrastructure/Manager/PluginManager.cs
r2513 r2527 104 104 AppDomain.Unload(pluginDomain); 105 105 // unload all plugins 106 foreach (var pluginDescription in plugins )106 foreach (var pluginDescription in plugins.Where(x => x.PluginState == PluginState.Loaded)) 107 107 pluginDescription.Unload(); 108 108 initialized = true; … … 128 128 setup.PrivateBinPath = pluginDir; 129 129 applicationDomain = AppDomain.CreateDomain(appInfo.Name, null, setup); 130 Type applicationManagerType = typeof( DefaultApplicationManager);131 DefaultApplicationManager applicationManager =132 ( DefaultApplicationManager)applicationDomain.CreateInstanceAndUnwrap(applicationManagerType.Assembly.FullName, applicationManagerType.FullName, true, BindingFlags.NonPublic | BindingFlags.Instance, null, null, null, null, null);130 Type applicationManagerType = typeof(ApplicationManager); 131 ApplicationManager applicationManager = 132 (ApplicationManager)applicationDomain.CreateInstanceAndUnwrap(applicationManagerType.Assembly.FullName, applicationManagerType.FullName, true, BindingFlags.NonPublic | BindingFlags.Instance, null, null, null, null, null); 133 133 applicationManager.PluginLoaded += applicationManager_PluginLoaded; 134 134 applicationManager.PluginUnloaded += applicationManager_PluginUnloaded; -
branches/PluginInfrastructure Refactoring/HeuristicLab.PluginInfrastructure/Manager/PluginValidator.cs
r2517 r2527 27 27 using System.Diagnostics; 28 28 using System.Linq; 29 using System.Security; 29 30 30 31 … … 86 87 pluginDependencies.Clear(); 87 88 88 IEnumerable<Assembly> reflectionOnlyAssemblies = ReflectionOnlyLoadDlls( );89 IEnumerable<Assembly> reflectionOnlyAssemblies = ReflectionOnlyLoadDlls(PluginDir); 89 90 IEnumerable<PluginDescription> pluginDescriptions = GatherPluginDescriptions(reflectionOnlyAssemblies); 90 91 CheckPluginFiles(pluginDescriptions); 92 93 CheckPluginAssemblies(pluginDescriptions); 91 94 92 95 // a full list of plugin descriptions is available now we can build the dependency tree … … 137 140 } 138 141 139 private IEnumerable<Assembly> ReflectionOnlyLoadDlls() {142 private static IEnumerable<Assembly> ReflectionOnlyLoadDlls(string baseDir) { 140 143 List<Assembly> assemblies = new List<Assembly>(); 144 // recursively load .dll files in subdirectories 145 foreach (string dirName in Directory.GetDirectories(baseDir)) { 146 assemblies.AddRange(ReflectionOnlyLoadDlls(dirName)); 147 } 141 148 // try to load each .dll file in the plugin directory into the reflection only context 142 foreach (string filename in Directory.GetFiles( PluginDir, "*.dll")) {149 foreach (string filename in Directory.GetFiles(baseDir, "*.dll")) { 143 150 try { 144 151 assemblies.Add(Assembly.ReflectionOnlyLoadFrom(filename)); 145 152 } 146 153 catch (BadImageFormatException) { } // just ignore the case that the .dll file is not a CLR assembly (e.g. a native dll) 154 catch (FileLoadException) { } 155 catch (SecurityException) { } 147 156 } 148 157 return assemblies; 149 158 } 159 160 /// <summary> 161 /// Checks if all plugin assemblies can be loaded. If an assembly can't be loaded the plugin is disabled. 162 /// </summary> 163 /// <param name="pluginDescriptions"></param> 164 private void CheckPluginAssemblies(IEnumerable<PluginDescription> pluginDescriptions) { 165 foreach (var desc in pluginDescriptions.Where(x => x.PluginState != PluginState.Disabled)) { 166 try { 167 foreach (var asm in desc.Assemblies) { 168 Assembly.ReflectionOnlyLoadFrom(asm); 169 } 170 } 171 catch (BadImageFormatException) { 172 // disable the plugin 173 desc.Disable(); 174 } 175 catch (FileNotFoundException) { 176 // disable the plugin 177 desc.Disable(); 178 } 179 catch (FileLoadException) { 180 // disable the plugin 181 desc.Disable(); 182 } 183 catch (ArgumentException) { 184 // disable the plugin 185 desc.Disable(); 186 } 187 catch (SecurityException) { 188 // disable the plugin 189 desc.Disable(); 190 } 191 } 192 } 193 150 194 151 195 // find all types implementing IPlugin in the reflectionOnlyAssemblies and create a list of plugin descriptions … … 158 202 // of the current assembly is missing. 159 203 try { 160 foreach (Type t in assembly.GetExportedTypes()) { 161 // if there is a type that implements IPlugin 162 // use AssemblyQualifiedName to compare the types because we can't directly 163 // compare ReflectionOnly types and Execution types 164 if (!t.IsAbstract && 165 t.GetInterfaces().Any(x => x.AssemblyQualifiedName == typeof(IPlugin).AssemblyQualifiedName)) { 166 // fetch the attributes of the IPlugin type 167 pluginDescriptions.Add(GetPluginDescription(t)); 168 } 169 } 204 // if there is a type that implements IPlugin 205 // use AssemblyQualifiedName to compare the types because we can't directly 206 // compare ReflectionOnly types and execution types 207 var assemblyPluginDescriptions = from t in assembly.GetExportedTypes() 208 where !t.IsAbstract && t.GetInterfaces().Any(x => x.AssemblyQualifiedName == typeof(IPlugin).AssemblyQualifiedName) 209 select GetPluginDescription(t); 210 pluginDescriptions.AddRange(assemblyPluginDescriptions); 170 211 } 171 212 // ignore exceptions. Just don't yield a plugin description when an exception is thrown … … 206 247 string pluginFileName = (string)attributeData.ConstructorArguments[0].Value; 207 248 PluginFileType fileType = (PluginFileType)attributeData.ConstructorArguments[1].Value; 208 pluginFiles.Add(Path. Combine(PluginDir, pluginFileName));249 pluginFiles.Add(Path.GetFullPath(Path.Combine(PluginDir, pluginFileName))); 209 250 if (fileType == PluginFileType.Assembly) { 210 pluginAssemblies.Add(Path. Combine(PluginDir, pluginFileName));251 pluginAssemblies.Add(Path.GetFullPath(Path.Combine(PluginDir, pluginFileName))); 211 252 } 212 253 } … … 302 343 303 344 // checks if all declared plugin files are actually available and disables plugins with missing files 304 private staticvoid CheckPluginFiles(IEnumerable<PluginDescription> pluginDescriptions) {345 private void CheckPluginFiles(IEnumerable<PluginDescription> pluginDescriptions) { 305 346 foreach (PluginDescription desc in pluginDescriptions) { 306 347 if (!CheckPluginFiles(desc)) { … … 310 351 } 311 352 312 private staticbool CheckPluginFiles(PluginDescription pluginDescription) {353 private bool CheckPluginFiles(PluginDescription pluginDescription) { 313 354 foreach (string filename in pluginDescription.Files) { 314 if (!File.Exists(filename)) { 355 if (!FileLiesInDirectory(PluginDir, filename) || 356 !File.Exists(filename)) { 315 357 return false; 316 358 } 317 359 } 318 360 return true; 361 } 362 363 private static bool FileLiesInDirectory(string dir, string fileName) { 364 var basePath = Path.GetFullPath(dir); 365 return Path.GetFullPath(fileName).StartsWith(basePath); 319 366 } 320 367
Note: See TracChangeset
for help on using the changeset viewer.