The Concern Separation Aspect Registrar (Csar /zɑːr/) provides access to some concern (usually cross-cutting) that may configured globally or locally to some section of the program. Csar acts like a global service locator that provides flexible, transparent local configuration. Like a czar
in American politics, Csar governs configuration and access to program concerns.
Quick Start
1. Include Csar Dependency
Include the appropriate Csar dependency from Maven.
pom.xml
<project>
…
<dependencies>
…
<dependency>
<groupId>io.csar</groupId>
<artifactId>csar</artifactId>
<version>x.x.x</version>
</dependency>
</dependencies>
</project>
2. Make a Concern
Implement Concern
to represent one of your program's concerns for Csar to register.
Environment.java
public class Environment extends Properties implements Concern {
…
3. Set the Default Concern
Indicate a fallback concern using Csar.setDefaultConcerns(Concern... concerns)
to be accessible by default in any context that has no specific concern configured.
MyApp.java
final Environment defaultEnvironment = new Environment();
defaultEnvironment.setProperty("test", "default");
Csar.setDefaultConcerns(defaultEnvironment);
4. Csar Provides a Concern Configuration
Any code at any time may ask Csar for a concern using Csar.getConcern(Class<C> concernType)
, indicating the desired type of concern.
If you run code in the context of a specially configured concern using Csar.run(Concern concern, Runnable runnable)
, Csar.getConcern(Class<C> concernType)
will return the locally configured concern; otherwise, it will return the default, fallback concern configuration.
MyApp.java
final Environment defaultEnvironment = new Environment();
defaultEnvironment.setProperty("test", "default");
Csar.setDefaultConcerns(defaultEnvironment);
…
System.out.println(env.getProperty("test")); //prints "default"
…
final Environment localEnvironment = new Environment();
localEnvironment.setProperty("test", "local");
Csar.run(localEnvironment, () -> {
final Environment env = Csar.getConcern(Environment.class);
System.out.println(env.getProperty("test")); //prints "local"
});
Learn More
You can access an in-depth introduction to Csar.