/// <summary>
        /// Registers a local RegistrationCallback
        /// </summary>
        /// <param name="unknownParameterCallback">the callback to use fo local object-requests</param>
        /// <returns>a value indicating whether the callback was registered or ignored due to an already registered callback</returns>
        private static bool RegisterLocalCallback(UnknownConstructorParameterEventHandler unknownParameterCallback)
        {
            if (!requestObjOnLoad.IsValueCreated || requestObjOnLoad.Value == null)
            {
                requestObjOnLoad.Value = unknownParameterCallback;
                return(true);
            }

            return(false);
        }
 /// <summary>
 /// Initializes a singleton plugin
 /// </summary>
 /// <param name="uniqueName">the unique name of the plugin that is being initialized</param>
 /// <param name="constructorString">the constructor of the demanded plugin</param>
 /// <param name="buffer">indicates whether to buffer the requested item</param>
 /// <param name="unknownParameterCallback">a callback that is used to resolve unkown constructor arguments</param>
 /// <returns>a value indicating whether to buffer the item on the local factory or not</returns>
 internal static IPlugin InitializeSingletonPlugin(string uniqueName, string constructorString, bool buffer, UnknownConstructorParameterEventHandler unknownParameterCallback)
 {
     CheckInitialized();
     lock (initializationLock)
     {
         bool registered = RegisterLocalCallback(unknownParameterCallback);
         try
         {
             return(singletonFactory.LoadPlugin <IPlugin>(uniqueName, constructorString, buffer));
         }
         finally
         {
             if (registered)
             {
                 UnregisterLocalCallback();
             }
         }
     }
 }
Exemple #3
0
        /// <summary>
        /// Initializes a new i PluginFactory instance
        /// </summary>
        /// <returns>a Factory that can be used to load further plugins</returns>
        private PluginFactory CreateFactory()
        {
            var retVal = new PluginFactory();

            LogEnvironment.OpenRegistrationTicket(retVal);
            retVal.AllowFactoryParameter = true;
            retVal.RegisterObject(Global.ServiceProviderName, serviceProvider);
            UnknownConstructorParameterEventHandler handler = (sender, args) =>
            {
                PluginFactory       pi = (PluginFactory)sender;
                IWebPluginsSelector availablePlugins = pluginProvider;
                var globalProvider            = serviceProvider.GetService <IGlobalSettingsProvider>();
                var tenantProvider            = serviceProvider.GetService <IScopedSettingsProvider>();
                var preInitializationSequence = tenantProvider?.GetJsonSetting($"PreInitSequenceFor{args.RequestedName}")
                                                ?? globalProvider?.GetJsonSetting($"PreInitSequenceFor{args.RequestedName}");
                var postInitializationSequence = tenantProvider?.GetJsonSetting($"PostInitSequenceFor{args.RequestedName}")
                                                 ?? globalProvider?.GetJsonSetting($"PostInitSequenceFor{args.RequestedName}");
                var       preInitSequence  = DeserializeInitArray(preInitializationSequence);
                var       postInitSequence = DeserializeInitArray(postInitializationSequence);
                WebPlugin plugin           =
                    availablePlugins.GetPlugin(args.RequestedName);
                if (plugin != null)
                {
                    if (serviceProvider.VerifyUserPermissions(new [] { args.RequestedName }, true))
                    {
                        if (preInitSequence.Length != 0)
                        {
                            foreach (var s in preInitSequence)
                            {
                                var tmp = pi[s, true];
                            }
                        }

                        if (!string.IsNullOrEmpty(plugin.Constructor))
                        {
                            args.Value   = pi.LoadPlugin <IPlugin>(plugin.UniqueName, plugin.Constructor);
                            args.Handled = true;
                        }

                        if (postInitSequence.Length != 0)
                        {
                            foreach (var s in postInitSequence)
                            {
                                var tmp = pi[s, true];
                            }
                        }
                    }
                }
                else
                {
                    var tmp = factoryOptions?.GetDependency(args.RequestedName, serviceProvider);
                    args.Handled = tmp != null;
                    args.Value   = tmp;
                }
            };

            void Initializer(object sender, PluginInitializedEventArgs args)
            {
                PluginLoadInterceptHelper.RunInterceptors(retVal, args.Plugin);
            }

            void Finalizer(object sender, EventArgs e)
            {
                LogEnvironment.DisposeRegistrationTicket(sender);
                var pi = (PluginFactory)sender;
                var dp = (IServiceProvider)pi.GetRegisteredObject(Global.ServiceProviderName);

                if (dp != null)
                {
                    factory = null;
                }
                pi.Disposed -= Finalizer;
                pi.UnknownConstructorParameter -= handler;
                pi.PluginInitialized           -= Initializer;
                pi.ImplementGenericType        -= Implementer;
            }

            void Implementer(object sender, ImplementGenericTypeEventArgs args)
            {
                PluginFactory       pi = (PluginFactory)sender;
                IWebPluginsSelector availablePlugins = pluginProvider;
                var impl = availablePlugins.GetGenericParameters(args.PluginUniqueName);

                if (impl != null)
                {
                    var dic         = new Dictionary <string, object>();
                    var assignments = (from t in args.GenericTypes
                                       join a in impl on t.GenericTypeName equals a.GenericTypeName
                                       select new { Arg = t, Type = a.TypeExpression });
                    foreach (var item in assignments)
                    {
                        item.Arg.TypeResult = (Type)ExpressionParser.Parse(item.Type.ApplyFormat(args), dic);
                    }

                    args.Handled = true;
                }
            }

            retVal.UnknownConstructorParameter += handler;
            retVal.PluginInitialized           += Initializer;
            retVal.Disposed             += Finalizer;
            retVal.ImplementGenericType += Implementer;
            return(retVal);
        }