Giter Site home page Giter Site logo

graylog2 / jadconfig Goto Github PK

View Code? Open in Web Editor NEW
22.0 17.0 11.0 3.21 MB

Annotation-driven configuration library for the Java programming language

Home Page: https://joschi.github.io/JadConfig/0.12.1/

License: Apache License 2.0

Java 100.00%
java configuration java-library guice joda google-guava guava google-guice

jadconfig's Introduction

JadConfig

Maven Central

JadConfig is a minimalistic annotation-driven configuration parsing framework for Java with minimal dependencies.

Example

Here is a quick example of a Java class used as configuration bean:

public class ConfigurationBean {
  @Parameter("my.stringList")
  public List<String> myList = new ArrayList<String>();

  @Parameter("my.integer")
  public int myInteger = 1;

  @Parameter(value = "my.uri", required = true)
  public URI myURI;
}

and how you initialize it with JadConfig:

ConfigurationBean bean = new ConfigurationBean();
new JadConfig(new PropertiesRepository("my.properties"), bean).process();

Assert.assertNotNull(bean.myList);

You can also use multiple repositories as source for your configuration (first match wins):

ConfigurationBean bean = new ConfigurationBean();
new JadConfig(
        Arrays.asList(
            new EnvironmentRepository(),
            new PropertiesRepository("my.properties")
        ),
        bean)
    .process();

Assert.assertNotNull(bean.myList);

Joda-Time

JadConfig optionally supports Joda-Time. In order to use it just add the Joda-Time dependency to your pom.xml:

<dependency>
    <groupId>joda-time</groupId>
    <artifactId>joda-time</artifactId>
    <version>2.9</version>
</dependency>

And register JodaTimeConverterFactory with the JadConfig instance:

JadConfig jadConfig = new JadConfig(repository, configurationBean);
jadConfig.addConverterFactory(new JodaTimeConverterFactory());
jadConfig.process();

Guava

JadConfig optionally supports some data types from Google Guava. In order to use it just add the Google Guava dependency to your pom.xml:

<dependency>
    <groupId>com.google.guava</groupId>
    <artifactId>guava</artifactId>
    <version>18.0</version>
</dependency>

And register GuavaConverterFactory with the JadConfig instance:

JadConfig jadConfig = new JadConfig(repository, configurationBean);
jadConfig.addConverterFactory(new GuavaConverterFactory());
jadConfig.process();

Currently the following data types are being supported:

  • CacheBuilderSpec
  • HashCode
  • HostAndPort
  • HostSpecifier
  • InternetDomainName
  • MediaType
  • UnsignedInteger
  • UnsignedLong

Guice

JadConfig optionally supports registering named bindings in Google Guice. In order to use it just add the Google Guice dependency to your pom.xml:

<dependency>
    <groupId>com.google.inject</groupId>
    <artifactId>guice</artifactId>
    <version>4.0</version>
</dependency>

And register NamedConfigParametersModule with the Guice Injector:

Injector injector = Guice.createInjector(new NamedConfigParametersModule(Collections.singleton(configurationBean)));

The name of the bindings are identical to the @Parameter name.

Example:

public class MyConfigBean {
    @Parameter("my.custom.config")
    public String customConfig;
}

// Create injector and register NamedConfigParametersModule.
// [...]

public class MyClass {
    @Inject
    public MyClass(@Named("my.custom.config") String customConfig) {
        // ...
    }
}

// MyClass will be instantiated with the value of customConfig from the MyConfigBean instance.
MyClass myClass = injector.getInstance(MyClass.class);

Please note that nullable properties which should be injected by Guice have to be annotated with @Nullable, see UseNullable in the Guice wiki for details.

Maven

To use JadConfig in your project using Maven add the following lines into the dependencies section of your pom.xml:

<dependency>
    <groupId>org.graylog</groupId>
    <artifactId>jadconfig</artifactId>
    <version>0.13.0</version>
</dependency>

Support

Please file bug reports and feature requests in GitHub issues.

License

JadConfig is being released under the Apache License, Version 2.0. You can download the complete license text at http://www.apache.org/licenses/LICENSE-2.0.html

jadconfig's People

Contributors

bernd avatar dennisoelkers avatar dependabot-preview[bot] avatar dependabot[bot] avatar jlleitschuh avatar joschi avatar todvora avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

jadconfig's Issues

Include parameter name in ParameterException

If a user mistypes a boolean value (in this case leaving out the e) the BooleanConverter throws an excption.
Unfortunately JadConfig.processClassFields does not add to or wrap the resulting ParameterException with the field name (or better the actual parameter name) so the exception is very generic and a user cannot figure out which entry was wrong.

I don't think there's a way finding this out in the calling code.

2015-06-03T13:59:38.586-04:00 ERROR [CmdLineTool] Invalid configuration
com.github.joschi.jadconfig.ParameterException: Couldn't convert value "tru" to Boolean.
        at com.github.joschi.jadconfig.converters.BooleanConverter.convertFrom(BooleanConverter.java:25)
        at com.github.joschi.jadconfig.converters.BooleanConverter.convertFrom(BooleanConverter.java:11)
        at com.github.joschi.jadconfig.JadConfig.convertStringValue(JadConfig.java:160)
        at com.github.joschi.jadconfig.JadConfig.processClassFields(JadConfig.java:138)
        at com.github.joschi.jadconfig.JadConfig.process(JadConfig.java:99)
        at org.graylog2.bootstrap.CmdLineTool.readConfiguration(CmdLineTool.java:316)
        at org.graylog2.bootstrap.CmdLineTool.run(CmdLineTool.java:161)
        at org.graylog2.bootstrap.Main.main(Main.java:58)

Collect all validation errors and then fail

Current implementation stops with the first validation/parameter exception. If users have several problems in the configuration, they need to fix one by one and always restart the graylog server meanwhile. If we could collect all errors and only then fail, it would simplify initial setups significantly.

Maybe it would be enough to adapt https://github.com/Graylog2/JadConfig/blob/main/src/main/java/com/github/joschi/jadconfig/JadConfig.java#L91 , instead of throwing exceptions collect them and then fail if there is at least one, throwing one wrapping all failures.

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.