Last active
December 23, 2015 22:49
-
-
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...
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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 contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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(); | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface EnviromentRule { | |
public boolean matches(String name); | |
} | |
@interface Enviroment { | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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 file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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"?