1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
package org.mule.config.processors; |
11 | |
|
12 | |
import org.mule.api.MuleContext; |
13 | |
import org.mule.api.annotations.expressions.Lookup; |
14 | |
import org.mule.api.context.MuleContextAware; |
15 | |
import org.mule.api.expression.RequiredValueException; |
16 | |
import org.mule.api.registry.InjectProcessor; |
17 | |
import org.mule.config.i18n.AnnotationsMessages; |
18 | |
import org.mule.util.StringUtils; |
19 | |
|
20 | |
import java.lang.reflect.Field; |
21 | |
|
22 | |
import org.apache.commons.logging.Log; |
23 | |
import org.apache.commons.logging.LogFactory; |
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
public class LookupInjectionProcessor implements InjectProcessor, MuleContextAware |
29 | |
{ |
30 | |
|
31 | |
|
32 | |
|
33 | 0 | protected transient final Log logger = LogFactory.getLog(LookupInjectionProcessor.class); |
34 | |
|
35 | |
private MuleContext context; |
36 | |
|
37 | |
public LookupInjectionProcessor() |
38 | 0 | { |
39 | 0 | } |
40 | |
|
41 | |
public LookupInjectionProcessor(MuleContext context) |
42 | 0 | { |
43 | 0 | this.context = context; |
44 | 0 | } |
45 | |
|
46 | |
public void setMuleContext(MuleContext context) |
47 | |
{ |
48 | 0 | this.context = context; |
49 | 0 | } |
50 | |
|
51 | |
public Object process(Object object) |
52 | |
{ |
53 | |
Field[] fields; |
54 | |
try |
55 | |
{ |
56 | 0 | fields = object.getClass().getDeclaredFields(); |
57 | |
} |
58 | 0 | catch (NoClassDefFoundError e) |
59 | |
{ |
60 | |
|
61 | 0 | if (logger.isDebugEnabled()) |
62 | |
{ |
63 | 0 | logger.warn(e.toString()); |
64 | |
} |
65 | 0 | return object; |
66 | 0 | } |
67 | 0 | for (int i = 0; i < fields.length; i++) |
68 | |
{ |
69 | 0 | Field field = fields[i]; |
70 | 0 | if (field.isAnnotationPresent(Lookup.class)) |
71 | |
{ |
72 | |
try |
73 | |
{ |
74 | 0 | field.setAccessible(true); |
75 | |
Object value; |
76 | 0 | String name = field.getAnnotation(Lookup.class).value(); |
77 | 0 | boolean optional = field.getAnnotation(Lookup.class).optional(); |
78 | 0 | if(StringUtils.isBlank(name)) |
79 | |
{ |
80 | 0 | value = context.getRegistry().lookupObject(field.getType()); |
81 | |
} |
82 | |
else |
83 | |
{ |
84 | 0 | value = context.getRegistry().lookupObject(name); |
85 | |
} |
86 | 0 | if (value == null && !optional) |
87 | |
{ |
88 | 0 | throw new RequiredValueException(AnnotationsMessages.lookupNotFoundInRegistry(field.getType(), name, object.getClass())); |
89 | |
} |
90 | |
|
91 | 0 | field.set(object, value); |
92 | |
} |
93 | 0 | catch (RequiredValueException e) |
94 | |
{ |
95 | 0 | throw e; |
96 | |
} |
97 | 0 | catch (Exception e) |
98 | |
{ |
99 | 0 | throw new RequiredValueException(AnnotationsMessages.lookupFailedSeePreviousException(object), e); |
100 | 0 | } |
101 | |
} |
102 | |
} |
103 | 0 | return object; |
104 | |
} |
105 | |
} |