2

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!

user1820620
  • 672
  • 2
  • 13
  • 27
  • 1
    Why would you need to do that? – Kayaman Jan 31 '14 at 08:47
  • What sort of tasks do you want to do? It's unusual because the point of a war is that it can run in any appserver and shouldn't know about it. – mikea Jan 31 '14 at 08:47
  • I update the question to give more infos – user1820620 Jan 31 '14 at 08:55
  • Refer this link http://stackoverflow.com/questions/35842/how-can-a-java-program-get-its-own-process-id – user1428716 Jan 31 '14 at 08:56
  • Do a request and read the header with some luck you get a hint which server is used. I understand that your app can work in any server and you propably don't have control over the server, right ? – PeterMmm Jan 31 '14 at 08:57

2 Answers2

2

Quite unusual requirement, however can be achieved using System property. In server startup script pass System property in JAVA_OPTS like:

-Dserver=tomcat

Pass different values for different server and then You can read this system property by following code:

System.getProperty("server");
Ankit
  • 3,083
  • 7
  • 35
  • 59
  • Isn't it better to put this parameter into the webdescriptor of the webapp ? – PeterMmm Jan 31 '14 at 09:05
  • @PeterMmm: Usually yes, but not for this case, as the user wants to use same web app in two application servers. – Ankit Jan 31 '14 at 09:13
2

Since your application can run on Tomcat which is a simple servlet container then you might want to use ServletContext#getServerInfo(). to get Information about the environment your application is running inside.

A4L
  • 17,353
  • 6
  • 49
  • 70