コード例 #1
0
ファイル: PluginService.cs プロジェクト: astorch/motoi
        /// <summary>
        /// Activates the given <paramref name="plugin"/>. Activating a plug-in means to notify the corresponding <see cref="IPluginActivator"/> of
        /// the plug-in. If there isn't any, the activation will be done silently. An activated plug-in will run a state transition from
        /// <see cref="EPluginState.Provided"/> to <see cref="EPluginState.Activated"/>. An already activated plug-in cannot be activated again.
        /// To activate a plug-in its dependencies must be resolvable, otherwise the activation will fail. If the given <paramref name="plugin"/>
        /// is NULL, nothing will happen.
        /// </summary>
        /// <param name="plugin">Plug-in to activate</param>
        /// <exception cref="PluginActivationException">If an error occurs</exception>
        public void ActivatePlugin(IPluginInfo plugin)
        {
            if (plugin == null)
            {
                return;
            }
            if (plugin.State != EPluginState.Provided)
            {
                return;
            }

            bool isProvided = ProvidedPlugins.Contains(plugin);

            if (!isProvided)
            {
                throw new PluginActivationException($"Plug-in '{plugin}' cannot be started due to missing dependencies");
            }

            IPluginSignature pluginSignature         = plugin.Signature;
            IBundle          bundle                  = plugin.Bundle;
            string           pluginActivatorTypeName = pluginSignature.PluginActivatorType;

            pluginActivatorTypeName = pluginActivatorTypeName.Replace("/", ".");

            if (!string.IsNullOrEmpty(pluginActivatorTypeName))
            {
                string           fullyQualifiedActivatorTypeName = string.Format("{0}.{1},{0}", bundle.Name, pluginActivatorTypeName);
                IPluginActivator pluginActivator;

                // Try to create an instance of
                try {
                    Type activatorType = Type.GetType(fullyQualifiedActivatorTypeName, true);
                    pluginActivator = activatorType.NewInstance <IPluginActivator>();
                    ((PluginSignatureImpl)pluginSignature).PluginActivator = pluginActivator;
                } catch (Exception ex) {
                    _log.Error($"Error on creating instance of '{fullyQualifiedActivatorTypeName}'", ex);
                    throw new PluginActivationException($"Error on creating an instance of '{plugin}'", ex);
                }

                try {
                    pluginActivator.OnActivate();
                } catch (Exception ex) {
                    _log.Error($"Error on activating plug-in '{plugin}'", ex);
                    throw new PluginActivationException($"Error on activating plug-in '{plugin}'", ex);
                }
            }
            else
            {
                _log.Debug($"Plug-in '{plugin}' does not have a plug-in activator. It is activated silently.");
            }

            // Update state model
            SetPluginState(plugin, EPluginState.Activated);
            _activatedPlugins.AddLast(plugin);
        }
コード例 #2
0
 /// <summary> Creates a new instance for the given signature. </summary>
 /// <param name="bundle">Bundle which provides the plug-in</param>
 /// <param name="signature">Plug-in signature</param>
 /// <returns>New instance of <see cref="IPluginInfo"/></returns>
 public IPluginInfo CreatePluginInfo(IBundle bundle, IPluginSignature signature)
 {
     return(new PluginInfoImpl {
         State = EPluginState.Found, Signature = signature, Bundle = bundle
     });
 }