1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
package org.mule.expression; |
8 | |
|
9 | |
import org.mule.DefaultMuleMessage; |
10 | |
import org.mule.api.MuleContext; |
11 | |
import org.mule.api.MuleMessage; |
12 | |
import org.mule.api.context.MuleContextAware; |
13 | |
import org.mule.api.expression.ExpressionEvaluator; |
14 | |
import org.mule.api.expression.ExpressionRuntimeException; |
15 | |
import org.mule.api.registry.RegistrationException; |
16 | |
import org.mule.config.i18n.CoreMessages; |
17 | |
import org.mule.endpoint.AbstractEndpointBuilder; |
18 | |
import org.mule.util.ClassUtils; |
19 | |
|
20 | |
import org.apache.commons.logging.Log; |
21 | |
import org.apache.commons.logging.LogFactory; |
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | 0 | public class RegistryExpressionEvaluator implements ExpressionEvaluator, MuleContextAware |
30 | |
{ |
31 | |
|
32 | |
|
33 | |
|
34 | 0 | protected transient final Log logger = LogFactory.getLog(RegistryExpressionEvaluator.class); |
35 | |
|
36 | |
|
37 | |
public static final String NAME = "registry"; |
38 | |
|
39 | |
private MuleContext muleContext; |
40 | |
|
41 | |
public void setMuleContext(MuleContext context) |
42 | |
{ |
43 | 0 | this.muleContext = context; |
44 | 0 | } |
45 | |
|
46 | |
public Object evaluate(String expression, MuleMessage message) |
47 | |
{ |
48 | 0 | if(expression.startsWith("type:")) |
49 | |
{ |
50 | 0 | String c = expression.substring(5); |
51 | |
Class clazz; |
52 | |
try |
53 | |
{ |
54 | 0 | clazz = ClassUtils.loadClass(c, getClass()); |
55 | |
} |
56 | 0 | catch (ClassNotFoundException e) |
57 | |
{ |
58 | 0 | throw new IllegalArgumentException("Class not on the classpath: " + c); |
59 | 0 | } |
60 | |
|
61 | |
try |
62 | |
{ |
63 | 0 | return muleContext.getRegistry().lookupObject(clazz); |
64 | |
} |
65 | 0 | catch (RegistrationException e) |
66 | |
{ |
67 | 0 | return null; |
68 | |
} |
69 | |
} |
70 | |
|
71 | 0 | int i = expression.indexOf("."); |
72 | |
String name; |
73 | 0 | String property = null; |
74 | 0 | boolean propertyRequired = true; |
75 | 0 | boolean objectRequired = true; |
76 | 0 | if (i > 0) |
77 | |
{ |
78 | 0 | name = expression.substring(0, i); |
79 | 0 | property = expression.substring(i + 1); |
80 | 0 | if (property.endsWith("*")) |
81 | |
{ |
82 | 0 | propertyRequired = false; |
83 | 0 | property = property.substring(property.length() - 1); |
84 | |
} |
85 | |
} |
86 | |
else |
87 | |
{ |
88 | 0 | name = expression; |
89 | |
} |
90 | |
|
91 | 0 | if (name.endsWith("*")) |
92 | |
{ |
93 | 0 | objectRequired = false; |
94 | 0 | name = name.substring(name.length() - 1); |
95 | |
} |
96 | |
|
97 | 0 | Object o = muleContext.getRegistry().lookupObject(name); |
98 | |
|
99 | 0 | if (o == null && objectRequired) |
100 | |
{ |
101 | 0 | throw new ExpressionRuntimeException(CoreMessages.expressionEvaluatorReturnedNull(NAME, expression)); |
102 | |
} |
103 | 0 | else if (o == null || property == null) |
104 | |
{ |
105 | 0 | return o; |
106 | |
} |
107 | 0 | else if(muleContext.getExpressionManager().isEvaluatorRegistered("bean")) |
108 | |
{ |
109 | |
|
110 | 0 | if(o instanceof AbstractEndpointBuilder) |
111 | |
{ |
112 | 0 | property = "endpointBuilder.endpoint." + property; |
113 | |
} |
114 | |
|
115 | 0 | Object p = muleContext.getExpressionManager().evaluate("#[bean:" + property + "]", new DefaultMuleMessage(o, muleContext)); |
116 | 0 | if (p == null && propertyRequired) |
117 | |
{ |
118 | 0 | throw new ExpressionRuntimeException(CoreMessages.expressionEvaluatorReturnedNull(NAME, name + "." + property)); |
119 | |
} |
120 | |
else |
121 | |
{ |
122 | 0 | return p; |
123 | |
} |
124 | |
} |
125 | |
else |
126 | |
{ |
127 | 0 | throw new ExpressionRuntimeException(CoreMessages.expressionEvaluatorNotRegistered("bean")); |
128 | |
} |
129 | |
} |
130 | |
|
131 | |
|
132 | |
|
133 | |
|
134 | |
public String getName() |
135 | |
{ |
136 | 0 | return NAME; |
137 | |
} |
138 | |
|
139 | |
|
140 | |
|
141 | |
|
142 | |
public void setName(String name) |
143 | |
{ |
144 | 0 | throw new UnsupportedOperationException(); |
145 | |
} |
146 | |
} |