Help configuring JNDI with embedded JBoss in Tomcat 5.5.x - java

When I try the following lookup in my code:
Context initCtx = new InitialContext();
Context envCtx = (Context) initCtx.lookup("java:comp/env");
return (DataSource) envCtx.lookup("jdbc/mydb");
I get the following exception:
java.sql.SQLException: QueryResults: Unable to initialize naming context:
Name java:comp is not bound in this Context at
com.onsitemanager.database.ThreadLocalConnection.getConnection
(ThreadLocalConnection.java:130) at
...
I installed embedded JBoss following the JBoss wiki instructions. And I configured Tomcat using the "Scanning every WAR by default" deployment as specified in the configuration wiki page.
Quoting the config page:
JNDI
Embedded JBoss components like connection pooling, EJB, JPA, and transactions make
extensive use of JNDI to publish services. Embedded JBoss overrides Tomcat's JNDI
implementation by layering itself on top of Tomcat's JNDI instantiation. There are a few > reasons for this:
To avoid having to declare each and every one of these services within server.xml
To allow seemeless integration of the java:comp namespace between web apps and
EJBs.
Tomcat's JNDI implementation has a few critical bugs in it that hamper some JBoss
components ability to work
We want to provide the option for you of remoting EJBs and other services that can > be remotely looked up
Anyone have any thoughts on how I can configure the JBoss naming service which according to the above quote is overriding Tomcat's JNDI implementation so that I can do a lookup on java:comp/env?
FYI - My environment Tomcat 5.5.9, Seam 2.0.2sp, Embedded JBoss (Beta 3),
Note: I do have a -ds.xml file for my database connection properly setup and accessible on the class path per the instructions.
Also note: I have posted this question in embedded Jboss forum and seam user forum.

Thanks for the response toolkit.... yes, I can access my datasource by going directly to java:jdbc/mydb, but I'm using an existing code base that connects via the ENC. Here's some interesting info that I've found out ....
The above code works with JBoss 4.2.2.GA and here's the JNDI ctx parameters being used:
java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces:
org.jboss.naming:org.jnp.interfaces
The above code works with Tomcat 5.5.x and here's the JNDI ctx parameters being used:
java.naming.factory.initial=org.apache.naming.java.javaURLContextFactory
java.naming.factory.url.pkgs=org.apache.naming
The above code fails with Embedded JBoss (Beta 3) in Tomcat 5.5.x with the above error message.
java.naming.factory.initial=org.apache.naming.java.javaURLContextFactory
java.naming.factory.url.pkgs=org.apache.namingThe above code fails with the above error using JBoss Embedded in tomcat 5.5.x
Anyone have any thoughts I what I need to do with configuring embedded JBoss JNDI configuration?

java:comp/env is known as the Enterprise Naming Context (ENC) and is not globally visible. See here for more information. You will need to locate the global JNDI name which your datasource is regsitered at.
The easiest way to do this is to navigate to JBoss' web-based JMX console and look for a 'JNDIView' (not exactly sure of the name - currently at home) mbean. This mbean should have a list method which you can invoke, which will display the context path for all of the JNDI-bound objects.

I had some similar issue with Jboss Embedded and i finally fix playing in the file:
test-Datasource-ds.xml
adding
<mbean code="org.jboss.naming.NamingAlias" name="jboss.jmx:alias=testDatasource">
<attribute name="FromName">jdbc/Example DataSource</attribute>
<attribute name="ToName">java:/testDatasource</attribute>
</mbean>
The problem was jboss add the prefix java:/ for all data source declared. So finally i had a datasource named testDatasource, overrided with that directive to jdbc/Example DataSource
Hope it works

Related

Not able to access JNDI Url context from Spring boot application deployed in WAS Liberty server

I have deployed an spring boot web application in WAS Liberty server (WebSphere Application Server Version 8.5.5.9 Liberty Profile).
I have a JNDI url entry added in server.xml which is given below but one of my class in api is not able to access it.
<jndiURLEntry jndiName="url/SSOService" value="http://ssoserver.dev.intranet/SSOService/SSOFacade" />
I found one weird thing in Liberty is that whenever I add java:comp/env/ to any of the JNDI entry , application is not able to pick it and getting javax.naming.NameNotFoundException:
I fixed the datasource issue by not adding the prefix, but the above URL is used in only of the api which is not in my control.
So how can we enable java:com/env prefix in Websphere Liberty server ?
or is there any alternative available to make this working ?
When you define a JNDI URL entry in the following way:
<jndiURLEntry jndiName="url/SSOService" value="..."/>
It will make the java.net.URL available literally at the url/SSOService name (i.e. no sort of java: prefix). Looking up a JNDI entry from this location is known as a "direct lookup", because you are looking something up that was defined directly in server configuration.
If you want to register this in the java:comp/env/ namespace, you must define a "resource reference", which will create a binding from the server-defined entry to a java:comp/env/ entry. A resource reference can be defined annotatively or via web.xml.
Here is how you can define an annotative resource reference:
#Resource(lookup="url/SSOService", name="url/SSOServiceRef")
URL ssoServiceURL;
In this example, we're saying:
Perform a direct lookup of url/SSOService
Bind that object to java:comp/env/url/SSOServiceRef

Is it possible to query/browse the JNDI tree in WebSphere Liberty?

I have a EJB with a remote interface that I want to access from a client. I have difficulties to build the string for the JNDI lookup. Is there a command line tool which shows all register enterprise beans in the naming service? Or can I see this in a log file?
The JNDI name should look like this:
corbaname:localhost:2809#ejb/global/MyApp/MyModule/EJBName!full.package.remote.interface.Name
I’m also not really sure if my app and module name in the string is correct.
As far as I know there is no way to dump the JNDI namespace on WebSphere Liberty (there is a way to do this in WebSphere traditional).
The easiest way to check what JNDI name your EJBs are registered at, is to check the messages.log file. In the messages.log file you should see some CNTR0167I messages which indicate where the server has bound your EJBs. The messages.log file can be found in ${server.config.dir}/logs/messages.log.
Example EJB binding message:
CNTR0167I: The server is binding the com.example.DatabaseBean interface of the DatabaseBean enterprise bean in the TestProject.war module of the TestProject application. The binding location is: java:global/TestProject/DatabaseBean!com.example.DatabaseBean
For more info, check out the IBM doc:
Using enterprise JavaBeans with remote interfaces on Liberty

JNDI lookups in OSGi in the bridged mode

Background
I am embedding an OSGi runtime based on Apache Felix in the bridged mode in a Tomcat6 container. There are a bunch of JDBC connections specified in the conf/server.xml file in Tomcat6.
<Resource name="foo/bar" ... />
Question
The standard JNDI lookup doesn't seem to work from bundles deployed to the OSGi container.
ctx.lookup("java:comp/env/foo/bar");
Is there anything special that needs to be done for the OSGi runtime to be able to lookup resources registered in the parent Tomcat6 container?
#Martin's comment got me thinking about multiple threads part. I got this to work by:
Ensuring that ctx.lookup(...) is invoked in the BundleActivator. This is because, in the bridged mode, the ClassLoader of the thread running the Activator code can look up all Classes and Resources that are defined in the parent WAR file.
Ensuring that the parent WAR's META-INF/context.xml exposes the JNDI resource via <ResourceLink global="foo/bar" name="foo/bar" type="javax.sql.DataSource"/>

Datasource configuration in java

I have one query regarding datasource configuration.
I had configured one data source in server. Where my JNDI name is "MyDS".
To acquire the data-source in JBoss environment i had used like "java:/MyDS" in persistence, spring even in Birt reports.
But when i am running same thing in Tomcat environment, then it is showing error that unable to get the data-source.
I had googled about tomcat datasource communication, where i had seen like "jdbc/MyDS"
So, my query is what is the difference between above two types of configurations? (java:/MyDS and jdbc/MyDS) and how can i use the common configuration for both the servers (tomcat6 and jboss 5.1.0.GA).
Please help me.

JBoss JNP as standalone server and using Resource configs from tomcat's context.xml

I have a couple of modules which now have the need of JNDI.
One of these modules runs via Apache Tomcat while the rest run standalone as J2SE application.
I was able to configure the module which uses tomcat without a problem and I've googled a bit and gathered that JBoss JNP can be a good standalone JNDI server to use.
What I fail to understand is how I can reuse the resources definitions which I have already configured (for my module which runs via tomcat in the context.xml file).
Let's say I have the following resource defined in the XML file:
<Resource name="jdbc/dataSource" auth="Container" type="javax.sql.DataSource"
driverClassName="com.mchange.v2.c3p0.ComboPooledDataSource"
url="jdbc:sqlserver://******
username="**" password="**"/>
And the relevant code which starts the JNP is:
System.setProperty("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
System.setProperty("java.naming.factory.url.pkgs", "org.jboss.naming:org.jnp.interfaces");
NamingBeanImpl jnpServer = new NamingBeanImpl();
jnpServer.start();
How can I have an InitialContext instance identify the jdbc/dataSource binding?
I don't think you can.
You probably can define your jdbs resource in jboss naming context and look it up in your tomcat web app.
More info here: http://www.amitysolutions.com.au/documents/JBossTomcatJNDI-technote.pdf

Categories