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