Пример #1
0
            public void RegistryPlugin_WhenRegistrationContainsNullPluginDependency_Throws()
            {
                var registry = new Registry();
                var pluginRegistration = new PluginRegistration("pluginId", new TypeName("Plugin, Assembly"), new DirectoryInfo(@"C:\"));
                pluginRegistration.PluginDependencies.Add(null);

                var ex = Assert.Throws<ArgumentException>(() => registry.RegisterPlugin(pluginRegistration));
                Assert.Contains(ex.Message, "The plugin dependencies must not contain a null reference.");
            }
        public void PluginTypeName_Accessor_EnforcesConstraints()
        {
            var registration = new PluginRegistration("pluginId", new TypeName("Plugin, Assembly"), new DirectoryInfo(@"C:\"));

            Assert.AreEqual(new TypeName("Plugin, Assembly"), registration.PluginTypeName);
            Assert.Throws<ArgumentNullException>(() => { registration.PluginTypeName = null; });

            registration.PluginTypeName = new TypeName("DifferentPlugin, Assembly");

            Assert.AreEqual(new TypeName("DifferentPlugin, Assembly"), registration.PluginTypeName);
        }
Пример #3
0
        /// <inheritdoc />
        public IPluginDescriptor RegisterPlugin(PluginRegistration pluginRegistration)
        {
            if (pluginRegistration == null)
            {
                throw new ArgumentNullException("pluginRegistration");
            }
            if (pluginRegistration.AssemblyBindings.Contains(null))
            {
                throw new ArgumentException("The assembly references must not contain a null reference.", "pluginRegistration");
            }
            if (pluginRegistration.PluginDependencies.Contains(null))
            {
                throw new ArgumentException("The plugin dependencies must not contain a null reference.", "pluginRegistration");
            }
            if (pluginRegistration.ProbingPaths.Contains(null))
            {
                throw new ArgumentException("The probing paths must not contain a null reference.", "pluginRegistration");
            }

            return(dataBox.Write(data =>
            {
                if (data.GetPluginById(pluginRegistration.PluginId) != null)
                {
                    throw new ArgumentException(string.Format("There is already a plugin registered with id '{0}'.", pluginRegistration.PluginId), "pluginRegistration");
                }

                List <IPluginDescriptor> pluginDependencies = new List <IPluginDescriptor>(pluginRegistration.PluginDependencies);

                foreach (IPluginDescriptor pluginDependency in pluginRegistration.PluginDependencies)
                {
                    if (data.GetPluginById(pluginDependency.PluginId) != pluginDependency)
                    {
                        throw new ArgumentException(
                            "One of the plugin dependencies does not belong to this registry.", "pluginRegistration");
                    }

                    foreach (IPluginDescriptor secondOrderPluginDependency in pluginDependency.PluginDependencies)
                    {
                        if (!pluginDependencies.Contains(secondOrderPluginDependency))
                        {
                            pluginDependencies.Add(secondOrderPluginDependency);
                        }
                    }
                }

                PluginDescriptor plugin = new PluginDescriptor(this, pluginRegistration, pluginDependencies);
                data.RegisterPlugin(plugin);
                return plugin;
            }));
        }
Пример #4
0
 public PluginDescriptor(Registry registry, PluginRegistration pluginRegistration, IList <IPluginDescriptor> completePluginDependenciesCopy)
 {
     this.registry               = registry;
     pluginId                    = pluginRegistration.PluginId;
     pluginTypeName              = pluginRegistration.PluginTypeName;
     baseDirectory               = pluginRegistration.BaseDirectory;
     pluginProperties            = pluginRegistration.PluginProperties.Copy().AsReadOnly();
     traitsProperties            = pluginRegistration.TraitsProperties.Copy().AsReadOnly();
     pluginHandlerFactory        = pluginRegistration.PluginHandlerFactory;
     assemblyBindings            = new ReadOnlyCollection <AssemblyBinding>(GenericCollectionUtils.ToArray(pluginRegistration.AssemblyBindings));
     pluginDependencies          = new ReadOnlyCollection <IPluginDescriptor>(completePluginDependenciesCopy);
     probingPaths                = new ReadOnlyCollection <string>(GenericCollectionUtils.ToArray(pluginRegistration.ProbingPaths));
     enableCondition             = pluginRegistration.EnableCondition;
     recommendedInstallationPath = pluginRegistration.RecommendedInstallationPath;
     filePaths                   = new ReadOnlyCollection <string>(GenericCollectionUtils.ToArray(pluginRegistration.FilePaths));
 }
Пример #5
0
 public PluginDescriptor(Registry registry, PluginRegistration pluginRegistration, IList<IPluginDescriptor> completePluginDependenciesCopy)
 {
     this.registry = registry;
     pluginId = pluginRegistration.PluginId;
     pluginTypeName = pluginRegistration.PluginTypeName;
     baseDirectory = pluginRegistration.BaseDirectory;
     pluginProperties = pluginRegistration.PluginProperties.Copy().AsReadOnly();
     traitsProperties = pluginRegistration.TraitsProperties.Copy().AsReadOnly();
     pluginHandlerFactory = pluginRegistration.PluginHandlerFactory;
     assemblyBindings = new ReadOnlyCollection<AssemblyBinding>(GenericCollectionUtils.ToArray(pluginRegistration.AssemblyBindings));
     pluginDependencies = new ReadOnlyCollection<IPluginDescriptor>(completePluginDependenciesCopy);
     probingPaths = new ReadOnlyCollection<string>(GenericCollectionUtils.ToArray(pluginRegistration.ProbingPaths));
     enableCondition = pluginRegistration.EnableCondition;
     recommendedInstallationPath = pluginRegistration.RecommendedInstallationPath;
     filePaths = new ReadOnlyCollection<string>(GenericCollectionUtils.ToArray(pluginRegistration.FilePaths));
 }
        public void BaseDirectory_Accessor_EnforcesConstraints()
        {
            var registration = new PluginRegistration("pluginId", new TypeName("Plugin, Assembly"), new DirectoryInfo(@"C:\"));

            Assert.AreEqual(@"C:\", registration.BaseDirectory.ToString());
            Assert.Throws<ArgumentNullException>(() => { registration.BaseDirectory = null; });

            registration.BaseDirectory = new DirectoryInfo(@"D:\");

            Assert.AreEqual(@"D:\", registration.BaseDirectory.ToString());
        }
        public void FilePaths_Accessor_EnforcesConstraints()
        {
            var registration = new PluginRegistration("pluginId", new TypeName("Plugin, Assembly"), new DirectoryInfo(@"C:\"));

            Assert.IsEmpty(registration.FilePaths);
            Assert.Throws<ArgumentNullException>(() => { registration.FilePaths = null; });

            var differentPaths = new[] { "file1.txt", "file2.dll" };
            registration.FilePaths = differentPaths;

            Assert.AreSame(differentPaths, registration.FilePaths);
        }
        public void EnableCondition_Accessor_EnforcesConstraints()
        {
            var registration = new PluginRegistration("pluginId", new TypeName("Plugin, Assembly"), new DirectoryInfo(@"C:\"));
            var condition = Condition.Parse("${minFramework:NET35}");

            Assert.IsNull(registration.EnableCondition);

            registration.EnableCondition = condition;
            Assert.AreEqual(condition, registration.EnableCondition);

            registration.EnableCondition = null;
            Assert.IsNull(registration.EnableCondition);
        }
        public void RecommendedInstallationPath_Accessor_EnforcesConstraints()
        {
            var registration = new PluginRegistration("pluginId", new TypeName("Plugin, Assembly"), new DirectoryInfo(@"C:\"));

            Assert.IsNull(registration.RecommendedInstallationPath);

            registration.RecommendedInstallationPath = "MyPlugin";
            Assert.AreEqual("MyPlugin", registration.RecommendedInstallationPath);

            registration.RecommendedInstallationPath = null;
            Assert.IsNull(registration.RecommendedInstallationPath);
        }
        public void PluginDependencies_Accessor_EnforcesConstraints()
        {
            var registration = new PluginRegistration("pluginId", new TypeName("Plugin, Assembly"), new DirectoryInfo(@"C:\"));

            Assert.IsEmpty(registration.PluginDependencies);
            Assert.Throws<ArgumentNullException>(() => { registration.PluginDependencies = null; });

            var differentDependencies = new[] { MockRepository.GenerateStub<IPluginDescriptor>() };
            registration.PluginDependencies = differentDependencies;

            Assert.AreSame(differentDependencies, registration.PluginDependencies);
        }
        public void AssemblyBindings_Accessor_EnforcesConstraints()
        {
            var registration = new PluginRegistration("pluginId", new TypeName("Plugin, Assembly"), new DirectoryInfo(@"C:\"));

            Assert.IsEmpty(registration.AssemblyBindings);
            Assert.Throws<ArgumentNullException>(() => { registration.AssemblyBindings = null; });

            var differentReferences = new[] { new AssemblyBinding(new AssemblyName("Gallio")) };
            registration.AssemblyBindings = differentReferences;

            Assert.AreSame(differentReferences, registration.AssemblyBindings);
        }
        public void PluginHandlerFactory_Accessor_EnforcesConstraints()
        {
            var registration = new PluginRegistration("pluginId", new TypeName("Plugin, Assembly"), new DirectoryInfo(@"C:\"));

            Assert.IsInstanceOfType<SingletonHandlerFactory>(registration.PluginHandlerFactory);
            Assert.Throws<ArgumentNullException>(() => { registration.PluginHandlerFactory = null; });

            var differentHandlerFactory = MockRepository.GenerateStub<IHandlerFactory>();
            registration.PluginHandlerFactory = differentHandlerFactory;

            Assert.AreSame(differentHandlerFactory, registration.PluginHandlerFactory);
        }
        public void TraitsProperties_Accessor_EnforcesConstraints()
        {
            var registration = new PluginRegistration("pluginId", new TypeName("Plugin, Assembly"), new DirectoryInfo(@"C:\"));

            Assert.IsEmpty(registration.TraitsProperties);
            Assert.Throws<ArgumentNullException>(() => { registration.TraitsProperties = null; });

            var differentProperties = new PropertySet();
            registration.TraitsProperties = differentProperties;

            Assert.AreSame(differentProperties, registration.TraitsProperties);
        }
Пример #14
0
        private static IList <IPluginDescriptor> RegisterPlugins(IRegistry registry, IList <PluginData> topologicallySortedPlugins)
        {
            IPluginDescriptor[] pluginDescriptors = new IPluginDescriptor[topologicallySortedPlugins.Count];
            for (int i = 0; i < topologicallySortedPlugins.Count; i++)
            {
                Plugin        plugin        = topologicallySortedPlugins[i].Plugin;
                DirectoryInfo baseDirectory = topologicallySortedPlugins[i].BaseDirectory;

                try
                {
                    var pluginType = plugin.PluginType != null
                        ? new TypeName(plugin.PluginType)
                        : new TypeName(typeof(DefaultPlugin));

                    List <string> disabledReasons = new List <string>();

                    var pluginRegistration = new PluginRegistration(plugin.PluginId,
                                                                    pluginType, baseDirectory);
                    if (plugin.Parameters != null)
                    {
                        pluginRegistration.PluginProperties = plugin.Parameters.PropertySet;
                    }
                    if (plugin.Traits != null)
                    {
                        pluginRegistration.TraitsProperties = plugin.Traits.PropertySet;
                    }

                    pluginRegistration.ProbingPaths = plugin.ProbingPaths;
                    pluginRegistration.RecommendedInstallationPath = plugin.RecommendedInstallationPath;

                    if (plugin.EnableCondition != null)
                    {
                        pluginRegistration.EnableCondition = Condition.Parse(plugin.EnableCondition);
                    }

                    foreach (var file in plugin.Files)
                    {
                        pluginRegistration.FilePaths.Add(file.Path);
                    }

                    foreach (var dependency in plugin.Dependencies)
                    {
                        string pluginDependencyId = dependency.PluginId;

                        IPluginDescriptor pluginDependency = registry.Plugins[pluginDependencyId];
                        if (pluginDependency == null)
                        {
                            disabledReasons.Add(string.Format("Could not find plugin '{0}' upon which this plugin depends.", pluginDependencyId));
                        }
                        else
                        {
                            pluginRegistration.PluginDependencies.Add(pluginDependency);
                        }
                    }

                    foreach (var assembly in plugin.Assemblies)
                    {
                        Uri absoluteCodeBase;
                        if (assembly.CodeBase != null)
                        {
                            List <string> attemptedPaths    = new List <string>();
                            string        foundCodeBasePath = ProbeForCodeBase(baseDirectory, plugin.ProbingPaths, assembly.CodeBase, attemptedPaths);
                            if (foundCodeBasePath == null)
                            {
                                StringBuilder formattedPaths = new StringBuilder();
                                foreach (string path in attemptedPaths)
                                {
                                    if (formattedPaths.Length != 0)
                                    {
                                        formattedPaths.Append(", ");
                                    }
                                    formattedPaths.Append("'").Append(path).Append("'");
                                }

                                disabledReasons.Add(string.Format("Could not find assembly '{0}' after probing for its code base in {1}.",
                                                                  assembly.FullName, formattedPaths));
                                absoluteCodeBase = null;
                            }
                            else
                            {
                                absoluteCodeBase = new Uri(foundCodeBasePath);
                            }
                        }
                        else
                        {
#if STRICT_GAC_CHECKS
                            if (!IsAssemblyRegisteredInGAC(assembly.FullName))
                            {
                                disabledReasons.Add(
                                    string.Format("Could not find assembly '{0}' in the global assembly cache.",
                                                  assembly.FullName));
                            }
#endif

                            absoluteCodeBase = null;
                        }

                        var assemblyBinding = new AssemblyBinding(new AssemblyName(assembly.FullName))
                        {
                            CodeBase             = absoluteCodeBase,
                            QualifyPartialName   = assembly.QualifyPartialName,
                            ApplyPublisherPolicy = assembly.ApplyPublisherPolicy
                        };

                        foreach (BindingRedirect redirect in assembly.BindingRedirects)
                        {
                            assemblyBinding.AddBindingRedirect(new AssemblyBinding.BindingRedirect(redirect.OldVersion));
                        }

                        pluginRegistration.AssemblyBindings.Add(assemblyBinding);
                    }

                    IPluginDescriptor pluginDescriptor = registry.RegisterPlugin(pluginRegistration);
                    pluginDescriptors[i] = pluginDescriptor;

                    if (disabledReasons.Count != 0)
                    {
                        pluginDescriptor.Disable(disabledReasons[0]);
                    }
                }
                catch (Exception ex)
                {
                    throw new RuntimeException(string.Format("Could not register plugin '{0}'.",
                                                             plugin.PluginId), ex);
                }
            }
            return(pluginDescriptors);
        }
Пример #15
0
        private static IList<IPluginDescriptor> RegisterPlugins(IRegistry registry, IList<PluginData> topologicallySortedPlugins)
        {
            IPluginDescriptor[] pluginDescriptors = new IPluginDescriptor[topologicallySortedPlugins.Count];
            for (int i = 0; i < topologicallySortedPlugins.Count; i++)
            {
                Plugin plugin = topologicallySortedPlugins[i].Plugin;
                DirectoryInfo baseDirectory = topologicallySortedPlugins[i].BaseDirectory;

                try
                {
                    var pluginType = plugin.PluginType != null
                        ? new TypeName(plugin.PluginType)
                        : new TypeName(typeof(DefaultPlugin));

                    List<string> disabledReasons = new List<string>();

                    var pluginRegistration = new PluginRegistration(plugin.PluginId,
                        pluginType, baseDirectory);
                    if (plugin.Parameters != null)
                        pluginRegistration.PluginProperties = plugin.Parameters.PropertySet;
                    if (plugin.Traits != null)
                        pluginRegistration.TraitsProperties = plugin.Traits.PropertySet;

                    pluginRegistration.ProbingPaths = plugin.ProbingPaths;
                    pluginRegistration.RecommendedInstallationPath = plugin.RecommendedInstallationPath;

                    if (plugin.EnableCondition != null)
                        pluginRegistration.EnableCondition = Condition.Parse(plugin.EnableCondition);

                    foreach (var file in plugin.Files)
                        pluginRegistration.FilePaths.Add(file.Path);

                    foreach (var dependency in plugin.Dependencies)
                    {
                        string pluginDependencyId = dependency.PluginId;

                        IPluginDescriptor pluginDependency = registry.Plugins[pluginDependencyId];
                        if (pluginDependency == null)
                        {
                            disabledReasons.Add(string.Format("Could not find plugin '{0}' upon which this plugin depends.", pluginDependencyId));
                        }
                        else
                        {
                            pluginRegistration.PluginDependencies.Add(pluginDependency);
                        }
                    }

                    foreach (var assembly in plugin.Assemblies)
                    {
                        Uri absoluteCodeBase;
                        if (assembly.CodeBase != null)
                        {
                            List<string> attemptedPaths = new List<string>();
                            string foundCodeBasePath = ProbeForCodeBase(baseDirectory, plugin.ProbingPaths, assembly.CodeBase, attemptedPaths);
                            if (foundCodeBasePath == null)
                            {
                                StringBuilder formattedPaths = new StringBuilder();
                                foreach (string path in attemptedPaths)
                                {
                                    if (formattedPaths.Length != 0)
                                        formattedPaths.Append(", ");
                                    formattedPaths.Append("'").Append(path).Append("'");
                                }

                                disabledReasons.Add(string.Format("Could not find assembly '{0}' after probing for its code base in {1}.",
                                    assembly.FullName, formattedPaths));
                                absoluteCodeBase = null;
                            }
                            else
                            {
                                absoluteCodeBase = new Uri(foundCodeBasePath);
                            }
                        }
                        else
                        {
            #if STRICT_GAC_CHECKS
                            if (!IsAssemblyRegisteredInGAC(assembly.FullName))
                            {
                                disabledReasons.Add(
                                    string.Format("Could not find assembly '{0}' in the global assembly cache.",
                                        assembly.FullName));
                            }
            #endif

                            absoluteCodeBase = null;
                        }

                        var assemblyBinding = new AssemblyBinding(new AssemblyName(assembly.FullName))
                        {
                            CodeBase = absoluteCodeBase,
                            QualifyPartialName = assembly.QualifyPartialName,
                            ApplyPublisherPolicy = assembly.ApplyPublisherPolicy
                        };

                        foreach (BindingRedirect redirect in assembly.BindingRedirects)
                            assemblyBinding.AddBindingRedirect(new AssemblyBinding.BindingRedirect(redirect.OldVersion));

                        pluginRegistration.AssemblyBindings.Add(assemblyBinding);
                    }

                    IPluginDescriptor pluginDescriptor = registry.RegisterPlugin(pluginRegistration);
                    pluginDescriptors[i] = pluginDescriptor;

                    if (disabledReasons.Count != 0)
                        pluginDescriptor.Disable(disabledReasons[0]);
                }
                catch (Exception ex)
                {
                    throw new RuntimeException(string.Format("Could not register plugin '{0}'.",
                        plugin.PluginId), ex);
                }
            }
            return pluginDescriptors;
        }
Пример #16
0
        /// <inheritdoc />
        public IPluginDescriptor RegisterPlugin(PluginRegistration pluginRegistration)
        {
            if (pluginRegistration == null)
                throw new ArgumentNullException("pluginRegistration");
            if (pluginRegistration.AssemblyBindings.Contains(null))
                throw new ArgumentException("The assembly references must not contain a null reference.", "pluginRegistration");
            if (pluginRegistration.PluginDependencies.Contains(null))
                throw new ArgumentException("The plugin dependencies must not contain a null reference.", "pluginRegistration");
            if (pluginRegistration.ProbingPaths.Contains(null))
                throw new ArgumentException("The probing paths must not contain a null reference.", "pluginRegistration");

            return dataBox.Write(data =>
            {
                if (data.GetPluginById(pluginRegistration.PluginId) != null)
                    throw new ArgumentException(string.Format("There is already a plugin registered with id '{0}'.", pluginRegistration.PluginId), "pluginRegistration");

                List<IPluginDescriptor> pluginDependencies = new List<IPluginDescriptor>(pluginRegistration.PluginDependencies);

                foreach (IPluginDescriptor pluginDependency in pluginRegistration.PluginDependencies)
                {
                    if (data.GetPluginById(pluginDependency.PluginId) != pluginDependency)
                        throw new ArgumentException(
                            "One of the plugin dependencies does not belong to this registry.", "pluginRegistration");

                    foreach (IPluginDescriptor secondOrderPluginDependency in pluginDependency.PluginDependencies)
                    {
                        if (!pluginDependencies.Contains(secondOrderPluginDependency))
                            pluginDependencies.Add(secondOrderPluginDependency);
                    }
                }

                PluginDescriptor plugin = new PluginDescriptor(this, pluginRegistration, pluginDependencies);
                data.RegisterPlugin(plugin);
                return plugin;
            });
        }