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.impl;
17  
18  
19  import org.apache.commons.chain.Command;
20  import org.apache.commons.chain.Context;
21  
22  
23  /***
24   * <p>Implementation of {@link Command} that simply logs its identifier
25   * and returns.</p>
26   *
27   * @author Craig R. McClanahan
28   * @version $Revision: 1.6 $ $Date: 2004/02/25 00:01:05 $
29   */
30  
31  public class NonDelegatingCommand implements Command {
32  
33  
34      // ------------------------------------------------------------ Constructor
35  
36  
37      public NonDelegatingCommand() {
38      this("");
39      }
40  
41  
42      // Construct an instance that will log the specified identifier
43      public NonDelegatingCommand(String id) {
44          this.id = id;
45      }
46  
47  
48      // ----------------------------------------------------- Instance Variables
49  
50  
51      // The identifier to log for this Command instance
52      protected String id = null;
53  
54      String getId() {
55          return (this.id);
56      }
57  
58      public void setId(String id) {
59      this.id = id;
60      }
61  
62  
63      // -------------------------------------------------------- Command Methods
64  
65  
66      // Execution method for this Command
67      public boolean execute(Context context) throws Exception {
68  
69          if (context == null) {
70              throw new IllegalArgumentException();
71          }
72          log(context, id);
73          return (true);
74  
75      }
76  
77  
78  
79      // ------------------------------------------------------ Protected Methods
80  
81  
82      /***
83       * <p>Log the specified <code>id</code> into a StringBuffer attribute
84       * named "log" in the specified <code>context</code>, creating it if
85       * necessary.</p>
86       *
87       * @param context The {@link Context} into which we log the identifiers
88       * @param id The identifier to be logged
89       */
90      protected void log(Context context, String id) {
91          StringBuffer sb = (StringBuffer) context.get("log");
92          if (sb == null) {
93              sb = new StringBuffer();
94              context.put("log", sb);
95          }
96          if (sb.length() > 0) {
97              sb.append('/');
98          }
99          sb.append(id);
100     }
101 
102 
103 }