How to find war file name programmatically?

classic Classic list List threaded Threaded
4 messages Options
W
Reply | Threaded
Open this post in threaded view
|

How to find war file name programmatically?

W
Hi,
I would like to find the war file name (for example, ROOT##2019-08-12-10-44.war) inside  ServletContextListener.contextInitialized() and ServletContextListener.contextDestroyed(). So I can send email to admins warn them which app is up and down. 
Is there a way to do it?
Any information would be appreciated. Thanks in advance. 
Reply | Threaded
Open this post in threaded view
|

Re: How to find war file name programmatically?

markt
On 12/08/2019 23:18, W wrote:
> Hi,
> I would like to find the war file name (for example, ROOT##2019-08-12-10-44.war) inside  ServletContextListener.contextInitialized() and ServletContextListener.contextDestroyed(). So I can send email to admins warn them which app is up and down. 
> Is there a way to do it?

It isn't guaranteed to work in all circumstances but try:

ServletContext.getRealPath("/")

If that doesn't work then - assuming you can use reflection - something
along these lines:

Obtain the ServletConext.
Cast it to org.apache.catalina.core.ApplicationContext

Use reflection to read the context field or call getContext() which will
return a StandardContext instance.

Then you have various options:
- call getDocbase()
- call getPath() and getWebappVersion()

and there are probably plenty of other similar approaches that would work.

Mark

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: How to find war file name programmatically?

Christopher Schultz-2
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256

Mark,

On 8/13/19 04:43, Mark Thomas wrote:

> On 12/08/2019 23:18, W wrote:
>> Hi, I would like to find the war file name (for example,
>> ROOT##2019-08-12-10-44.war) inside
>> ServletContextListener.contextInitialized() and
>> ServletContextListener.contextDestroyed(). So I can send email to
>> admins warn them which app is up and down. Is there a way to do
>> it?
>
> It isn't guaranteed to work in all circumstances but try:
>
> ServletContext.getRealPath("/")
>
> If that doesn't work then - assuming you can use reflection -
> something along these lines:
>
> Obtain the ServletConext. Cast it to
> org.apache.catalina.core.ApplicationContext
>
> Use reflection to read the context field or call getContext() which
> will return a StandardContext instance.
>
> Then you have various options: - call getDocbase() - call getPath()
> and getWebappVersion()
>
> and there are probably plenty of other similar approaches that
> would work.

It seems like this might be an opportunity to add some information to
the context as a convenience for the application. I'm not sure it's
really relevant to the application what it's "host file/directory" is,
but it would be easy to add something like a context attribute
"org.apache.tomcat.context.source" which is of type java.io.File and
is either a directory or a WAR file.

- -chris
-----BEGIN PGP SIGNATURE-----
Comment: Using GnuPG with Thunderbird - https://www.enigmail.net/

iQIzBAEBCAAdFiEEMmKgYcQvxMe7tcJcHPApP6U8pFgFAl1S+aoACgkQHPApP6U8
pFgRkQ/+OTHo6Hdv+wZATo4ahnupSLIjYo58UJOFkdi1qu84x6S1ZqXxG4An6cfB
/lRskrn+7zn/dVgh4VJvYfZTZ2m+sGv8XqI+X7LRQPiwOXh2HXjxNLXjU3PeY08C
S6M6Nvc40MOHEYHylTVuKizRsNSEED50ekrDUwXx5TVH3ygV+Kn2doDN6y8uhc8P
VQEd4Z1e+yVPMFuJLY/utHsC8ey7tE7aioBLasAD14jyRMWtE3ePpnbf76cyhaLj
vD41XrPjYobdfmxAYyPdHiMSE+KPqL96ZjmPTom880Hjm8qIs4amTjrM7z831mNx
tCsv/5rI1C2b+Ol8gq69y0tP4rEkAwBAul5CV6ilNSJoDwGDjyt+e0lNsxBRBLJ3
POXWtGAGD3uov0BHrXcpqyYhxse5z6iTVLokNuFW6O3AS9NyhnWogYG9XzfGGLm6
+G6YaFGFSnvIkVCujYMYYHAfGEotHzYbRN/gPgh3sJdAwglDej9Cb8jVr1okeulg
F64R6TJCzUCAUhz4L7p6UDvKRzc+opRxd4QYrB7TlqnYFkK9UK90wA+BhOt404JQ
iq0wj2pl/cvRFOolxlJE488NonXYMvoNBZNQiwam144ONPKQ8+i82RFZeC3VSpUj
oAfDrYPc25sezg68NjA+XPPiQyI0yjwk+GChvzDYTCn//gUy33k=
=DMlI
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

W
Reply | Threaded
Open this post in threaded view
|

Re: How to find war file name programmatically?

W
In reply to this post by markt
 ServletContext.getRealPath("/") works for me.Thanks.
    On Tuesday, August 13, 2019, 01:43:35 AM PDT, Mark Thomas <[hidden email]> wrote:  
 
 On 12/08/2019 23:18, W wrote:
> Hi,
> I would like to find the war file name (for example, ROOT##2019-08-12-10-44.war) inside  ServletContextListener.contextInitialized() and ServletContextListener.contextDestroyed(). So I can send email to admins warn them which app is up and down. 
> Is there a way to do it?

It isn't guaranteed to work in all circumstances but try:

ServletContext.getRealPath("/")

If that doesn't work then - assuming you can use reflection - something
along these lines:

Obtain the ServletConext.
Cast it to org.apache.catalina.core.ApplicationContext

Use reflection to read the context field or call getContext() which will
return a StandardContext instance.

Then you have various options:
- call getDocbase()
- call getPath() and getWebappVersion()

and there are probably plenty of other similar approaches that would work.

Mark

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]