In tomcat if we put context.xml file in META-INF folder tomcat create resource for us and we can lookup that resource. Here is my context file:
<Context>
<Resource
name="jdbc/referenceData"
auth="Container"
type="javax.sql.DataSource"
description="Reference Data "
username=" "
password=""
driverClassName="org.hsqldb.jdbcDriver"
url=" "/>
</Context>
I am trying to get same functionality on spring-boot. I have overridden the following method of TomcatEmbeddedServletContainer:
@Override
protected TomcatEmbeddedServletContainer getTomcatEmbeddedServletContainer(
Tomcat tomcat) {
tomcat.enableNaming();
return super.getTomcatEmbeddedServletContainer(tomcat);
}
@Override
protected void postProcessContext(Context context) {
ContextResource resource = new ContextResource();
resource.setAuth("Container");
resource.setName("jdbc/referenceData");
resource.setType(DataSource.class.getName());
resource.setProperty("driverClassName", "org.hsqldb.jdbcDriver");
resource.setProperty("url", "url of jndi");
resource.setProperty("password", "");
resource.setProperty("username", "");
context.getNamingResources().addResource(resource);
}
But the problem is I need to look-up this JNDI resource in configuration class (@Configuration annotated class). Here is the code when I look up this resource:
Context ctx = new InitialContext();
(DataSource) ctx.lookup(referenceJndiName)
I get javax.naming.NameNotFoundException: Name [jdbc/referenceData] is not bound in this Context. Because @Configuration class called before tomcat is fully ready. I have tried to look up by "java:comp/env/jdbc/referenceData" but same result. I have also tried with application.properties file but no luck. Every time give the error: resource is not bound in this context.
Is there any way like cargo-maven2-plugin does: it can copy defined context.xml to context.xml.default of embaded tomcat. Is spring-boot-maven-plugin doing anything like that? Or can we load JNDI resource and look-up on startup?
Thanks for the time and any help will be highly appreciated.