ServiceMix can be embedded in any application server, Java SE or EE platform. When working inside an application server you probably want to reuse the application servers JNDI provider and tooling.

When you are working in embedded mode, you probably want a JNDI provider to use to configure the services available to other JBI components which do not use dependency injection and Spring to configure themselves.

For a standalone JNDI provider, we do recommend using our own Spring based JNDI provider which is the default in the stand alone ServiceMix server.

This is provided in xbean-spring-common (currently version 2.4) and, a bonus, is not confined to ServiceMix either. It can be used for unit testing in your dev environment and more.

To use it, add the following jndi.properties file to your classpath

java.naming.factory.initial = org.apache.xbean.spring.jndi.SpringInitialContextFactory

Note that you can also add a line

java.naming.provider.url = classpath:foo/bar.xml

Where the value is any valid Spring resource string such as a file://foo/bar.xml or a URL such as http://acme.com/whatnot.xml. or classpath:jndi-entries.xml.

The default value is assumed to be classpath://jndi.xml.

When InitialContext is called upon, the JndiProvider will respond.

For the JNDI entries you need, create a regular Spring XML configuration file with a JNDI context bean (called jndi), which by default is located in a file called jndi.xml on the classpath

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
  <bean id="jndi" class="org.apache.xbean.spring.jndi.DefaultContext">
    <property name="entries">
      <map>
        <entry key="java:comp/env/jdbc/myDatabase">
			<bean class="org.springframework.jdbc.datasource.DriverManagerDataSource"
				destroy-method="close" singleton="false">
				<property name="driverClassName" value="com.sybase.jdbc3.jdbc.SybDataSource"/>
				<property name="url" value="jdbc:sybase:Tds:sybase-server:5000/MyDatabase"/>
				<property name="username" value="db-username"/>
				<property name="password" value="myS3cr3tpassword"/>
			</bean>
        </entry>
      </map>
    </property>
  </bean>
</beans>

.

  • No labels

1 Comment

  1. JNDI has support has moved to xbean.
    Need to update this page...