001: /*
002: * BEGIN_HEADER - DO NOT EDIT
003: *
004: * The contents of this file are subject to the terms
005: * of the Common Development and Distribution License
006: * (the "License"). You may not use this file except
007: * in compliance with the License.
008: *
009: * You can obtain a copy of the license at
010: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
011: * See the License for the specific language governing
012: * permissions and limitations under the License.
013: *
014: * When distributing Covered Code, include this CDDL
015: * HEADER in each file and include the License file at
016: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
017: * If applicable add the following below this CDDL HEADER,
018: * with the fields enclosed by brackets "[]" replaced with
019: * your own identifying information: Portions Copyright
020: * [year] [name of copyright owner]
021: */
022:
023: /*
024: * @(#)Operation.java
025: * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
026: *
027: * END_HEADER - DO NOT EDIT
028: */
029: package com.sun.jbi.framework;
030:
031: /**
032: * This is an abstract class that provides a generalized way for the framework
033: * to perform a set of operations on BCs and SEs in parallel on separate
034: * threads. In conjunction with the OperationCounter class, it can wait for
035: * all of the operations to complete, with an optional timeout.
036: *
037: * @author Sun Microsystems, Inc.
038: */
039: abstract class Operation implements Runnable {
040: /**
041: * Arguments to be passed to the operation.
042: */
043: private Object mArguments[];
044:
045: /**
046: * Completion flag set to true if the operation completed.
047: */
048: private boolean mCompleted;
049:
050: /**
051: * The OperationCounter instance to use.
052: */
053: private OperationCounter mCounter;
054:
055: /**
056: * An exception thrown by the operation, if any.
057: */
058: private Throwable mException;
059:
060: /**
061: * A return value from the operation, if any.
062: */
063: private Object mReturnValue;
064:
065: /**
066: * The thread on which this operation is running.
067: */
068: private Thread mThread;
069:
070: /**
071: * Constructor.
072: *
073: * @param counter - the optional OperationCounter instance that should be
074: * associated with this Operation. If this is not null, its increment()
075: * method is called here.
076: * @param arguments - the arguments to be passed to the process() method,
077: * which is called to perform the operation.
078: */
079: Operation(OperationCounter counter, Object arguments[]) {
080: mArguments = arguments;
081: mCounter = counter;
082: mCompleted = false;
083: if (null != mCounter) {
084: mCounter.increment();
085: }
086: }
087:
088: /**
089: * Run the operation. Save the thread on which this operation is running in
090: * case of a timeout, so that the parent thread can interrupt this one.
091: * When the operation completes, save its return value and set the completed
092: * flag. If an OperationCounter is present, decrement its counter. If the
093: * operation throws an exception, save it. Upon return from this method,
094: * this thread terminates.
095: */
096: public final void run() {
097: mThread = Thread.currentThread();
098: try {
099: mReturnValue = process(mArguments);
100: } catch (Throwable ex) {
101: mException = ex;
102: }
103: mCompleted = true;
104: if (null != mCounter) {
105: mCounter.decrement();
106: }
107: return;
108: }
109:
110: /**
111: * Returns true if this operation completed.
112: *
113: * @return true if the operation has completed, false if it has not.
114: */
115: final boolean completed() {
116: return mCompleted;
117: }
118:
119: /**
120: * Returns one argument that was provided to the constructor and passed
121: * to the operation.
122: *
123: * @param index the index of the argument to be returned.
124: * @return The argument that was provided to the operation.
125: */
126: final Object getArgument(int index) {
127: return mArguments[index];
128: }
129:
130: /**
131: * Returns the arguments that were provided to the constructor and passed
132: * to the operation.
133: *
134: * @return The arguments that were provided to the operation.
135: */
136: final Object[] getArguments() {
137: return mArguments;
138: }
139:
140: /**
141: * Returns any exception thrown by the operation, or null if no exception
142: * was thrown.
143: *
144: * @return The exception from the operation or null.
145: */
146: final Throwable getException() {
147: return mException;
148: }
149:
150: /**
151: * Returns any return value from the operation, or null if there was none.
152: *
153: * @return The return value from the operation or null.
154: */
155: final Object getReturnValue() {
156: return mReturnValue;
157: }
158:
159: /**
160: * Returns the thread on which this operation is running.
161: *
162: * @return The thread on which the operation is running.
163: */
164: final Thread getThread() {
165: return mThread;
166: }
167:
168: /**
169: * Process the operation. This method must be overridden to perform the
170: * desired processing.
171: *
172: * @param arguments the arguments to be provided to the operation.
173: * @return the returned value from the operation as an object or null if
174: * no value was returned.
175: * @throws Throwable if any error occurs.
176: */
177: abstract Object process(Object arguments[]) throws Throwable;
178:
179: /**
180: * Reset this instance for a new operation. This clears all results of
181: * a previous operation.
182: */
183: final void reset() {
184: mCompleted = false;
185: mException = null;
186: mReturnValue = null;
187: mThread = null;
188: }
189: }
|