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: 41908 $
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 getLabel() {
056            if (ref != null) {
057                return "ref:  " + ref;
058            } else if (policy != null) {
059                return policy.toString();
060            } else {
061                return "";
062            }
063        }
064    
065        public String getRef() {
066            return ref;
067        }
068    
069        public void setRef(String ref) {
070            this.ref = ref;
071        }
072    
073        @Override
074        public Processor createProcessor(RouteContext routeContext) throws Exception {
075            Processor childProcessor = createOutputsProcessor(routeContext);
076    
077            Policy policy = resolvePolicy(routeContext);
078            if (policy == null) {
079                throw new IllegalArgumentException("No policy configured: " + this);
080            }
081            return policy.wrap(childProcessor);
082        }
083    
084        protected Policy resolvePolicy(RouteContext routeContext) {
085            if (policy == null) {
086                policy = routeContext.lookup(getRef(), Policy.class);
087            }
088            return policy;
089        }
090    
091        protected String description() {
092            if (policy != null) {
093                return policy.toString();
094            } else {
095                return "ref:  " + ref;
096            }
097        }
098    }