private Optional<Class<?>> loadClass(ClassLoader classLoader, String className) { try { return Optional.of(classLoader.loadClass(className)); } catch (ClassNotFoundException e) { getLogger().warn( "Failed to load class {}, ignoring it when deploying Vaadin servlets", className, e); return Optional.empty(); } }
private boolean isVaadinServlet(ClassLoader classLoader, String className) { return loadClass(classLoader, className) .map(VaadinServlet.class::isAssignableFrom).orElse(false); }
private void createAppServlet(ServletContext context) { if (!ApplicationRouteRegistry.getInstance(context).hasNavigationTargets()) { getLogger().info( "{} there are no navigation targets registered to the registry", SKIPPING_AUTOMATIC_SERVLET_REGISTRATION_BECAUSE); return; } ServletRegistration vaadinServlet = findVaadinServlet(context); if (vaadinServlet != null) { getLogger().info( "{} there is already a Vaadin servlet with the name {}", SKIPPING_AUTOMATIC_SERVLET_REGISTRATION_BECAUSE, vaadinServlet.getName()); return; } createServletIfNotExists(context, getClass().getName(), "/*"); }
@Override public void contextInitialized(ServletContextEvent sce) { ServletContext context = sce.getServletContext(); Collection<DeploymentConfiguration> servletConfigurations = getServletConfigurations( context); boolean enableServlets = true; boolean hasDevelopmentMode = servletConfigurations.isEmpty(); for (DeploymentConfiguration configuration : servletConfigurations) { enableServlets = enableServlets && !configuration.disableAutomaticServletRegistration(); hasDevelopmentMode = hasDevelopmentMode || !configuration.useCompiledFrontendResources(); } if (enableServlets) { createAppServlet(context); if (hasDevelopmentMode) { createServletIfNotExists(context, "frontendFilesServlet", "/frontend/*"); } } }
private Collection<DeploymentConfiguration> getServletConfigurations( ServletContext context) { Collection<? extends ServletRegistration> registrations = context .getServletRegistrations().values(); Collection<DeploymentConfiguration> result = new ArrayList<>( registrations.size()); for (ServletRegistration registration : registrations) { loadClass(context.getClassLoader(), registration.getClassName()) .ifPresent( servletClass -> result .add(createDeploymentConfiguration( new StubServletConfig(context, registration), servletClass))); } return result; }
private void createServletIfNotExists(ServletContext context, String name, String path) { ServletRegistration existingServlet = findServletByPathPart(context, path); if (existingServlet != null) { getLogger().info( "{} there is already a {} servlet with the name {} for path {} given", SKIPPING_AUTOMATIC_SERVLET_REGISTRATION_BECAUSE, existingServlet, existingServlet.getName(), path); return; } ServletRegistration.Dynamic registration = context.addServlet(name, VaadinServlet.class); if (registration == null) { // Not expected to ever happen getLogger().info("{} there is already a servlet with the name {}", SKIPPING_AUTOMATIC_SERVLET_REGISTRATION_BECAUSE, name); return; } getLogger().info( "Automatically deploying Vaadin servlet with name {} to {}", name, path); registration.setAsyncSupported(true); registration.addMapping(path); }
private ServletRegistration findVaadinServlet(ServletContext context) { return context.getServletRegistrations().values().stream() .filter(registration -> isVaadinServlet( context.getClassLoader(), registration.getClassName())) .findAny().orElse(null); }