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 logs its identifier and
025 * and attempts to add a new {@link Command} to the {@link Chain}. This
026 * should cause an IllegalStateException if the {@link Chain} implementation
027 * subclasses {@code ChainBase}.
028 *
029 * @author Craig R. McClanahan
030 * @version $Revision$ $Date$
031 */
032public class AddingCommand extends NonDelegatingCommand {
033
034    // ------------------------------------------------------------ Constructor
035
036    public AddingCommand() {
037        this("", null);
038    }
039
040    /**
041     * Construct an instance that will log the specified identifier
042     *
043     * @param id identifier to log for this Command instance
044     * @param parent the parent Chain
045     */
046    public AddingCommand(String id, Chain<Context> parent) {
047        super(id);
048        this.parent = parent;
049    }
050
051    /**
052     * The parent Chain
053     */
054    private Chain<Context> parent = null;
055
056    // -------------------------------------------------------- Command Methods
057
058    /**
059     * Execution method for this Command
060     *
061     * @param context The {@link Context} to be processed by this
062     *        {@link Command}
063     * @param chain the parent Chain
064     *
065     * @return {@code true} if the processing of this {@link Context}
066     *         has been completed, or {@code false} if the processing
067     *         of this {@link Context} should be delegated to a
068     *         subsequent {@link Command} in an enclosing {@link Chain}
069     *
070     * @throws Exception general purpose exception return
071     *         to indicate abnormal termination
072     * @throws IllegalArgumentException if {@code context}
073     *         is {@code null}
074     */
075    public boolean execute(Context context, Chain<Context> chain) throws Exception {
076        super.execute(context);
077        parent.addCommand(new NonDelegatingCommand("NEW")); // Should cause ISE
078        return true;
079    }
080}