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