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