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.reflect.Method;
020import java.util.ArrayList;
021import java.util.List;
022
023import org.apache.camel.CamelContext;
024import org.apache.camel.Exchange;
025import org.apache.camel.component.bean.ParameterInfo;
026
027/**
028 * Method information about the POJO method to call when using the {@link AggregationStrategyBeanAdapter}.
029 */
030public class AggregationStrategyMethodInfo {
031
032    private final Method method;
033    private final List<ParameterInfo> oldParameters;
034    private final List<ParameterInfo> newParameters;
035
036    
037    @Deprecated
038    public AggregationStrategyMethodInfo(CamelContext camelContext, Class<?> type, Method method,
039                                         List<ParameterInfo> oldParameters, List<ParameterInfo> newParameters) {
040        this(method, oldParameters, newParameters);
041    }
042    public AggregationStrategyMethodInfo(Method method,
043                                         List<ParameterInfo> oldParameters, 
044                                         List<ParameterInfo> newParameters) {
045        this.method = method;
046        this.oldParameters = oldParameters;
047        this.newParameters = newParameters;
048    }
049
050    public Object invoke(Object pojo, Exchange oldExchange, Exchange newExchange) throws Exception {
051        // evaluate the parameters
052        List<Object> list = new ArrayList<>(oldParameters.size() + newParameters.size());
053        for (ParameterInfo info : oldParameters) {
054            if (oldExchange != null) {
055                Object value = info.getExpression().evaluate(oldExchange, info.getType());
056                list.add(value);
057            } else {
058                // use a null value as oldExchange is null
059                list.add(null);
060            }
061        }
062        for (ParameterInfo info : newParameters) {
063            if (newExchange != null) {
064                Object value = info.getExpression().evaluate(newExchange, info.getType());
065                list.add(value);
066            } else {
067                // use a null value as newExchange is null
068                list.add(null);
069            }
070        }
071
072        Object[] args = list.toArray();
073        return method.invoke(pojo, args);
074    }
075}