Reputation: 123
What I want to achieve is to get the JNDI name as a String from a DataSource object.
I have the following code to get the DataSource:
Context ctx = new InitialContext();
DataSource ds = (DataSource) ctx.lookup("java:/comp/env/dataPool");
My weblogic-ejb-jar.xml
where JNDI name is set:
<?xml version="1.0" encoding="UTF-8"?>
<weblogic-ejb-jar xmlns="http://xmlns.oracle.com/weblogic/weblogic-ejb-jar" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.oracle.com/weblogic/weblogic-ejb-jar http://xmlns.oracle.com/weblogic/weblogic-ejb-jar/1.6/weblogic-ejb-jar.xsd">
<weblogic-enterprise-bean>
<ejb-name>AdminBean</ejb-name>
<stateless-session-descriptor></stateless-session-descriptor>
<resource-description>
<res-ref-name>dataPool</res-ref-name>
<jndi-name>jdbc/CARGAS</jndi-name>
</resource-description>
</weblogic-enterprise-bean>
</weblogic-ejb-jar>
So, when I get the ds
object actually I can see the JndiNames but I cannot find the way to get it out from it:
My first guess was to do something like ds.getJndiNames
but I only have the folowing options:
Any ideas how to do it?
Upvotes: 1
Views: 2495
Reputation: 123
Ok!
So at the end I use Reflection to achieve what I want to.
Here my solution:
public String getJndiName() {
try {
Context ctx = new InitialContext();
DataSource ds = (DataSource) ctx.lookup("java:/comp/env/dataPool");
Method method = ds.getClass().getMethod("getJNDINames");
String[] jndi = (String[])method.invoke(ds);
return jndi[0];
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
Hope it will be helpfull for anyone else in the future :-)
Upvotes: 0