Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suport properties in DataBase #124

Open
erickjx opened this issue Apr 22, 2015 · 3 comments
Open

Suport properties in DataBase #124

erickjx opened this issue Apr 22, 2015 · 3 comments

Comments

@erickjx
Copy link

erickjx commented Apr 22, 2015

Hi, great work.

It's possible add suport to read properties from DataBase?

@lviggiano
Copy link
Collaborator

Thanks @erickjx .

At the moment there is no built-in support for proprierties from Database, but there is a pluggable mechanism to load properties from different sources.

See ConfigFactory.registerLoader() method and the Loader interface.

Basically your Loader should be able to accept a custom URI (for instance database://mytable or something similar, then be able to parse this url and produce a properties object.

As example you can have a look how the XMLLoader is implemented.

In short, you have to implement the accept() and load() method in your custom Loader:

public class DatabaseLoader extends Loader {

    public DatabaseLoader(DataSource dataSource) {
        this.dataSource = dataSource;
    } 

    public boolean accept(URI uri) {
        // this tells owner that this loader is able to parse the given uri
        return uri.getScheme().equals("database"); 
    }

    public void load(Properties result, URI uri) throws IOException {
        // obtain a connection
        // make the query 
        //  fill the result object.
    }

    // you probably don't need to implement this, or just return the urlPrefix.
    public String defaultSpecFor(String urlPrefix) {
        return urlPrefix; 
    }
}


// Then from the calling code you need to do:

@Sources("database:mytable")
public class MyConfig extends Config {
    //...
}

DataSource myDataSource = ... // get it from jndi or whatever you use.
ConfigFactory.registerLoader(new DatabaseLoader(myDataSource))
MyConfig cfg = ConfigFactory.create(...);

I am planning to change this mechanism, sometime in future. Since this doesn't allow the "automatic hot reload" feature (you can still trigger the reload programmatically or via jmx).
For now, it works this way.

@erickjx
Copy link
Author

erickjx commented Apr 27, 2015

Its possible read from DB, and if not available fall back to properties or url o jar?, i will made a Generic Hibernate Loader

@lviggiano
Copy link
Collaborator

yes, http://owner.aeonbits.org/docs/loading-strategies/

In particular have a look at the "merge" loading strategy.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants