You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Juha Laiho <Ju...@iki.fi> on 2007/07/01 11:21:14 UTC

How to inject a webapp listener?

Hello,

I would like to have the Tomcat container to inject a specific
ServletContextListener to each loaded webapp on my server - without
touching the individual web.xml files, and am looking for advise on
how to achieve this.

In other words, I'd like to have the container act as if there was
<listener>
  <listener-class>MyListenerClass</listener-class>
</listener>
section in the web.xml file for each application?

Currently, I have added a ContainerListener on my Host, which
is listening on ADD_CHILD messages to discover newly added
StandardContext instances, and performs the following on each
new StandardContext (where ctx is a reference to the added context):

  Object[] listeners = ctx.getApplicationLifecycleListeners();
  Object[] newListeners = new Object[listeners.length+1];
  System.arraycopy(listeners,0,newListeners,0,listeners.length);
  newListeners[newListeners.length-1]=ctxListener;
  ctx.setApplicationLifecycleListeners(newListeners);

With this, my listener is already able to capture the contextDestroyed
events from the webapp - but not the contextInitialized. It looks like the
Context is added to the host only after it has been already initialized.

What I'm looking for is a way to be able to inject the webapp listener
in an earlier phase, so that I can also capture the contextInitialized
messages. Any ideas? Also, if there's an overall better way to inject
the webapp listener, please let me know -- the above way feels rather
crude.

Thanks,
-- 
..Juha

---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


RE: How to inject a webapp listener?

Posted by "Caldarale, Charles R" <Ch...@unisys.com>.
> From: Juha Laiho [mailto:Juha.Laiho@iki.fi] 
> Subject: How to inject a webapp listener?
> 
> I would like to have the Tomcat container to inject a specific
> ServletContextListener to each loaded webapp on my server - without
> touching the individual web.xml files, and am looking for advise on
> how to achieve this.

Add it to the existing $CATALINA_HOME/conf/web.xml file.  This file
becomes part of every webapp's configuration, unless overridden by a
particular WEB-INF/web.xml's settings.  Note that this will also affect
the various webapps supplied with Tomcat.

 - Chuck


THIS COMMUNICATION MAY CONTAIN CONFIDENTIAL AND/OR OTHERWISE PROPRIETARY
MATERIAL and is thus for use only by the intended recipient. If you
received this in error, please contact the sender and delete the e-mail
and its attachments from all computers.

---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: How to inject a webapp listener?

Posted by Juha Laiho <Ju...@iki.fi>.
Thanks,

the reason why I believe I need to do this from the context level
is that the code running in the listener needs to access the context
classloader of the individual webapps - and they seem to me at least
be most straightforward to access from the webapp listener.

Of course, a way to access the individual webapp context classloaders
from ADD_CHILD/REMOVE_CHILD events of the container would be a good
alternative (definitely more clean approach than my current idea of
injecting the listener).
-- 
..Juha

David Delbecq wrote:
> Am not sure, but, whatever job your injected contextlistener is supposed
> to do, it could perhaps be done in the container listener without
> ressorting to context listener, isn't it? (using ADD_CHILD for the
> 'context initialized' and REMOVE_CHILD for contect destroyed)?
> As for calling init on your injected listern, is suppose you will have
> to do it yourself.
> 
> Juha Laiho a écrit :
>> Hello,
>>
>> I would like to have the Tomcat container to inject a specific
>> ServletContextListener to each loaded webapp on my server - without
>> touching the individual web.xml files, and am looking for advise on
>> how to achieve this.
>>
>> In other words, I'd like to have the container act as if there was
>> <listener>
>>  <listener-class>MyListenerClass</listener-class>
>> </listener>
>> section in the web.xml file for each application?
>>
>> Currently, I have added a ContainerListener on my Host, which
>> is listening on ADD_CHILD messages to discover newly added
>> StandardContext instances, and performs the following on each
>> new StandardContext (where ctx is a reference to the added context):
>>
>>  Object[] listeners = ctx.getApplicationLifecycleListeners();
>>  Object[] newListeners = new Object[listeners.length+1];
>>  System.arraycopy(listeners,0,newListeners,0,listeners.length);
>>  newListeners[newListeners.length-1]=ctxListener;
>>  ctx.setApplicationLifecycleListeners(newListeners);
>>
>> With this, my listener is already able to capture the contextDestroyed
>> events from the webapp - but not the contextInitialized. It looks like
>> the
>> Context is added to the host only after it has been already initialized.
>>
>> What I'm looking for is a way to be able to inject the webapp listener
>> in an earlier phase, so that I can also capture the contextInitialized
>> messages. Any ideas? Also, if there's an overall better way to inject
>> the webapp listener, please let me know -- the above way feels rather
>> crude.
>>
>> Thanks,
> 
> ---------------------------------------------------------------------
> To start a new topic, e-mail: users@tomcat.apache.org
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
> 


---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: How to inject a webapp listener?

Posted by David Delbecq <de...@oma.be>.
Am not sure, but, whatever job your injected contextlistener is supposed
to do, it could perhaps be done in the container listener without
ressorting to context listener, isn't it? (using ADD_CHILD for the
'context initialized' and REMOVE_CHILD for contect destroyed)?
As for calling init on your injected listern, is suppose you will have
to do it yourself.

Juha Laiho a écrit :
> Hello,
>
> I would like to have the Tomcat container to inject a specific
> ServletContextListener to each loaded webapp on my server - without
> touching the individual web.xml files, and am looking for advise on
> how to achieve this.
>
> In other words, I'd like to have the container act as if there was
> <listener>
>  <listener-class>MyListenerClass</listener-class>
> </listener>
> section in the web.xml file for each application?
>
> Currently, I have added a ContainerListener on my Host, which
> is listening on ADD_CHILD messages to discover newly added
> StandardContext instances, and performs the following on each
> new StandardContext (where ctx is a reference to the added context):
>
>  Object[] listeners = ctx.getApplicationLifecycleListeners();
>  Object[] newListeners = new Object[listeners.length+1];
>  System.arraycopy(listeners,0,newListeners,0,listeners.length);
>  newListeners[newListeners.length-1]=ctxListener;
>  ctx.setApplicationLifecycleListeners(newListeners);
>
> With this, my listener is already able to capture the contextDestroyed
> events from the webapp - but not the contextInitialized. It looks like
> the
> Context is added to the host only after it has been already initialized.
>
> What I'm looking for is a way to be able to inject the webapp listener
> in an earlier phase, so that I can also capture the contextInitialized
> messages. Any ideas? Also, if there's an overall better way to inject
> the webapp listener, please let me know -- the above way feels rather
> crude.
>
> Thanks,

---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: How to inject a webapp listener?

Posted by Martin Gainty <mg...@hotmail.com>.
I would suggest spring ..but springs injected entities are *usually* 
classical pojos such as DBDrivers

Here is a project which shows great promise of injecting whole listeners 
which may be of help for your effort
http://oranjestad.sourceforge.net/spring-oranjestad.html

hyva?
Martin-

This email message and any files transmitted with it contain confidential
information intended only for the person(s) to whom this email message is
addressed.  If you have received this email message in error, please notify
the sender immediately by telephone or email and destroy the original
message without making a copy.  Thank you.

----- Original Message ----- 
From: "Juha Laiho" <Ju...@iki.fi>
To: <us...@tomcat.apache.org>
Sent: Sunday, July 01, 2007 5:21 AM
Subject: How to inject a webapp listener?


> Hello,
>
> I would like to have the Tomcat container to inject a specific
> ServletContextListener to each loaded webapp on my server - without
> touching the individual web.xml files, and am looking for advise on
> how to achieve this.
>
> In other words, I'd like to have the container act as if there was
> <listener>
>  <listener-class>MyListenerClass</listener-class>
> </listener>
> section in the web.xml file for each application?
>
> Currently, I have added a ContainerListener on my Host, which
> is listening on ADD_CHILD messages to discover newly added
> StandardContext instances, and performs the following on each
> new StandardContext (where ctx is a reference to the added context):
>
>  Object[] listeners = ctx.getApplicationLifecycleListeners();
>  Object[] newListeners = new Object[listeners.length+1];
>  System.arraycopy(listeners,0,newListeners,0,listeners.length);
>  newListeners[newListeners.length-1]=ctxListener;
>  ctx.setApplicationLifecycleListeners(newListeners);
>
> With this, my listener is already able to capture the contextDestroyed
> events from the webapp - but not the contextInitialized. It looks like the
> Context is added to the host only after it has been already initialized.
>
> What I'm looking for is a way to be able to inject the webapp listener
> in an earlier phase, so that I can also capture the contextInitialized
> messages. Any ideas? Also, if there's an overall better way to inject
> the webapp listener, please let me know -- the above way feels rather
> crude.
>
> Thanks,
> -- 
> ..Juha
>
> ---------------------------------------------------------------------
> To start a new topic, e-mail: users@tomcat.apache.org
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>
> 


---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org