Java Code Examples for javax.servlet.ServletContextListener

The following code examples are extracted from open source projects. You can click to vote up the examples that are useful to you.

Example 1

From project org.ops4j.pax.web, under directory /pax-web-jetty/src/main/java/org/ops4j/pax/web/service/jetty/internal/.

Source file: HttpServiceContext.java

  21 
vote

@Override protected void doStart() throws Exception {
  if (servletContainerInitializers != null) {
    for (    final Entry<ServletContainerInitializer,Set<Class<?>>> entry : servletContainerInitializers.entrySet()) {
      ServletContextListener listener=new ServletContextListener(){
        ServletContainerInitializer sci=entry.getKey();
        Set<Class<?>> clazzes=entry.getValue();
        @Override public void contextInitialized(        ServletContextEvent sce){
          try {
            sci.onStartup(clazzes,_scontext);
          }
 catch (          ServletException ignore) {
            LOG.error("Startup issue with ServletContainerInitializer",ignore);
          }
        }
        @Override public void contextDestroyed(        ServletContextEvent sce){
        }
      }
;
      this.addEventListener(listener);
    }
  }
  this.setVirtualHosts(virtualHosts.toArray(EMPTY_STRING_ARRAY));
  this.setConnectorNames(connectors.toArray(EMPTY_STRING_ARRAY));
  if (jettyWebXmlURL != null) {
    DOMJettyWebXmlParser jettyWebXmlParser=new DOMJettyWebXmlParser();
    jettyWebXmlParser.parse(this,jettyWebXmlURL.openStream());
  }
  super.doStart();
  if (m_attributes != null) {
    for (    Map.Entry<String,?> attribute : m_attributes.entrySet()) {
      _scontext.setAttribute(attribute.getKey(),attribute.getValue());
    }
  }
  LOG.debug("Started servlet context for http context [" + m_httpContext + "]");
}
 

Example 2

From project jsf-test, under directory /stage/src/main/java/org/jboss/test/faces/staging/.

Source file: StagingServer.java

  19 
vote

@Override public void init(){
  log.info("Init staging server");
  readDefaultMimeTypes();
  JspFactory.setDefaultFactory(new StaggingJspFactory(this.context));
  context.addInitParameters(initParameters);
  ClassLoader loader=Thread.currentThread().getContextClassLoader();
  if (null == loader) {
    loader=this.getClass().getClassLoader();
  }
  this.contextProxy=(ServletContext)Proxy.newProxyInstance(loader,new Class[]{ServletContext.class},getInvocationHandler(context));
  final ServletContextEvent event=new ServletContextEvent(context);
  if (fireEvent(CONTEXT_LISTENER_CLASS,new EventInvoker<ServletContextListener>(){
    public void invoke(    ServletContextListener listener){
      listener.contextInitialized(event);
    }
  }
) > 0) {
    throw new TestException("Server not started due to listener error ");
  }
  try {
    for (    RequestChain servlet : servlets) {
      servlet.init(context);
    }
    defaultServlet.init(context);
  }
 catch (  ServletException e) {
    throw new TestException("Servlet initialisation error ",e);
  }
  try {
    NamingManager.setInitialContextFactoryBuilder(new StagingInitialContextFactoryBuilder());
  }
 catch (  NamingException e) {
    log.warning("Error set initial context factory builder.");
  }
catch (  IllegalStateException e) {
    log.warning("Initial context factory builder already set.");
  }
  this.initialised=true;
}
 

Example 3

From project jsf-test, under directory /stage/src/main/java/org/jboss/test/faces/staging/.

Source file: StagingServer.java

  19 
vote

@Override public void destroy(){
  if (!initialised) {
    throw new TestException("Staging server have not been initialised");
  }
  this.initialised=false;
  for (Iterator<ServerHttpSession> sessionIterator=sessionInstances.iterator(); sessionIterator.hasNext(); ) {
    ServerHttpSession session=sessionIterator.next();
    final HttpSessionEvent event=new HttpSessionEvent(session);
    fireEvent(SESSION_LISTENER_CLASS,new EventInvoker<HttpSessionListener>(){
      public void invoke(      HttpSessionListener listener){
        listener.sessionDestroyed(event);
      }
    }
);
    session.invalidate();
    sessionIterator.remove();
  }
  setCurrentSession(null);
  final ServletContextEvent event=new ServletContextEvent(context);
  fireEvent(CONTEXT_LISTENER_CLASS,new EventInvoker<ServletContextListener>(){
    public void invoke(    ServletContextListener listener){
      listener.contextDestroyed(event);
    }
  }
);
  for (  RequestChain servlet : servlets) {
    servlet.destroy();
  }
  defaultServlet.destroy();
  JspFactory.setDefaultFactory(null);
  this.contextProxy=null;
  log.info("Staging server have been destroyed");
}
 

Example 4

From project nuxeo-webengine, under directory /nuxeo-webengine-jaxrs/src/main/java/org/nuxeo/ecm/webengine/jaxrs/servlet/config/.

Source file: ListenerSetDescriptor.java

  19 
vote

public synchronized void init(ServletConfig config) throws Exception {
  if (event == null && !listenerDescriptors.isEmpty()) {
    event=new ServletContextEvent(config.getServletContext());
    listeners=new ServletContextListener[listenerDescriptors.size()];
    for (int i=0; i < listeners.length; i++) {
      ListenerDescriptor ld=listenerDescriptors.get(i);
      listeners[i]=ld.getListener();
      listeners[i].contextInitialized(event);
    }
  }
}
 

Example 5

From project nuxeo-webengine, under directory /nuxeo-webengine-jaxrs/src/main/java/org/nuxeo/ecm/webengine/jaxrs/servlet/config/.

Source file: ListenerSetDescriptor.java

  19 
vote

public synchronized boolean destroy(){
  if (event != null) {
    if (listeners != null) {
      try {
        for (        ServletContextListener listener : listeners) {
          listener.contextDestroyed(event);
        }
      }
  finally {
        event=null;
        listeners=null;
      }
      return true;
    }
  }
  return false;
}
 

Example 6

From project openwebbeans, under directory /webbeans-tomcat6/src/main/java/org/apache/webbeans/web/tomcat/.

Source file: TomcatWebPlugin.java

  19 
vote

/** 
 * {@inheritDoc}
 */
@Override public boolean supportsJavaEeComponentInjections(Class<?> clazz){
  if (Servlet.class.isAssignableFrom(clazz) || Filter.class.isAssignableFrom(clazz) || ServletContextListener.class.isAssignableFrom(clazz)|| ServletContextAttributeListener.class.isAssignableFrom(clazz)|| HttpSessionActivationListener.class.isAssignableFrom(clazz)|| HttpSessionAttributeListener.class.isAssignableFrom(clazz)|| HttpSessionBindingListener.class.isAssignableFrom(clazz)|| HttpSessionListener.class.isAssignableFrom(clazz)|| ServletRequestListener.class.isAssignableFrom(clazz)|| ServletRequestAttributeListener.class.isAssignableFrom(clazz)|| clazz.isAnnotationPresent(WebService.class)) {
    return true;
  }
  return false;
}
 

Example 7

From project openwebbeans, under directory /webbeans-tomcat7/src/main/java/org/apache/webbeans/web/tomcat/.

Source file: TomcatWebPlugin.java

  19 
vote

/** 
 * {@inheritDoc}
 */
@Override public boolean supportsJavaEeComponentInjections(Class<?> clazz){
  if (Servlet.class.isAssignableFrom(clazz) || Filter.class.isAssignableFrom(clazz) || ServletContextListener.class.isAssignableFrom(clazz)|| ServletContextAttributeListener.class.isAssignableFrom(clazz)|| HttpSessionActivationListener.class.isAssignableFrom(clazz)|| HttpSessionAttributeListener.class.isAssignableFrom(clazz)|| HttpSessionBindingListener.class.isAssignableFrom(clazz)|| HttpSessionListener.class.isAssignableFrom(clazz)|| ServletRequestListener.class.isAssignableFrom(clazz)|| ServletRequestAttributeListener.class.isAssignableFrom(clazz)|| clazz.isAnnotationPresent(WebService.class)) {
    return true;
  }
  return false;
}
 

Example 8

From project org.ops4j.pax.web, under directory /pax-web-jetty/src/main/java/org/ops4j/pax/web/service/jetty/internal/.

Source file: HttpServiceContext.java

  19 
vote

/** 
 * If the listener is a servlet context listener and the context is already started, notify the servlet context listener about the fact that context is started. This has to be done separately as the listener could be added after the context is already started, case when servlet context listeners are not notified anymore.
 * @param listener to be notified.
 */
@Override public void addEventListener(final EventListener listener){
  super.addEventListener(listener);
  if (isStarted() && listener instanceof ServletContextListener) {
    try {
      ContextClassLoaderUtils.doWithClassLoader(getClassLoader(),new Callable<Void>(){
        public Void call(){
          ((ServletContextListener)listener).contextInitialized(new ServletContextEvent(_scontext));
          return null;
        }
      }
);
    }
 catch (    Exception e) {
      if (e instanceof RuntimeException) {
        throw (RuntimeException)e;
      }
      LOG.error("Ignored exception during listener registration",e);
    }
  }
}
 

Example 9

From project undertow, under directory /servlet/src/main/java/io/undertow/servlet/core/.

Source file: ApplicationListeners.java

  19 
vote

public void addListener(final ManagedListener listener){
  if (ServletContextListener.class.isAssignableFrom(listener.getListenerInfo().getListenerClass())) {
    servletContextListeners.add(listener);
  }
  if (ServletContextAttributeListener.class.isAssignableFrom(listener.getListenerInfo().getListenerClass())) {
    servletContextAttributeListeners.add(listener);
  }
  if (ServletRequestListener.class.isAssignableFrom(listener.getListenerInfo().getListenerClass())) {
    servletRequestListeners.add(listener);
  }
  if (ServletRequestAttributeListener.class.isAssignableFrom(listener.getListenerInfo().getListenerClass())) {
    servletRequestAttributeListeners.add(listener);
  }
  if (HttpSessionListener.class.isAssignableFrom(listener.getListenerInfo().getListenerClass())) {
    httpSessionListeners.add(listener);
  }
  if (HttpSessionAttributeListener.class.isAssignableFrom(listener.getListenerInfo().getListenerClass())) {
    httpSessionAttributeListeners.add(listener);
  }
  this.allListeners.add(listener);
}
 

Example 10

From project undertow, under directory /servlet/src/main/java/io/undertow/servlet/core/.

Source file: ApplicationListeners.java

  19 
vote

public void contextInitialized(){
  final ServletContextEvent event=new ServletContextEvent(servletContext);
  for (  ManagedListener listener : servletContextListeners) {
    this.<ServletContextListener>get(listener).contextInitialized(event);
  }
}
 

Example 11

From project undertow, under directory /servlet/src/main/java/io/undertow/servlet/core/.

Source file: ApplicationListeners.java

  19 
vote

public void contextDestroyed(){
  final ServletContextEvent event=new ServletContextEvent(servletContext);
  for (  ManagedListener listener : servletContextListeners) {
    try {
      this.<ServletContextListener>get(listener).contextDestroyed(event);
    }
 catch (    Exception e) {
      UndertowServletLogger.REQUEST_LOGGER.errorInvokingListener("contextDestroyed",listener.getListenerInfo().getListenerClass(),e);
    }
  }
}
 

Example 12

From project winstone, under directory /src/java/winstone/.

Source file: WebAppConfiguration.java

  19 
vote

private void addListenerInstance(Object listenerInstance,List contextAttributeListeners,List contextListeners,List requestAttributeListeners,List requestListeners,List sessionActivationListeners,List sessionAttributeListeners,List sessionListeners){
  if (listenerInstance instanceof ServletContextAttributeListener)   contextAttributeListeners.add(listenerInstance);
  if (listenerInstance instanceof ServletContextListener)   contextListeners.add(listenerInstance);
  if (listenerInstance instanceof ServletRequestAttributeListener)   requestAttributeListeners.add(listenerInstance);
  if (listenerInstance instanceof ServletRequestListener)   requestListeners.add(listenerInstance);
  if (listenerInstance instanceof HttpSessionActivationListener)   sessionActivationListeners.add(listenerInstance);
  if (listenerInstance instanceof HttpSessionAttributeListener)   sessionAttributeListeners.add(listenerInstance);
  if (listenerInstance instanceof HttpSessionListener)   sessionListeners.add(listenerInstance);
}