Apache databaseConfiguration不适用于MapConfiguration

时间:2015-05-13 14:42:26

标签: database apache properties configuration

我正在尝试将mapConfiguration(带有属性的文件)与databaseConfiguration结合使用。但是,只能查找文件属性,而不能查找数据库属性。我做错了什么?

public class MapConfigProvider extends ConfigurationProvider {
    Map properties = null;
    public MapConfigProvider(Map inProps) {
        super(MapConfiguration.class);
        properties = inProps;
    }

    @Override
    public AbstractConfiguration getConfiguration(ConfigurationDeclaration decl) throws Exception {
        MapConfiguration mapConfig = new MapConfiguration(properties);
        return mapConfig;
  }
}
public class DatabaseConfigurationProvider extends ConfigurationProvider {
    private DatabaseConfigDef dbConfigDef;

  public DatabaseConfigurationProvider(DatabaseConfigDef databaseConfigDef)
  {
    super(DatabaseConfiguration.class);
    this.dbConfigDef = databaseConfigDef;
  }

  public DatabaseConfigurationProvider() {
    super();
  }

  @Override
  public AbstractConfiguration getConfiguration(ConfigurationDeclaration decl) throws Exception {
        DataSource ds = DataSourceFactory.getInstance().getDataSource(DATASOURCE.SEMS);
        return new DatabaseConfiguration(ds, "CSW_TABLE",  "PROP_COL", "VALUE_COL"); //String values sought from databaseConfigDef
  }
}
private static final Map<String, Object> propertiesMap= new HashMap<String, Object>();

DefaultConfigurationBuilder builder = new DefaultConfigurationBuilder(propFileUrl);

builder.addConfigurationProvider("filebased", new MapConfigProvider(propertiesMap));

builder.addConfigurationProvider("ctkdatabase", new DatabaseConfigurationProvider(databaseConfigDef));

CombinedConfiguration combinedConfig = builder.getConfiguration(true);

String prop1 = combinedConfig.getString("WCM_WEBSERVICE_PORT");  //From DB, gets null
String prop2 = combinedConfig.getString("temp"); //From file, fetches good
LOGGER.info(prop1 + prop2); 

我做错了什么?请帮忙。

1 个答案:

答案 0 :(得分:0)

1)在propFileUrl:mainconfig.xml中,我必须指定一个

    <configuration>
  <header>
    <result delimiterParsingDisabled="true" forceReloadCheck="false">
        <nodeCombiner config-class="org.apache.commons.configuration.tree.OverrideCombiner" />
        <expressionEngine config-class="org.apache.commons.configuration.tree.xpath.XPathExpressionEngine" />
    </result>
  </header>

  <system /> <!-- load system properties into config -->

  <properties fileName="common/datasources.properties" />
  <properties fileName="application.properties" />

  <ctkdatabase jndi="jdbc/shaDs" table="TABLE_APPL_PROPERTIES" keyColumn="PROP_NAME" valueColumn="PROP_VALUE"/>

  <!--  load host's config file -->
  <xml fileName="app/cluster/datasources.xml" />
</configuration>

2)创建一个新的getter / setter到&#34; jndi&#34; DatabaseConfigDef类中的变量。

3)并将getConfiguration()更改为:

 @Override
 public AbstractConfiguration getConfiguration(ConfigurationDeclaration decl) throws Exception {
   DatabaseConfigDef def = (DatabaseConfigDef)createBean(DatabaseConfigDef.class, decl, null);
   Context env = (Context) new InitialContext().lookup("java:comp/env"); 
   DataSource ds = (DataSource)env.lookup((def.getJndi()));
   return new DatabaseConfiguration(ds, def.getTable(), def.getKeyColumn(), def.getValueColumn());
}