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