Skip to content
yli2 edited this page Jun 13, 2013 · 116 revisions

Overview

Read this document if you need to

  • customize data source configurations, for example, change database settings;
  • write Java applications using the Wikapidia configuration API;
  • extend the Wikapidia library.

Please start from the next section if you are new to Wikapidia configuration.

Customizing Your Wikapidia Configuration

This section explains how to customize data source configurations without changing/writing Java codes.

Basics

  • Whenever a program loads configuration, it loads the default configuration file /wikapidia-core/src/main/resources/reference.conf.
  • Generally, you do NOT need to edit the reference.conf file.
  • The configuration file formats in a HOCON tree like this:
// reference.conf
dao : {
    dataSource : {
        h2db : {
           driver : org.h2.Driver
           url: "jdbc:h2:db/h2"
           username : sa
           password : ""
        }
        sql : {
            usr : ta
            password : ""
        }
    }
    localPage : {
        default : sql
        sql : {
            type : sql
            dataSource : h2db
        }
        live : {}
    }
    localLink : {
        default : sql
        sql : {
            type : sql
            dataSource : h2db
        }
        live : {}
    }
    rawPage : {
        default : sql
        sql : {
            type : sql
            dataSource : h2db
            localPageDao : sql
        }
        live : {}
    }
}

This file specifies configurations for four data source providers. Take dataSource provider as an example, it specifies configurations for two kinds of databases, h2 and sql.
(Please follow the links if you are not familiar with this JSON-like syntax or wish to learn more about the typesafe library or HOCON Syntax.)

Override configuration files

  • Inheritance of configuration takes the latest object value of duplicate fields, thus the following code overrides the value of the url field of dataSource provider's h2 database. It preserves all other default configurations.
// Overriding file myConf1.conf
dao.dataSource.h2db.url : "jdbc:h2:db/foo"
  • To run a program with part of the default configurations overridden by myConf1.conf, do
$ ProgramCommand -c /directory/myConf1.conf

Extra notes

  • You can create multiple configuration files and save them at convenient directories.
  • The configuration files can be saved with flexible file types, .conf, .txt, etc.

Use the Wikapidia Configuration API

Please read this section if you write Java codes using the Wikapidia configuration API.
This part of the documentation builds on the previous section.

Create a configurator object

  • First, create a configuration object by
  • default configuration
Configuration configuration = new Configuration();
  • or override configuration file
Configuration configuration = new Configuration(myConf);
  • Then you can create the configurator object:
Configurator conf = new Configurator(configuration);

Get an instance with a specified class

  • You can use a Configurator object to get an instance of a specified class. For example:
LocalPageDao lpDao = configurator.get(LocalPageDao.class);

returns an object of type LocalPageDao.

Get configuration values from the configuration file

  • Configuration values are NOT required to associate with a provider.
  • The typesafe.config library helps you get configuration values in this case:
Configuration config = new Configuration();
config.get().getAnyRef(String string);

Extending the Wikapidia Configuration API

Please refer to the Configurator and Configuration Javadocs.