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 import org.apache.camel.spi.TransactedPolicy;
029 import org.apache.camel.util.ObjectHelper;
030
031 /**
032 * Represents an XML <policy/> element
033 *
034 * @version $Revision: 896185 $
035 */
036 @XmlRootElement(name = "policy")
037 @XmlAccessorType(XmlAccessType.FIELD)
038 public class PolicyDefinition extends OutputDefinition<ProcessorDefinition> {
039
040 // TODO: Align this code with TransactedDefinition
041
042 @XmlTransient
043 protected Class<? extends Policy> type;
044 @XmlAttribute(required = true)
045 protected String ref;
046 @XmlTransient
047 private Policy policy;
048
049 public PolicyDefinition() {
050 }
051
052 public PolicyDefinition(Policy policy) {
053 this.policy = policy;
054 }
055
056 @Override
057 public String toString() {
058 return "Policy[" + description() + "]";
059 }
060
061 @Override
062 public String getShortName() {
063 // a policy can be a hidden disguise for a transacted definition
064 boolean transacted = type != null && type.isAssignableFrom(TransactedPolicy.class);
065 return transacted ? "transacted" : "policy";
066 }
067
068 @Override
069 public String getLabel() {
070 if (ref != null) {
071 return getShortName() + "[ref:" + ref + "]";
072 } else if (policy != null) {
073 return getShortName() + "[" + policy.toString() + "]";
074 } else {
075 return getShortName();
076 }
077 }
078
079 @Override
080 public boolean isAbstract() {
081 return true;
082 }
083
084 public String getRef() {
085 return ref;
086 }
087
088 public void setRef(String ref) {
089 this.ref = ref;
090 }
091
092 /**
093 * Sets a policy type that this definition should scope within.
094 * <p/>
095 * Is used for convention over configuration situations where the policy
096 * should be automatic looked up in the registry and it should be based
097 * on this type. For instance a {@link org.apache.camel.spi.TransactedPolicy}
098 * can be set as type for easy transaction configuration.
099 * <p/>
100 * Will by default scope to the wide {@link Policy}
101 *
102 * @param type the policy type
103 */
104 public void setType(Class<? extends Policy> type) {
105 this.type = type;
106 }
107
108 /**
109 * Sets a reference to use for lookup the policy in the registry.
110 *
111 * @param ref the reference
112 * @return the builder
113 */
114 public PolicyDefinition ref(String ref) {
115 setRef(ref);
116 return this;
117 }
118
119 @Override
120 public Processor createProcessor(RouteContext routeContext) throws Exception {
121 Processor childProcessor = createOutputsProcessor(routeContext);
122
123 Policy policy = resolvePolicy(routeContext);
124 ObjectHelper.notNull(policy, "policy", this);
125 return policy.wrap(routeContext, childProcessor);
126 }
127
128 protected String description() {
129 if (policy != null) {
130 return policy.toString();
131 } else {
132 return "ref:" + ref;
133 }
134 }
135
136 protected Policy resolvePolicy(RouteContext routeContext) {
137 if (policy != null) {
138 return policy;
139 }
140 // reuse code on transacted definition to do the resolution
141 return TransactedDefinition.doResolvePolicy(routeContext, getRef(), type);
142 }
143
144 }