Can Java code determine if it is on the application server?

Is there something I can call from POJO to find out if this code is currently on the application server or outside the application server?

Something like this (in a rough PseudoCode):

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

or

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

or

 System.getRunningEnvironment().equals(Environment.JavaSE) 
+4
source share
7 answers

If you can change AppServer initialization scripts (see this link ):

Add -DRunningInAppServer = true when initializing the AppServer script.

Add -DRunningInAppServer = false when initializing your application script.

Then use this method:

 public boolean isRunningInAppServer() { if ("true".equals(System.getProperty("RunningAppServer"))) { return true; } return false; } 
+4
source

The easiest way is to check for Java EE / App Server classes.

+2
source

I do not believe that you can do this trivially. And you want to distinguish between application server, web container, etc.?

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

+2
source

I have never used an application server, but maybe you can achieve this with System.getProperties() / System.getProperty(...)

0
source

Consider checking the current SecurityManager if your application server uses it.

0
source

I do not think there is a way to define this directly. Yes, since SourceRebel says you can set the system property. Personally, I would not do this, since then you had some kind of hidden connection: your function depends on a system property that must be correctly configured for it to work, but there is nothing clearly defined in the interface to reflect this, I think you it would be much better if you just pass in a parameter that says what it is and let the caller answer for the correct parameter. Then the existence of this parameter can be clearly seen in the signature of the function, and anyone who uses it will have a clear idea that they need to set it correctly. The correct assignment of the caller should be trivial, since presumably at some point in the call chain you are either calling from a desktop application or from a web page, and this caller knows what it is.

0
source

Some server application systems, for example, JBoss: http://community.jboss.org/wiki/JBossProperties

0
source

All Articles