Konstantin Kolinko schrieb:
2012/3/6  <sg...@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-unsubscr...@tomcat.apache.org
For additional commands, e-mail: users-h...@tomcat.apache.org

Reply via email to