One Star

[resolved] How to retrieve datasource with it's JNDI name in Talend Route?

Hi Team,
I am trying to retrieve all available data sources and their JNDI names in Talend Runtime. However, I only can get either all data sources or all JNDIs, but cannot map JNDIs to relavent data source instances. 
For example, I managed to get all JNDIs and then try to get data sources with below code:
Registry reg = camelContext.getRegistry();
Set<org.apache.karaf.jndi.JndiService> s = reg.findByType(org.apache.karaf.jndi.JndiService.class);
if(s == null)
System.out.println("JndiService is null");
for(org.apache.karaf.jndi.JndiService js : s)
Map<String, String> jndis = js.names();
for(Map.Entry<String, String> row_jndi : jndis.entrySet() )
String key = row_jndi.getKey();
String value = row_jndi.getValue();
System.out.println("Key = " + key + "; Value = " + value);
Object obj = reg.lookupByName(key);
if(obj != null)
System.out.println("Obj is " + obj.toString());
System.out.println("Obj is null" );

}catch(Exception e)

However, the reg.lookupByName(key) failed to get objects and log shows:
Key = osgi:service/WeChatDS; Value = org.apache.commons.dbcp2.managed.ManagedDataSource
Obj is null
Key = osgi:service/jndi; Value = org.apache.karaf.jndi.internal.JndiServiceImpl
Obj is null
Key = osgi:service/MobileCRMDS; Value = org.apache.commons.dbcp2.managed.ManagedDataSource
Obj is null
Key = osgi:service/jdbc/WeChat2; Value = org.postgresql.ds.PGPoolingDataSource
Obj is null

When I try to use code of: "Set<javax.sql.DataSource> o = reg.findByType(javax.sql.DataSource.class);", it only gives me instances of data sources, but I cannot use data source instance to fetch JNDI name.
Anyone could advice me how to get JNDI and relevant data source instance? BTW, I am working on TOS 6.0

Accepted Solutions
One Star

Re: [resolved] How to retrieve datasource with it's JNDI name in Talend Route?


Re: [resolved] How to retrieve datasource with it's JNDI name in Talend Route?

You could try to use plain jndi like:
Context context = new InitialContext();
and then use the context to look up and retrieve the DataSources.
Another option is to get hold of the OSGi BundleContext and lookup the DataSources as OSGi services.
One Star

Re: [resolved] How to retrieve datasource with it's JNDI name in Talend Route?

Hi Schneider,
Thank you for your reply. However, it throws exceptions as such:
javax.naming.NoInitialContextException: 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
at javax.naming.spi.NamingManager.getInitialContext(Unknown Source)
at javax.naming.InitialContext.getDefaultInitCtx(Unknown Source)
at javax.naming.InitialContext.getURLOrDefaultInitCtx(Unknown Source)
at javax.naming.InitialContext.lookup(Unknown Source)
at test_datasource.mysqljob_0_1.mysqljob.tJava_1Process(
at test_datasource.mysqljob_0_1.mysqljob.tPostgresqlConnection_1Process(
at test_datasource.mysqljob_0_1.mysqljob.tPrejob_1Process(
at test_datasource.mysqljob_0_1.mysqljob.runJobInTOS(
at org.talend.camel.TalendProducer.invokeTalendJob(
at org.talend.camel.TalendProducer.process(
at org.apache.camel.util.AsyncProcessorConverterHelper$ProcessorToAsyncProcessorBridge.process(
at org.apache.camel.processor.SendProcessor.process(
at org.apache.camel.processor.RedeliveryErrorHandler.process(
at org.apache.camel.processor.CamelInternalProcessor.process(
at org.apache.camel.processor.Pipeline.process(
at org.apache.camel.processor.Pipeline.process(
at org.apache.camel.processor.CamelInternalProcessor.process(
at org.apache.camel.component.jetty.CamelContinuationServlet.service(
at javax.servlet.http.HttpServlet.service(
at org.eclipse.jetty.servlet.ServletHolder.handle(
at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(
at org.eclipse.jetty.servlets.MultiPartFilter.doFilter(
at org.apache.camel.component.jetty.CamelFilterWrapper.doFilter(
at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(
at org.eclipse.jetty.servlet.ServletHandler.doHandle(
at org.eclipse.jetty.server.session.SessionHandler.doHandle(
at org.eclipse.jetty.server.handler.ContextHandler.doHandle(
at org.eclipse.jetty.servlet.ServletHandler.doScope(
at org.eclipse.jetty.server.session.SessionHandler.doScope(
at org.eclipse.jetty.server.handler.ContextHandler.doScope(
at org.eclipse.jetty.server.handler.ScopedHandler.handle(
at org.eclipse.jetty.server.handler.HandlerWrapper.handle(
at org.eclipse.jetty.server.Server.handle(
at org.eclipse.jetty.server.HttpChannel.handle(
at org.eclipse.jetty.server.HttpConnection.onFillable(
at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(
at org.eclipse.jetty.util.thread.QueuedThreadPool$
at Source)

The codes are:
javax.naming.Context ctx = new javax.naming.InitialContext();
Object o = ctx.lookup("somename");

Am I using a wrong Context class? Context is unknown class in Talend....
One Star

Re: [resolved] How to retrieve datasource with it's JNDI name in Talend Route?

Finally, I managed to achieve my requirements using are my codes in cConfig in Talend Route:
import org.osgi.framework.Bundle;
import org.osgi.framework.Constants;
import org.osgi.framework.ServiceReference;

Bundle[] bundles = bundleContext.getBundles();
for (Bundle bundle : bundles) {

ServiceReference[] services = bundle.getRegisteredServices();
if (services != null) {
for (ServiceReference serviceReference : services) {
String[] objectClasses = (String[])serviceReference.getProperty(Constants.OBJECTCLASS);
boolean t = false;
for(String s : objectClasses){
t = true;
Object actualService = bundleContext.getService(serviceReference);
System.out.println("actualService is " + actualService);
String jndiProperty = (String)serviceReference.getProperty("");
System.out.println("actualService JNDI is " + jndiProperty);


So actualService here refers to real data source and jndiProperty is its JNDI name.
One Star

Re: [resolved] How to retrieve datasource with it's JNDI name in Talend Route?