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.model;
018    
019    import javax.xml.bind.annotation.XmlAccessType;
020    import javax.xml.bind.annotation.XmlAccessorType;
021    import javax.xml.bind.annotation.XmlAttribute;
022    import javax.xml.bind.annotation.XmlRootElement;
023    import javax.xml.bind.annotation.XmlTransient;
024    
025    import org.apache.camel.Processor;
026    import org.apache.camel.impl.RouteContext;
027    import org.apache.camel.processor.DelegateProcessor;
028    
029    /**
030     * @version $Revision: 36565 $
031     */
032    @XmlRootElement(name = "interceptor")
033    @XmlAccessorType(XmlAccessType.FIELD)
034    public class InterceptorRef extends InterceptorType {
035        @XmlAttribute(required = true)
036        private String ref;
037        @XmlTransient
038        private DelegateProcessor interceptor;
039    
040        public InterceptorRef() {
041        }
042    
043        public InterceptorRef(String ref) {
044            setRef(ref);
045        }
046    
047        public InterceptorRef(DelegateProcessor interceptor) {
048            this.interceptor = interceptor;
049        }
050    
051        @Override
052        public String toString() {
053            return "Interceptor[" + getLabel() + "]";
054        }
055    
056        @Override
057        public Processor createProcessor(RouteContext routeContext) throws Exception {
058            DelegateProcessor processor = createInterceptor(routeContext);
059            Processor child = createOutputsProcessor(routeContext);
060            processor.setProcessor(child);
061            return processor;
062        }
063    
064        public DelegateProcessor createInterceptor(RouteContext routeContext) {
065            if (interceptor == null) {
066                interceptor = routeContext.lookup(getRef(), DelegateProcessor.class);
067            }
068            if (interceptor == null) {
069                throw new IllegalArgumentException("No DelegateProcessor bean available for reference: " + getRef());
070            }
071            return interceptor;
072        }
073    
074        public String getRef() {
075            return ref;
076        }
077    
078        public void setRef(String ref) {
079            this.ref = ref;
080        }
081    
082        public String getLabel() {
083            if (ref != null) {
084                return "ref:  " + ref;
085            } else if (interceptor != null) {
086                return interceptor.toString();
087            } else {
088                return "";
089            }
090        }
091        
092        /**
093         * Get the underlying {@link DelegateProcessor} implementation
094         * 
095         * @return the {@link DelegateProcessor}
096         */
097        @XmlTransient
098        public DelegateProcessor getInterceptor() {
099            return interceptor;
100        }
101    }