-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathExtendedServiceProxy.java
More file actions
87 lines (78 loc) · 2.58 KB
/
Copy pathExtendedServiceProxy.java
File metadata and controls
87 lines (78 loc) · 2.58 KB
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
// -*- mode: java; c-basic-offset: 2; -*-
// Copyright 2009-2011 Google, All Rights reserved
// Copyright 2011-2012 MIT, All rights reserved
// Released under the MIT License https://raw.github.com/mit-cml/app-inventor/master/mitlicense.txt
package com.google.appinventor.client;
import com.google.gwt.user.client.rpc.RemoteService;
import java.util.HashSet;
import java.util.Set;
/**
* Extended interface that is implemented by all service proxies that are
* generated by
* {@link com.google.appinventor.rebind.ExtendedServiceProxyGenerator}. This
* interface adds two features to service proxies:
* <ol>
* <li>Clients can be notified of all service calls by attaching
* {@link RpcListener}s to the service proxy using the
* {@link #addRpcListener(RpcListener)} method.
* </ol>
*
* @param <T> the synchronous service interface
*/
public class ExtendedServiceProxy<T extends RemoteService> {
// Listeners to be notified of RPC events
private final Set<RpcListener> listeners = new HashSet<RpcListener>();
/**
* Adds a listener that is notified of RPC events.
*
* @param listener the listener
*/
public void addRpcListener(RpcListener listener) {
listeners.add(listener);
}
/**
* Removes a listener that has been added using the
* {@link #addRpcListener(RpcListener)} method before.
*
* @param listener the listener
*/
public void removeRpcListener(RpcListener listener) {
listeners.remove(listener);
}
/**
* Calls the {@link RpcListener#onStart(String, Object...)} method for all
* listeners.
*
* @param method the name of the RPC method that was called
* @param params the params of the RPC method that was called
*/
protected void fireStart(String method, Object... params) {
for (RpcListener listener : listeners) {
listener.onStart(method, params);
}
}
/**
* Calls the {@link RpcListener#onSuccess(String, Object)} method for all
* listeners.
*
* @param method the name of the RPC method that was successfully completed
* @param result the result of the RPC method
*/
protected void fireSuccess(String method, Object result) {
for (RpcListener listener : listeners) {
listener.onSuccess(method, result);
}
}
/**
* Calls the {@link RpcListener#onFailure(String, Throwable)} method for all
* listeners.
*
* @param method the name of the RPC method that threw an exception.
* @param caught the thrown exception
*/
protected void fireFailure(String method, Throwable caught) {
for (RpcListener listener : listeners) {
listener.onFailure(method, caught);
}
}
}