class ConnectEndpointsValidator implements ClassLoaderAwareServletContainerInitializer, Serializable {

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

        if (classSet == null) {
            // This case happens when initializing in a CDI environment.
            //
            // We cannot check anything here to give a message.
            // Continue with the initialization, java will throw
            // the proper exception if application tries to use
            // an endpoint and dependencies are not added to the project.
            return;
        }

        ClassFinder finder = new DefaultClassFinder(classSet);
        Set<Class<?>> endpoints = finder.getAnnotatedClasses(Endpoint.class);
        if (!endpoints.isEmpty()) {
            try {
                finder.loadClass(classToCheck);
            } catch (ClassNotFoundException e) {
                throw new ServletException(
                        "ERROR: Vaadin Connect Endpoints only work for Spring "
                                + "enabled projects.\n"
                                + "This is not a spring application but there are connect endpoints in these classes: "
                                + endpoints.stream().map(Class::getName)
                                        .collect(
                                                Collectors.joining("\n    - ")),
                        e);
            }
        }
    }

}