简体   繁体   中英

Use JNDI lookup to check for being in an application server?

I have a library that can be used both inside an application server like Wildfly, or outside. It shall behave differently, when running in an Enterprise Java environment and the question is - how can I determine where I'm running, without creating a dependency on any 3rd party library?

The idea was to use a simple JNDI lookup, but I wonder what I would look up?

There are lots of questions here explaining the different JNDI namespaces, but I fail to find a list of actual objects you'd expect to find in the directory.

So, can anyone point to more specific documentation of stuff to find in the java:global, java:app, or java:jboss namespaces? Looking for things that "are always there".

Can I use this approach, for example to distinguish between Wildfly and Glassfish, and - let's say - find out the version I'm running on?

Or is there a different approach to determine what kind of context I'm running in?

I would instead use the fact that there are a bunch of other classes available to you when you're in a JEE environment. For example:

try {
    Class<?> clazz = Class.forName("javax.servlet.http.HttpServlet");
}
catch( ClassNotFoundException classNotFoundException ) {
    // not on an Java EE server       
}

That does not tell you the version or other information about what you're running on. That part usually requires some JMX calls to determine. For example, for Wildfly/JBoss, take a look here for some details. Properly written it shouldn't matter for recent versions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM