Git Product home page Git Product logo

Comments (10)

lviggiano avatar lviggiano commented on August 23, 2024

Hi Alexeyr

There are several ways that could possibly help:

  1. The @Sources annotation does support variable expansion from system properties:
    class Example {
        MyConfig cfg = createMyConfig();

        @Sources("file:${mypath}/myconfig.properties");
        interface MyConfig extends Config {
        }

       static MyConfig createMyConfig() {
           String myPath = ...;
           System.setProperty("mypath", myPath);
           return ConfigFactory.create(MyConfig.class);
       }

One could also specify -Dmypath=... to the JVM at the command line, ie. from a batch file.

You can also use ${user.dir} in the @Sources annotation, that is contained in the System properties by default when the JVM starts and points to the work dir from which the JVM has been launched, as somebody suggested on StackOverflow as well.

By default, the url specifed with file: protocol are relative to the workdir (unless they start with a / char), so if you specify:

    @Sources("file:etc/myconfig.properties");
    interface MyConfig extends Config { ... }

the path etc/myconfig.properties will be relative to the path from where you launched the JVM.
So, for instance, if you launch the JVM from the directory /home/luigi/myapp/ this will be resolved as /home/luigi/myapp/etc/myconfig.properties.

  1. OWNER supports a feature called 'importing properties', so basically you can load properties as you prefer then wrap into an OWNER Config object:
     Properties props = new Properties();
     props.load(new FileInputStream(new File("path/to/whatever.properties"))); 
     MyConfig cfg = ConfigFactory.create(MyConfig.class, props);
  1. On the master branch (that will be released on the next version) it is possible to config objects to implement the Mutable interface, that allows the Config object to be altered. If you like I can add some ways to load properties programmatically from an InputStream:
     interface MyConfig extends Config, Mutable { ... }

     MyConfig cfg = ConfigFactory.create(MyConfig.class);

     cfg.load(new FileInputStream(...)); 
     // or 
     cfg.load(new FileReader(...));
     // or 
     cfg.loadFromXML(new FileInputStream(...));

Those methods will delegate the loading to the internal Properties object. At the moment those methods are not available, but I think I may add them. What do you think?

Option 3 will be (eventually) available in version 1.0.4 and superior. Option 1 and 2 should already work with released version 1.0.3 (and 1.0.2 too).

Let me know.

from owner.

alexeyr avatar alexeyr commented on August 23, 2024

Yes, option 3 fits what I want quite well and this isn't an immediate requirement.

from owner.

lviggiano avatar lviggiano commented on August 23, 2024

Ok, I will add the load methods. It's very quick to do and it will be included in the next released version. I'll probably implement it today, so you can test with the master branch. I can send you the jar if you need to verify that it does what you need and you don't have time to package it by yourself.

from owner.

alexeyr avatar alexeyr commented on August 23, 2024

And now I thought that it won't play well with hot reload and change events coming in 1.0.4. Unless this could also be easily added?

from owner.

lviggiano avatar lviggiano commented on August 23, 2024

Hot reload is optional (if you don't specify the @HotReload annotation it won't be enabled). If you decide to load properties manually, you need to implement hot reload by your own if you want it.

The HotReload only works with file: (or jar:file:) URLs specified by @Sources annotation. It's quite inconvenient to monitor for changes in InputStreams or Reader objects.

from owner.

lviggiano avatar lviggiano commented on August 23, 2024

I could possibly add something like:

@Sources("file:${mypath}/myconfig.properties");   // notice ${mypath} here
interface MyConfig extends Config { ... }

ConfigFactory.setProperty("mypath", "/foo/bar/baz"); // ${mypath} will be expanded as "/foo/bar/baz"
MyConfig cfg = ConfigFactory.create(MyConfig.class);

from owner.

alexeyr avatar alexeyr commented on August 23, 2024

Yes, this seems to cover cases I can think of.

from owner.

lviggiano avatar lviggiano commented on August 23, 2024

👍

from owner.

lviggiano avatar lviggiano commented on August 23, 2024

Implemented.
See 61605e7 and 0449ecd.

Things to do before closing:

  • document in the website or the wiki.

from owner.

lviggiano avatar lviggiano commented on August 23, 2024

Documented here: http://owner.aeonbits.org/docs/configuring/

from owner.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.