Reputation: 5408
I am trying to solve an issue where by I have to .p12 files that correspond to production and development. I am looking for a way for java to know whether it is running in prod or dev so that I can choose the appropriate p12 to use.
Currently I am using the following logic
private static final String PATH_TO_P12_DEV = "/JavaPNSDEV.p12";
private static final String PATH_TO_P12_PROD = "/JavaPNSPROD.p12";
private InputStream keystoreInputStream = null;
private final Logger logger = Logger.getLogger(ApplePushNotificationSystem.class);
private PushManager<SimpleApnsPushNotification> pushManager = null;
private void connect() throws NoSuchAlgorithmException, CertificateException, IOException, UnrecoverableKeyException, KeyManagementException, KeyStoreException {
try {
if(InetAddress.getLocalHost().getHostName().toLowerCase().equals("hostname")){
keystoreInputStream = this.getClass().getResourceAsStream(PATH_TO_P12_PROD);
}else{
keystoreInputStream = this.getClass().getResourceAsStream(PATH_TO_P12_DEV);
}
However this worries me because as soon as the hostname changes / someone changes the hostname this will fail.
What is the correct approach for solving as issue like this?
Thanks
Upvotes: 2
Views: 7009
Reputation: 82008
The details depend on the kind of infrastructure you run on. Assuming you run in some kind of Webapplication server it should be possible to set an Environment variable to an identificating String. If you have only this single configuration element (which .p12 file to use) you can just put in an if condition:
if ("PROD".equals(System.getenv("STAGE"))){
// use on file
} else {
// use a different file
}
Most of the time you have multiple configuration elements. In that case I would put all that information in property files. One per stage. Then you use the stage environment variable to determine which file to use.
Upvotes: 1
Reputation: 9716
Correct approach would be passing some configuration option to the application, by configuration file or just environment variable. Then you can easily distinguish between different environments.
As trivial example: you can run production application with -DConfig=PRO
, and development with -DConfig=DEV
. Then you can distinguish them by checking System.getProperty("Config")
Upvotes: 3
Reputation: 212
I would make use of a configuration file. This way you can have 1 file pointing to your development environment and 1 file pointing to your production environment.
Perhaps you can look into apache commons config for this?
Upvotes: 1