tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Michael <sg...@gmx.net>
Subject Re: Retrieve context path in lifecycle listener
Date Thu, 08 Mar 2012 17:50:59 GMT
Konstantin Kolinko schrieb:
> 2012/3/6  <sgfan@gmx.net>:
>> Hi folks,
>>
>> I'd like to retrieve the context path from the a startup event in a context lifecycle
listener. My usecase is register that contextpath in the initial context and access its value
through Logback.
>> Unfortunately, LifecycleEvent does not seem to contain this data.
>>
> 
> 1. Exact Tomcat version =?
> 
> 2. LifecycleEvent extends EventObject,  EventObject.getSource() ?

Thanks für the pointer, I have managed to make it work.
To whom it might be interesting, here's the code:

if (le.getLifecycle() instanceof Context)
       context = (Context) le.getLifecycle();
     else
       return;

     if (le.getType().equals(Lifecycle.START_EVENT)) {
       ContextEnvironment ce = new ContextEnvironment();
       ce.setName(getName());
       ce.setOverride(false);
       ce.setType("java.lang.String");
       String value = StringUtils.remove(context.getServletContext()
           .getContextPath(), '/');
       ce.setValue(value);
       logger.debug(String.format("Adding env entry '%s' with value '%s'",
           getName(), value));
       context.getNamingResources().addEnvironment(ce);
     }

     if (le.getType().equals(Lifecycle.STOP_EVENT)) {
       logger.debug(String.format("Removing env entry '%s'", getName()));
       context.getNamingResources().removeEnvironment(name);
     }


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


Mime
View raw message