001: /*
002: * Copyright 1999-2004 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.chain.generic;
017:
018: import org.apache.commons.chain.Command;
019: import org.apache.commons.chain.Context;
020:
021: /**
022: * <p>Copy a specified literal value, or a context attribute stored under
023: * the <code>fromKey</code> (if any), to the <code>toKey</code>.</p>
024: *
025: * @author Craig R. McClanahan
026: * @version $Revision: 410386 $ $Date: 2006-05-30 22:48:31 +0100 (Tue, 30 May 2006) $
027: */
028:
029: public class CopyCommand implements Command {
030:
031: // -------------------------------------------------------------- Properties
032:
033: private String fromKey = null;
034:
035: /**
036: * <p>Return the context attribute key for the source attribute.</p>
037: * @return The source attribute key.
038: */
039: public String getFromKey() {
040:
041: return (this .fromKey);
042:
043: }
044:
045: /**
046: * <p>Set the context attribute key for the source attribute.</p>
047: *
048: * @param fromKey The new key
049: */
050: public void setFromKey(String fromKey) {
051:
052: this .fromKey = fromKey;
053:
054: }
055:
056: private String toKey = null;
057:
058: /**
059: * <p>Return the context attribute key for the destination attribute.</p>
060: * @return The destination attribute key.
061: */
062: public String getToKey() {
063:
064: return (this .toKey);
065:
066: }
067:
068: /**
069: * <p>Set the context attribute key for the destination attribute.</p>
070: *
071: * @param toKey The new key
072: */
073: public void setToKey(String toKey) {
074:
075: this .toKey = toKey;
076:
077: }
078:
079: private String value = null;
080:
081: /**
082: * <p>Return the literal value to be copied.</p>
083: * @return The literal value.
084: */
085: public String getValue() {
086:
087: return (this .value);
088:
089: }
090:
091: /**
092: * <p>Set the literal value to be copied.</p>
093: *
094: * @param value The new value
095: */
096: public void setValue(String value) {
097:
098: this .value = value;
099:
100: }
101:
102: // ---------------------------------------------------------- Filter Methods
103:
104: /**
105: * <p>Copy a specified literal value, or a context attribute stored under
106: * the <code>fromKey</code> (if any), to the <code>toKey</code>.</p>
107: *
108: * @param context {@link Context} in which we are operating
109: *
110: * @return <code>false</code> so that processing will continue
111: * @throws Exception in the if an error occurs during execution.
112: */
113: public boolean execute(Context context) throws Exception {
114:
115: Object value = this .value;
116:
117: if (value == null) {
118: value = context.get(getFromKey());
119: }
120:
121: if (value != null) {
122: context.put(getToKey(), value);
123: } else {
124: context.remove(getToKey());
125: }
126:
127: return (false);
128:
129: }
130:
131: }
|