001: /*
002: * $Id: AsynchronousCall.java,v 1.13 2007/09/18 08:45:07 agoubard Exp $
003: *
004: * Copyright 2003-2007 Orange Nederland Breedband B.V.
005: * See the COPYRIGHT file for redistribution and use restrictions.
006: */
007: package org.xins.client.async;
008:
009: import java.util.ArrayList;
010: import java.util.Collections;
011: import java.util.Iterator;
012: import java.util.List;
013: import org.xins.client.AbstractCAPI;
014: import org.xins.client.AbstractCAPICallRequest;
015:
016: /**
017: * Class used to register the {@link CallListener}s and to call the API
018: * asynchronously.
019: *
020: * @version $Revision: 1.13 $ $Date: 2007/09/18 08:45:07 $
021: * @author <a href="mailto:anthony.goubard@japplis.com">Anthony Goubard</a>
022: *
023: * @since XINS 1.4.0
024: */
025: public class AsynchronousCall {
026:
027: /**
028: * List containing the registered {@link CallListener}.
029: */
030: private List _listeners = new ArrayList();
031:
032: /**
033: * Adds a new listener for the call.
034: *
035: * @param listener
036: * the listener that will be notified of the result of the call.
037: */
038: public void addCallListener(CallListener listener) {
039: _listeners.add(listener);
040: }
041:
042: /**
043: * Removes a listener for the call. If the listener was not previously added
044: * nothing happens.
045: *
046: * @param listener
047: * the listener that will be notified of the result of the call.
048: */
049: public void removeCallListener(CallListener listener) {
050: _listeners.remove(listener);
051: }
052:
053: /**
054: * Calls a function asynchronously. This function does not return anything as
055: * the result and exception will be received by the registered {@link CallListener}.
056: *
057: * @param capi
058: * the CAPI to use to call the function.
059: *
060: * @param request
061: * the input parameters for this call.
062: */
063: public void call(AbstractCAPI capi, AbstractCAPICallRequest request) {
064: CallNotifyThread thread = new CallNotifyThread(capi, request,
065: _listeners);
066: thread.start();
067: }
068:
069: /**
070: * Thread that executes the call to the function.
071: *
072: * @version $Revision: 1.13 $ $Date: 2007/09/18 08:45:07 $
073: * @author <a href="mailto:anthony.goubard@japplis.com">Anthony Goubard</a>
074: */
075: private static class CallNotifyThread extends CallCAPIThread {
076:
077: /**
078: * The listeners to notify.
079: */
080: private final List _listeners;
081:
082: /**
083: * Calls a CAPI function on a separate thread and notifies the listeners
084: * of the result.
085: *
086: * @param capi
087: * the CAPI to use to call the function.
088: *
089: * @param request
090: * the input parameters for this call.
091: *
092: * @param listeners
093: * the listeners to notify of the result of the call.
094: */
095: CallNotifyThread(AbstractCAPI capi,
096: AbstractCAPICallRequest request, List listeners) {
097: super (capi, request);
098:
099: // Notify the listeners registered at the moment of the call and not
100: // when the result is received.
101: _listeners = Collections.unmodifiableList(listeners);
102: }
103:
104: public void run() {
105:
106: // Call the API
107: super .run();
108:
109: // Get the result and notify the listeners
110: if (getException() == null) {
111: CallSucceededEvent event = new CallSucceededEvent(
112: getCAPI(), getRequest(), getDuration(),
113: getResult());
114: Iterator itListeners = _listeners.iterator();
115: while (itListeners.hasNext()) {
116: CallListener listener = (CallListener) itListeners
117: .next();
118: listener.callSucceeded(event);
119: }
120: } else {
121: CallFailedEvent event = new CallFailedEvent(getCAPI(),
122: getRequest(), getDuration(), getException());
123: Iterator itListeners = _listeners.iterator();
124: while (itListeners.hasNext()) {
125: CallListener listener = (CallListener) itListeners
126: .next();
127: listener.callFailed(event);
128: }
129: }
130: }
131: }
132: }
|