1 /*******************************************************************************
2 * Copyright (c) 2010 Association for Decentralized Information Management in
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.databoard.method;
14 import java.util.concurrent.Semaphore;
15 import java.util.concurrent.TimeUnit;
16 import java.util.logging.Level;
17 import java.util.logging.Logger;
19 import org.simantics.databoard.adapter.AdaptException;
20 import org.simantics.databoard.adapter.Adapter;
21 import org.simantics.databoard.method.MethodInterface.AsyncRequestStatus;
22 import org.simantics.databoard.method.MethodInterface.AsyncResult;
23 import org.simantics.databoard.method.MethodInterface.ExecutionError;
24 import org.simantics.databoard.method.MethodInterface.InvokeListener;
26 public class AsyncResultImpl implements AsyncResult {
30 * Security settings are logged with DEBUG level.
31 * Unexpected errors are logged with ERROR level.
33 static Logger LOGGER = Logger.getLogger("AsyncResultImpl");
35 InvokeException invokeException;
36 Object executionError;
40 InvokeListener listener;
43 Adapter responseAdapter, errorAdapter;
45 Semaphore sleeper = new Semaphore(0);
47 public AsyncResultImpl() {
51 public Object getExecutionError() {
52 return executionError;
56 public InvokeException getInvokeException() {
57 return invokeException;
60 public void setInvokeException(final InvokeException error)
62 this.invokeException = error;
63 final InvokeListener l = listener;
64 sleeper.release(Integer.MAX_VALUE);
67 // TODO use executor instead
71 l.onException((Exception) error.getCause());
72 } catch (RuntimeException e) {
73 LOGGER.log(Level.SEVERE, "Unexpected runtime exception", e);
81 public void setResponse(Object response) {
82 if (responseAdapter!=null) {
84 response = responseAdapter.adapt(response);
85 } catch (AdaptException e) {
86 setInvokeException( new InvokeException("Failed to adapt the respose "+response, e) );
91 this.response = response;
92 final InvokeListener l = listener;
93 sleeper.release(Integer.MAX_VALUE);
96 // TODO use executor instead
100 l.onCompleted(AsyncResultImpl.this.response);
101 } catch (RuntimeException e) {
102 LOGGER.log(Level.SEVERE, "Unexpected runtime exception", e);
109 public void setExecutionError(Object executionError) {
111 if (errorAdapter!=null) {
112 // Adapt execution error using adapter
114 executionError = errorAdapter.adapt(executionError);
115 } catch (AdaptException e) {
116 // Unable to adapt execution error. This is now an invokcation error
117 setInvokeException(new InvokeException("Execution and Invoke failed. Failed to adapt executionError ("+executionError+")", e));
122 this.executionError = executionError;
123 final InvokeListener l = listener;
124 sleeper.release(Integer.MAX_VALUE);
127 // TODO use executor instead
130 l.onExecutionError(AsyncResultImpl.this.executionError);
131 // TODO Capture runtimeException, push to log4j
139 public Object getResponse() {
144 public AsyncRequestStatus getStatus() {
145 if (invokeException!=null || executionError!=null)
146 return AsyncRequestStatus.Failed;
148 return AsyncRequestStatus.Succeed;
149 return AsyncRequestStatus.Waiting;
153 public void setListener(InvokeListener listener) {
154 // FIXME event is lost if error/result is set at the same time
155 if (listener!=null) {
156 if (response!=null) listener.onCompleted(response);
157 if (invokeException!=null) listener.onException((Exception)invokeException.getCause());
158 if (executionError!=null) listener.onExecutionError(executionError);
160 this.listener = listener;
164 public Object waitForResponse() throws InvokeException, ExecutionError, InterruptedException {
166 if (response!=null) return response;
167 if (invokeException!=null) throw invokeException;
168 if (executionError!=null) throw new ExecutionError(executionError);
173 public Object waitForResponse(long timeout, TimeUnit unit)
174 throws InvokeException, ExecutionError, InterruptedException {
175 sleeper.tryAcquire(timeout, unit);
176 if (response!=null) return response;
177 if (invokeException!=null) throw invokeException;
178 if (executionError!=null) throw new ExecutionError(executionError);
182 public void setResponseAdapter(Adapter responseAdapter) {
183 this.responseAdapter = responseAdapter;
186 public void setErrorAdapter(Adapter errorAdapter) {
187 this.errorAdapter = errorAdapter;