1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package info.magnolia.module.blossom.urimapping;
35
36 import java.lang.reflect.Method;
37 import java.lang.reflect.Modifier;
38
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41 import org.springframework.aop.support.AopUtils;
42 import org.springframework.util.ClassUtils;
43 import org.springframework.util.ReflectionUtils;
44
45 import info.magnolia.cms.beans.config.VirtualURIMapping;
46 import info.magnolia.module.blossom.annotation.VirtualURIMapper;
47 import info.magnolia.module.blossom.support.AbstractBeanDetector;
48
49
50
51
52
53
54
55 public class AnnotatedVirtualURIMappingExporter extends AbstractBeanDetector {
56
57 private final Logger logger = LoggerFactory.getLogger(getClass());
58
59 @Override
60 protected boolean include(Class<?> beanType, String beanName) {
61 beanType = AopUtils.isCglibProxyClass(beanType) ? beanType.getSuperclass() : beanType;
62 if (beanType.isAnnotationPresent(VirtualURIMapper.class)) {
63 if (ClassUtils.isAssignable(VirtualURIMapping.class, beanType)) {
64 if (logger.isWarnEnabled()) {
65 logger.warn("Bean [" + beanName + "] annotated with VirtualUriMapper should not also implement VirtualURIMapping, will not have methods exposed");
66 }
67 return false;
68 }
69 return true;
70 }
71 return false;
72 }
73
74 @Override
75 protected void onBeanDetection(final Object bean, final String beanName) {
76
77 if (logger.isDebugEnabled()) {
78 logger.debug("Detected VirtualURIMapper on bean [" + beanName + "] scanning methods to expose");
79 }
80
81 final Class<?> beanType = bean.getClass();
82 ReflectionUtils.doWithMethods(beanType, new ReflectionUtils.MethodCallback() {
83
84 @Override
85 public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException {
86 if (isEligibleMethod(method) && method.equals(ClassUtils.getMostSpecificMethod(method, beanType))) {
87
88 if (logger.isDebugEnabled()) {
89 logger.debug("Exposing VirtualURIMapping for method [" + method + "] on bean [" + beanName + "]");
90 }
91
92 BlossomVirtualURIMapping.addMapping(createMethodAdapter(method, bean));
93 }
94 }
95 });
96 }
97
98 protected boolean isEligibleMethod(Method method) {
99 return method.getParameterTypes().length > 0 &&
100 (method.getReturnType().equals(String.class) || method.getReturnType().equals(VirtualURIMapping.MappingResult.class)) &&
101 Modifier.isPublic(method.getModifiers()) &&
102 !Modifier.isStatic(method.getModifiers());
103 }
104
105 protected VirtualURIMappingAdapter createMethodAdapter(Method method, Object bean) {
106 return new VirtualURIMappingAdapter(bean, method);
107 }
108 }