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