Search code examples
javagrailsjbossjndi

Add config file based on deploymentName


I am trying to load a config file that is specific to a deployment on a JBoss server. The idea is that I can have multiple deployments of the same application (Example, training, testing, development) on the same server. And each deployment will have a different configuration.

Right now the direction I'm taking is to get the deployment name from jboss via the JNDI. Here is a part from my grails-app/conf/Config.groovy

appCtx = new InitialContext().lookup("java:app")
if(appCtx) {
  deploymentName = appCtx.lookup("AppName")
  grails.config.locations << "classpath:${deploymentName}-config.properties"
  grails.config.locations << "classpath:${deploymentName}-config.groovy"
}

Then if I name the war file training.war, stick it in the deployments folder, then it should pick up a config from

configurations/training-config.properties

The issue I'm having is I get an error while packaging...

Need to specify class name in environment or system property,
or as an applet parameter, or in an application resource file:
java.naming.factory.initial

Anyone have ideas on how to fix this? Or if there's an easier way?

I'm using JBoss 7.1.1


Solution

  • I figured it out... it's as simple as placing the code in a try/catch block...

    import javax.naming.Context
    import javax.naming.InitialContext
    try {
      Context appCtx = (Context)(new InitialContext().lookup("java:app"))
    
      // JBoss's deployment name
      // you can see this at Profile >> Container >> Naming >> applications
      deploymentName = appCtx.lookup("AppName")
    
      grails.config.locations << "classpath:${deploymentName}-config.properties"
      grails.config.locations << "classpath:${deploymentName}-config.groovy"
      if(System.properties["${deploymentName}.config.location"]) {
        grails.config.locations << "file:" +
          System.properties["${deploymentName}.config.location"]
      } else if(System.getenv("${deploymentName}.config.location")) {
        grails.config.locations << "file:" + 
          System.getenv("${deploymentName}.config.location")
      }
    }catch(Exception ex) {
      // Initial Context does not exist
      // aka, not deployed
    }
    

    The modified code also allows for a system property or environment variable that specifies the deployment name. This can be configured via JBoss 7.1.1. For a deployed training.war application...

    Profile >> General Configuration >> System Properties >> Add

    Enter the key like so...

    training.config.location
    

    And the value/path. If on windows, double escape the path

    E:\\Servers\\jboss\\jboss7.1.1\\standalone\\configurations\\training-config.properties
    

    Then deploy the war file to jboss. Configuration should be picked up. :D

    Props go to Mike over at this blog for coming up with the System properties/env variable idea