Example usage for javax.servlet ServletContext addServlet

List of usage examples for javax.servlet ServletContext addServlet

Introduction

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

Prototype

public ServletRegistration.Dynamic addServlet(String servletName, Class<? extends Servlet> servletClass);

Source Link

Document

Adds the servlet with the given name and class type to this servlet context.

Usage

From source file:org.pidster.tomcat.websocket.jmx.WebSocketJMXInitializer.java

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

    // Allows the path of the Servlet & Filter to be configurable
    // relative to the web application's own path
    String path = System.getProperty("org.pidster.tomcat.websocket.jmx.path", "/jmx");

    log.info("Registering " + WebSocketJMXResourceFilter.class.getName() + ", with paths: " + path);
    FilterRegistration.Dynamic freg = ctx.addFilter(WebSocketJMXResourceFilter.class.getSimpleName(),
            WebSocketJMXResourceFilter.class);
    freg.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST), true, path + "/*");
    freg.setInitParameter("path", path);

    String wspath = path + "/connect";

    log.info("Registering " + WebSocketJMXServlet.class.getName() + ", with path: " + wspath);
    ServletRegistration.Dynamic wreg = ctx.addServlet(WebSocketJMXServlet.class.getSimpleName(),
            WebSocketJMXServlet.class);
    wreg.addMapping(wspath);// w ww . j  av a  2  s .com
    wreg.setInitParameter("path", path);

}

From source file:org.springframework.boot.actuate.endpoint.web.ServletEndpointRegistrar.java

private void register(ServletContext servletContext, ExposableServletEndpoint endpoint) {
    String name = endpoint.getId() + "-actuator-endpoint";
    String path = this.basePath + "/" + endpoint.getRootPath();
    String urlMapping = path.endsWith("/") ? path + "*" : path + "/*";
    EndpointServlet endpointServlet = endpoint.getEndpointServlet();
    Dynamic registration = servletContext.addServlet(name, endpointServlet.getServlet());
    registration.addMapping(urlMapping);
    registration.setInitParameters(endpointServlet.getInitParameters());
    logger.info("Registered '" + path + "' to " + name);
}

From source file:org.springframework.boot.context.embedded.ServletRegistrationBean.java

@Override
public void onStartup(ServletContext servletContext) throws ServletException {
    Assert.notNull(this.servlet, "Servlet must not be null");
    String name = getServletName();
    if (!isEnabled()) {
        logger.info("Servlet " + name + " was not registered (disabled)");
        return;//w w w  . ja v  a 2s . com
    }
    logger.info("Mapping servlet: '" + name + "' to " + this.urlMappings);
    Dynamic added = servletContext.addServlet(name, this.servlet);
    if (added == null) {
        logger.info("Servlet " + name + " was not registered " + "(possibly already registered?)");
        return;
    }
    configure(added);
}

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 w  ww . j  av 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);
        }
    }
}