01: /*
02: * Copyright 1999-2004 The Apache Software Foundation
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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: import org.apache.commons.chain.Command;
19: import org.apache.commons.chain.Context;
20:
21: /**
22: * <p>Implementation of {@link Command} that simply logs its identifier
23: * and returns.</p>
24: *
25: * @author Craig R. McClanahan
26: * @version $Revision: 155403 $ $Date: 2005-02-26 12:52:46 +0000 (Sat, 26 Feb 2005) $
27: */
28:
29: public class NonDelegatingCommand implements Command {
30:
31: // ------------------------------------------------------------ Constructor
32:
33: public NonDelegatingCommand() {
34: this ("");
35: }
36:
37: // Construct an instance that will log the specified identifier
38: public NonDelegatingCommand(String id) {
39: this .id = id;
40: }
41:
42: // ----------------------------------------------------- Instance Variables
43:
44: // The identifier to log for this Command instance
45: protected String id = null;
46:
47: String getId() {
48: return (this .id);
49: }
50:
51: public void setId(String id) {
52: this .id = id;
53: }
54:
55: // -------------------------------------------------------- Command Methods
56:
57: // Execution method for this Command
58: public boolean execute(Context context) throws Exception {
59:
60: if (context == null) {
61: throw new IllegalArgumentException();
62: }
63: log(context, id);
64: return (true);
65:
66: }
67:
68: // ------------------------------------------------------ Protected Methods
69:
70: /**
71: * <p>Log the specified <code>id</code> into a StringBuffer attribute
72: * named "log" in the specified <code>context</code>, creating it if
73: * necessary.</p>
74: *
75: * @param context The {@link Context} into which we log the identifiers
76: * @param id The identifier to be logged
77: */
78: protected void log(Context context, String id) {
79: StringBuffer sb = (StringBuffer) context.get("log");
80: if (sb == null) {
81: sb = new StringBuffer();
82: context.put("log", sb);
83: }
84: if (sb.length() > 0) {
85: sb.append('/');
86: }
87: sb.append(id);
88: }
89:
90: }
|