001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.commons.chain.impl; 018 019import org.apache.commons.chain.Chain; 020import org.apache.commons.chain.Command; 021import org.apache.commons.chain.Context; 022 023/** 024 * Implementation of {@link Command} that simply logs its identifier 025 * and returns. 026 * 027 * @author Craig R. McClanahan 028 * @version $Revision$ $Date$ 029 */ 030public class NonDelegatingCommand implements Command<Context> { 031 032 // ------------------------------------------------------------ Constructor 033 034 public NonDelegatingCommand() { 035 this(""); 036 } 037 038 /** 039 * Construct an instance that will log the specified identifier 040 * 041 * @param id identifier to log for this Command instance 042 */ 043 public NonDelegatingCommand(String id) { 044 this.id = id; 045 } 046 047 // ----------------------------------------------------- Instance Variables 048 049 /** 050 * The identifier to log for this Command instance 051 */ 052 protected String id = null; 053 054 String getId() { 055 return (this.id); 056 } 057 058 public void setId(String id) { 059 this.id = id; 060 } 061 062 // -------------------------------------------------------- Command Methods 063 064 /** 065 * Execution method for this Command 066 * 067 * @param context The {@link Context} to be processed by this 068 * {@link Command} 069 * 070 * @return {@code true} if the processing of this {@link Context} 071 * has been completed, or {@code false} if the processing 072 * of this {@link Context} should be delegated to a 073 * subsequent {@link Command} in an enclosing {@link Chain} 074 * 075 * @throws Exception general purpose exception return 076 * to indicate abnormal termination 077 * @throws IllegalArgumentException if {@code context} 078 * is {@code null} 079 */ 080 @Override 081 public boolean execute(Context context) throws Exception { 082 if (context == null) { 083 throw new IllegalArgumentException(); 084 } 085 log(context, id); 086 return true; 087 } 088 089 // ------------------------------------------------------ Protected Methods 090 091 /** 092 * Log the specified {@code id} into a StringBuffer attribute 093 * named "log" in the specified {@code context}, creating it if 094 * necessary. 095 * 096 * @param context The {@link Context} into which we log the identifiers 097 * @param id The identifier to be logged 098 */ 099 protected void log(Context context, String id) { 100 StringBuffer sb = (StringBuffer) context.get("log"); 101 if (sb == null) { 102 sb = new StringBuffer(); 103 context.put("log", sb); 104 } 105 if (sb.length() > 0) { 106 sb.append('/'); 107 } 108 sb.append(id); 109 } 110}