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.component.bean;
018    
019    import java.lang.annotation.Annotation;
020    import java.lang.reflect.Method;
021    
022    import org.apache.camel.CamelContext;
023    import org.apache.camel.Expression;
024    import org.apache.camel.Predicate;
025    import org.apache.camel.language.LanguageAnnotation;
026    import org.apache.camel.spi.Language;
027    import org.apache.camel.util.ObjectHelper;
028    import org.apache.camel.util.PredicateToExpressionAdapter;
029    
030    /**
031     * Default implementation of the {@link AnnotationExpressionFactory}.
032     *
033     * @version 
034     */
035    public class DefaultAnnotationExpressionFactory implements AnnotationExpressionFactory {
036    
037        public Expression createExpression(CamelContext camelContext, Annotation annotation, LanguageAnnotation languageAnnotation, Class<?> expressionReturnType) {
038            String languageName = languageAnnotation.language();
039            if (languageName == null) {
040                throw new IllegalArgumentException("Cannot determine the language from the annotation: " + annotation);
041            }
042            Language language = camelContext.resolveLanguage(languageName);
043            if (language == null) {
044                throw new IllegalArgumentException("Cannot find the language: " + languageName + " on the classpath");
045            }
046            String expression = getExpressionFromAnnotation(annotation);
047    
048            if (expressionReturnType == Boolean.class || expressionReturnType == boolean.class) {
049                Predicate predicate = language.createPredicate(expression);
050                return PredicateToExpressionAdapter.toExpression(predicate);
051            } else {
052                return language.createExpression(expression);
053            }
054        }
055    
056        protected String getExpressionFromAnnotation(Annotation annotation) {
057            Object value = getAnnotationObjectValue(annotation, "value");
058            if (value == null) {
059                throw new IllegalArgumentException("Cannot determine the expression from the annotation: " + annotation);
060            }
061            return value.toString();
062        }
063        
064        /**
065         * @param annotation The annotation to get the value of 
066         * @param methodName The annotation name 
067         * @return The value of the annotation
068         */
069        protected Object getAnnotationObjectValue(Annotation annotation, String methodName) {        
070            try {
071                Method method = annotation.getClass().getMethod(methodName);
072                Object value = ObjectHelper.invokeMethod(method, annotation);
073                return value;
074            } catch (NoSuchMethodException e) {
075                throw new IllegalArgumentException("Cannot determine the Object value of the annotation: " + annotation
076                    + " as it does not have the method: " + methodName + "() method", e);
077            }
078        }
079    }