0

I know JNDI lookup in grails is as simple as

datasource {
  jndiName = "java:comp/env/myDataSource"
}

this works great on Tomcat.

On other containers (WebLogic, JBOSS, etc.) the jndiName for the same thing would be just myDataSource . The question is how do I configure this to be generic and portable so that the code does not need to be changed based on the deploy target?

dbrin
  • 15,525
  • 4
  • 56
  • 83

1 Answers1

1

You might be able to key off a system property that you know will be set when running in Tomcat and not set anywhere else, e.g.

datasource {
  jndiName = "${System.getProperty('catalina.home') ? 'java:comp/env/' : ''}myDataSource"
}
Ian Roberts
  • 120,891
  • 16
  • 170
  • 183
  • interesting idea! but isn't there a common straight forward way? – dbrin Nov 17 '12 at 02:56
  • I used that solution for a while, and it worked good for distuingishing a Tomcat server vs a Weblogic server. But, I recently tried putting my Grails app on a Glassfish server and it doesn't work, because... I believe Glassfish also uses Catalina, so the expression does return a value. But Glassfish needs the naming to just be the "myDatasource" whereas Tomcat requires the whole "jav:comp/env/". – user542103 Jun 13 '16 at 14:03
  • So I did this... not sure if the "best way", but I tested on Glassfish, Tomcat, and Weblogic and it does work for those: jndiName = "${(System.getProperty('catalina.home') && (System.getProperty('java.class.path')).trim().toLowerCase().indexOf('tomcat') > 0 ) ? 'java:comp/env/' : ''}jdbc/myGrails" – user542103 Jun 13 '16 at 14:04