mainstringargs
mainstringargs

Reputation: 13923

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)

Upvotes: 4

Views: 537

Answers (7)

trunkc
trunkc

Reputation: 6293

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

Upvotes: 2

mjn
mjn

Reputation: 36684

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

Upvotes: 0

Jay
Jay

Reputation: 27512

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.

Upvotes: 0

sourcerebels
sourcerebels

Reputation: 5190

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

Upvotes: 4

Merzbow
Merzbow

Reputation:

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

Upvotes: 0

Brian Agnew
Brian Agnew

Reputation: 272427

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.

Upvotes: 2

Koraktor
Koraktor

Reputation: 42983

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

Upvotes: 0

Related Questions