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.processor.aggregate;
018    
019    import java.lang.reflect.Method;
020    import java.util.ArrayList;
021    import java.util.List;
022    
023    import org.apache.camel.CamelContext;
024    import org.apache.camel.Expression;
025    import org.apache.camel.builder.ExpressionBuilder;
026    import org.apache.camel.component.bean.ParameterInfo;
027    import org.slf4j.Logger;
028    import org.slf4j.LoggerFactory;
029    
030    /**
031     * Class information about the POJO method to call when using the {@link AggregationStrategyBeanAdapter}.
032     */
033    public class AggregationStrategyBeanInfo {
034    
035        // TODO: We could potential merge this logic into AggregationStrategyMethodInfo and only have 1 class
036    
037        private static final Logger LOG = LoggerFactory.getLogger(AggregationStrategyBeanInfo.class);
038    
039        private final CamelContext camelContext;
040        private final Class<?> type;
041        private final Method method;
042    
043        public AggregationStrategyBeanInfo(CamelContext camelContext, Class<?> type, Method method) {
044            this.camelContext = camelContext;
045            this.type = type;
046            this.method = method;
047        }
048    
049        protected AggregationStrategyMethodInfo createMethodInfo() {
050            Class<?>[] parameterTypes = method.getParameterTypes();
051    
052            int size = parameterTypes.length;
053            if (LOG.isTraceEnabled()) {
054                LOG.trace("Creating MethodInfo for class: {} method: {} having {} parameters", new Object[]{type, method, size});
055            }
056    
057            // must have equal number of parameters
058            if (size < 2) {
059                throw new IllegalArgumentException("The method " + method.getName() + " must have at least two parameters, has: " + size);
060            } else if (size % 2 != 0) {
061                throw new IllegalArgumentException("The method " + method.getName() + " must have equal number of parameters, has: " + size);
062            }
063    
064            // must not have annotations as they are not supported (yet)
065            for (int i = 0; i < size; i++) {
066                Class<?> type = parameterTypes[i];
067                if (type.getAnnotations().length > 0) {
068                    throw new IllegalArgumentException("Parameter annotations at index " + i + " is not supported on method: " + method);
069                }
070            }
071    
072            List<ParameterInfo> oldParameters = new ArrayList<ParameterInfo>();
073            List<ParameterInfo> newParameters = new ArrayList<ParameterInfo>();
074    
075            for (int i = 0; i < size / 2; i++) {
076                Class<?> oldType = parameterTypes[i];
077                if (oldParameters.size() == 0) {
078                    // the first parameter is the body
079                    Expression oldBody = ExpressionBuilder.mandatoryBodyExpression(oldType);
080                    ParameterInfo info = new ParameterInfo(i, oldType, null, oldBody);
081                    oldParameters.add(info);
082                } else if (oldParameters.size() == 1) {
083                    // the 2nd parameter is the headers
084                    Expression oldHeaders = ExpressionBuilder.headersExpression();
085                    ParameterInfo info = new ParameterInfo(i, oldType, null, oldHeaders);
086                    oldParameters.add(info);
087                } else if (oldParameters.size() == 2) {
088                    // the 3rd parameter is the properties
089                    Expression oldProperties = ExpressionBuilder.propertiesExpression();
090                    ParameterInfo info = new ParameterInfo(i, oldType, null, oldProperties);
091                    oldParameters.add(info);
092                }
093            }
094    
095            for (int i = size / 2; i < size; i++) {
096                Class<?> newType = parameterTypes[i];
097                if (newParameters.size() == 0) {
098                    // the first parameter is the body
099                    Expression newBody = ExpressionBuilder.mandatoryBodyExpression(newType);
100                    ParameterInfo info = new ParameterInfo(i, newType, null, newBody);
101                    newParameters.add(info);
102                } else if (newParameters.size() == 1) {
103                    // the 2nd parameter is the headers
104                    Expression newHeaders = ExpressionBuilder.headersExpression();
105                    ParameterInfo info = new ParameterInfo(i, newType, null, newHeaders);
106                    newParameters.add(info);
107                } else if (newParameters.size() == 2) {
108                    // the 3rd parameter is the properties
109                    Expression newProperties = ExpressionBuilder.propertiesExpression();
110                    ParameterInfo info = new ParameterInfo(i, newType, null, newProperties);
111                    newParameters.add(info);
112                }
113            }
114    
115            return new AggregationStrategyMethodInfo(camelContext, type, method, oldParameters, newParameters);
116        }
117    
118    }