001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.camel.model.language;
018    
019    import java.util.List;
020    
021    import javax.xml.bind.annotation.XmlAccessType;
022    import javax.xml.bind.annotation.XmlAccessorType;
023    import javax.xml.bind.annotation.XmlAttribute;
024    import javax.xml.bind.annotation.XmlID;
025    import javax.xml.bind.annotation.XmlRootElement;
026    import javax.xml.bind.annotation.XmlTransient;
027    import javax.xml.bind.annotation.XmlType;
028    import javax.xml.bind.annotation.XmlValue;
029    import javax.xml.bind.annotation.adapters.CollapsedStringAdapter;
030    import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
031    
032    import org.apache.camel.CamelContext;
033    import org.apache.camel.Exchange;
034    import org.apache.camel.Expression;
035    import org.apache.camel.Predicate;
036    import org.apache.camel.builder.ExpressionClause;
037    import org.apache.camel.impl.DefaultRouteContext;
038    import org.apache.camel.spi.Language;
039    import org.apache.camel.spi.RouteContext;
040    import org.apache.camel.util.CollectionStringBuffer;
041    import org.apache.camel.util.IntrospectionSupport;
042    import org.apache.camel.util.ObjectHelper;
043    
044    /**
045     * A useful base class for an expression
046     *
047     * @version $Revision: 54143 $
048     */
049    @XmlRootElement
050    @XmlType(name = "expressionType")
051    @XmlAccessorType(XmlAccessType.FIELD)
052    public class ExpressionType implements Expression<Exchange>, Predicate<Exchange> {
053        @XmlAttribute
054        @XmlJavaTypeAdapter(CollapsedStringAdapter.class)
055        @XmlID
056        private String id;
057        @XmlValue
058        private String expression;
059        @XmlTransient
060        private Predicate predicate;
061        @XmlTransient
062        private Expression expressionValue;
063        @XmlTransient
064        private ExpressionType expressionType;
065    
066        public ExpressionType() {
067        }
068    
069        public ExpressionType(String expression) {
070            this.expression = expression;
071        }
072    
073        public ExpressionType(Predicate predicate) {
074            this.predicate = predicate;
075        }
076    
077        public ExpressionType(Expression expression) {
078            this.expressionValue = expression;
079        }
080    
081        public static String getLabel(List<ExpressionType> expressions) {
082            CollectionStringBuffer buffer = new CollectionStringBuffer();
083            for (ExpressionType expression : expressions) {
084                buffer.append(expression.getLabel());
085            }
086            return buffer.toString();
087        }
088    
089        @Override
090        public String toString() {
091            StringBuilder sb = new StringBuilder();
092            if (getLanguage() != null) {
093                sb.append(getLanguage() + "{");
094            }
095            if (getExpression() != null) {
096                sb.append(getExpression());
097            }
098            if (getPredicate() != null) {
099                sb.append(getPredicate().toString());
100            }
101            if (getExpressionValue() != null) {
102                sb.append(getExpressionValue().toString());
103            }
104            if (getLanguage() != null) {
105                sb.append("}");
106            }
107            return sb.toString();
108        }
109    
110        public Object evaluate(Exchange exchange) {
111            if (expressionValue == null) {
112                RouteContext routeContext = new DefaultRouteContext(exchange.getContext());
113                expressionValue = createExpression(routeContext);
114            }
115            ObjectHelper.notNull(expressionValue, "expressionValue");
116            return expressionValue.evaluate(exchange);
117        }
118    
119        public void assertMatches(String text, Exchange exchange) throws AssertionError {
120            if (!matches(exchange)) {
121                throw new AssertionError(text + getExpression() + " for exchange: " + exchange);
122            }
123        }
124    
125        public boolean matches(Exchange exchange) {
126            if (predicate == null) {
127                RouteContext routeContext = new DefaultRouteContext(exchange.getContext());
128                predicate = createPredicate(routeContext);
129            }
130            ObjectHelper.notNull(predicate, "predicate");
131            return predicate.matches(exchange);
132        }
133    
134        public String getLanguage() {
135            return "";
136        }
137    
138        public Predicate<Exchange> createPredicate(RouteContext routeContext) {
139            if (predicate == null) {
140                if (expressionType != null) {
141                    predicate = expressionType.createPredicate(routeContext);
142                } else {
143                    CamelContext camelContext = routeContext.getCamelContext();
144                    Language language = camelContext.resolveLanguage(getLanguage());
145                    predicate = language.createPredicate(getExpression());
146                    configurePredicate(routeContext, predicate);
147                }
148            }
149            return predicate;
150        }
151    
152        public Expression createExpression(RouteContext routeContext) {
153            if (expressionValue == null) {
154                if (expressionType != null) {
155                    expressionValue = expressionType.createExpression(routeContext);
156                } else {
157                    CamelContext camelContext = routeContext.getCamelContext();
158                    Language language = camelContext.resolveLanguage(getLanguage());
159                    expressionValue = language.createExpression(getExpression());
160                    configureExpression(routeContext, expressionValue);
161                }
162            }
163            return expressionValue;
164        }
165    
166        public String getExpression() {
167            return expression;
168        }
169    
170        public void setExpression(String expression) {
171            this.expression = expression;
172        }
173    
174        /**
175         * Gets the value of the id property.
176         */
177        public String getId() {
178            return id;
179        }
180    
181        /**
182         * Sets the value of the id property.
183         */
184        public void setId(String value) {
185            this.id = value;
186        }
187    
188        public Predicate getPredicate() {
189            return predicate;
190        }
191    
192        public Expression getExpressionValue() {
193            return expressionValue;
194        }
195    
196        protected void setExpressionValue(Expression expressionValue) {
197            this.expressionValue = expressionValue;
198        }
199    
200        /**
201         * Returns some descriptive text to describe this node
202         */
203        public String getLabel() {
204            String language = getExpression();
205            if (ObjectHelper.isNullOrBlank(language)) {
206                Predicate predicate = getPredicate();
207                if (predicate != null) {
208                    return predicate.toString();
209                }
210                Expression expressionValue = getExpressionValue();
211                if (expressionValue != null) {
212                    return expressionValue.toString();
213                }
214            } else {
215                return language;
216            }
217            return "";
218        }
219    
220        /**
221         * Allows derived classes to set a lazily created expressionType instance
222         * such as if using the {@link ExpressionClause}
223         */
224        protected void setExpressionType(ExpressionType expressionType) {
225            this.expressionType = expressionType;
226        }
227    
228        protected void configurePredicate(RouteContext routeContext, Predicate predicate) {
229        }
230    
231        protected void configureExpression(RouteContext routeContext, Expression expression) {
232        }
233    
234        /**
235         * Sets a named property on the object instance using introspection
236         */
237        protected void setProperty(Object bean, String name, Object value) {
238            try {
239                IntrospectionSupport.setProperty(bean, name, value);
240            } catch (Exception e) {
241                throw new IllegalArgumentException("Failed to set property " + name + " on " + bean
242                                                   + ". Reason: " + e, e);
243            }
244        }
245    }