View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.chain.generic;
18  
19  
20  import org.apache.commons.chain.Command;
21  import org.apache.commons.chain.Context;
22  
23  
24  /**
25   * <p>Copy a specified literal value, or a context attribute stored under
26   * the <code>fromKey</code> (if any), to the <code>toKey</code>.</p>
27   *
28   * @author Craig R. McClanahan
29   * @version $Revision: 480477 $ $Date: 2006-11-29 08:34:52 +0000 (Wed, 29 Nov 2006) $
30   */
31  
32  public class CopyCommand implements Command {
33  
34  
35      // -------------------------------------------------------------- Properties
36  
37  
38      private String fromKey = null;
39  
40  
41      /**
42       * <p>Return the context attribute key for the source attribute.</p>
43       * @return The source attribute key.
44       */
45      public String getFromKey() {
46  
47      return (this.fromKey);
48  
49      }
50  
51  
52      /**
53       * <p>Set the context attribute key for the source attribute.</p>
54       *
55       * @param fromKey The new key
56       */
57      public void setFromKey(String fromKey) {
58  
59      this.fromKey = fromKey;
60  
61      }
62  
63  
64      private String toKey = null;
65  
66  
67      /**
68       * <p>Return the context attribute key for the destination attribute.</p>
69       * @return The destination attribute key.
70       */
71      public String getToKey() {
72  
73      return (this.toKey);
74  
75      }
76  
77  
78      /**
79       * <p>Set the context attribute key for the destination attribute.</p>
80       *
81       * @param toKey The new key
82       */
83      public void setToKey(String toKey) {
84  
85      this.toKey = toKey;
86  
87      }
88  
89  
90      private String value = null;
91  
92  
93      /**
94       * <p>Return the literal value to be copied.</p>
95       * @return The literal value.
96       */
97      public String getValue() {
98  
99          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 }