`
damoqingquan
  • 浏览: 47812 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

Solr1.3的启动过程分析二

阅读更多

这里讲讲CoreContainer的load(String dir, File configFile )方法所做的事情,也就是如何根据主目录下的solr.xml配置文件的数据以及主目录来对每个ScoreCore进行初始设置的,这些工作属于服务器启 动的一部分。
     先来说说参数吧,配置文件对象直接赋予CoreContainer的configFile 属性,而主目录路径dir则是用来构建一个Solr资源加载器(SolrResourceLoader),将该加载器作为当前CoreContainer 的loader属性。代码如下。
    ///////////
    this.configFile = configFile;
    this.loader = new SolrResourceLoader(dir);
    //////////

    SolrResourceLoader是管理当前目录下面的资源的类,这些资源包括目录名称,索引数据目录,类等等。
   
    下面通过这个语句获得配置文件的内容流。
     //////
     FileInputStream cfgis = new FileInputStream(configFile);
     //////
  
     获得流以后,所做的工作概括来说就是读取配置文件中的类容并设置相关的属性,我们来具体看一看。
    
     Config cfg = new Config(loader, null, cfgis, null);这句代码,我们知道返回一个Confige对象,该对象的本质是什么?构造函数的参数又是什么意思呢?
    先看看构造函数的参数说明:
    //////
   

/* Font definitions */html { font-family: 'Tahoma',sans-serif; font-size: 8pt; font-style: normal; font-weight: normal; }body, h4, h5, h6, p, table, td, caption, th, ul, ol, dl, li, dd, dt { font-size: 1em; }pre { font-family: monospace; }h1 { font-size: 1.8em; } h2 { font-size: 1.2em; }h3 { font-size: 1.1em; }/* Margins */h1 { margin-top: 0.3em; margin-bottom: 0.04em } h2 { margin-top: 2em; margin-bottom: 0.25em }h3 { margin-top: 1.7em; margin-bottom: 0.25em }h4 { margin-top: 2em; margin-bottom: 0.3em }h5 { margin-top: 0px; margin-bottom: 0px }p { margin-top: 1em; margin-bottom: 1em }pre { margin-left: 0.6em }ul { margin-top: 0px; margin-bottom: 1em; }li { margin-top: 0px; margin-bottom: 0px; } li p { margin-top: 0px; margin-bottom: 0px; } ol { margin-top: 0px; margin-bottom: 1em; }dl { margin-top: 0px; margin-bottom: 1em; }dt { margin-top: 0px; margin-bottom: 0px; font-weight: bold; }dd { margin-top: 0px; margin-bottom: 0px; }/* Styles and colors */a:link { color: #0000FF; }a:hover { color: #000080; }a:visited { text-decoration: underline; }h4 { font-style: italic; }strong { font-weight: bold; }em { font-style: italic; }var { font-style: italic; }th { font-weight: bold; }

    Builds a config:

    Note that the 'name' parameter is used to obtain a valid input stream if no valid one is provided through 'is'. If no valid stream is provided, a valid      SolrResourceLoader instance should be provided through 'loader' so the resource can be opened (@see SolrResourceLoader#openResource); if no SolrResourceLoader instance is provided, a default one will be created.

 

    Consider passing a non-null 'name' parameter in all use-cases since it is used for logging & exception reporting.

 

    Parameters:
loader the resource loader used to obtain an input stream if 'is' is null
name the resource name used if the input stream 'is' is null
is the resource as a stream
prefix an optional prefix that will be preprended to all non-absolute xpath expressions
    Throws:
javax.xml.parsers.ParserConfigurationException
java.io.IOException
org.xml.sax.SAXException
    public Config(SolrResourceLoader loader, String name, InputStream is, String prefix) ......
  //////
    这段话的意思是,name参数存在的意义是在is不能提供正确的输入流的情况下发挥作用以获得正确的流。is为空,那么将一个 SolrResourceLoader的实例通过loader来传递以确保能打开资源,如果没有提供一个 SolrResourceLoader的实例,那么会建立一个默认的。name参数最好为非空,以便记录日志。成功加载配置文件solr.xml之后,下面的代码不过是读取配置文件中的内容来设置一些属性而已。

 

    //////

      persistent = cfg.getBool( "solr/@persistent", false );
      libDir     = cfg.get(     "solr/@sharedLib", null);
      adminPath  = cfg.get(     "solr/cores/@adminPath", null );
      managementPath  = cfg.get("solr/cores/@managementPath", null );

    //////

     通常上面几个属性的值如下:

    persistent:false
    libDirnull
    adminPath:/admin/cores
    managementPathnull

    下面这段代码是看solr.xml配置文件中是否指定了一个lib目录,如果有则加载之。

    //////

    if (libDir != null) {
        // relative dir to conf
        File f = new File(dir, libDir);
        libDir = f.getPath();
        log.info( "loading shared library: "+f.getAbsolutePath() );
        libLoader = SolrResourceLoader.createClassLoader(f, null);
      }

    //////

    下面的代码返回管理处理器

    //////

 if( adminPath != null ) {
        coreAdminHandler = this.createMultiCoreHandler();
      }
    //////

    下面的代码读取solr.xml文件中solr节点,如果有多个只返回第一个,其它的都忽略掉。

    //////

   containerProperties = readProperties(cfg, ((NodeList) cfg.evaluate("solr", XPathConstants.NODESET)).item(0));
    //////

   下面的代码获得xpath为solr/cores/core的节点,这里可能有多个节点,因此是一个NodeList类型。

    //////

 NodeList nodes = (NodeList)cfg.evaluate("solr/cores/core", XPathConstants.NODESET);

    //////

    下面的代码就是通过一个for循环来处理nodes中的信息,根据这里的信息来建立每个核(对应每个库),例如如果我的solr.xml中有多个core节点,并且主目录下有多个与这些节点对应的库的话,那么这里就是针对每一个库的初始化处理。

   具体细节请看Solr1.3的启动过程分析三

敬请关注:http://www.lucas.gd.cn / 之Solr板块
----------------------------------------
原创文章:敬请著名出处http://www.lucas.gd.cn
作者:宋永维
email:lucas.song.cn@gmail.com

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics