Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tomcat vs Jetty JNDI Lookup

I use Spring to configure my Java Web App and in my Spring configuration I obtain a datasource via JNDI for Jetty as follows:

<jee:jndi-lookup id="dataSource" jndi-name="jdbc/myDataSource" />

but this won't work with Tomcat. With Tomcat I have to do this:

<jee:jndi-lookup id="dataSource" jndi-name="java:comp/env/jdbc/myDataSource" />

Whats the best way to solve this? I am already using JNDI as a way to externalize configuration, so I can't externalize my externalized configuration! At the same time I absolutely loath the idea of having two separate Spring configuration files. HELP!!!

like image 848
HDave Avatar asked Nov 30 '10 03:11

HDave


1 Answers

I found an answer here, but I thought it was a bit complicated, but it did give me the idea to use the very cool ServerDetector class that blogger had found.

Once I can dynamically figure what type of server I am running in, I was able to use the Spring expression language to do the rest of the work:

<jee:jndi-lookup id="myAppDataSource" 
    jndi-name="#{ (AppServerType == 'Jetty' ? 'jdbc/' : 'java:comp/env/jdbc/') + 
                  'myAppDataSource' }" />

Easy!

like image 86
HDave Avatar answered Sep 20 '22 23:09

HDave