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; 023 024import org.apache.camel.Expression; 025import org.apache.camel.Processor; 026import org.apache.camel.builder.ExpressionBuilder; 027import org.apache.camel.model.language.ExpressionDefinition; 028import org.apache.camel.processor.SetPropertyProcessor; 029import org.apache.camel.spi.Metadata; 030import org.apache.camel.spi.RouteContext; 031import org.apache.camel.util.ObjectHelper; 032 033/** 034 * Sets a named property on the message exchange 035 */ 036@Metadata(label = "eip,transformation") 037@XmlRootElement(name = "setProperty") 038@XmlAccessorType(XmlAccessType.FIELD) 039public class SetPropertyDefinition extends NoOutputExpressionNode { 040 @XmlAttribute(required = true) 041 private String propertyName; 042 043 public SetPropertyDefinition() { 044 } 045 046 public SetPropertyDefinition(String propertyName, ExpressionDefinition expression) { 047 super(expression); 048 setPropertyName(propertyName); 049 } 050 051 public SetPropertyDefinition(String propertyName, Expression expression) { 052 super(expression); 053 setPropertyName(propertyName); 054 } 055 056 public SetPropertyDefinition(String propertyName, String value) { 057 super(ExpressionBuilder.constantExpression(value)); 058 setPropertyName(propertyName); 059 } 060 061 @Override 062 public String toString() { 063 return "SetProperty[" + getPropertyName() + ", " + getExpression() + "]"; 064 } 065 066 @Override 067 public String getShortName() { 068 return "setProperty"; 069 } 070 071 @Override 072 public String getLabel() { 073 return "setProperty[" + getPropertyName() + "]"; 074 } 075 076 @Override 077 public Processor createProcessor(RouteContext routeContext) throws Exception { 078 ObjectHelper.notNull(getPropertyName(), "propertyName", this); 079 Expression expr = getExpression().createExpression(routeContext); 080 Expression nameExpr = ExpressionBuilder.parseSimpleOrFallbackToConstantExpression(getPropertyName(), routeContext.getCamelContext()); 081 return new SetPropertyProcessor(nameExpr, expr); 082 } 083 084 /** 085 * Expression to return the value of the message exchange property 086 */ 087 @Override 088 public void setExpression(ExpressionDefinition expression) { 089 // override to include javadoc what the expression is used for 090 super.setExpression(expression); 091 } 092 093 /** 094 * Name of exchange property to set a new value. 095 * <p/> 096 * The <tt>simple</tt> language can be used to define a dynamic evaluated exchange property name to be used. 097 * Otherwise a constant name will be used. 098 */ 099 public void setPropertyName(String propertyName) { 100 this.propertyName = propertyName; 101 } 102 103 public String getPropertyName() { 104 return propertyName; 105 } 106 107}