zoukankan      html  css  js  c++  java
  • mojoPortalprovider模式学习之1.1 IndexBuilderConfiguration


    使用provider模式的第三步为 建立Provider Configuration

    在mojoPortal中索引provider中相关类为IndexBuilderConfiguration

    这个类的主要作用是读取配置文件中所有相关provider的信息 。

    代码如下

      1   public class IndexBuilderConfiguration
      2    {
      3        private static readonly ILog log
      4            = LogManager.GetLogger(typeof(IndexBuilderConfiguration));
      5
      6
      7        private ProviderSettingsCollection providerSettingsCollection 
      8            = new ProviderSettingsCollection();
      9
     10        public ProviderSettingsCollection Providers
     11        {
     12            get return providerSettingsCollection; }
     13        }

     14
     15        public static IndexBuilderConfiguration GetConfig()
     16        {
     17            try
     18            {
     19
     20                //判断缓存中是否已经存在IndexBuilder的配置文件,如果存在则返回缓存中的文件
     21                if (
     22                    (HttpRuntime.Cache["mojoIndexBuilderConfiguration"!= null)
     23                    && (HttpRuntime.Cache["mojoIndexBuilderConfiguration"is IndexBuilderConfiguration)
     24                )
     25                {
     26                    return (IndexBuilderConfiguration)HttpRuntime.Cache["mojoIndexBuilderConfiguration"];
     27                }

     28
     29                //建立同本类相同的类型。
     30                IndexBuilderConfiguration indexBuilderConfig
     31                    = new IndexBuilderConfiguration();
     32
     33                //指定配置文件所在的路径
     34                String configFolderName = "~/Setup/indexbuilderconfig/";
     35
     36                string pathToConfigFolder
     37                    = HttpContext.Current.Server.MapPath(configFolderName);
     38
     39
     40                if (!Directory.Exists(pathToConfigFolder)) return indexBuilderConfig;
     41
     42                DirectoryInfo directoryInfo
     43                    = new DirectoryInfo(pathToConfigFolder);
     44
     45                //所有配置文件列表
     46                FileInfo[] configFiles = directoryInfo.GetFiles("*.config");
     47
     48                //把所有配置文件中的相关信息加到当前类的ProviderSettingCollection集合中
     49                foreach (FileInfo fileInfo in configFiles)
     50                {
     51                    XmlDocument configXml = new XmlDocument();
     52                    configXml.Load(fileInfo.FullName);
     53                    //具体添加信息到ProviderSettingCollection集合中的方法
     54                    indexBuilderConfig.LoadValuesFromConfigurationXml(configXml.DocumentElement);
     55
     56                }

     57
     58                //下面几行的意思应该是如果web.config文件变动,更新缓存
     59                AggregateCacheDependency aggregateCacheDependency
     60                    = new AggregateCacheDependency();
     61
     62                string pathToWebConfig
     63                    = HttpContext.Current.Server.MapPath("~/Web.config");
     64
     65                aggregateCacheDependency.Add(new CacheDependency(pathToWebConfig));
     66
     67                //把当前类加入到缓存中
     68                System.Web.HttpRuntime.Cache.Insert(
     69                    "mojoIndexBuilderConfiguration",
     70                    indexBuilderConfig,
     71                    aggregateCacheDependency,
     72                    DateTime.Now.AddYears(1),
     73                    TimeSpan.Zero,
     74                    System.Web.Caching.CacheItemPriority.Default,
     75                    null);
     76
     77                //返回当前类的信息。
     78
     79                return (IndexBuilderConfiguration)HttpRuntime.Cache["mojoIndexBuilderConfiguration"];
     80
     81            }

     82            catch (HttpException ex)
     83            {
     84                log.Error(ex);
     85
     86            }

     87            catch (System.Xml.XmlException ex)
     88            {
     89                log.Error(ex);
     90
     91            }

     92            catch (ArgumentException ex)
     93            {
     94                log.Error(ex);
     95
     96            }

     97            catch (NullReferenceException ex)
     98            {
     99                log.Error(ex);
    100
    101            }

    102
    103            return null;
    104
    105            
    106        }

    107
    108
    109        //从Xml文件中获取信息
    110        public void LoadValuesFromConfigurationXml(XmlNode node)
    111        {
    112            foreach (XmlNode child in node.ChildNodes)
    113            {
    114                if (child.Name == "providers")
    115                {
    116                    foreach (XmlNode providerNode in child.ChildNodes)
    117                    {
    118                        //在providers的子结点中,如果类型为add,并且name,type属性不为空,则添加到ProviderSettingsCollection集合中
    119                        if (
    120                            (providerNode.NodeType == XmlNodeType.Element)
    121                            && (providerNode.Name == "add")
    122                            )
    123                        {
    124                            if (
    125                                (providerNode.Attributes["name"!= null)
    126                                && (providerNode.Attributes["type"!= null)
    127                                )
    128                            {
    129                                ProviderSettings providerSettings
    130                                    = new ProviderSettings(
    131                                    providerNode.Attributes["name"].Value,
    132                                    providerNode.Attributes["type"].Value);
    133
    134                                providerSettingsCollection.Add(providerSettings);
    135                            }

    136
    137                        }

    138                    }

    139
    140                }

    141            }

    142        }

    143
    144
    145    }

    配置文件实例如下:

    <?xml version="1.0" encoding="utf-8" ?>
    <IndexBuilderProviders>
        
    <providers>

            
    <add name="BlogIndexBuilderProvider"
                 type
    ="mojoPortal.Business.WebHelpers.BlogIndexBuilderProvider, mojoPortal.Business.WebHelpers"
                 description
    ="An IndexBuilder to index blog content for Lucene.NET Search Engine" />

            
        
    </providers>
    </IndexBuilderProviders>

    <?xml version="1.0" encoding="utf-8" ?>
    <IndexBuilderProviders>
        
    <providers>

            
    <add name="GalleryImageIndexBuilderProvider"
                 type
    ="mojoPortal.Business.WebHelpers.GalleryImageIndexBuilderProvider, mojoPortal.Business.WebHelpers"
                 description
    ="An IndexBuilder to index Gallery content for Lucene.NET Search Engine" />

            
        
    </providers>
    </IndexBuilderProviders>

    <?xml version="1.0" encoding="utf-8" ?>
    <IndexBuilderProviders>
        
    <providers>

            
    <add name="HtmlContentIndexBuilderProvider"
                 type
    ="mojoPortal.Business.WebHelpers.HtmlContentIndexBuilderProvider, mojoPortal.Business.WebHelpers"
                 description
    ="An IndexBuilder to index Html module content for Lucene.NET Search Engine" />

            
        
    </providers>
    </IndexBuilderProviders>
  • 相关阅读:
    关于Linux内核版本
    什么是操作系统?
    进程三种基本状态
    Repo实践指南
    在TortoiseSVN/TortoiseGit中使用BeyondCompare进行差异对比
    Redis安装
    Outlook配置163邮箱
    Nginx安装
    MySQL字符串操作函数
    Java各种反射性能对比
  • 原文地址:https://www.cnblogs.com/wenjie/p/1167514.html
Copyright © 2011-2022 走看看