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