Exemple #1
0
        /// <summary>
        /// Creates an instance of <see cref="IPreferenceStoreManager"/>. First, it is looked up if a custom implementation
        /// is provided by an extension point. If not, a default implementation is returned. Otherwise the contribution with
        /// the highest priority is taken. There is always just one implementation of <see cref="IPreferenceStoreManager"/>.
        /// </summary>
        /// <returns>
        /// An instance of <see cref="IPreferenceStoreManager"/> either provided by an extension point or the
        /// default implementation.
        /// </returns>
        private static IPreferenceStoreManager CreatePreferenceStoreManager()
        {
            ILog logWriter = LogManager.GetLog(typeof(PreferenceStoreManager));

            IConfigurationElement[] configurationElements = ExtensionService.Instance.GetConfigurationElements(PreferenceStoreManagerExtensionPointId);

            // Default
            if (configurationElements == null || configurationElements.Length == 0)
            {
                logWriter.Info("No preference store manager contributed. Using default implementation.");
                return(GetDefaultPreferenceStoreManager());
            }

            // Max priority
            IConfigurationElement maxPriorityElement = configurationElements.OrderByDescending(element => {
                string priorityValue = element["priority"];
                if (!int.TryParse(priorityValue, out int priority))
                {
                    return(0);
                }
                return(priority);
            }).First();

            // Grab the bundle
            IBundle providingBundle = ExtensionService.Instance.GetProvidingBundle(maxPriorityElement);

            logWriter.Debug($"Plug-in '{providingBundle}' provides a custom preference store manager implementation with highest priority.");

            // Grab the type
            string className = maxPriorityElement["class"];

            if (string.IsNullOrEmpty(className))
            {
                logWriter.Error($"Plug-in '{providingBundle}' provides a custom preference store manager implementation with an empty " +
                                "class attribute. Using default implementation.");
                return(GetDefaultPreferenceStoreManager());
            }

            // Allocate
            try {
                Type instanceType = TypeLoader.TypeForName(providingBundle, className);
                IPreferenceStoreManager preferenceStoreManager = instanceType.NewInstance <IPreferenceStoreManager>();
                logWriter.Info($"Preference store manager of type '{preferenceStoreManager.GetType()}' successfully installed");
                return(preferenceStoreManager);
            } catch (Exception ex) {
                logWriter.Error($"Error on installing preference store manager from plug-in '{providingBundle}' (Class '{className}').", ex);
                return(GetDefaultPreferenceStoreManager());
            }
        }
Exemple #2
0
 /// <summary>
 /// Returns the currently used <see cref="IPreferenceStoreManager"/>.
 /// </summary>
 /// <returns>Currently used <see cref="IPreferenceStoreManager"/></returns>
 public static IPreferenceStoreManager GetInstance()
 {
     return(_preferenceStoreManager ?? (_preferenceStoreManager = CreatePreferenceStoreManager()));
 }