-
Notifications
You must be signed in to change notification settings - Fork 20
Constretto
Constretto is as configuration management framework for Java applications. It allows you to “tag” configuration values, so that Constretto could choose the correct value at runtime.
It also works as a bridge between different configuration formats, and currently Java property files, Ini files, Java Beans, JSON and LDAP are supported.
Short version history:
- version 2.2 was released in 2014, mainly with changes in dependencies and some bugfixes
- version 2.1 was released in 2013 dropping support for Spring 2.5.X for the Constretto-Spring module (3.2.X is now required) and adding support for using LDAP for storing configuration.
- version-2.0 was released in 2012 dropping the dependency on Spring-Core and adding support for JSON configuration stores (as well as some breaking API changes)
- version 1.0 was released in 2010 and contained support for Java 1.5+ and Spring 2.5 with support for Java property, Java Beans and INI files
As Constretto is distributed through the Sonatype OSS Nexus repository it should be easily available.
<dependency>
<groupId>org.constretto</groupId>
<artifactId>constretto-api</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>org.constretto</groupId>
<artifactId>constretto-core</artifactId>
<version>2.2.2</version>
</dependency>
If you would like to use the Spring support add:
<dependency>
<groupId>org.constretto</groupId>
<artifactId>constretto-spring</artifactId>
<version>2.2.1</version>
</dependency>
Two main mechanisms exist for configuring Constretto:
Now that you've configured Constretto, by Java API or Spring, you may query your configuration using the methods in the ConstrettoConfiguration interface like in the examples below:
// Simple lookup
String aDataSourceUrl = configuration.evaluateToString("datasources.customer.url");
In much the same way as dependency injection work in e.g. Spring and Guice, Constretto allows you to inject configuration into your classes. It supports injection in fields, and methods as seen in the example below:
Java class to be injected with configuration:
public class DataSourceConfiguration {
private String myUrl;
private String myPassword;
private Integer version;
// When no expression is explicitly given Constretto will use field name as key
@Configuration
private String vendor;
@Configuration("username")
private String myUsername;
@Configure
public void configure(String url, @Configuration("password") String secret) {
this.myUrl = url;
this.myPassword = secret;
}
@Configure
public void setVersion(Integer version) {
this.version = version;
}
public String getUrl() {
return myUrl;
}
public String getUsername() {
return myUsername;
}
public String getPassword() {
return myPassword;
}
public String getVendor() {
return vendor;
}
public Integer getVersion() {
return version;
}
}
A test that shows this feature used with the Java API:
public class ConfigurationAnnotationsTest {
private ConstrettoConfiguration configuration;
@Before
public void prepareTests() {
setProperty("url", "jdbc://url");
setProperty("username", "username");
setProperty("password", "password");
setProperty("vendor", "derby");
setProperty("datasources.vendor", "derby");
setProperty("version", "10");
configuration = new ConstrettoBuilder().createSystemPropertiesStore().getConfiguration();
}
@Test
public void applyConfigrationToAnnotatedConfigurationObject() {
DataSourceConfiguration customerDataSource = new DataSourceConfiguration();
configuration.on(customerDataSource);
assertEquals("derby", customerDataSource.getVendor());
assertEquals("username", customerDataSource.getUsername());
assertEquals("jdbc://url", customerDataSource.getUrl());
assertEquals("password", customerDataSource.getPassword());
assertEquals(new Integer(10), customerDataSource.getVersion());
}
}