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.config.source;
35
36 import info.magnolia.config.registry.DefinitionProvider;
37 import info.magnolia.config.registry.DefinitionProviderWrapper;
38
39 import java.lang.reflect.Method;
40
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43
44 import com.thoughtworks.proxy.factory.CglibProxyFactory;
45 import com.thoughtworks.proxy.toys.decorate.Decorating;
46 import com.thoughtworks.proxy.toys.decorate.Decorator;
47
48
49
50
51
52
53
54 public class DefinitionProviderWrapperWithProxyFallback<T> extends DefinitionProviderWrapper<T> {
55
56 private static final Logger log = LoggerFactory.getLogger(DefinitionProviderWrapperWithProxyFallback.class);
57
58 private final T fallbackDefinition;
59
60 public DefinitionProviderWrapperWithProxyFallback(DefinitionProvider<T> delegate, T fallbackDefinition) {
61 super(delegate);
62 this.fallbackDefinition = fallbackDefinition;
63 }
64
65
66
67
68 @Deprecated
69 public DefinitionProviderWrapperWithProxyFallback(DefinitionProvider<T> delegate) {
70 this(delegate, delegate.get());
71 }
72
73 @Override
74 public final T get() {
75 final T decoratedBean = doGet();
76
77 if (decoratedBean == null) {
78 return null;
79 }
80
81 return Decorating.proxy(decoratedBean).visiting(new Decorator<T>() {
82 @Override
83 public Object decorateResult(T proxy, Method method, Object[] args, Object result) {
84 if (result == null) {
85 try {
86 return method.invoke(fallbackDefinition);
87 } catch (ReflectiveOperationException e) {
88 log.debug("Failed to invoke fallback method call due to a reflection operation problem: {}", method.toString(), e.getMessage());
89 } catch (Exception e) {
90 log.debug("Failed to invoke fallback method call due to: {}", method.toString(), e.getMessage());
91 }
92 }
93 return super.decorateResult(proxy, method, args, result);
94 }
95 }).build(new CglibProxyFactory());
96 }
97
98 protected T doGet() {
99 return getDelegate().get();
100 }
101 }