I am having a Spring Boot application with embedded Tomcat server. My application does JNDI look up of datasource and config file URL. I am able to configure JNDI datasource but i am unable to configure JNDI URL in embedded Tomcat. Same application when deployed as WAR to standalone Liberty server is working fine as Liberty is configured properly with JNDI datasource and config file URL.
Also JNDI data sources and URLs are defined in spring config file in a jar of my application which is common and I cannot modify it but only import.
This is how i am trying to configure embedded tomcat
import java.net.URL;
import javax.sql.DataSource;
import org.apache.catalina.Context;
import org.apache.catalina.startup.Tomcat;
import org.apache.tomcat.util.descriptor.web.ContextResource;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.jmx.JmxAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainer;
import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ImportResource;
import org.springframework.context.annotation.Profile;
@SpringBootApplication(exclude = { JmxAutoConfiguration.class,DataSourceAutoConfiguration.class })
@ImportResource(locations = { "classpath:common-services.xml" })
public class MyApplication extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(MyApplication.class);
}
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
@Bean
@Profile("dev")
public TomcatEmbeddedServletContainerFactory tomcatFactory() {
return new TomcatEmbeddedServletContainerFactory() {
@Override
protected TomcatEmbeddedServletContainer getTomcatEmbeddedServletContainer(Tomcat tomcat) {
tomcat.enableNaming();
return super.getTomcatEmbeddedServletContainer(tomcat);
}
@Override
protected void postProcessContext(Context context) {
ContextResource resource = new ContextResource();
resource.setName("jdbc/myDataSource");
resource.setType(DataSource.class.getName());
resource.setProperty("driverClassName", "oracle.jdbc.OracleDriver");
resource.setProperty("url", "jdbc:oracle:thin:@sadasd");
resource.setProperty("username", "admin");
resource.setProperty("password", "admin");
resource.setProperty("factory", "org.apache.tomcat.jdbc.pool.DataSourceFactory");
context.getNamingResources().addResource(resource);
ContextResource queryOnlyDataSourceResource = new ContextResource();
queryOnlyDataSourceResource.setName("jdbc/queryOnlyDataSource");
queryOnlyDataSourceResource.setType(DataSource.class.getName());
queryOnlyDataSourceResource.setProperty("driverClassName", "oracle.jdbc.OracleDriver");
queryOnlyDataSourceResource.setProperty("url", "jdbc:oracle:thin:@osajodoajo");
queryOnlyDataSourceResource.setProperty("username", "admin");
queryOnlyDataSourceResource.setProperty("password", "admin");
queryOnlyDataSourceResource.setProperty("factory", "org.apache.tomcat.jdbc.pool.DataSourceFactory");
context.getNamingResources().addResource(queryOnlyDataSourceResource);
ContextResource dataSourceOrbisExport = new ContextResource();
dataSourceOrbisExport.setName("jdbc/exportDataSource");
dataSourceOrbisExport.setType(DataSource.class.getName());
dataSourceOrbisExport.setProperty("driverClassName", "oracle.jdbc.OracleDriver");
dataSourceOrbisExport.setProperty("url", "jdbc:oracle:thin:@saddalsdkla");
dataSourceOrbisExport.setProperty("username", "admin");
dataSourceOrbisExport.setProperty("password", "admin");
dataSourceOrbisExport.setProperty("factory", "org.apache.tomcat.jdbc.pool.DataSourceFactory");
context.getNamingResources().addResource(dataSourceOrbisExport);
// File Configuration
ContextResource engineConfigResource = new ContextResource();
evolutionEngineConfigResource.setName("url/engineConfig");
evolutionEngineConfigResource.setType(URL.class.getName()); resource.setProperty("protocol","file");
evolutionEngineConfigResource.setProperty("file", "C:/configuration_DIT/EngineConfig.config");
context.getNamingResources().addResource(engineConfigResource);
ContextResource evolutionPresentationConfigResource = new ContextResource();
presentationConfigResource.setName("url/presentationConfig");
presentationConfigResource.setType(URL.class.getName()); resource.setProperty("protocol","file");
evolutionPresentationConfigResource.setProperty("file", "C:/configuration_DIT/presentation.config");
context.getNamingResources().addResource(presentationConfigResource);
}
};
}
}
This is what error says :
required a bean of type 'java.net.URL' that could not be found.
Related cause: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'engineConfig': Invocation of init method failed; nested exception is javax.naming.NameNotFoundException: Name [url/engineConfig] is not bound in this Context. Unable to find [url].