Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can Java Code tell if it is in an App Server?

Is there something I can call from a POJO to see if the code is currently in an App Server or outside of an App Server?

Something like this (In rough PseudoCode):

System.getRunningEnvironment().equals(Environment.Glassfish)

or

System.getRunningEnvironment().equals(Environment.ApplicationServer)

or

System.getRunningEnvironment().equals(Environment.JavaSE)
like image 740
mainstringargs Avatar asked Mar 01 '23 21:03

mainstringargs


2 Answers

If you can change AppServer initialization scripts (take a look at this link):

Add -DRunningInAppServer=true at your AppServer initialization script.

Add -DRunningInAppServer=false at your application initialization script.

Then use this method:

public boolean isRunningInAppServer() {

        if ("true".equals(System.getProperty("RunningAppServer"))) {
            return true;
        }
        return false;
}
like image 63
sourcerebels Avatar answered Mar 12 '23 08:03

sourcerebels


I don't believe you can do this trivially. And would you want to distinguish between an app server, a web container etc.?

What is the reason for determining this ? To allow your POJOs to behave differently in different environments ? If so then I think this points to an object/component structure that is not quite correct, or at least where the object responsibilities are not clearly defined.

like image 25
Brian Agnew Avatar answered Mar 12 '23 06:03

Brian Agnew