Question

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)
Was it helpful?

Solution

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;
}

OTHER TIPS

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.

The easiest way is, to check the existence of Java EE/App Server specific classes.

I never used an application server, but maybe you'll be able to achieve this with System.getProperties() / System.getProperty(...)

Consider checking for the current SecurityManager, if your application server uses one.

I don't think there's any way to determine this directly. Yes, as SourceRebel says you could set a system property. Personally I'd avoid doing this, though, as you then have some hidden coupling going on: your function is dependent on a system property that must be set correctly for it to work, but there is nothing clearly defined in the interface to reflect this. I think you'd be far better off to just pass in a parameter that says which it is, and let the caller be responsible to pass in the correct parameter. Then the existence of this parameter can be clearly seen in the function signature, and anyone using it will have a strong clue that they need to set it correctly. Having the caller set it correctly should be trivial, as presumably at some point in the call chain you are either calling from a desktop app or from a web page, and that caller knows which it is.

Some applications server set system properties, JBoss for example: http://community.jboss.org/wiki/JBossProperties

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top