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.commons.chain.generic; 018 019import org.apache.commons.chain.Command; 020import org.apache.commons.chain.Context; 021 022/** 023 * Copy a specified literal value, or a context attribute stored under 024 * the {@code fromKey} (if any), to the {@code toKey}. 025 * 026 * @param <C> Type of the context associated with this command 027 * 028 * @author Craig R. McClanahan 029 * @version $Revision$ $Date$ 030 */ 031public class CopyCommand<C extends Context> implements Command<C> { 032 033 // -------------------------------------------------------------- Properties 034 035 private String fromKey = null; 036 037 /** 038 * Return the context attribute key for the source attribute. 039 * 040 * @return The source attribute key. 041 */ 042 public String getFromKey() { 043 return this.fromKey; 044 } 045 046 /** 047 * Set the context attribute key for the source attribute. 048 * 049 * @param fromKey The new key 050 */ 051 public void setFromKey(String fromKey) { 052 this.fromKey = fromKey; 053 } 054 055 private String toKey = null; 056 057 /** 058 * Return the context attribute key for the destination attribute. 059 * 060 * @return The destination attribute key. 061 */ 062 public String getToKey() { 063 return this.toKey; 064 } 065 066 /** 067 * Set the context attribute key for the destination attribute. 068 * 069 * @param toKey The new key 070 */ 071 public void setToKey(String toKey) { 072 this.toKey = toKey; 073 } 074 075 private String value = null; 076 077 /** 078 * Return the literal value to be copied. 079 * 080 * @return The literal value. 081 */ 082 public String getValue() { 083 return this.value; 084 } 085 086 /** 087 * Set the literal value to be copied. 088 * 089 * @param value The new value 090 */ 091 public void setValue(String value) { 092 this.value = value; 093 } 094 095 // ------------------------------------------------------------ Constructors 096 097 /** 098 * The Default-Constructor for this class. 099 */ 100 public CopyCommand() { 101 } 102 103 // ---------------------------------------------------------- Filter Methods 104 105 /** 106 * Copy a specified literal value, or a context attribute stored under 107 * the {@code fromKey} (if any), to the {@code toKey}. 108 * 109 * @param context {@link Context} in which we are operating 110 * 111 * @return {@code false} so that processing will continue 112 * 113 * @throws Exception in the if an error occurs during execution. 114 */ 115 @Override 116 public boolean execute(C context) throws Exception { 117 Object value = this.value; 118 119 if (value == null) { 120 value = context.get(getFromKey()); 121 } 122 123 if (value != null) { 124 context.put(getToKey(), value); 125 } else { 126 context.remove(getToKey()); 127 } 128 129 return false; 130 } 131}