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.spring.remoting;
018    
019    import org.aopalliance.intercept.MethodInterceptor;
020    import org.aopalliance.intercept.MethodInvocation;
021    
022    import org.apache.camel.CamelContext;
023    import org.apache.camel.CamelContextAware;
024    import org.apache.camel.Endpoint;
025    import org.apache.camel.Producer;
026    import org.apache.camel.component.bean.CamelInvocationHandler;
027    import org.apache.camel.component.bean.MethodInfoCache;
028    import org.apache.camel.util.CamelContextHelper;
029    
030    import org.springframework.beans.factory.DisposableBean;
031    import org.springframework.beans.factory.InitializingBean;
032    
033    import static org.apache.camel.util.ObjectHelper.notNull;
034    
035    /**
036     * A Spring interceptor which sends a message exchange to an endpoint before the method is invoked
037     * 
038     * @version $Revision: 45731 $
039     */
040    public class SendBeforeInterceptor implements MethodInterceptor, CamelContextAware, InitializingBean, DisposableBean {
041        private String uri;
042        private CamelContext camelContext;
043        private CamelInvocationHandler invocationHandler;
044        private Producer producer;
045    
046        public Object invoke(MethodInvocation invocation) throws Throwable {
047            invocationHandler.invoke(invocation.getThis(), invocation.getMethod(), invocation.getArguments());
048            return invocation.proceed();
049        }
050    
051        public void afterPropertiesSet() throws Exception {
052            notNull(uri, "uri");
053            notNull(camelContext, "camelContext");
054    
055            Endpoint endpoint = CamelContextHelper.getMandatoryEndpoint(camelContext, uri);
056            producer = endpoint.createProducer();
057            producer.start();
058            invocationHandler = new CamelInvocationHandler(endpoint, producer, new MethodInfoCache(endpoint.getCamelContext()));
059        }
060    
061        public void destroy() throws Exception {
062            if (producer != null) {
063                producer.stop();
064            }
065        }
066    
067        public void setCamelContext(CamelContext camelContext) {
068            this.camelContext = camelContext;
069        }
070    
071        // Properties
072        //-----------------------------------------------------------------------
073        public String getUri() {
074            return uri;
075        }
076    
077        public void setUri(String uri) {
078            this.uri = uri;
079        }
080    }