How build my own Application Setting

Posted by adisembiring on Stack Overflow See other posts from Stack Overflow or by adisembiring
Published on 2010-05-22T01:41:16Z Indexed on 2010/05/22 1:50 UTC
Read the original article Hit count: 328

I want to build a ApplicationSetting for my application. The ApplicationSetting can be stored in a properties file or in a database table. The settings are stored in key-value pairs. E.g.

ftp.host = blade
ftp.username = dummy
ftp.pass = pass
content.row_pagination = 20
content.title = How to train your dragon.

I have designed it as follows:

Application settings reader:

interface IApplicationSettingReader {
    read();
}

DatabaseApplicationSettingReader {
    dao appSettingDao;

    AppSettings read() {
        List<AppSettingEntity> listEntity = appSettingsDao.findAll();
        Map<String, String> map = new HaspMap<String, String>();
        foreach (AppSettingEntity entity : listEntity) {
            map.put(entity.getConfigName(), entity.getConfigValue());
        }

        return new AppSettings(map);
    }
}

DatabaseApplicationSettingReader {
    dao appSettingDao;

    AppSettings read() {
        //read from some properties file 
        return new AppSettings(map);
    }
}

Application settings class:

AppSettings {
    private static AppSettings instance;
    private Map map;

    Public AppSettings(Map map) {
        this.map = map;
    }

    public static AppSettings getInstance() {
        if (instance == null) {
            throw new RuntimeException("Object not configure yet");
        } 

        return instance;
    }

    public static configure(IApplicationSettingReader reader) {
        instance = reader.read();
    }

    public String getFtpSetting(String param) {
        return map.get("ftp." + param);
    }

    public String getContentSetting(String param) {
        return map.get("content." + param);
    }
}

Test class:

AppSettingsTest {
    IApplicationSettingReader reader;

    @Before
    public void setUp() throws Exception {
        reader = new DatabaseApplicationSettingReader();
    }

    @Test
    public void  getContentSetting_should_get_content_title() {
        AppSettings.configure(reader);

        Instance settings = AppSettings.getInstance();
        String title = settings.getContentSetting("title");
        assertNotNull(title);
        Sysout(title);
    }

}

My questions are:

  1. Can you give your opinion about my code, is there something wrong?

  2. I configure my application setting once, while the application start, I configure the application setting with appropriate reader (DbReader or PropertiesReader), I make it singleton. The problem is, when some user edit the database or file directly to database or file, I can't get the changed values. Now, I want to implement something like ApplicationSettingChangeListener. So if the data changes, I will refresh my application settings. Do you have any suggestions how this can be implemented?

© Stack Overflow or respective owner

Related posts about java

Related posts about configuration