View Javadoc
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;
18  
19  /**
20   * A {@link Chain} represents a configured list of
21   * {@link Command}s that will be executed in order to perform processing
22   * on a specified {@link Context}. Each included {@link Command} will be
23   * executed in turn, until either one of them returns {@code true},
24   * one of the executed {@link Command}s throws an exception,
25   * or the end of the chain has been reached. The {@link Chain} itself will
26   * return the return value of the last {@link Command} that was executed
27   * (if no exception was thrown), or rethrow the thrown exception.
28   *
29   * <p>Note that {@link Chain} extends {@link Command}, so that the two can
30   * be used interchangeably when a {@link Command} is expected. This makes it
31   * easy to assemble workflows in a hierarchical manner by combining subchains
32   * into an overall processing chain.</p>
33   *
34   * <p>To protect applications from evolution of this interface, specialized
35   * implementations of {@link Chain} should generally be created by extending
36   * the provided base class {@link org.apache.commons.chain.impl.ChainBase})
37   * rather than directly implementing this interface.</p>
38   *
39   * <p>{@link Chain} implementations should be designed in a thread-safe
40   * manner, suitable for execution on multiple threads simultaneously. In
41   * general, this implies that the state information identifying which
42   * {@link Command} is currently being executed should be maintained in a
43   * local variable inside the {@code execute()} method, rather than
44   * in an instance variable. The {@link Command}s in a {@link Chain} may be
45   * configured (via calls to {@code addCommand()}) at any time before
46   * the {@code execute()} method of the {@link Chain} is first called.
47   * After that, the configuration of the {@link Chain} is frozen.</p>
48   *
49   * @param <C> Type of the context associated with this chain
50   *
51   * @author Craig R. McClanahan
52   * @version $Revision$ $Date$
53   */
54  public interface Chain<C extends Context> extends Command<C> {
55  
56      /**
57       * Add a {@link Command} to the list of {@link Command}s that will
58       * be called in turn when this {@link Chain}'s {@code execute()}
59       * method is called. Once {@code execute()} has been called
60       * at least once, it is no longer possible to add additional
61       * {@link Command}s; instead, an exception will be thrown.
62       *
63       * @param <CMD> the {@link Command} type to be added in the {@link Chain}
64       * @param command The {@link Command} to be added
65       *
66       * @throws IllegalArgumentException if {@code command}
67       *         is {@code null}
68       * @throws IllegalStateException if this {@link Chain} has already
69       *         been executed at least once, so no further configuration
70       *         is allowed
71       */
72      <CMD extends Command<C>> void addCommand(CMD command);
73  
74      /**
75       * Execute the processing represented by this {@link Chain} according
76       * to the following algorithm.
77       * <ul>
78       * <li>If there are no configured {@link Command}s in the {@link Chain},
79       *     return {@code false}.</li>
80       * <li>Call the {@code execute()} method of each {@link Command}
81       *     configured on this chain, in the order they were added via calls
82       *     to the {@code addCommand()} method, until the end of the
83       *     configured {@link Command}s is encountered, or until one of
84       *     the executed {@link Command}s returns {@code true}
85       *     or throws an exception.</li>
86       * <li>Walk backwards through the {@link Command}s whose
87       *     {@code execute()} methods, starting with the last one that
88       *     was executed. If this {@link Command} instance is also a
89       *     {@link Filter}, call its {@code postprocess()} method,
90       *     discarding any exception that is thrown.</li>
91       * <li>If the last {@link Command} whose {@code execute()} method
92       *     was called threw an exception, rethrow that exception.</li>
93       * <li>Otherwise, return the value returned by the {@code execute()}
94       *     method of the last {@link Command} that was executed. This will be
95       *     {@code true} if the last {@link Command} indicated that
96       *     processing of this {@link Context} has been completed, or
97       *     {@code false} if none of the called {@link Command}s
98       *     returned {@code true}.</li>
99       * </ul>
100      *
101      * @param context The {@link Context} to be processed by this
102      *        {@link Chain}
103      *
104      * @return {@code true} if the processing of this {@link Context}
105      *         has been completed, or {@code false} if the processing
106      *         of this {@link Context} should be delegated to a
107      *         subsequent {@link Command} in an enclosing {@link Chain}
108      *
109      * @throws Exception if thrown by one of the {@link Command}s
110      *         in this {@link Chain} but not handled by a
111      *         {@code postprocess()} method of a {@link Filter}
112      * @throws IllegalArgumentException if {@code context}
113      *         is {@code null}
114      */
115     @Override
116     boolean execute(C context) throws Exception;
117 }