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!

5
  • 1
    Why would you need to do that? Commented Jan 31, 2014 at 8: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. Commented Jan 31, 2014 at 8:47
  • I update the question to give more infos Commented Jan 31, 2014 at 8:55
  • Refer this link stackoverflow.com/questions/35842/… Commented Jan 31, 2014 at 8: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 ? Commented Jan 31, 2014 at 8:57

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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");

2 Comments

Isn't it better to put this parameter into the webdescriptor of the webapp ?
@PeterMmm: Usually yes, but not for this case, as the user wants to use same web app in two application servers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.