2

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

3
  • Use a properties file or an environment setting or a run-time flag. Commented Aug 26, 2014 at 12:36
  • 1
    use a configuration file. there's no way to tell if a server is devel or production, since this is decided by someone, not by the system Commented Aug 26, 2014 at 12:36
  • All good answers below, but beware! The more time you spend working in the "development" configuration, the longer it will take to discover bugs that only appear in the "production" config. (Don't ask me how I know!) Commented Aug 26, 2014 at 12:50

3 Answers 3

3

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")

Sign up to request clarification or add additional context in comments.

Comments

1

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?

Comments

1

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.

1 Comment

This is a simple and fast solution if all that you want is showing/determining the stage. No config files, no Maven building fuss. Note, that when working locally with Eclipse and you change the env var while running Eclipse, you must restart Eclipse after doing so (if you start the server via the Servers tab).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.