001: /*
002: * $Id: CallCAPIThread.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.lang.reflect.InvocationTargetException;
010: import java.lang.reflect.Method;
011: import org.xins.client.AbstractCAPI;
012: import org.xins.client.AbstractCAPICallRequest;
013: import org.xins.client.AbstractCAPICallResult;
014: import org.xins.common.service.CallException;
015:
016: /**
017: * Class used to call an API in a separate thread.
018: * To call the API, you will need to invoke the {@link #start()} method.
019: * If you want to wait for the result at a certain point in your program,
020: * invoke the {@link #join()} method.
021: *
022: * @version $Revision: 1.13 $ $Date: 2007/09/18 08:45:07 $
023: * @author <a href="mailto:anthony.goubard@japplis.com">Anthony Goubard</a>
024: *
025: * @since XINS 1.4.0
026: */
027: public class CallCAPIThread extends Thread {
028:
029: /**
030: * The CAPI.
031: */
032: private AbstractCAPI _capi;
033:
034: /**
035: * The request of the function.
036: */
037: private AbstractCAPICallRequest _request;
038:
039: /**
040: * The duration of the call.
041: */
042: private long _duration = -1L;
043:
044: /**
045: * The successful result returned by the function.
046: */
047: private AbstractCAPICallResult _result;
048:
049: /**
050: * The exception thrown by the call.
051: */
052:
053: /**
054: * Calls a CAPI function on a separate thread.
055: *
056: * @param capi
057: * the CAPI to use to call the function.
058: *
059: * @param request
060: * the input parameters for this call.
061: */
062: public CallCAPIThread(AbstractCAPI capi,
063: AbstractCAPICallRequest request) {
064: _capi = capi;
065: _request = request;
066: }
067:
068: private Exception _exception;
069:
070: public void run() {
071: long startTime = System.currentTimeMillis();
072: try {
073: // Execute the function
074: String functionName = "call" + _request.functionName();
075: Class[] callArgumentsClass = { _request.getClass() };
076: Object[] callArguments = { _request };
077: Method callMethod = _capi.getClass().getMethod(
078: functionName, callArgumentsClass);
079: _result = (AbstractCAPICallResult) callMethod.invoke(_capi,
080: callArguments);
081:
082: // Get the result of the call and notify the listeners
083: _duration = _result.duration();
084: } catch (InvocationTargetException itex) {
085: _exception = (Exception) itex.getTargetException();
086:
087: // Get the exception thrown by the call and notify the listeners
088: if (_exception instanceof CallException) {
089: _duration = ((CallException) _exception).getDuration();
090: } else {
091: _duration = System.currentTimeMillis() - startTime;
092: }
093: } catch (Exception ex) {
094: _exception = ex;
095: _duration = -1L;
096: }
097: }
098:
099: /**
100: * Gets the CAPI used to call the function.
101: *
102: * @return
103: * the CAPI used to call the function.
104: */
105: public AbstractCAPI getCAPI() {
106: return _capi;
107: }
108:
109: /**
110: * Gets the request used to the call the function.
111: *
112: * @return
113: * the request used to the call the function.
114: */
115: public AbstractCAPICallRequest getRequest() {
116: return _request;
117: }
118:
119: /**
120: * Gets the result returned by the function. You may want then to cast the
121: * {@link org.xins.client.AbstractCAPICallResult AbstractCAPICallResult}
122: * to the generated result file normally returned by the CAPI call.
123: *
124: * @return
125: * the successful result returned by the function.
126: */
127: public AbstractCAPICallResult getResult() {
128: return _result;
129: }
130:
131: /**
132: * Gets the exception thrown by the CAPI call.
133: *
134: * @return
135: * the exception, most probably a sub class of the
136: * {@link org.xins.common.service.CallException CallException}.
137: */
138: public Exception getException() {
139: return _exception;
140: }
141:
142: /**
143: * Gets the time it took to call the function.
144: *
145: * @return
146: * the duration of the call in milliseconds.
147: */
148: public long getDuration() {
149: return _duration;
150: }
151: }
|