Exemple #1
0
        /// <summary>
        /// Initialise all the plugins.
        ///
        /// Every available plugin (added with <see cref="AddPlugin"/> or <see cref="LoadPlugins"/>)
        /// that is not marked as disabled will be initialised.
        /// </summary>
        public void InitialisePlugins()
        {
            MasterObjectTable mot = Locator.Current.GetService <MasterObjectTable>();

            lock (m_pluginsAvailable)
            {
                lock (m_pluginsEnabled)
                {
                    foreach (Guid uuid in m_pluginsAvailable.Keys)
                    {
                        // Create the IPluginHost for this plugin and enable it
                        PluginHost host = new PluginHost(m_pluginsAvailable[uuid]);
                        if (host.EnablePlugin())
                        {
                            this.Log().Debug("Enabled plugin {0} (ID = '{1}')", m_pluginsAvailable[uuid].GetType().Name, m_pluginsAvailable[uuid].UUID);
                            m_pluginsEnabled[uuid] = host;
                            mot.AddInstance(m_pluginsAvailable[uuid]);
                        }
                    }
                }
            }
        }
        /// <summary>
        /// Enable the plugin
        /// </summary>
        /// <returns></returns>
        public bool EnablePlugin()
        {
            // Add the plugin to the MOT on the assumption it will work
            MasterObjectTable mot = Locator.Current.GetService <MasterObjectTable>();

            mot.AddInstance(m_plugin);
            // If the plugin supports configuration we need to give it one before
            // initialisation
            IConfigurable configurable = m_plugin as IConfigurable;

            if (configurable != null)
            {
                IConfigurationDescription    description = mot.GetConfigurationDescription(m_plugin.UUID);
                IDictionary <string, object> values      = mot.GetConfiguration(m_plugin.UUID);
                Dictionary <string, string>  failures    = new Dictionary <string, string>();
                if (!configurable.ValidateConfiguration(description, values, failures))
                {
                    StringBuilder sb        = new StringBuilder();
                    string        separator = "";
                    foreach (KeyValuePair <string, string> message in failures)
                    {
                        sb.Append(separator);
                        separator = ", ";
                        sb.Append(String.Format("'{0}' - {1}", message.Key, message.Value));
                    }
                    this.Log().Error("One or more configuration values are not applicable - " + sb.ToString());
                    mot.RemoveInstance(m_plugin.UUID);
                    return(false);
                }
                try
                {
                    configurable.ApplyConfiguration(description, values);
                }
                catch (Exception ex)
                {
                    this.Log().Error("Unable to apply configuration to plugin - {0}.", ex.ToString());
                    mot.RemoveInstance(m_plugin.UUID);
                    return(false);
                }
            }
            // Now try and initialise it
            bool initialised = false;

            try
            {
                initialised = m_plugin.Initialise(this);
                if (initialised)
                {
                    this.Log().Info("Initialised plugin");
                }
                else
                {
                    this.Log().Error("Failed to initialise plugin.");
                }
            }
            catch (Exception ex)
            {
                this.Log().Error("Failed to initialise plugin - {0}", ex.Message);
            }
            if (!initialised)
            {
                mot.RemoveInstance(m_plugin.UUID);
            }
            return(initialised);
        }