Skip to main content

Get application context root through JNDI

2 replies [Last post]
sohan
Offline
Joined: 2012-12-01
Points: 0

Hi,

Is there a way to get the context-root of the currently running application at runtime? The name of the application is no problem:

String appName = (String)InitialContext.doLookup("java:app/AppName")

Likewise, I tried:

String appContextRoot = (String)InitialContext.doLookup("java:app/ContextRoot")

but no dice. When I look at the application details in the admin panel, I see a number of "properties":

    Name
    Status
    Virtual server
    Context root
    Description
    Location
    Libraries
    Modules

I would expect that all these properties can be queried, but so far I only succeeded in getting the Name of the application. Is it possible to get to the others as well?

I need to retrieve the context-root from a static method, so I cannot use HttpRequest.

Cheers,
sohan

Glassfish 3.1.2
JDK 1.7

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Harald Wellmann

Have you tried the following names defined in the Servlet 3.0 specification:

15.2.3 JNDI Name for Web Module Context Root URL

The name of the pre-defined java.net.URL resource for the context
root of a web application has the following syntax:

java:global[/]/!ROOT in the global namespace
and

java:app/!ROOT in the application-specific namespace.

Best regards,
Harald

Am 01.12.2012 16:17, schrieb :
> Hi, Is there a way to get the context-root of the currently running
> application at runtime? The name of the application is no problem: String
> appName = (String)InitialContext.doLookup("java:app/AppName") Likewise, I
> tried: String appContextRoot =
> (String)InitialContext.doLookup("java:app/ContextRoot") but no dice. When I
> look at the application details in the admin panel, I see a number of
> "properties":
> Name Status Virtual server Context root Description Location Libraries
> Modules
>
> I would expect that all these properties can be queried, but so far I only
> succeeded in getting the Name of the application. Is it possible to get to
> the others as well? I need to retrieve the context-root from a static
> method,
> so I cannot use HttpRequest. Cheers, sohan Glassfish 3.1.2 JDK 1.7
>
> --
>
> [Message sent by forum member 'sohan']
>
> View Post: http://forums.java.net/node/892853
>
>

sohan
Offline
Joined: 2012-12-01
Points: 0

Yes, I saw that mentioned somewhere. I already gave it a try, but without result. Because you also bring it up, and because you give a slightly different syntax as stated in the spec, I decided to look at it once more. I tried all syntaxes I can think of:

"java:global!ROOT",
"java:global/!ROOT",
"java:global//!ROOT",
"java:global/ROOT",
"java:global//ROOT",
"java:global/$app!ROOT",
"java:global/$app/!ROOT",
"java:global//$app/!ROOT",
"java:global/$app/ROOT",
"java:global//$app/ROOT",
"java:global/$app/$app!ROOT",
"java:global//$app/$app!ROOT",
"java:global/$app/$app/!ROOT",
"java:global//$app/$app/!ROOT",
"java:global/$app/$app/ROOT",
"java:global//$app/$app/ROOT",
"java:app!ROOT",
"java:app/!ROOT",
"java:app//!ROOT",
"java:app/ROOT",
"java:app//ROOT",
"java:app/$app!ROOT",
"java:app//$app!ROOT",
"java:app/$app/!ROOT",
"java:app//$app/!ROOT",
"java:app/$app/ROOT",
"java:app//$app/ROOT",
("$app" gets substituted with InitialContext.doLookup("java:app/AppName"))

They all result in a lookup failure. I also tried "Root" and "root" instead of "ROOT", and even "CONTEXT_ROOT", "CONTEXT-ROOT", "CONTEXTROOT", in different uppercase/lowercase variations. Nothing seems to work.

Since this is clearly described in the spec, perhaps it should be reported to the Glassfish team. Any idea how to do that?

Cheers and thanks,
sohan