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 public class RegistryExpressionEvaluator implements ExpressionEvaluator, MuleContextAware
34 {
35
36
37
38 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 this.muleContext = context;
48 }
49
50 public Object evaluate(String expression, MuleMessage message)
51 {
52 if(expression.startsWith("type:"))
53 {
54 String c = expression.substring(5);
55 Class clazz;
56 try
57 {
58 clazz = ClassUtils.loadClass(c, getClass());
59 }
60 catch (ClassNotFoundException e)
61 {
62 throw new IllegalArgumentException("Class not on the classpath: " + c);
63 }
64
65 try
66 {
67 return muleContext.getRegistry().lookupObject(clazz);
68 }
69 catch (RegistrationException e)
70 {
71 return null;
72 }
73 }
74
75 int i = expression.indexOf(".");
76 String name;
77 String property = null;
78 boolean propertyRequired = true;
79 boolean objectRequired = true;
80 if (i > 0)
81 {
82 name = expression.substring(0, i);
83 property = expression.substring(i + 1);
84 if (property.endsWith("*"))
85 {
86 propertyRequired = false;
87 property = property.substring(property.length() - 1);
88 }
89 }
90 else
91 {
92 name = expression;
93 }
94
95 if (name.endsWith("*"))
96 {
97 objectRequired = false;
98 name = name.substring(name.length() - 1);
99 }
100
101 Object o = muleContext.getRegistry().lookupObject(name);
102
103 if (o == null && objectRequired)
104 {
105 throw new ExpressionRuntimeException(CoreMessages.expressionEvaluatorReturnedNull(NAME, expression));
106 }
107 else if (o == null || property == null)
108 {
109 return o;
110 }
111 else if(muleContext.getExpressionManager().isEvaluatorRegistered("bean"))
112 {
113
114 if(o instanceof AbstractEndpointBuilder)
115 {
116 property = "endpointBuilder.endpoint." + property;
117 }
118
119 Object p = muleContext.getExpressionManager().evaluate("#[bean:" + property + "]", new DefaultMuleMessage(o, muleContext));
120 if (p == null && propertyRequired)
121 {
122 throw new ExpressionRuntimeException(CoreMessages.expressionEvaluatorReturnedNull(NAME, name + "." + property));
123 }
124 else
125 {
126 return p;
127 }
128 }
129 else
130 {
131 throw new ExpressionRuntimeException(CoreMessages.expressionEvaluatorNotRegistered("bean"));
132 }
133 }
134
135
136
137
138 public String getName()
139 {
140 return NAME;
141 }
142
143
144
145
146 public void setName(String name)
147 {
148 throw new UnsupportedOperationException();
149 }
150 }