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 */
017package org.apache.camel.model;
018
019import java.util.List;
020
021import javax.xml.bind.annotation.XmlAccessType;
022import javax.xml.bind.annotation.XmlAccessorType;
023import javax.xml.bind.annotation.XmlAttribute;
024import javax.xml.bind.annotation.XmlElementRef;
025import javax.xml.bind.annotation.XmlRootElement;
026import javax.xml.bind.annotation.XmlTransient;
027
028import org.apache.camel.spi.Metadata;
029import org.apache.camel.spi.Policy;
030import org.apache.camel.spi.TransactedPolicy;
031
032/**
033 * Defines a policy the route will use
034 */
035@Metadata(label = "configuration")
036@XmlRootElement(name = "policy")
037@XmlAccessorType(XmlAccessType.FIELD)
038public class PolicyDefinition extends OutputDefinition<PolicyDefinition> {
039
040    @XmlTransient
041    protected Class<? extends Policy> type;
042    @XmlAttribute(required = true)
043    protected String ref;
044    @XmlTransient
045    private Policy policy;
046
047    public PolicyDefinition() {
048    }
049
050    public PolicyDefinition(Policy policy) {
051        this.policy = policy;
052    }
053
054    @Override
055    public List<ProcessorDefinition<?>> getOutputs() {
056        return outputs;
057    }
058
059    @XmlElementRef
060    @Override
061    public void setOutputs(List<ProcessorDefinition<?>> outputs) {
062        super.setOutputs(outputs);
063    }
064
065    @Override
066    public String toString() {
067        return "Policy[" + description() + "]";
068    }
069
070    public Policy getPolicy() {
071        return policy;
072    }
073
074    public Class<? extends Policy> getType() {
075        return type;
076    }
077
078    protected String description() {
079        if (policy != null) {
080            return policy.toString();
081        } else {
082            return "ref:" + ref;
083        }
084    }
085
086    @Override
087    public String getShortName() {
088        // a policy can be a hidden disguise for a transacted definition
089        boolean transacted = type != null && type.isAssignableFrom(TransactedPolicy.class);
090        return transacted ? "transacted" : "policy";
091    }
092
093    @Override
094    public String getLabel() {
095        return getShortName() + "[" + getDescription() + "]";
096    }
097
098    @Override
099    public boolean isAbstract() {
100        // policy should NOT be abstract
101        return false;
102    }
103
104    @Override
105    public boolean isTopLevelOnly() {
106        // a policy is often top-level but you can have it in lower-levels as
107        // well
108        return false;
109    }
110
111    @Override
112    public boolean isWrappingEntireOutput() {
113        return true;
114    }
115
116    public String getRef() {
117        return ref;
118    }
119
120    public void setRef(String ref) {
121        this.ref = ref;
122    }
123
124    /**
125     * Sets a policy type that this definition should scope within.
126     * <p/>
127     * Is used for convention over configuration situations where the policy
128     * should be automatic looked up in the registry and it should be based on
129     * this type. For instance a {@link org.apache.camel.spi.TransactedPolicy}
130     * can be set as type for easy transaction configuration.
131     * <p/>
132     * Will by default scope to the wide {@link Policy}
133     *
134     * @param type the policy type
135     */
136    public void setType(Class<? extends Policy> type) {
137        this.type = type;
138    }
139
140    /**
141     * Sets a reference to use for lookup the policy in the registry.
142     *
143     * @param ref the reference
144     * @return the builder
145     */
146    public PolicyDefinition ref(String ref) {
147        setRef(ref);
148        return this;
149    }
150
151}