1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
|
/**
* ===========================================
* LibLayout : a free Java layouting library
* ===========================================
*
* Project Info: http://reporting.pentaho.org/liblayout/
*
* (C) Copyright 2006-2007, by Pentaho Corporation and Contributors.
*
* This library is free software; you can redistribute it and/or modify it under the terms
* of the GNU Lesser General Public License as published by the Free Software Foundation;
* either version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with this
* library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307, USA.
*
* [Java is a trademark or registered trademark of Sun Microsystems, Inc.
* in the United States and other countries.]
*
* ------------
* $Id: ChainingComponent.java 3524 2007-10-16 11:26:31Z tmorgner $
* ------------
* (C) Copyright 2006-2007, by Pentaho Corporation.
*/
package org.jfree.layouting.util;
import java.util.ArrayList;
import java.util.Arrays;
/**
* A chaining component accepts calls from outside, forwards them to its
* wrapped object, and records all calls to the next chain element. Only
* after the initial call has been fully completed (and thus the wrapped
* object is back in a consistent state) all generated sub-calls will be
* forwarded to the next chain element.
*
* Of course, the whole architecture assumes, that the execution flow is a
* one-way street and that the execution and computation of the n-th step does
* not rely on results and/or the current state of the n+1-th step.
*
* It is guaranteed, that all calls are executed in the same order they have
* been recorded.
*
* @author Thomas Morgner
*/
public abstract class ChainingComponent
{
public static final int STATE_FRESH = 0;
public static final int STATE_ERROR = 1;
public static final int STATE_DONE = 2;
public static class RecordedCall
{
private int methodId;
private Object parameters;
private int state;
public RecordedCall(final int method, final Object parameters)
{
this.methodId = method;
this.parameters = parameters;
}
public int getState()
{
return state;
}
public void setState(final int state)
{
this.state = state;
}
public int getMethod()
{
return methodId;
}
public Object getParameters()
{
return parameters;
}
}
private ArrayList calls;
public ChainingComponent()
{
calls = new ArrayList();
}
public void addCall (final RecordedCall c)
{
calls.add(c);
}
public void clear ()
{
calls.clear();
}
protected RecordedCall[] retrieveRecordedCalls()
{
final RecordedCall[] recordedCalls = (RecordedCall[]) calls.toArray(new RecordedCall[calls.size()]);
calls.clear();
return recordedCalls;
}
public void setRecordedCalls (final RecordedCall[] recordedCalls)
{
calls.addAll(Arrays.asList(recordedCalls));
}
public synchronized void replay (final Object target) throws ChainingCallException
{
// Copy all recorded calls and be done with it.
final RecordedCall[] recordedCalls = retrieveRecordedCalls();
for (int i = 0; i < recordedCalls.length; i++)
{
final RecordedCall call = recordedCalls[i];
if (call.getState() == STATE_FRESH)
{
try
{
invoke(target, call.getMethod(), call.getParameters());
call.setState(STATE_DONE);
}
catch(Exception e)
{
call.setState(STATE_ERROR);
throw new ChainingCallException("Chained Call failed:", e);
}
}
}
}
protected abstract void invoke (Object target, int methodId, Object parameters)
throws Exception;
}
|