Skip to content

Instantly share code, notes, and snippets.

@guilhermesilveira
Last active December 23, 2015 22:49
Show Gist options
  • Save guilhermesilveira/6705671 to your computer and use it in GitHub Desktop.
Save guilhermesilveira/6705671 to your computer and use it in GitHub Desktop.
points: . type safe . type safe . no missing parameter . option to create MANDATORY parameter . applicationscoped if the custom vraptor plugin is applicationscoped, the plugin docs will let you know...
/**
* My application desires
**/
@ApplicationScoped // PROBABLY IMPORTANT, or will CDI read the parent one?
@Environment({"development", "test", "acceptance"})
public class AcceptanceAndTest extends EmailConfig {
// mandatory configuration
public File getBaseDir() {
return new File("/tmp");
}
}
/**
* This file is inside our vraptor-simplemail.jar.
* It contains the defaults.
**/
@ApplicationScoped // PROBABLY IMPORTANT
public abstract class EmailConfig {
// string example
public String getEmailHost() {
return "host";
}
// int example
public int getEmailPort() {
return 30;
}
// typesafe example
public Class getCrazyType() {
return String.class;
}
// mandatory configuration
public abstract File getBaseDir();
}
interface EnviromentRule {
public boolean matches(String name);
}
@interface Enviroment {
}
environment definition would still happen through (in order):
. VRAPTOR_ENVIRONMENT environment variable (System.getEnv)
. VRAPTOR_ENVIRONMENT system property (System.getProperty)
. web.xml
. default: development
/**
* This file is inside our vraptor-simplemail.jar
**/
@ApplicationScoped
public class EmailPlugin {
private final EmailSession session;
EmailPlugin(EmailConfig config) {
this.session = new EmailSession(config.getHost(), config.getEmailPort());
}
public void send(Email email) {
session.send(email);
}
}
/**
* My application production
**/
@ApplicationScoped // PROBABLY IMPORTANT, or will CDI read the parent one?
@Environment("production")
public class ProductionConfig extends EmailConfig {
// int example
public int getEmailPort() {
return 3333;
}
// mandatory configuration
public File getBaseDir() {
return new File("/production");
}
}
// THIS IS THE WORTH IMPLEMENTATION, but would work somehow...
// it is one line, but sucks. if not, better
/**
* This file is inside our vraptor-simplemail.jar
**/
@ApplicationScoped
public class EmailPlugin {
private final EmailSession session;
EmailPlugin(Class<EmailConfig> configs, EnvironmentInstancePicker picker) {
EmailConfig config = picker.pickFor(configs);
this.session = new EmailSession(config.getHost(), config.getEmailPort());
}
public void send(Email email) {
session.send(email);
}
}
@jweyrich
Copy link

Hmm, I'm wondering the same thing as your comment "PROBABLY IMPORTANT, or will CDI read the parent one?" - Did you find out how if the parent annotation is "inherited"?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment