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