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