Esempio n. 1
0
        /// <summary>
        /// Returns extension object
        /// </summary>
        /// <param name="name">
        /// Extension name
        /// </param>
        /// <returns>
        /// The Extension
        /// </returns>
        private static ManagedExtension DataStoreExtension(string name)
        {
            ManagedExtension ex = null;
            var xs         = new DataStore.ExtensionSettings(name);
            var serializer = new XmlSerializer(typeof(ManagedExtension));
            var o          = xs.GetSettings();

            if (o != null)
            {
                if (o.GetType().Name == "FileStream")
                {
                    Stream stm = (FileStream)o;
                    ex = (ManagedExtension)serializer.Deserialize(stm);
                    stm.Close();
                }
                else
                {
                    if (!string.IsNullOrEmpty((string)o))
                    {
                        using (var reader = new StringReader(o.ToString()))
                        {
                            ex = (ManagedExtension)serializer.Deserialize(reader);
                        }
                    }
                }
            }

            return(ex);
        }
Esempio n. 2
0
        /// <summary>
        /// Save individual extension to storage
        /// </summary>
        /// <param name="ext">
        /// The Extension
        /// </param>
        /// <returns>
        /// True if saved
        /// </returns>
        public static bool SaveToStorage(ManagedExtension ext)
        {
            var xs = new DataStore.ExtensionSettings(ext.Name);

            xs.SaveSettings(ext);
            return(true);
        }
Esempio n. 3
0
        /// <summary>
        /// Enabled / Disabled
        /// </summary>
        /// <param name="extensionName">
        /// Name of the extension.
        /// </param>
        /// <returns>
        /// True if enabled
        /// </returns>
        public static bool ExtensionEnabled(string extensionName)
        {
            LoadExtensions();
            ManagedExtension extension = GetExtension(extensionName);

            if (Blog.CurrentInstance.IsPrimary)
            {
                return(extension == null ? false : extension.Enabled);
            }
            else
            {
                if (extension != null)
                {
                    // if the extension is disabled at the primary blog, then
                    // it is automatically considered disabled for all child blogs.
                    if (!extension.Enabled)
                    {
                        return(false);
                    }

                    if (extension.Blogs != null && extension.Blogs.Contains(Blog.CurrentInstance.Id))
                    {
                        // when non-primary blog disables extension,
                        // this blog added to Blogs list for this extension
                        return(false);
                    }
                }
                return(true);
            }
        }
Esempio n. 4
0
        /// <summary>
        /// Enabled / Disabled
        /// </summary>
        /// <param name="extensionName">
        /// Name of the extension.
        /// </param>
        /// <returns>
        /// True if enabled
        /// </returns>
        public static bool ExtensionEnabled(string extensionName)
        {
            LoadExtensions();
            ManagedExtension extension = GetExtension(extensionName);

            return(extension == null ? false : extension.Enabled);
        }
Esempio n. 5
0
        /// <summary>
        /// Returns settings for specified extension
        /// </summary>
        /// <param name="extensionName">
        /// Extension Name
        /// </param>
        /// <param name="settingName">
        /// Settings Name
        /// </param>
        /// <returns>
        /// Settings object
        /// </returns>
        public static ExtensionSettings GetSettings(string extensionName, string settingName)
        {
            ManagedExtension extension = GetExtension(extensionName);

            if (extension == null)
            {
                return(null);
            }

            return(extension.Settings.Where(setting => setting != null && setting.Name == settingName).
                   FirstOrDefault());
        }
Esempio n. 6
0
        /// <summary>
        /// If extensions not in the cache will load
        ///     from the XML file. If file not exists
        ///     will load from assembly using reflection
        /// </summary>
        private static void LoadExtensions()
        {
            if (HttpRuntime.Cache["Extensions"] != null &&
                ((Dictionary<string, ManagedExtension>)HttpRuntime.Cache["Extensions"]).Count != 0)
            {
                return;
            }

            var codeAssemblies = Utils.CodeAssemblies();

            var meta = DataStoreExtension("MetaExtension");
            if (meta == null)
            {
                extensions.Add("MetaExtension", new ManagedExtension("MetaExtension", "1.0", "Meta extension", "BlogEngine.net"));
            }
            else
            {
				if (!extensions.ContainsKey("MetaExtension"))
                {
                    extensions.Add("MetaExtension", meta);
                }
            }

            foreach (Assembly a in codeAssemblies)
            {
                var types = a.GetTypes();
                foreach (var type in types)
                {
                    var attributes = type.GetCustomAttributes(typeof(ExtensionAttribute), false);
                    foreach (var xa in attributes.Cast<ExtensionAttribute>())
                    {
                        // try to load from storage
                        try
                        {
                            var x = DataStoreExtension(type.Name);

                            // if nothing, crete new extension
                            if (x == null)
                            {
                                x = new ManagedExtension(type.Name, xa.Version, xa.Description, xa.Author);
                                newExtensions.Add(type.Name);
                                SaveToStorage(x);
                            }
                            else
                            {
                                // update attributes from assembly
                                x.Version = xa.Version;
                                x.Description = xa.Description;
                                x.Author = xa.Author;

                                if (x.Priority == 0)
                                {
                                    x.Priority = xa.Priority;
                                }
                            }
							if (!extensions.ContainsKey(x.Name))
								extensions.Add(x.Name, x);
                        }
                        catch (Exception e)
                        {
                            Utils.Log(string.Format("Can not load {0}: {1}", type.Name, e.Message));
                        }
                    }
                }
            }

            // SaveToStorage();
            SaveToCache();
        }
Esempio n. 7
0
 /// <summary>
 /// Save individual extension to storage
 /// </summary>
 /// <param name="ext">
 /// The Extension
 /// </param>
 /// <returns>
 /// True if saved
 /// </returns>
 public static bool SaveToStorage(ManagedExtension ext)
 {
     var xs = new DataStore.ExtensionSettings(ext.Name);
     xs.SaveSettings(ext);
     return true;
 }
Esempio n. 8
0
        /// <summary>
        /// If extensions not in the cache will load
        ///     from the XML file. If file not exists
        ///     will load from assembly using reflection
        /// </summary>
        private static void LoadExtensions()
        {
            if (HttpRuntime.Cache["Extensions"] != null &&
                ((Dictionary <string, ManagedExtension>)HttpRuntime.Cache["Extensions"]).Count != 0)
            {
                return;
            }

            var codeAssemblies = Utils.CodeAssemblies();

            var meta = DataStoreExtension("MetaExtension");

            if (meta == null)
            {
                extensions.Add("MetaExtension", new ManagedExtension("MetaExtension", "1.0", "Meta extension", "BlogEngine.net"));
            }
            else
            {
                if (!extensions.ContainsKey("MetaExtension"))
                {
                    extensions.Add("MetaExtension", meta);
                }
            }

            foreach (Assembly a in codeAssemblies)
            {
                var types = a.GetTypes();
                foreach (var type in types)
                {
                    var attributes = type.GetCustomAttributes(typeof(ExtensionAttribute), false);
                    foreach (var xa in attributes.Cast <ExtensionAttribute>())
                    {
                        // try to load from storage
                        try
                        {
                            var x = DataStoreExtension(type.Name);

                            // if nothing, crete new extension
                            if (x == null)
                            {
                                x = new ManagedExtension(type.Name, xa.Version, xa.Description, xa.Author);
                                newExtensions.Add(type.Name);
                                SaveToStorage(x);
                            }
                            else
                            {
                                // update attributes from assembly
                                x.Version     = xa.Version;
                                x.Description = xa.Description;
                                x.Author      = xa.Author;

                                if (x.Priority == 0)
                                {
                                    x.Priority = xa.Priority;
                                }
                            }
                            if (!extensions.ContainsKey(x.Name))
                            {
                                extensions.Add(x.Name, x);
                            }
                        }
                        catch (Exception e)
                        {
                            Utils.Log(string.Format("Can not load {0}: {1}", type.Name, e.Message));
                        }
                    }
                }
            }

            // SaveToStorage();
            SaveToCache();
        }
 /// <summary>
 /// Save individual extension to storage
 /// </summary>
 /// <param name="ext">
 /// The Extension
 /// </param>
 /// <returns>
 /// True if saved
 /// </returns>
 public static bool SaveToStorage(ManagedExtension ext)
 {
     return true;
 }