View Javadoc

1   /**
2    * This file Copyright (c) 2013 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.commands.chain;
35  
36  import java.util.Collection;
37  import java.util.Collections;
38  import java.util.LinkedList;
39  import java.util.List;
40  
41  
42  /**
43   * Replacement for org.apache.commons.chain.impl.ChainBase. Base Chain
44   * implementation.
45   */
46  public class ChainBase implements Chain {
47  
48      protected List<Command> commands = new LinkedList<Command>();
49  
50      protected boolean frozen = false;
51  
52      public ChainBase() {
53      }
54  
55      public ChainBase(Command command) {
56          addCommand(command);
57      }
58  
59      public ChainBase(Command... commands) {
60          for (Command command : commands) {
61              addCommand(command);
62          }
63      }
64  
65      public ChainBase(Collection<Command> commands) {
66          this(commands.toArray(new Command[0]));
67      }
68  
69      @Override
70      public void addCommand(Command command) {
71          if (command == null) {
72              throw new IllegalArgumentException();
73          }
74          if (frozen) {
75              throw new IllegalStateException();
76          }
77          commands.add(command);
78      }
79  
80      @Override
81      public boolean execute(Context context) throws Exception {
82          // Verify our parameters
83          if (context == null) {
84              throw new IllegalArgumentException();
85          }
86          // Freeze the configuration of the command list
87          frozen = true;
88          // Execute the commands in this list until one returns true
89          // or throws an exception
90          boolean saveResult = false;
91          Exception saveException = null;
92          int i = 0;
93          int n = commands.size();
94          for (i = 0; i < n; i++) {
95              try {
96                  saveResult = commands.get(i).execute(context);
97                  if (saveResult) {
98                      break;
99                  }
100             } catch (Exception e) {
101                 saveException = e;
102                 break;
103             }
104         }
105         // Call postprocess methods on Filters in reverse order
106         if (i >= n) { // Fell off the end of the chain
107             i--;
108         }
109         boolean handled = false;
110         boolean result = false;
111         for (int j = i; j >= 0; j--) {
112             if (commands.get(j) instanceof Filter) {
113                 try {
114                     result = ((Filter) commands.get(j)).postprocess(context, saveException);
115                     if (result) {
116                         handled = true;
117                     }
118                 } catch (Exception e) {
119                     // Silently ignore
120                 }
121             }
122         }
123         // Return the exception or result state from the last execute()
124         if ((saveException != null) && !handled) {
125             throw saveException;
126         }
127         return (saveResult);
128     }
129 
130     /**
131      * Returns <b>unmodifiable</b> list of commands.
132      */
133     public List<Command> getCommands() {
134         return Collections.unmodifiableList(commands);
135     }
136 
137     @Override
138     public Chain copy(Command command) {
139         ChainBase master = (ChainBase) command;
140         ChainBase copy = new ChainBase();
141         for (Command c : master.commands) {
142             copy.addCommand(c.copy(c));
143         }
144         return copy;
145     }
146 
147 }