Search Tomcat vs Jetty JNDI

I use Spring to configure my Java application and in my Spring configuration I get the data source via JNDI for Jetty as follows:

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

but this will not work with Tomcat. With Tomcat, I have to do this:

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

What is the best way to solve this problem? I am already using JNDI as a means of externalizing the configuration, so I cannot externalize my external configuration! At the same time, I absolutely do not want to have two separate Spring configuration files. HELP !!!

+6
spring tomcat tomcat6 jetty jndi
source share
3 answers

I found the answer here , but I thought it was a bit complicated, but it gave me the idea to use a very cool <class href = "http://svn.liferay.com/browse/~raw,r=60314/portal/trunk /portal-service/src/com/liferay/portal/kernel/util/ServerDetector.java "rel =" noreferrer "> ServerDetector that the blogger found.

As soon as I can dynamically determine what type of server I'm running, 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!

+7
source share

After some experimentation, I realized that I could just get Jetty to use the same JNDI path as Tomcat. The following snippet from my jetty-env.xml :

  <New id="myDataSource" class="org.mortbay.jetty.plus.naming.Resource"> <!-- We MUST specify the entire JNDI path here to force compliance with the Tomcat/J2EE convention --> <Arg>java:comp/env/jdbc/myDataSource</Arg> <Arg> <New class="com.atomikos.jdbc.nonxa.AtomikosNonXADataSourceBean"> <Set name="uniqueResourceName">sbeDatabase</Set> ............... </New> </Arg> </New> 

Not sure if this is ideal, but it works.

Update

It works if you put the jetty-env.xml file in the WAR ... but for some reason you move this configuration outside of the WAR and into the context fragment file in the Jetty “contexts” directory, then it throws an exception:

Check it out: http://jira.codehaus.org/browse/JETTY-273

+4
source share

The cleanest way to do this is to configure the configuration .;)

Use the Spring Placeholder. Cm

http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/beans.html#beans-factory-placeholderconfigurer

The basic idea is that you simply put the placeholder in the Spring configuration with the property, and then read the matching property from the properties file. You create a properties file during the build process. I saw how this happened when the build tool (ant) reads the environment variable and then creates a properties file suitable for an environment based on a skeleton file filled with tokens.

+1
source share

All Articles