I have a java app that can be run both on tomcat and jboss.
I need to do a "if condition" for performing certain tasks based on the webserver type.
How can I access this information?
I need to do that because I need to connect to the datasource, and I can get the Context in different ways based on the webserver:
try{
String webserver = getWebServer();
Logger logger=Logger.getLogger("myLog");
if(webserver.equalsIgnoreCase("Jboss")){
logger.severe("Webserver: " + webserver);
Hashtable<String, String> ht= new Hashtable();
ht.put(InitialContext.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory");
ht.put(InitialContext.PROVIDER_URL,"jnp://localhost:1099");
ht.put(InitialContext.URL_PKG_PREFIXES,"org.jboss.naming:org.jnp.interfaces");
InitialContext ic=new InitialContext(ht);
if(ic!=null){
logger.severe("success");/*I am getting success as output here*/
DataSource ds=(DataSource)ic.lookup(getDatasource());
/*this is where it's failing*/
if(ds!=null){
logger.severe("success1");
}
return ds.getConnection();
}
return null;
}
else{ //TOMCAT
logger.severe("Webserver: " + webserver);
// Obtain our environment naming context
Context initCtx = new InitialContext();
Context envCtx = (Context) initCtx.lookup("java:comp/env");
// Look up our data source
DataSource ds = (DataSource) envCtx.lookup(getDatasource());
// Allocate and use a connection from the pool
Connection conn = ds.getConnection();
return conn;
}
}
Tomcat version doesn't work on jboss, and viceversa Thank you!