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