君子博学而日参省乎己 则知明而行无过矣

博客园 首页 新随笔 联系 订阅 管理

solr dataimport 数据导入源码分析(五) 提到了contextimpl类调用DataImporter对象获取数据源 

 contextimpl.java

 private DataSource ds;

 

  

private DataImporter dataImporter;

   

@Override

  public DataSource getDataSource() {

    if (ds != nullreturn ds;

    if(entity == nullreturn  null;

    if (entity.dataSrc == null) {

      entity.dataSrc = dataImporter.getDataSourceInstance(entity, entity.dataSource, this);

    }

    if (entity.dataSrc != null && docBuilder != null && docBuilder.verboseDebug &&

             Context.FULL_DUMP.equals(currentProcess())) {

      //debug is not yet implemented properly for deltas

      entity.dataSrc = docBuilder.getDebugLogger().wrapDs(entity.dataSrc);

    }

    return entity.dataSrc;

  }

我们跟踪源码,在DataImporter.java里面调用了jdbcDataSource对象方法

参考solr dataimport 数据导入源码分析(二) DataImporter.java 源码

DataImporter.java  

 DataSource getDataSourceInstance(DataConfig.Entity key, String name, Context ctx) {

    Properties p = dataSourceProps.get(name);
    if (p == null)
      p = config.dataSources.get(name);
    if (p == null)
      p = dataSourceProps.get(null);// for default data source
    if (p == null)
      p = config.dataSources.get(null);
    if (p == null)  
      throw new DataImportHandlerException(SEVERE,
              "No dataSource :" + name + " available for entity :"
                      + key.name);
    String type = p.getProperty(TYPE);
    DataSource dataSrc = null;
    if (type == null) {
      dataSrc = new JdbcDataSource();
    } else {
      try {
        dataSrc = (DataSource) DocBuilder.loadClass(type, getCore()).newInstance();
      } catch (Exception e) {
        wrapAndThrow(SEVERE, e, "Invalid type for data source: " + type);
      }
    }
    try {
      Properties copyProps = new Properties();
      copyProps.putAll(p);
      Map<String, Object> map = ctx.getRequestParameters();
      if (map.containsKey("rows")) {
        int rows = Integer.parseInt((String) map.get("rows"));
        if (map.containsKey("start")) {
          rows += Integer.parseInt((String) map.get("start"));
        }
        copyProps.setProperty("maxRows", String.valueOf(rows));
      }
      dataSrc.init(ctx, copyProps);
    } catch (Exception e) {
      wrapAndThrow(SEVERE, e, "Failed to initialize DataSource: " + key.dataSource);
    }
    return dataSrc;
  }


posted on 2012-09-10 00:18  刺猬的温驯  阅读(579)  评论(0编辑  收藏  举报