Example usage for javax.servlet ServletContext addListener

List of usage examples for javax.servlet ServletContext addListener

Introduction

In this page you can find the example usage for javax.servlet ServletContext addListener.

Prototype

public void addListener(Class<? extends EventListener> listenerClass);

Source Link

Document

Adds a listener of the given class type to this ServletContext.

Usage

From source file:org.springframework.boot.web.servlet.support.SpringBootServletInitializer.java

@Override
public void onStartup(ServletContext servletContext) throws ServletException {
    // Logger initialization is deferred in case an ordered
    // LogServletContextInitializer is being used
    this.logger = LogFactory.getLog(getClass());
    WebApplicationContext rootAppContext = createRootApplicationContext(servletContext);
    if (rootAppContext != null) {
        servletContext.addListener(new ContextLoaderListener(rootAppContext) {
            @Override//  w  w  w . j  a va 2s .c om
            public void contextInitialized(ServletContextEvent event) {
                // no-op because the application context is already initialized
            }
        });
    } else {
        this.logger.debug("No ContextLoaderListener registered, as " + "createRootApplicationContext() did not "
                + "return an application context");
    }
}

From source file:org.springframework.boot.web.SpringBootServletInitializer.java

@Override
public void onStartup(ServletContext servletContext) throws ServletException {
    WebApplicationContext rootAppContext = this.createRootApplicationContext(servletContext);
    if (rootAppContext != null) {
        servletContext.addListener(new ContextLoaderListener(rootAppContext) {
            @Override/*  ww  w  .  j av a 2 s . co  m*/
            public void contextInitialized(ServletContextEvent event) {
                // no-op because the application context is already initialized
            }
        });
    } else {
        this.logger.debug("No ContextLoaderListener registered, as " + "createRootApplicationContext() did not "
                + "return an application context");
    }
}

From source file:org.springframework.web.context.AbstractContextLoaderInitializer.java

/**
 * Register a {@link ContextLoaderListener} against the given servlet context. The
 * {@code ContextLoaderListener} is initialized with the application context returned
 * from the {@link #createRootApplicationContext()} template method.
 * @param servletContext the servlet context to register the listener against
 *//*from w  ww  .j a v a  2s .  co m*/
protected void registerContextLoaderListener(ServletContext servletContext) {
    WebApplicationContext rootAppContext = createRootApplicationContext();
    if (rootAppContext != null) {
        ContextLoaderListener listener = new ContextLoaderListener(rootAppContext);
        listener.setContextInitializers(getRootApplicationContextInitializers());
        servletContext.addListener(listener);
    } else {
        logger.debug("No ContextLoaderListener registered, as "
                + "createRootApplicationContext() did not return an application context");
    }
}

From source file:org.wso2.carbon.protobuf.listener.ProtobufServletContainerInitializer.java

@Override
public void onStartup(Set<Class<?>> classes, ServletContext servletContext) throws ServletException {

    if (classes == null || classes.size() == 0) {
        return;//from   ww w.j a v  a 2s  . c  o  m
    }
    // adding a listener to remove services when wars are undeployed
    servletContext.addListener(new ProtobufServletContextListener());
    // keeps track of PB services in a PB war
    // Please note that, a PB war can contain many PB services
    List<ProtobufServiceData> serviceList = new ArrayList<ProtobufServiceData>();
    // servlet to display proto files (like WSDL files)
    ServletRegistration.Dynamic dynamic = servletContext.addServlet("ProtoBufServlet", ProtobufServlet.class);

    for (Class<?> clazz : classes) {
        // Getting binary service registry
        ProtobufRegistry binaryServiceRegistry = (ProtobufRegistry) PrivilegedCarbonContext
                .getThreadLocalCarbonContext().getOSGiService(ProtobufRegistry.class);
        // Is it a blocking service or not
        boolean blocking = clazz.getAnnotation(ProtobufService.class).blocking();
        Method reflectiveMethod = null;
        Object serviceObj = null;
        String serviceName;
        String serviceType;
        try {
            if (blocking) {
                // getting newReflectiveBlocking method which will return a
                // blocking service
                reflectiveMethod = clazz.getInterfaces()[0].getDeclaringClass()
                        .getMethod("newReflectiveBlockingService", clazz.getInterfaces()[0]);
                // Since it is a static method, we pass null
                serviceObj = reflectiveMethod.invoke(null, clazz.newInstance());
                BlockingService blockingService = (BlockingService) serviceObj;
                // register service into Binary Service Registry
                serviceName = binaryServiceRegistry.registerBlockingService(blockingService);
                serviceType = "BlockingService";
                // keeps PB service information in a bean
                // we need these when removing the services from Binary
                // Service Registry
                // we are using these beans instances inside our destroyer
                serviceList.add(new ProtobufServiceData(serviceName, serviceType));
                servletContext.setAttribute("services", serviceList);
                dynamic.addMapping("/");
            } else {
                // getting newReflectiveService which will return a non
                // blocking service
                reflectiveMethod = clazz.getInterfaces()[0].getDeclaringClass()
                        .getMethod("newReflectiveService", clazz.getInterfaces()[0]);
                // Since it is a static method, we pass null
                serviceObj = reflectiveMethod.invoke(null, clazz.newInstance());
                Service service = (Service) serviceObj;
                // register service into Binary Service Registry
                serviceName = binaryServiceRegistry.registerService(service);
                serviceType = "NonBlockingService";
                // keeps PB service information in a bean
                // we need these information to remove the service from
                // Binary Service Registry later
                // we are using these bean instances in our destroyer
                serviceList.add(new ProtobufServiceData(serviceName, serviceType));
                servletContext.setAttribute("services", serviceList);
                dynamic.addMapping("/");
            }
        } catch (InvocationTargetException e) {
            String msg = "InvocationTargetException" + e.getLocalizedMessage();
            log.error(msg, e);
        } catch (NoSuchMethodException e) {
            String msg = "NoSuchMethodException" + e.getLocalizedMessage();
            log.error(msg, e);
        } catch (InstantiationException e) {
            String msg = "InstantiationException" + e.getLocalizedMessage();
            log.error(msg, e);
        } catch (IllegalAccessException e) {
            String msg = "IllegalAccessException" + e.getLocalizedMessage();
            log.error(msg, e);
        }
    }
}