/// <summary>
        /// Initializes the runtime.
        /// </summary>
        /// <remarks>
        /// <para>
        /// Loads plugins and initalizes the runtime component model.  The
        /// specifics of the system can be configured by editing the appropriate
        /// *.plugin files to register new components and facilities as required.
        /// </para>
        /// </remarks>
        /// <param name="setup">The runtime setup parameters.</param>
        /// <param name="logger">The logger to attach, or null if none.</param>
        /// <returns>An object that when disposed automatically calls <see cref="Shutdown" />.
        /// This is particularly useful in combination with the C# "using" statement
        /// or its equivalent.</returns>
        /// <exception cref="ArgumentNullException">Thrown if <paramref name="setup"/> is null.</exception>
        /// <exception cref="InvalidOperationException">Thrown if the runtime has already been initialized.</exception>
        public static IDisposable Initialize(RuntimeSetup setup, ILogger logger)
        {
            if (setup == null)
                throw new ArgumentNullException("setup");

            if (RuntimeAccessor.IsInitialized)
                throw new InvalidOperationException("The runtime has already been initialized.");

            var registry = new Registry();
            var assemblyLoader = new DefaultAssemblyLoader();
            var pluginLoader = new CachingPluginLoader();
            IRuntime runtime = new DefaultRuntime(registry, pluginLoader, assemblyLoader, setup); // TODO: make me configurable via setup
            if (logger != null)
                runtime.AddLogListener(logger);

            try
            {
                RuntimeAccessor.SetRuntime(runtime);

                runtime.Initialize();

                if (!UnhandledExceptionPolicy.HasReportUnhandledExceptionHandler)
                    UnhandledExceptionPolicy.ReportUnhandledException += HandleUnhandledExceptionNotification;
            }
            catch (Exception)
            {
                RuntimeAccessor.SetRuntime(null);
                throw;
            }

            return new AutoShutdown();
        }
 public ServiceDescriptor(Registry registry, ServiceRegistration serviceRegistration)
 {
     this.registry = registry;
     pluginDescriptor = (PluginDescriptor) serviceRegistration.Plugin;
     serviceId = serviceRegistration.ServiceId;
     serviceTypeName = serviceRegistration.ServiceTypeName;
     defaultComponentTypeName = serviceRegistration.DefaultComponentTypeName;
     traitsHandlerFactory = serviceRegistration.TraitsHandlerFactory;
 }
            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 ComponentDescriptor(Registry registry, ComponentRegistration componentRegistration)
 {
     this.registry = registry;
     pluginDescriptor = (PluginDescriptor) componentRegistration.Plugin;
     serviceDescriptor = (ServiceDescriptor) componentRegistration.Service;
     componentId = componentRegistration.ComponentId;
     componentTypeName = componentRegistration.ComponentTypeName ?? serviceDescriptor.DefaultComponentTypeName;
     componentProperties = componentRegistration.ComponentProperties.Copy().AsReadOnly();
     traitsProperties = componentRegistration.TraitsProperties.Copy().AsReadOnly();
     componentHandlerFactory = componentRegistration.ComponentHandlerFactory;
 }
 public void Execute(IProgressMonitor progressMonitor)
 {
     using (progressMonitor.BeginTask(Resources.UpdatePluginFolderCommand_Updating_list_of_plugins, 100))
     {
         var registry = new Registry();
         var pluginLoader = SetupPluginLoader(progressMonitor);
         var pluginCatalog = PopulateCatalog(progressMonitor, pluginLoader);
         ApplyCatalogToRegistry(progressMonitor, pluginCatalog, registry);
         PluginTreeModel.UpdatePluginList(registry);
         eventAggregator.Send(this, new PluginFolderUpdated(folder));
     }
 }
 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 RegistryService_WhenRegistrationNull_Throws()
            {
                var registry = new Registry();

                Assert.Throws<ArgumentNullException>(() => registry.RegisterService(null));
            }
            public void ResolvePlugin_WhenWellFormed_ReturnsPluginObject()
            {
                var registry = new Registry();
                var handlerFactory = MockRepository.GenerateMock<IHandlerFactory>();
                var plugin = registry.RegisterPlugin(new PluginRegistration("pluginId", new TypeName(typeof(DummyPlugin)), new DirectoryInfo(@"C:\"))
                {
                    PluginHandlerFactory = handlerFactory,
                    PluginProperties = { { "Name", "Value" } }
                });
                var handler = MockRepository.GenerateMock<IHandler>();
                var pluginInstance = new DummyPlugin();

                handlerFactory.Expect(x => x.CreateHandler(null, null, null, null))
                    .Constraints(Is.NotNull(), Is.Equal(typeof(IPlugin)), Is.Equal(typeof(DummyPlugin)), Is.Equal(new PropertySet() { { "Name", "Value" } }))
                    .Return(handler);
                handler.Expect(x => x.Activate()).Return(pluginInstance);

                var result = (DummyPlugin) plugin.ResolvePlugin();

                handlerFactory.VerifyAllExpectations();
                handler.VerifyAllExpectations();
                Assert.AreSame(pluginInstance, result);
            }
            public void ResolvePluginHandler_WhenComponentHasDependencyOnPluginDescriptor_OwnPluginDescriptorIsResolved()
            {
                var registry = new Registry();
                var handlerFactory = MockRepository.GenerateMock<IHandlerFactory>();
                var plugin = registry.RegisterPlugin(new PluginRegistration("pluginId", new TypeName(typeof(DummyPlugin)), new DirectoryInfo(@"C:\"))
                {
                    PluginHandlerFactory = handlerFactory,
                    PluginProperties = { { "Name", "Value" } },
                    TraitsProperties = { { "name", "pluginName" } }
                });
                var handler = MockRepository.GenerateStub<IHandler>();

                handlerFactory.Expect(x => x.CreateHandler(null, null, null, null))
                    .IgnoreArguments()
                    .Return(handler);

                plugin.ResolvePluginHandler();

                var args = handlerFactory.GetArgumentsForCallsMadeOn(x => x.CreateHandler(null, null, null, null), x => x.IgnoreArguments());
                var suppliedDependencyResolver = (IObjectDependencyResolver)args[0][0];

                var dependencyResolution = suppliedDependencyResolver.ResolveDependency("descriptor", typeof(IPluginDescriptor), null);
                Assert.AreSame(plugin, dependencyResolution.Value);
            }
            public void ResolvePluginType_WhenTypeIsInvalid_Throws()
            {
                var registry = new Registry();
                var plugin = registry.RegisterPlugin(new PluginRegistration("pluginId", new TypeName("InvalidPlugin, Assembly"), new DirectoryInfo(@"C:\")));

                var ex = Assert.Throws<RuntimeException>(() => plugin.ResolvePluginType());
                Assert.Contains(ex.Message, "Could not resolve the plugin type of plugin 'pluginId'.");
            }
            public void GetSearchPaths_WhenResourcePathIsAbsolute_ReturnsResourcePathItself()
            {
                var registry = new Registry();
                var plugin = registry.RegisterPlugin(new PluginRegistration("pluginId", new TypeName("InvalidPlugin, Assembly"), new DirectoryInfo(@"C:\")));

                IList<string> result = plugin.GetSearchPaths(@"C:\SomePath\resource.txt").ToList();

                Assert.AreElementsEqual(new[]
                {
                    @"C:\SomePath\resource.txt"
                }, result);
            }
            public void GetSearchPaths_WhenResourcePathIsEmpty_Throws()
            {
                var registry = new Registry();
                var plugin = registry.RegisterPlugin(new PluginRegistration("pluginId", new TypeName("InvalidPlugin, Assembly"), new DirectoryInfo(@"C:\")));

                Assert.Throws<ArgumentException>(() => plugin.GetSearchPaths(""));
            }
            public void FindByServiceTypeName_WhenComponentsNotRegistered_ReturnsEmptyList()
            {
                var registry = new Registry();
                var plugin = registry.RegisterPlugin(new PluginRegistration("pluginId", new TypeName("Plugin, Assembly"), new DirectoryInfo(@"C:\")));
                registry.RegisterService(new ServiceRegistration(plugin, "serviceId", new TypeName(typeof(DummyService))));

                var result = registry.Components.FindByServiceTypeName(new TypeName(typeof(DummyService)));

                Assert.IsEmpty(result);
            }
            public void GetByServiceTypeName_WhenServiceTypeNameIsNull_Throws()
            {
                var registry = new Registry();

                Assert.Throws<ArgumentNullException>(() => registry.Services.GetByServiceTypeName(null));
            }
            public void ServiceLocator_ReturnsServiceResourceLocator()
            {
                var registry = new Registry();

                Assert.IsInstanceOfType<RegistryServiceLocator>(registry.ServiceLocator);
            }
            public void FindByServiceTypeName_WhenComponentsRegistered_ReturnsComponentList()
            {
                var registry = new Registry();
                var plugin = registry.RegisterPlugin(new PluginRegistration("pluginId", new TypeName("Plugin, Assembly"), new DirectoryInfo(@"C:\")));
                var service = registry.RegisterService(new ServiceRegistration(plugin, "serviceId", new TypeName(typeof(DummyService))));
                var component1 = registry.RegisterComponent(new ComponentRegistration(plugin, service, "component1Id", new TypeName("Component1, Assembly")));
                var component2 = registry.RegisterComponent(new ComponentRegistration(plugin, service, "component2Id", new TypeName("Component2, Assembly")));

                var result = registry.Components.FindByServiceTypeName(new TypeName(typeof(DummyService)));

                Assert.AreElementsEqualIgnoringOrder(new[] { component1, component2 }, result);
            }
            public void GetByServiceTypeName_WhenServiceTypeNameIsRegistered_ReturnsService()
            {
                var registry = new Registry();
                var plugin = registry.RegisterPlugin(new PluginRegistration("pluginId", new TypeName("Plugin, Assembly"), new DirectoryInfo(@"C:\")));
                var service = registry.RegisterService(new ServiceRegistration(plugin, "serviceId", new TypeName(typeof(DummyService))));

                var result = registry.Services.GetByServiceTypeName(new TypeName(typeof(DummyService)));

                Assert.AreSame(service, result);
            }
            public void ServiceLocator_Always_ReturnsARegistryServiceLocatorForTheRegistry()
            {
                var registry = new Registry();

                IServiceLocator serviceLocator = registry.ServiceLocator;

                Assert.Multiple(() =>
                {
                    Assert.IsInstanceOfType<RegistryServiceLocator>(serviceLocator);
                    Assert.AreSame(registry, ((RegistryServiceLocator)serviceLocator).Registry);
                });
            }
            public void GetByServiceTypeName_WhenServiceTypeNameIsNotRegistered_ReturnsNull()
            {
                var registry = new Registry();

                var result = registry.Services.GetByServiceTypeName(new TypeName(typeof(DummyService)));

                Assert.IsNull(result);
            }
            public void GetSearchPaths_WhenResourcePathIsRelative_ReturnsBinAndProbingPathsWithResourcePathAppended()
            {
                var registry = new Registry();
                var plugin = registry.RegisterPlugin(new PluginRegistration("pluginId", new TypeName("InvalidPlugin, Assembly"), new DirectoryInfo(@"C:\"))
                {
                    ProbingPaths = { "ProbeA", "ProbeB" }
                });

                IList<string> result = plugin.GetSearchPaths("resource.txt").ToList();

                Assert.AreElementsEqual(new[]
                {
                    @"C:\resource.txt",
                    @"C:\bin\resource.txt",
                    @"C:\ProbeA\resource.txt",
                    @"C:\bin\ProbeA\resource.txt",
                    @"C:\ProbeB\resource.txt",
                    @"C:\bin\ProbeB\resource.txt"
                }, result);
            }
            public void Indexer_WhenComponentIdIsNull_Throws()
            {
                var registry = new Registry();

                Assert.Throws<ArgumentNullException>(() => { var x = registry.Components[null]; });
            }
            public void Indexer_WhenComponentIdIsRegistered_ReturnsComponent()
            {
                var registry = new Registry();
                var plugin = registry.RegisterPlugin(new PluginRegistration("pluginId", new TypeName("Plugin, Assembly"), new DirectoryInfo(@"C:\")));
                var service = registry.RegisterService(new ServiceRegistration(plugin, "serviceId", new TypeName("Service, Assembly")));
                var component = registry.RegisterComponent(new ComponentRegistration(plugin, service, "componentId", new TypeName("Component, Assembly")));

                var result = registry.Components["componentId"];

                Assert.AreSame(component, result);
            }
            public void Indexer_WhenComponentIdIsNotRegistered_ReturnsNull()
            {
                var registry = new Registry();

                var result = registry.Components["componentId"];

                Assert.IsNull(result);
            }
            public void ResolvePluginType_WhenTypeIsValid_ReturnsType()
            {
                var registry = new Registry();
                var plugin = registry.RegisterPlugin(new PluginRegistration("pluginId", new TypeName(typeof(DummyPlugin)), new DirectoryInfo(@"C:\")));

                var pluginType = plugin.ResolvePluginType();

                Assert.AreEqual(typeof(DummyPlugin), pluginType);
            }
            public void FindByServiceId_WhenServiceIdNotRegistered_ReturnsEmptyList()
            {
                var registry = new Registry();

                var result = registry.Components.FindByServiceId("serviceId");

                Assert.IsEmpty(result);
            }
            public void ResolveTraitsHandler_WhenHandlerFactoryFails_Throws()
            {
                var registry = new Registry();
                var handlerFactory = MockRepository.GenerateMock<IHandlerFactory>();
                var plugin = registry.RegisterPlugin(new PluginRegistration("pluginId", new TypeName(typeof(DummyPlugin)), new DirectoryInfo(@"C:\"))
                {
                    TraitsProperties = { { "Name", "Value" } }
                });

                handlerFactory.Expect(x => x.CreateHandler(null, null, null, null))
                    .Constraints(Is.NotNull(), Is.Equal(typeof(PluginTraits)), Is.Equal(typeof(PluginTraits)), Is.Equal(new PropertySet() { { "Name", "Value" } }))
                    .Throw(new Exception("Boom"));

                PluginDescriptor.RunWithInjectedTraitsHandlerFactoryMock(handlerFactory, () =>
                {
                    var ex = Assert.Throws<RuntimeException>(() => plugin.ResolveTraitsHandler());
                    Assert.Contains(ex.Message, "Could not resolve the traits handler of plugin 'pluginId'.");
                });

                handlerFactory.VerifyAllExpectations();
            }
            public void FindByServiceTypeName_WhenServiceTypeNameIsNull_Throws()
            {
                var registry = new Registry();

                Assert.Throws<ArgumentNullException>(() => registry.Components.FindByServiceTypeName(null));
            }
            public void ResolvePlugin_WhenActivationFails_Throws()
            {
                var registry = new Registry();
                var handlerFactory = MockRepository.GenerateMock<IHandlerFactory>();
                var plugin = registry.RegisterPlugin(new PluginRegistration("pluginId", new TypeName(typeof(DummyPlugin)), new DirectoryInfo(@"C:\"))
                {
                    PluginHandlerFactory = handlerFactory,
                    PluginProperties = { { "Name", "Value" } }
                });
                var handler = MockRepository.GenerateMock<IHandler>();

                handlerFactory.Expect(x => x.CreateHandler(null, null, null, null))
                    .Constraints(Is.NotNull(), Is.Equal(typeof(IPlugin)), Is.Equal(typeof(DummyPlugin)), Is.Equal(new PropertySet() { { "Name", "Value" } }))
                    .Return(handler);
                handler.Expect(x => x.Activate()).Throw(new InvalidOperationException("Boom"));

                var ex = Assert.Throws<RuntimeException>(() => plugin.ResolvePlugin());
                Assert.AreEqual("Could not resolve instance of plugin 'pluginId'.", ex.Message);
                handlerFactory.VerifyAllExpectations();
                handler.VerifyAllExpectations();
            }
            public void FindByServiceTypeName_WhenServiceTypeNameNotRegistered_ReturnsEmptyList()
            {
                var registry = new Registry();

                var result = registry.Components.FindByServiceTypeName(new TypeName(typeof(DummyService)));

                Assert.IsEmpty(result);
            }
            public void ResolvePluginTraits_WhenWellFormed_ReturnsPluginTraitsObject()
            {
                var registry = new Registry();
                var handlerFactory = MockRepository.GenerateMock<IHandlerFactory>();
                var plugin = registry.RegisterPlugin(new PluginRegistration("pluginId", new TypeName(typeof(DummyPlugin)), new DirectoryInfo(@"C:\"))
                {
                    TraitsProperties = { { "Name", "Value" } }
                });
                var handler = MockRepository.GenerateMock<IHandler>();
                var traitsInstance = new PluginTraits("name");

                handlerFactory.Expect(x => x.CreateHandler(null, null, null, null))
                    .Constraints(Is.NotNull(), Is.Equal(typeof(PluginTraits)), Is.Equal(typeof(PluginTraits)), Is.Equal(new PropertySet() { { "Name", "Value" } }))
                    .Return(handler);
                handler.Expect(x => x.Activate()).Return(traitsInstance);

                PluginDescriptor.RunWithInjectedTraitsHandlerFactoryMock(handlerFactory, () =>
                {
                    var result = plugin.ResolveTraits();

                    Assert.AreSame(traitsInstance, result);
                });

                handlerFactory.VerifyAllExpectations();
                handler.VerifyAllExpectations();
            }
Exemple #31
0
 public Components(Registry registry)
 {
     this.registry = registry;
 }