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.spi.Policy;
027    import org.apache.camel.spi.RouteContext;
028    
029    /**
030     * Represents an XML <policy/> element
031     *
032     * @version $Revision: 54143 $
033     */
034    @XmlRootElement(name = "policy")
035    @XmlAccessorType(XmlAccessType.FIELD)
036    public class PolicyRef extends OutputType<ProcessorType> {
037        @XmlAttribute(required = true)
038        private String ref;
039        @XmlTransient
040        private Policy policy;
041    
042        public PolicyRef() {
043        }
044    
045        public PolicyRef(Policy policy) {
046            this.policy = policy;
047        }
048    
049        @Override
050        public String toString() {
051            return "Policy[" + description() + "]";
052        }
053    
054        @Override
055        public String getShortName() {
056            return "policy";
057        }
058    
059        @Override
060        public String getLabel() {
061            if (ref != null) {
062                return "ref:  " + ref;
063            } else if (policy != null) {
064                return policy.toString();
065            } else {
066                return "";
067            }
068        }
069    
070        public String getRef() {
071            return ref;
072        }
073    
074        public void setRef(String ref) {
075            this.ref = ref;
076        }
077    
078        @Override
079        public Processor createProcessor(RouteContext routeContext) throws Exception {
080            Processor childProcessor = createOutputsProcessor(routeContext);
081    
082            Policy policy = resolvePolicy(routeContext);
083            if (policy == null) {
084                throw new IllegalArgumentException("No policy configured: " + this);
085            }
086            return policy.wrap(childProcessor);
087        }
088    
089        protected Policy resolvePolicy(RouteContext routeContext) {
090            if (policy == null) {
091                policy = routeContext.lookup(getRef(), Policy.class);
092            }
093            return policy;
094        }
095    
096        protected String description() {
097            if (policy != null) {
098                return policy.toString();
099            } else {
100                return "ref: " + ref;
101            }
102        }
103    }