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