private void registerJeeComponents(ConfigurableListableBeanFactory beanFactory, ClassLoader cl, Set<BeanDefinition> candiates) { for (BeanDefinition bd : candiates) { try { Class<?> beanClass = ClassUtils.forName(bd.getBeanClassName(), cl); WebServlet webServlet = beanClass.getDeclaredAnnotation(WebServlet.class); WebFilter webFilter = beanClass.getDeclaredAnnotation(WebFilter.class); WebListener webListener = beanClass.getDeclaredAnnotation(WebListener.class); DefaultListableBeanFactory targetBeanFactory = (DefaultListableBeanFactory) beanFactory; if (webServlet != null) { createAndRegisterServletBean(targetBeanFactory, bd, beanClass, webServlet); } else if (webFilter != null) { createAndRegisterServletFilterBean(targetBeanFactory, bd, beanClass, webFilter); } else if (webListener != null) { createAndRegisterWebListenerBean(targetBeanFactory, bd, beanClass, webListener); } } catch (ClassNotFoundException e) { e.printStackTrace(); } } }
WebListenerHandler() { super(WebListener.class); }
@Override public boolean matches(final Class<?> type) { return FeatureUtils.is(type, EventListener.class) && FeatureUtils.hasAnnotation(type, WebListener.class) && hasMatch(type, SUPPORTED); }
public void write(ClassName linkers) throws IOException { TypeSpec.Builder typeBuilder = TypeSpec.classBuilder(linkers.className()) .addModifiers(PUBLIC, FINAL) .addSuperinterface(ServletContextListener.class) .addAnnotation(AnnotationSpec.builder(WebListener.class).build()) .addAnnotation(AnnotationSpec.builder(Generated.class) .addMember("value", "$S", LinkerAnnotationProcessor.class.getName()) .build() ) .addField(FieldSpec.builder(String.class, "contextPath", PRIVATE, STATIC) .initializer("\"\"") .build()) .addField(FieldSpec.builder(String.class, "applicationName", PRIVATE, STATIC) .initializer("$T.get()", get("fr.vidal.oss.jax_rs_linker", "ApplicationName")) .build()) .addMethod(MethodSpec.methodBuilder("contextInitialized") .addModifiers(PUBLIC) .addAnnotation(Override.class) .returns(void.class) .addParameter(ServletContextEvent.class, "sce") .addCode( "$L = $T.contextPath($L.getServletContext(), $L);\n", "contextPath", ContextPaths.class, "sce", "applicationName") .build()) .addMethod(MethodSpec.methodBuilder("contextDestroyed") .addModifiers(PUBLIC) .addAnnotation(Override.class) .returns(void.class) .addParameter(ServletContextEvent.class, "sce") .build()) .addMethod(MethodSpec.methodBuilder("getContextPath") .addModifiers(PUBLIC, STATIC) .returns(String.class) .addCode( "return contextPath;\n") .build()); JavaFile.builder(linkers.packageName(), typeBuilder.build()) .indent("\t") .build() .writeTo(filer); }
public void findListeners(@Observes @WithAnnotations({WebListener.class}) ProcessAnnotatedType<? extends ServletContextListener> pat) { listeners.add(pat.getAnnotatedType().getJavaClass()); }
private void createAndRegisterWebListenerBean(DefaultListableBeanFactory beanFactory, BeanDefinition bd, Class<?> beanClass, WebListener webListener) { BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(ServletListenerRegistrationBean.class); builder.addPropertyValue("listener", bd); String beanName = StringUtils.uncapitalize(beanClass.getSimpleName()); beanFactory.registerBeanDefinition(beanName, builder.getBeanDefinition()); }