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.servicemix.bean.support;
018    
019    import java.util.Map;
020    import java.util.concurrent.ConcurrentHashMap;
021    
022    import javax.jbi.messaging.MessageExchange;
023    import javax.jbi.messaging.MessagingException;
024    import javax.jbi.messaging.NormalizedMessage;
025    import javax.xml.transform.Source;
026    
027    import org.aopalliance.intercept.MethodInvocation;
028    import org.apache.servicemix.bean.BeanEndpoint;
029    import org.apache.servicemix.expression.Expression;
030    
031    /**
032     * Represents the strategy used to figure out how to map a JBI message exchange to a POJO method invocation
033     *
034     * @version $Revision: $
035     * @org.apache.xbean.XBean element="defaultMethodInvocationStrategy" 
036     *      description="The default strategy for invoking methods on POJOs from a JBI message exchange"
037     */
038    public class DefaultMethodInvocationStrategy implements MethodInvocationStrategy {
039    
040        private Map<Class, Expression> parameterTypeToExpressionMap = new ConcurrentHashMap<Class, Expression>();
041    
042        public DefaultMethodInvocationStrategy() {
043        }
044    
045    
046        public Expression getDefaultParameterTypeExpression(Class parameterType) {
047            return parameterTypeToExpressionMap.get(parameterType);
048        }
049    
050        /**
051         * Adds a default parameter type mapping to an expression
052         */
053        public void addParameterMapping(Class parameterType, Expression expression) {
054            parameterTypeToExpressionMap.put(parameterType, expression);
055        }
056    
057    
058        /**
059         * Creates an invocation on the given POJO using annotations to decide which method to invoke
060         * and to figure out which parameters to use
061         */
062        public MethodInvocation createInvocation(Object pojo, 
063                                                 BeanInfo beanInfo, 
064                                                 MessageExchange messageExchange, 
065                                                 BeanEndpoint pojoEndpoint) throws MessagingException {
066            return beanInfo.createInvocation(pojo, messageExchange);
067        }
068    
069    
070        public void loadDefaultRegistry() {
071            addParameterMapping(MessageExchange.class, new Expression() {
072                public Object evaluate(MessageExchange messageExchange, NormalizedMessage normalizedMessage)
073                    throws MessagingException {
074                    return messageExchange;
075                }
076            });
077    
078            addParameterMapping(NormalizedMessage.class, new Expression() {
079                public Object evaluate(MessageExchange messageExchange, NormalizedMessage normalizedMessage)
080                    throws MessagingException {
081                    return normalizedMessage;
082                }
083            });
084    
085            addParameterMapping(Source.class, new Expression() {
086                public Object evaluate(MessageExchange messageExchange, NormalizedMessage normalizedMessage)
087                    throws MessagingException {
088                    return normalizedMessage.getContent();
089                }
090            });
091        }
092    
093    
094    }