View Javadoc

1   /*
2    * Copyright 1999-2004 The Apache Software Foundation
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.apache.commons.chain.generic;
17  
18  
19  import org.apache.commons.chain.Command;
20  import org.apache.commons.chain.Context;
21  
22  
23  /***
24   * <p>Copy a specified literal value, or a context attribute stored under
25   * the <code>fromKey</code> (if any), to the <code>toKey</code>.</p>
26   *
27   * @author Craig R. McClanahan
28   * @version $Revision: 410386 $ $Date: 2006-05-30 22:48:31 +0100 (Tue, 30 May 2006) $
29   */
30  
31  public class CopyCommand implements Command {
32  
33  
34      // -------------------------------------------------------------- Properties
35  
36  
37      private String fromKey = null;
38  
39  
40      /***
41       * <p>Return the context attribute key for the source attribute.</p>
42       * @return The source attribute key.
43       */
44      public String getFromKey() {
45  
46      return (this.fromKey);
47  
48      }
49  
50  
51      /***
52       * <p>Set the context attribute key for the source attribute.</p>
53       *
54       * @param fromKey The new key
55       */
56      public void setFromKey(String fromKey) {
57  
58      this.fromKey = fromKey;
59  
60      }
61  
62  
63      private String toKey = null;
64  
65  
66      /***
67       * <p>Return the context attribute key for the destination attribute.</p>
68       * @return The destination attribute key.
69       */
70      public String getToKey() {
71  
72      return (this.toKey);
73  
74      }
75  
76  
77      /***
78       * <p>Set the context attribute key for the destination attribute.</p>
79       *
80       * @param toKey The new key
81       */
82      public void setToKey(String toKey) {
83  
84      this.toKey = toKey;
85  
86      }
87  
88  
89      private String value = null;
90  
91  
92      /***
93       * <p>Return the literal value to be copied.</p>
94       * @return The literal value.
95       */
96      public String getValue() {
97  
98          return (this.value);
99  
100     }
101 
102 
103     /***
104      * <p>Set the literal value to be copied.</p>
105      *
106      * @param value The new value
107      */
108     public void setValue(String value) {
109 
110         this.value = value;
111 
112     }
113 
114 
115     // ---------------------------------------------------------- Filter Methods
116 
117 
118     /***
119      * <p>Copy a specified literal value, or a context attribute stored under
120      * the <code>fromKey</code> (if any), to the <code>toKey</code>.</p>
121      *
122      * @param context {@link Context} in which we are operating
123      *
124      * @return <code>false</code> so that processing will continue
125      * @throws Exception in the if an error occurs during execution.
126      */
127     public boolean execute(Context context) throws Exception {
128 
129         Object value = this.value;
130 
131         if (value == null) {
132             value = context.get(getFromKey());
133         }
134 
135         if (value != null) {
136             context.put(getToKey(), value);
137         } else {
138             context.remove(getToKey());
139         }
140 
141         return (false);
142 
143     }
144 
145 
146 }