Reputation: 24457
Is it possible to list the avaiable JNDI datasources for the current application? If yes, how can I do this.
Upvotes: 1
Views: 830
Reputation: 17839
Here is some sample code to try in a Servlet:
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/plain");
PrintWriter out = response.getWriter();
try {
InitialContext ictx = new InitialContext();
Context ctx = (Context) ictx.lookup("java:");
out.println("java: = " + ctx.getClass().getName());
printContext(out, ctx, 1);
} catch (Exception exc) {
throw new ServletException(exc);
}
}
private void printContext(PrintWriter out, Context ctx, int indent) throws ServletException, IOException, NamingException {
NamingEnumeration en = ctx.listBindings("");
while (en.hasMore()) {
Binding b = (Binding) en.next();
char[] tabs = new char[indent];
Arrays.fill(tabs, '\t');
out.println(new String(tabs) + b.getName() + " = " + b.getClassName());
try {
if (b.getObject() instanceof Context) {
printContext(out, (Context) b.getObject(), indent + 1);
}
} catch (Exception exc) {
throw new ServletException(exc);
}
}
}
Try it out and let me know if it works
Upvotes: 1