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