My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/*
* Copyright www.gdevelop.com.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.gdevelop.gwt.syncrpc;


import com.google.gwt.user.client.rpc.RemoteServiceRelativePath;

import java.io.IOException;

import java.lang.reflect.Proxy;

import java.net.CookieManager;
import java.net.CookiePolicy;

import java.util.Map;


/**
* Sync Proxy for GWT RemoteService
* Usage:
* MyServiceInterface myService = newProxyInstance(MyServiceInterface.class,
* "http://localhost:8888/myapp/", "myServiceServlet", policyName);
* where policyName is the file name (with gwt.rpc extenstion) generated
* by GWT RPC backend
*
* Or
* MyServiceInterface myService = newProxyInstance(MyServiceInterface.class,
* "http://localhost:8888/myapp/", "myServiceServlet");
* In this case, the SyncProxy search for the appropriate policyName file in
* the system classpath
*
* If not specified, SyncProxy uses a <em>default</em> {@link CookieManager}
* to manage client-server communication session
*
* To perform multi-session:
* CookieManager cookieManager = LoginUtils.loginAppEngine(...);
* MyServiceInterface myService = newProxyInstance(MyServiceInterface.class,
* "http://localhost:8888/myapp/", "myServiceServlet", cookieManager);
* @see com.gdevelop.gwt.syncrpc.test.ProfileServiceTest example
*/
public class SyncProxy {
/**
* Map from ServiceInterface class name to Serialization Policy name
*/
private static final Map<String, String> POLICY_MAP = RpcPolicyFinder.searchPolicyFileInClassPath();

private static final CookieManager DEFAULT_COOKIE_MANAGER
= new CookieManager(null, CookiePolicy.ACCEPT_ALL);

/**
* Create a new Proxy for the specified <code>serviceIntf</code>
* @param serviceIntf The remote service interface
* @param moduleBaseURL Base URL
* @param remoteServiceRelativePath The remote service servlet relative path
* @param policyName Policy name (*.gwt.rpc) generated by GWT RPC backend
* @param cookieManager Used to perform session management such as login.
* @param waitForInvocation Used for Async RemoteService.
* @return A new proxy object which implements the service interface serviceIntf
*/
@SuppressWarnings("unchecked")
public static Object newProxyInstance(Class serviceIntf, String moduleBaseURL,
String remoteServiceRelativePath,
String policyName,
CookieManager cookieManager,
boolean waitForInvocation){
if(cookieManager == null){
cookieManager = DEFAULT_COOKIE_MANAGER;
}

if (policyName == null){
try {
POLICY_MAP.putAll(RpcPolicyFinder.fetchSerializationPolicyName(moduleBaseURL));
policyName = POLICY_MAP.get(serviceIntf.getName());
} catch (IOException e) {
e.printStackTrace();
}
}

return Proxy.newProxyInstance(SyncProxy.class.getClassLoader(),
new Class[]{serviceIntf},
new RemoteServiceInvocationHandler(moduleBaseURL,
remoteServiceRelativePath,
policyName,
cookieManager,
waitForInvocation));
}

public static Object newProxyInstance(Class serviceIntf, String moduleBaseURL,
String remoteServiceRelativePath,
String policyName,
CookieManager cookieManager){
return newProxyInstance(serviceIntf, moduleBaseURL,
remoteServiceRelativePath, policyName,
cookieManager, false);
}

/**
* Create a new Proxy for the specified service interface <code>serviceIntf</code>
*
* @param serviceIntf The remote service interface
* @param moduleBaseURL Base URL
* @param remoteServiceRelativePath The remote service servlet relative path
* @param policyName Policy name (*.gwt.rpc) generated by GWT RPC backend
* @return A new proxy object which implements the service interface serviceIntf
*/
@SuppressWarnings("unchecked")
public static Object newProxyInstance(Class serviceIntf, String moduleBaseURL,
String remoteServiceRelativePath,
String policyName){
return newProxyInstance(serviceIntf, moduleBaseURL, remoteServiceRelativePath,
policyName, DEFAULT_COOKIE_MANAGER);
}

/**
* Create a new Proxy for the specified service interface <code>serviceIntf</code>
*
* @param serviceIntf The remote service interface
* @param moduleBaseURL Base URL
* @param remoteServiceRelativePath The remote service servlet relative path
* @return A new proxy object which implements the service interface serviceIntf
*/
@SuppressWarnings("unchecked")
public static Object newProxyInstance(Class serviceIntf, String moduleBaseURL,
String remoteServiceRelativePath){
return newProxyInstance(serviceIntf, moduleBaseURL, remoteServiceRelativePath,
POLICY_MAP.get(serviceIntf.getName()), DEFAULT_COOKIE_MANAGER);
}
/**
* Create a new Proxy for the specified service interface <code>serviceIntf</code>
*
* @param serviceIntf The remote service interface
* @param moduleBaseURL Base URL
* @param remoteServiceRelativePath The remote service servlet relative path
* @param cookieManager Used to perform session management such as login.
* @return A new proxy object which implements the service interface serviceIntf
*/
@SuppressWarnings("unchecked")
public static Object newProxyInstance(Class serviceIntf, String moduleBaseURL,
String remoteServiceRelativePath,
CookieManager cookieManager){
return newProxyInstance(serviceIntf, moduleBaseURL, remoteServiceRelativePath,
POLICY_MAP.get(serviceIntf.getName()), cookieManager);
}

public static Object newProxyInstance(Class serviceIntf, String moduleBaseURL,
CookieManager cookieManager,
boolean waitForInvocation){
RemoteServiceRelativePath relativePathAnn = (RemoteServiceRelativePath)
serviceIntf.getAnnotation(RemoteServiceRelativePath.class);
if (serviceIntf.getName().endsWith("Async")){
// Try determine remoteServiceRelativePath from the 'sync' version of the Async one
String className = serviceIntf.getName();
try {
Class clazz = Class.forName(className.substring(0, className.length()-5));
relativePathAnn = (RemoteServiceRelativePath)
clazz.getAnnotation(RemoteServiceRelativePath.class);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
if (relativePathAnn == null){
throw new RuntimeException(serviceIntf + " does not has a RemoteServiceRelativePath annotation");
}
String remoteServiceRelativePath = relativePathAnn.value();
return newProxyInstance(serviceIntf, moduleBaseURL, remoteServiceRelativePath,
POLICY_MAP.get(serviceIntf.getName()), cookieManager, waitForInvocation);
}

public static Object newProxyInstance(Class serviceIntf, String moduleBaseURL,
CookieManager cookieManager){
return newProxyInstance(serviceIntf, moduleBaseURL, cookieManager, false);
}
public static Object newProxyInstance(Class serviceIntf, String moduleBaseURL){
return newProxyInstance(serviceIntf, moduleBaseURL, DEFAULT_COOKIE_MANAGER);
}

public static Object newProxyInstance(Class serviceIntf, String moduleBaseURL,
boolean waitForInvocation){
return newProxyInstance(serviceIntf, moduleBaseURL,
DEFAULT_COOKIE_MANAGER, waitForInvocation);
}
}

Change log

r43 by gwtdeveloper on Aug 20, 2011   Diff
Add waitForInvocation
Go to: 
Project members, sign in to write a code review

Older revisions

r41 by gwtdeveloper on Aug 19, 2011   Diff
Auto fetch the policy files from
server. Base on the  issue #13 
r38 by gwtdeveloper on Aug 19, 2011   Diff
Fixed test errors
r31 by gwtdeveloper on Aug 18, 2011   Diff
Fixed  issue 17 
All revisions of this file

File info

Size: 8714 bytes, 195 lines
Powered by Google Project Hosting