|
@@ -1,321 +1,321 @@ |
1 |
|
- |
// This Source Code is in the Public Domain per: http://unlicense.org |
2 |
|
- |
package org.litesoft.GWT.eventbus.client; |
3 |
|
- |
|
4 |
|
- |
import org.litesoft.GWT.client.*; |
5 |
|
- |
import org.litesoft.GWT.client.logging.*; |
6 |
|
- |
import org.litesoft.GWT.eventbus.client.eventpackages.*; |
7 |
|
- |
import org.litesoft.GWT.eventbus.client.nonpublic.*; |
8 |
|
- |
import org.litesoft.GWT.eventbus.client.rpc.*; |
9 |
|
- |
import org.litesoft.commonfoundation.typeutils.gregorian.*; |
10 |
|
- |
import org.litesoft.logger.*; |
11 |
|
- |
import org.litesoft.loggerconfig.*; |
12 |
|
- |
|
13 |
|
- |
import com.google.gwt.event.logical.shared.*; |
14 |
|
- |
import com.google.gwt.user.client.*; |
15 |
|
- |
import com.google.gwt.user.client.rpc.*; |
16 |
|
- |
import com.google.gwt.user.client.ui.*; |
17 |
|
- |
|
18 |
|
- |
public abstract class AbstractClientEventBusApp extends AbstractClientMainAppEntryPoint { |
19 |
|
- |
private static String sVersion = "N/A"; |
20 |
|
- |
private static ClientEventBus sEventBus = null; |
21 |
|
- |
private static int sClientIdNumber; |
22 |
|
- |
protected static String sEncodedClientId = null; |
23 |
|
- |
|
24 |
|
- |
private WindowNameClientIdCodec mWindowNameClientIdCodec; |
25 |
|
- |
protected DecendantManager mDecendantManager; |
26 |
|
- |
|
27 |
|
- |
private EventBusRemoteServiceAsync mRemoteServiceAsync = null; |
28 |
|
- |
|
29 |
|
- |
public static String getVersion() { |
30 |
|
- |
return sVersion; |
31 |
|
- |
} |
32 |
|
- |
|
33 |
|
- |
public static ClientEventBus getEventBus() { |
34 |
|
- |
return sEventBus; |
35 |
|
- |
} |
36 |
|
- |
|
37 |
|
- |
public static void initializeEventBus( String pEncodedClientId ) { |
38 |
|
- |
System.out.println( "initializeEventBus: " + pEncodedClientId ); |
39 |
|
- |
// todo: ... |
40 |
|
- |
} |
41 |
|
- |
|
42 |
|
- |
public static void pollEventBus() { |
43 |
|
- |
ClientEventBus zBus = getEventBus(); |
44 |
|
- |
if ( zBus != null ) { |
45 |
|
- |
zBus.pollNow(); |
46 |
|
- |
} |
47 |
|
- |
} |
48 |
|
- |
|
49 |
|
- |
public static void streamClosed() { |
50 |
|
- |
System.out.println( "streamClosed" ); |
51 |
|
- |
Window.alert( "streamClosed" ); |
52 |
|
- |
// todo: ... |
53 |
|
- |
} |
54 |
|
- |
|
55 |
|
- |
public static int getClientIdNumber() { |
56 |
|
- |
ClientEventBus zBus = getEventBus(); |
57 |
|
- |
return (zBus != null) ? zBus.getClientIdNumber() : 0; |
58 |
|
- |
} |
59 |
|
- |
|
60 |
|
- |
protected AbstractClientEventBusApp( AppNames pAppNames ) { |
61 |
|
- |
super( pAppNames ); |
62 |
|
- |
mWindowNameClientIdCodec = new WindowNameClientIdCodec( pAppNames ); |
63 |
|
- |
mDecendantManager = new DecendantManager( mWindowNameClientIdCodec ); |
64 |
|
- |
} |
65 |
|
- |
|
66 |
|
- |
@Override |
67 |
|
- |
public void onAppLoad() { |
68 |
|
- |
super.onAppLoad(); |
69 |
|
- |
|
70 |
|
- |
mRemoteServiceAsync = createRemoteServiceProxy(); |
71 |
|
- |
|
72 |
|
- |
String zEncodedClientId = mWindowNameClientIdCodec.extractEncodedClientId( WindowName.get() ); |
73 |
|
- |
|
74 |
|
- |
initializeEventBusConnection( zEncodedClientId ); |
75 |
|
- |
} |
76 |
|
- |
|
77 |
|
- |
@Override |
78 |
|
- |
public void onClose( CloseEvent<Window> pEvent ) { |
79 |
|
- |
super.onClose( pEvent ); |
80 |
|
- |
sEventBus.dispose( "WindowClosed" ); |
81 |
|
- |
if ( (mRemoteServiceAsync != null) && (sEncodedClientId != null) ) { |
82 |
|
- |
try { |
83 |
|
- |
mRemoteServiceAsync.clientDead( sClientIdNumber, new TossingAsyncCallback() ); |
84 |
|
- |
} |
85 |
|
- |
catch ( RuntimeException e ) { |
86 |
|
- |
System.err.println( "ClientEventBusApp.WindowClosed" ); |
87 |
|
- |
e.printStackTrace(); |
88 |
|
- |
} |
89 |
|
- |
} |
90 |
|
- |
} |
91 |
|
- |
|
92 |
|
- |
@Override |
93 |
|
- |
public void nativeCodeInjection() { |
94 |
|
- |
super.nativeCodeInjection(); |
95 |
|
- |
|
96 |
|
- |
NativeClientEventBusSupport.export(); |
97 |
|
- |
} |
98 |
|
- |
|
99 |
|
- |
@Override |
100 |
|
- |
public void nativeCodeDispose() { |
101 |
|
- |
super.nativeCodeDispose(); |
102 |
|
- |
|
103 |
|
- |
NativeClientEventBusSupport.dispose(); |
104 |
|
- |
} |
105 |
|
- |
|
106 |
|
- |
protected void initializeLoggerFactory() { |
107 |
|
- |
//noinspection NonJREEmulationClassesInClientCode |
108 |
|
- |
LoggerFactory.init( new ConfigurationLoggerLevel(), BrowserAsLogSyncFactory.INSTANCE ); |
109 |
|
- |
} |
110 |
|
- |
|
111 |
|
- |
protected void initializeEventBusConnection( String pEncodedClientId ) { |
112 |
|
- |
((ServiceDefTarget) mRemoteServiceAsync).setServiceEntryPoint( extractEntryName() ); |
113 |
|
- |
|
114 |
|
- |
mRemoteServiceAsync.initialize( pEncodedClientId, new AppInitializationCallback() ); |
115 |
|
- |
} |
116 |
|
- |
|
117 |
|
- |
private static final String ASYNC_NAME_PROXY_TAIL = "_Proxy"; |
118 |
|
- |
|
119 |
|
- |
private String extractEntryName() { |
120 |
|
- |
String zTypeName = mRemoteServiceAsync.getClass().getName(); |
121 |
|
- |
|
122 |
|
- |
if ( !zTypeName.endsWith( ASYNC_NAME_PROXY_TAIL ) ) { |
123 |
|
- |
throw new IllegalStateException( zTypeName + " did not end with '" + ASYNC_NAME_PROXY_TAIL + "'" ); |
124 |
|
- |
} |
125 |
|
- |
int lastDot = zTypeName.lastIndexOf( '.' ); |
126 |
|
- |
if ( lastDot == -1 ) { |
127 |
|
- |
throw new IllegalStateException( zTypeName + " may not live in the default package" ); |
128 |
|
- |
} |
129 |
|
- |
return zTypeName.substring( lastDot + 1, zTypeName.length() - ASYNC_NAME_PROXY_TAIL.length() ); |
130 |
|
- |
} |
131 |
|
- |
|
132 |
|
- |
protected int getPollSecs() { |
133 |
|
- |
return 5; |
134 |
|
- |
} |
135 |
|
- |
|
136 |
|
- |
abstract protected EventBusRemoteServiceAsync createRemoteServiceProxy(); |
137 |
|
- |
|
138 |
|
- |
/** |
139 |
|
- |
* @noinspection NonJREEmulationClassesInClientCode |
140 |
|
- |
*/ |
141 |
|
- |
protected Logger createClientEventBusLogger( int pClientIdNumber ) { |
142 |
|
- |
return null; |
143 |
|
- |
} |
144 |
|
- |
|
145 |
|
- |
abstract protected void appReady( AuthenticationData pAuthenticationData ); |
146 |
|
- |
|
147 |
|
- |
protected void sessionLost() { |
148 |
|
- |
UtilsGwt.closeWindowNoConfirm( getSessionLostWindowAlertText() ); |
149 |
|
- |
} |
150 |
|
- |
|
151 |
|
- |
protected String getSessionLostWindowAlertText() { |
152 |
|
- |
return "Your session with the server has been lost.\nYou will need to reconnect.\nThe application window will now attempt to close."; |
153 |
|
- |
} |
154 |
|
- |
|
155 |
|
- |
protected void addEventListenersForNewClient( EventSubscriptionCollector pCollector ) { |
156 |
|
- |
} |
157 |
|
- |
|
158 |
|
- |
private void initialSubscriptions( EventBus pEventBus ) { |
159 |
|
- |
EventSubscriptionCollector collector = new EventSubscriptionCollector(); |
160 |
|
- |
addEventListenersForNewClient( collector ); |
161 |
|
- |
collector.subscribe( new OpenNewClientPackageListener() ); |
162 |
|
- |
collector.subscribe( new CloseClientPackageListener() ); |
163 |
|
- |
collector.subscribe( new NoServerEventBusPackageListener() ); |
164 |
|
- |
collector.subscribe( new RelatedClientToFrontPackageListener() ); |
165 |
|
- |
mDecendantManager.addEventListenersForNewClient( collector ); |
166 |
|
- |
pEventBus.subscribe( collector ); |
167 |
|
- |
} |
168 |
|
- |
|
169 |
|
- |
protected void finalPrep( ChannelEventPackage pInitialChannelEvent, AuthenticationData pAuthenticationData ) { |
170 |
|
- |
System.err.println( new UtilDateAdaptor() + " | Client AppReady" ); |
171 |
|
- |
appReady( pAuthenticationData ); |
172 |
|
- |
} |
173 |
|
- |
|
174 |
|
- |
protected void closeClientRequested() { |
175 |
|
- |
UtilsGwt.closeWindowNoConfirm( 1 ); |
176 |
|
- |
} |
177 |
|
- |
|
178 |
|
- |
private class AppInitializationCallback implements AsyncCallback<AppInitializationResult> { |
179 |
|
- |
public void onFailure( Throwable pThrowable ) { |
180 |
|
- |
String message; |
181 |
|
- |
|
182 |
|
- |
if ( pThrowable instanceof InvocationException ) { |
183 |
|
- |
// see javadoc for InvocationException |
184 |
|
- |
message = "A connection could not be established with the server"; |
185 |
|
- |
} else if ( pThrowable instanceof IncompatibleRemoteServiceException ) { |
186 |
|
- |
/* |
187 |
|
- |
* This _should_ not happen to users, so long as the .nocache |
188 |
|
- |
* file isn't cached by the client. |
189 |
|
- |
*/ |
190 |
|
- |
message = |
191 |
|
- |
"Client and server versions are incompatible. Try ctrl-clicking " + "or shift-clicking the browser refresh button, or clearing your " + |
192 |
|
- |
"browser's cache in 'Internet Options' / your browser's preferences. "; |
193 |
|
- |
} else { |
194 |
|
- |
// probably an application-specific execption on the server |
195 |
|
- |
message = "An application error occured on the server"; |
196 |
|
- |
} |
197 |
|
- |
|
198 |
|
- |
reportFailure( pThrowable, "onFailure()", message ); |
199 |
|
- |
} |
200 |
|
- |
|
201 |
|
- |
public void onSuccess( AppInitializationResult result ) { |
202 |
|
- |
System.err.println( new UtilDateAdaptor() + " | Client AppInitialization" ); |
203 |
|
- |
|
204 |
|
- |
if ( result == null ) { |
205 |
|
- |
UtilsGwt.closeWindowNoConfirm( 5 ); |
206 |
|
- |
return; |
207 |
|
- |
} |
208 |
|
- |
|
209 |
|
- |
ClientEventBus ceb = null; |
210 |
|
- |
try { |
211 |
|
- |
new ClientConfiguration( result.getClientConfig() ); // This line must preceed the next |
212 |
|
- |
initializeLoggerFactory(); // ......................... This line must follow the previous |
213 |
|
- |
|
214 |
|
- |
AlertManager.setVersion( sVersion = result.getApplicationVersion() ); |
215 |
|
- |
|
216 |
|
- |
sClientIdNumber = result.getClientIdNumber(); |
217 |
|
- |
|
218 |
|
- |
sEncodedClientId = result.getEncodedClientId(); |
219 |
|
- |
WindowName.set( mWindowNameClientIdCodec.createWindowName( sEncodedClientId ) ); |
220 |
|
- |
|
221 |
|
- |
ceb = new ClientEventBus( sClientIdNumber ); |
222 |
|
- |
ceb.initialize( mRemoteServiceAsync, getPollSecs(), createClientEventBusLogger( sClientIdNumber ) ); |
223 |
|
- |
|
224 |
|
- |
if ( ceb.getLogger().debug.isEnabled() ) { |
225 |
|
- |
UtilsGwt.addWindowNameToWindowTitle(); |
226 |
|
- |
} |
227 |
|
- |
|
228 |
|
- |
ChannelServicePackage zPackage = result.getChannelServicePackage(); |
229 |
|
- |
if ( zPackage != null ) { |
230 |
|
- |
ceb.propagateFromRemotePeerService( zPackage ); |
231 |
|
- |
} |
232 |
|
- |
initialSubscriptions( ceb ); |
233 |
|
- |
sEventBus = ceb; |
234 |
|
- |
finalPrep( result.getInitialChannelEvent(), result.getAuthenticationData() ); |
235 |
|
- |
ceb.release(); |
236 |
|
- |
} |
237 |
|
- |
catch ( RedirectClientException e ) { |
238 |
|
- |
if ( ceb != null ) { |
239 |
|
- |
ceb.dispose( "Redirect" ); |
240 |
|
- |
} |
241 |
|
- |
UtilsGwt.redirect( e.getRedirectTo() ); |
242 |
|
- |
} |
243 |
|
- |
catch ( RuntimeException e ) { |
244 |
|
- |
reportFailure( e, "onSuccess()", null ); |
245 |
|
- |
} |
246 |
|
- |
} |
247 |
|
- |
|
248 |
|
- |
private void reportFailure( Throwable pThrowable, String method, String message ) { |
249 |
|
- |
pThrowable.printStackTrace(); // prints the trace in the GWT hosted shell |
250 |
|
- |
String html = "<h1>Application Initialization Failed</h1>" + "<p>" + message + "</p>" + "<p>at " + getClass().getName() + "." + method + "</p>" + |
251 |
|
- |
"<p>Exception: </p>" + "<p><pre>" + pThrowable + "</pre></p>"; |
252 |
|
- |
DOM.setInnerHTML( RootPanel.getBodyElement(), html ); |
253 |
|
- |
} |
254 |
|
- |
} |
255 |
|
- |
|
256 |
|
- |
private class NoServerEventBusPackageListener implements EventPackageSubscriber { |
257 |
|
- |
public String subscribeWith() { |
258 |
|
- |
return NoServerEventBusEventPackage.SubscribeWith; |
259 |
|
- |
} |
260 |
|
- |
|
261 |
|
- |
public void packageReceivedVia( EventPackage pEventPackage, EventBus pEventBus ) { |
262 |
|
- |
sessionLost(); |
263 |
|
- |
} |
264 |
|
- |
} |
265 |
|
- |
|
266 |
|
- |
private class RelatedClientToFrontPackageListener implements EventPackageSubscriber { |
267 |
|
- |
public String subscribeWith() { |
268 |
|
- |
return RelatedClientToFrontEventPackage.SubscribeWith; |
269 |
|
- |
} |
270 |
|
- |
|
271 |
|
- |
public void packageReceivedVia( EventPackage pEventPackage, EventBus pEventBus ) { |
272 |
|
- |
int id = ((RelatedClientToFrontEventPackage) pEventPackage).getClientId(); |
273 |
|
- |
mDecendantManager.decendantToFront( id ); |
274 |
|
- |
} |
275 |
|
- |
} |
276 |
|
- |
|
277 |
|
- |
private class CloseClientPackageListener implements EventPackageSubscriber { |
278 |
|
- |
public String subscribeWith() { |
279 |
|
- |
return CloseClientEventPackage.SubscribeWith; |
280 |
|
- |
} |
281 |
|
- |
|
282 |
|
- |
public void packageReceivedVia( EventPackage pEventPackage, EventBus pEventBus ) { |
283 |
|
- |
closeClientRequested(); |
284 |
|
- |
} |
285 |
|
- |
} |
286 |
|
- |
|
287 |
|
- |
private class OpenNewClientPackageListener implements EventPackageSubscriber { |
288 |
|
- |
public String subscribeWith() { |
289 |
|
- |
return OpenNewClientEventPackage.SubscribeWith; |
290 |
|
- |
} |
291 |
|
- |
|
292 |
|
- |
public void packageReceivedVia( EventPackage pEventPackage, EventBus pEventBus ) { |
293 |
|
- |
OpenNewClientEventPackage onc = (OpenNewClientEventPackage) pEventPackage; |
294 |
|
- |
String url = UtilsGwt.getURL(); |
295 |
|
- |
String windowName = mWindowNameClientIdCodec.createWindowName( onc.getNewEncodedClientId() ); |
296 |
|
- |
|
297 |
|
- |
openChildWindow( windowName, url ); |
298 |
|
- |
} |
299 |
|
- |
} |
300 |
|
- |
|
301 |
|
- |
protected void openChildWindow( String pWinName, String pUrl ) { |
302 |
|
- |
UtilsGwt.windowOpenChromeless( pUrl, pWinName ); |
303 |
|
- |
// Other Features: |
304 |
|
- |
// "hotkeys=0" + // Netscape!, FireFox?, IE? |
305 |
|
- |
// "screenX=nnnn,screenY=nnnn," + // Netscape!, FireFox?, |
306 |
|
- |
// IE? |
307 |
|
- |
// width=100,height=100," + |
308 |
|
- |
} |
309 |
|
- |
|
310 |
|
- |
private static class TossingAsyncCallback implements AsyncCallback<Object> { |
311 |
|
- |
public void onFailure( Throwable caught ) { |
312 |
|
- |
if ( !sWindowClosing && (caught != null) ) { |
313 |
|
- |
System.err.println( "ClientEventBusApp.TossingAsyncCallback.onFailure" ); |
314 |
|
- |
caught.printStackTrace(); |
315 |
|
- |
} |
316 |
|
- |
} |
317 |
|
- |
|
318 |
|
- |
public void onSuccess( Object result ) { |
319 |
|
- |
} |
320 |
|
- |
} |
321 |
|
- |
} |
|
1 |
+ |
// This Source Code is in the Public Domain per: http://unlicense.org |
|
2 |
+ |
package org.litesoft.GWT.eventbus.client; |
|
3 |
+ |
|
|
4 |
+ |
import org.litesoft.GWT.client.*; |
|
5 |
+ |
import org.litesoft.GWT.client.logging.*; |
|
6 |
+ |
import org.litesoft.GWT.eventbus.client.eventpackages.*; |
|
7 |
+ |
import org.litesoft.GWT.eventbus.client.nonpublic.*; |
|
8 |
+ |
import org.litesoft.GWT.eventbus.client.rpc.*; |
|
9 |
+ |
import org.litesoft.commonfoundation.typeutils.gregorian.*; |
|
10 |
+ |
import org.litesoft.logger.*; |
|
11 |
+ |
import org.litesoft.loggerconfig.*; |
|
12 |
+ |
|
|
13 |
+ |
import com.google.gwt.event.logical.shared.*; |
|
14 |
+ |
import com.google.gwt.user.client.*; |
|
15 |
+ |
import com.google.gwt.user.client.rpc.*; |
|
16 |
+ |
import com.google.gwt.user.client.ui.*; |
|
17 |
+ |
|
|
18 |
+ |
public abstract class AbstractClientEventBusApp extends AbstractClientMainAppEntryPoint { |
|
19 |
+ |
private static String sVersion = "N/A"; |
|
20 |
+ |
private static ClientEventBus sEventBus = null; |
|
21 |
+ |
private static int sClientIdNumber; |
|
22 |
+ |
protected static String sEncodedClientId = null; |
|
23 |
+ |
|
|
24 |
+ |
private WindowNameClientIdCodec mWindowNameClientIdCodec; |
|
25 |
+ |
protected DecendantManager mDecendantManager; |
|
26 |
+ |
|
|
27 |
+ |
private EventBusRemoteServiceAsync mRemoteServiceAsync = null; |
|
28 |
+ |
|
|
29 |
+ |
public static String getVersion() { |
|
30 |
+ |
return sVersion; |
|
31 |
+ |
} |
|
32 |
+ |
|
|
33 |
+ |
public static ClientEventBus getEventBus() { |
|
34 |
+ |
return sEventBus; |
|
35 |
+ |
} |
|
36 |
+ |
|
|
37 |
+ |
public static void initializeEventBus( String pEncodedClientId ) { |
|
38 |
+ |
System.out.println( "initializeEventBus: " + pEncodedClientId ); |
|
39 |
+ |
// todo: ... |
|
40 |
+ |
} |
|
41 |
+ |
|
|
42 |
+ |
public static void pollEventBus() { |
|
43 |
+ |
ClientEventBus zBus = getEventBus(); |
|
44 |
+ |
if ( zBus != null ) { |
|
45 |
+ |
zBus.pollNow(); |
|
46 |
+ |
} |
|
47 |
+ |
} |
|
48 |
+ |
|
|
49 |
+ |
public static void streamClosed() { |
|
50 |
+ |
System.out.println( "streamClosed" ); |
|
51 |
+ |
Window.alert( "streamClosed" ); |
|
52 |
+ |
// todo: ... |
|
53 |
+ |
} |
|
54 |
+ |
|
|
55 |
+ |
public static int getClientIdNumber() { |
|
56 |
+ |
ClientEventBus zBus = getEventBus(); |
|
57 |
+ |
return (zBus != null) ? zBus.getClientIdNumber() : 0; |
|
58 |
+ |
} |
|
59 |
+ |
|
|
60 |
+ |
protected AbstractClientEventBusApp( AppNames pAppNames ) { |
|
61 |
+ |
super( pAppNames ); |
|
62 |
+ |
mWindowNameClientIdCodec = new WindowNameClientIdCodec( pAppNames ); |
|
63 |
+ |
mDecendantManager = new DecendantManager( mWindowNameClientIdCodec ); |
|
64 |
+ |
} |
|
65 |
+ |
|
|
66 |
+ |
@Override |
|
67 |
+ |
public void onAppLoad() { |
|
68 |
+ |
super.onAppLoad(); |
|
69 |
+ |
|
|
70 |
+ |
mRemoteServiceAsync = createRemoteServiceProxy(); |
|
71 |
+ |
|
|
72 |
+ |
String zEncodedClientId = mWindowNameClientIdCodec.extractEncodedClientId( WindowName.get() ); |
|
73 |
+ |
|
|
74 |
+ |
initializeEventBusConnection( zEncodedClientId ); |
|
75 |
+ |
} |
|
76 |
+ |
|
|
77 |
+ |
@Override |
|
78 |
+ |
public void onClose( CloseEvent<Window> pEvent ) { |
|
79 |
+ |
super.onClose( pEvent ); |
|
80 |
+ |
sEventBus.dispose( "WindowClosed" ); |
|
81 |
+ |
if ( (mRemoteServiceAsync != null) && (sEncodedClientId != null) ) { |
|
82 |
+ |
try { |
|
83 |
+ |
mRemoteServiceAsync.clientDead( sClientIdNumber, new TossingAsyncCallback() ); |
|
84 |
+ |
} |
|
85 |
+ |
catch ( RuntimeException e ) { |
|
86 |
+ |
System.err.println( "ClientEventBusApp.WindowClosed" ); |
|
87 |
+ |
e.printStackTrace(); |
|
88 |
+ |
} |
|
89 |
+ |
} |
|
90 |
+ |
} |
|
91 |
+ |
|
|
92 |
+ |
@Override |
|
93 |
+ |
public void nativeCodeInjection() { |
|
94 |
+ |
super.nativeCodeInjection(); |
|
95 |
+ |
|
|
96 |
+ |
NativeClientEventBusSupport.export(); |
|
97 |
+ |
} |
|
98 |
+ |
|
|
99 |
+ |
@Override |
|
100 |
+ |
public void nativeCodeDispose() { |
|
101 |
+ |
super.nativeCodeDispose(); |
|
102 |
+ |
|
|
103 |
+ |
NativeClientEventBusSupport.dispose(); |
|
104 |
+ |
} |
|
105 |
+ |
|
|
106 |
+ |
protected void initializeLoggerFactory() { |
|
107 |
+ |
//noinspection NonJREEmulationClassesInClientCode |
|
108 |
+ |
LoggerFactory.init( new ConfigurationLoggerLevel(), BrowserAsLogSyncFactory.INSTANCE ); |
|
109 |
+ |
} |
|
110 |
+ |
|
|
111 |
+ |
protected void initializeEventBusConnection( String pEncodedClientId ) { |
|
112 |
+ |
((ServiceDefTarget) mRemoteServiceAsync).setServiceEntryPoint( extractEntryName() ); |
|
113 |
+ |
|
|
114 |
+ |
mRemoteServiceAsync.initialize( pEncodedClientId, new AppInitializationCallback() ); |
|
115 |
+ |
} |
|
116 |
+ |
|
|
117 |
+ |
private static final String ASYNC_NAME_PROXY_TAIL = "_Proxy"; |
|
118 |
+ |
|
|
119 |
+ |
private String extractEntryName() { |
|
120 |
+ |
String zTypeName = mRemoteServiceAsync.getClass().getName(); |
|
121 |
+ |
|
|
122 |
+ |
if ( !zTypeName.endsWith( ASYNC_NAME_PROXY_TAIL ) ) { |
|
123 |
+ |
throw new IllegalStateException( zTypeName + " did not end with '" + ASYNC_NAME_PROXY_TAIL + "'" ); |
|
124 |
+ |
} |
|
125 |
+ |
int lastDot = zTypeName.lastIndexOf( '.' ); |
|
126 |
+ |
if ( lastDot == -1 ) { |
|
127 |
+ |
throw new IllegalStateException( zTypeName + " may not live in the default package" ); |
|
128 |
+ |
} |
|
129 |
+ |
return zTypeName.substring( lastDot + 1, zTypeName.length() - ASYNC_NAME_PROXY_TAIL.length() ); |
|
130 |
+ |
} |
|
131 |
+ |
|
|
132 |
+ |
protected int getPollSecs() { |
|
133 |
+ |
return 5; |
|
134 |
+ |
} |
|
135 |
+ |
|
|
136 |
+ |
abstract protected EventBusRemoteServiceAsync createRemoteServiceProxy(); |
|
137 |
+ |
|
|
138 |
+ |
/** |
|
139 |
+ |
* @noinspection NonJREEmulationClassesInClientCode |
|
140 |
+ |
*/ |
|
141 |
+ |
protected Logger createClientEventBusLogger( int pClientIdNumber ) { |
|
142 |
+ |
return null; |
|
143 |
+ |
} |
|
144 |
+ |
|
|
145 |
+ |
abstract protected void appReady( AuthenticationData pAuthenticationData ); |
|
146 |
+ |
|
|
147 |
+ |
protected void sessionLost() { |
|
148 |
+ |
UtilsGwt.closeWindowNoConfirm( getSessionLostWindowAlertText() ); |
|
149 |
+ |
} |
|
150 |
+ |
|
|
151 |
+ |
protected String getSessionLostWindowAlertText() { |
|
152 |
+ |
return "Your session with the server has been lost.\nYou will need to reconnect.\nThe application window will now attempt to close."; |
|
153 |
+ |
} |
|
154 |
+ |
|
|
155 |
+ |
protected void addEventListenersForNewClient( EventSubscriptionCollector pCollector ) { |
|
156 |
+ |
} |
|
157 |
+ |
|
|
158 |
+ |
private void initialSubscriptions( EventBus pEventBus ) { |
|
159 |
+ |
EventSubscriptionCollector collector = new EventSubscriptionCollector(); |
|
160 |
+ |
addEventListenersForNewClient( collector ); |
|
161 |
+ |
collector.subscribe( new OpenNewClientPackageListener() ); |
|
162 |
+ |
collector.subscribe( new CloseClientPackageListener() ); |
|
163 |
+ |
collector.subscribe( new NoServerEventBusPackageListener() ); |
|
164 |
+ |
collector.subscribe( new RelatedClientToFrontPackageListener() ); |
|
165 |
+ |
mDecendantManager.addEventListenersForNewClient( collector ); |
|
166 |
+ |
pEventBus.subscribe( collector ); |
|
167 |
+ |
} |
|
168 |
+ |
|
|
169 |
+ |
protected void finalPrep( ChannelEventPackage pInitialChannelEvent, AuthenticationData pAuthenticationData ) { |
|
170 |
+ |
System.err.println( new UtilDateAdaptor() + " | Client AppReady" ); |
|
171 |
+ |
appReady( pAuthenticationData ); |
|
172 |
+ |
} |
|
173 |
+ |
|
|
174 |
+ |
protected void closeClientRequested() { |
|
175 |
+ |
UtilsGwt.closeWindowNoConfirm( 1 ); |
|
176 |
+ |
} |
|
177 |
+ |
|
|
178 |
+ |
private class AppInitializationCallback implements AsyncCallback<AppInitializationResult> { |
|
179 |
+ |
public void onFailure( Throwable pThrowable ) { |
|
180 |
+ |
String message; |
|
181 |
+ |
|
|
182 |
+ |
if ( pThrowable instanceof InvocationException ) { |
|
183 |
+ |
// see javadoc for InvocationException |
|
184 |
+ |
message = "A connection could not be established with the server"; |
|
185 |
+ |
} else if ( pThrowable instanceof IncompatibleRemoteServiceException ) { |
|
186 |
+ |
/* |
|
187 |
+ |
* This _should_ not happen to users, so long as the .nocache |
|
188 |
+ |
* file isn't cached by the client. |
|
189 |
+ |
*/ |
|
190 |
+ |
message = |
|
191 |
+ |
"Client and server versions are incompatible. Try ctrl-clicking " + "or shift-clicking the browser refresh button, or clearing your " + |
|
192 |
+ |
"browser's cache in 'Internet Options' / your browser's preferences. "; |
|
193 |
+ |
} else { |
|
194 |
+ |
// probably an application-specific execption on the server |
|
195 |
+ |
message = "An application error occured on the server"; |
|
196 |
+ |
} |
|
197 |
+ |
|
|
198 |
+ |
reportFailure( pThrowable, "onFailure()", message ); |
|
199 |
+ |
} |
|
200 |
+ |
|
|
201 |
+ |
public void onSuccess( AppInitializationResult result ) { |
|
202 |
+ |
System.err.println( new UtilDateAdaptor() + " | Client AppInitialization" ); |
|
203 |
+ |
|
|
204 |
+ |
if ( result == null ) { |
|
205 |
+ |
UtilsGwt.closeWindowNoConfirm( 5 ); |
|
206 |
+ |
return; |
|
207 |
+ |
} |
|
208 |
+ |
|
|
209 |
+ |
ClientEventBus ceb = null; |
|
210 |
+ |
try { |
|
211 |
+ |
new ClientConfiguration( result.getClientConfig() ); // This line must preceed the next |
|
212 |
+ |
initializeLoggerFactory(); // ......................... This line must follow the previous |
|
213 |
+ |
|
|
214 |
+ |
AlertManager.setVersion( sVersion = result.getApplicationVersion() ); |
|
215 |
+ |
|
|
216 |
+ |
sClientIdNumber = result.getClientIdNumber(); |
|
217 |
+ |
|
|
218 |
+ |
sEncodedClientId = result.getEncodedClientId(); |
|
219 |
+ |
WindowName.set( mWindowNameClientIdCodec.createWindowName( sEncodedClientId ) ); |
|
220 |
+ |
|
|
221 |
+ |
ceb = new ClientEventBus( sClientIdNumber ); |
|
222 |
+ |
ceb.initialize( mRemoteServiceAsync, getPollSecs(), createClientEventBusLogger( sClientIdNumber ) ); |
|
223 |
+ |
|
|
224 |
+ |
if ( ceb.getLogger().debug.isEnabled() ) { |
|
225 |
+ |
UtilsGwt.addWindowNameToWindowTitle(); |
|
226 |
+ |
} |
|
227 |
+ |
|
|
228 |
+ |
ChannelServicePackage zPackage = result.getChannelServicePackage(); |
|
229 |
+ |
if ( zPackage != null ) { |
|
230 |
+ |
ceb.propagateFromRemotePeerService( zPackage ); |
|
231 |
+ |
} |
|
232 |
+ |
initialSubscriptions( ceb ); |
|
233 |
+ |
sEventBus = ceb; |
|
234 |
+ |
finalPrep( result.getInitialChannelEvent(), result.getAuthenticationData() ); |
|
235 |
+ |
ceb.release(); |
|
236 |
+ |
} |
|
237 |
+ |
catch ( RedirectClientException e ) { |
|
238 |
+ |
if ( ceb != null ) { |
|
239 |
+ |
ceb.dispose( "Redirect" ); |
|
240 |
+ |
} |
|
241 |
+ |
UtilsGwt.redirect( e.getRedirectTo() ); |
|
242 |
+ |
} |
|
243 |
+ |
catch ( RuntimeException e ) { |
|
244 |
+ |
reportFailure( e, "onSuccess()", null ); |
|
245 |
+ |
} |
|
246 |
+ |
} |
|
247 |
+ |
|
|
248 |
+ |
private void reportFailure( Throwable pThrowable, String method, String message ) { |
|
249 |
+ |
pThrowable.printStackTrace(); // prints the trace in the GWT hosted shell |
|
250 |
+ |
String html = "<h1>Application Initialization Failed</h1>" + "<p>" + message + "</p>" + "<p>at " + getClass().getName() + "." + method + "</p>" + |
|
251 |
+ |
"<p>Exception: </p>" + "<p><pre>" + pThrowable + "</pre></p>"; |
|
252 |
+ |
DOM.setInnerHTML( RootPanel.getBodyElement(), html ); |
|
253 |
+ |
} |
|
254 |
+ |
} |
|
255 |
+ |
|
|
256 |
+ |
private class NoServerEventBusPackageListener implements EventPackageSubscriber { |
|
257 |
+ |
public String subscribeWith() { |
|
258 |
+ |
return NoServerEventBusEventPackage.SubscribeWith; |
|
259 |
+ |
} |
|
260 |
+ |
|
|
261 |
+ |
public void packageReceivedVia( EventPackage pEventPackage, EventBus pEventBus ) { |
|
262 |
+ |
sessionLost(); |
|
263 |
+ |
} |
|
264 |
+ |
} |
|
265 |
+ |
|
|
266 |
+ |
private class RelatedClientToFrontPackageListener implements EventPackageSubscriber { |
|
267 |
+ |
public String subscribeWith() { |
|
268 |
+ |
return RelatedClientToFrontEventPackage.SubscribeWith; |
|
269 |
+ |
} |
|
270 |
+ |
|
|
271 |
+ |
public void packageReceivedVia( EventPackage pEventPackage, EventBus pEventBus ) { |
|
272 |
+ |
int id = ((RelatedClientToFrontEventPackage) pEventPackage).getClientId(); |
|
273 |
+ |
mDecendantManager.decendantToFront( id ); |
|
274 |
+ |
} |
|
275 |
+ |
} |
|
276 |
+ |
|
|
277 |
+ |
private class CloseClientPackageListener implements EventPackageSubscriber { |
|
278 |
+ |
public String subscribeWith() { |
|
279 |
+ |
return CloseClientEventPackage.SubscribeWith; |
|
280 |
+ |
} |
|
281 |
+ |
|
|
282 |
+ |
public void packageReceivedVia( EventPackage pEventPackage, EventBus pEventBus ) { |
|
283 |
+ |
closeClientRequested(); |
|
284 |
+ |
} |
|
285 |
+ |
} |
|
286 |
+ |
|
|
287 |
+ |
private class OpenNewClientPackageListener implements EventPackageSubscriber { |
|
288 |
+ |
public String subscribeWith() { |
|
289 |
+ |
return OpenNewClientEventPackage.SubscribeWith; |
|
290 |
+ |
} |
|
291 |
+ |
|
|
292 |
+ |
public void packageReceivedVia( EventPackage pEventPackage, EventBus pEventBus ) { |
|
293 |
+ |
OpenNewClientEventPackage onc = (OpenNewClientEventPackage) pEventPackage; |
|
294 |
+ |
String url = UtilsGwt.getURL(); |
|
295 |
+ |
String windowName = mWindowNameClientIdCodec.createWindowName( onc.getNewEncodedClientId() ); |
|
296 |
+ |
|
|
297 |
+ |
openChildWindow( windowName, url ); |
|
298 |
+ |
} |
|
299 |
+ |
} |
|
300 |
+ |
|
|
301 |
+ |
protected void openChildWindow( String pWinName, String pUrl ) { |
|
302 |
+ |
UtilsGwt.windowOpenChromeless( pUrl, pWinName ); |
|
303 |
+ |
// Other Features: |
|
304 |
+ |
// "hotkeys=0" + // Netscape!, FireFox?, IE? |
|
305 |
+ |
// "screenX=nnnn,screenY=nnnn," + // Netscape!, FireFox?, |
|
306 |
+ |
// IE? |
|
307 |
+ |
// width=100,height=100," + |
|
308 |
+ |
} |
|
309 |
+ |
|
|
310 |
+ |
private static class TossingAsyncCallback implements AsyncCallback<Object> { |
|
311 |
+ |
public void onFailure( Throwable caught ) { |
|
312 |
+ |
if ( !sWindowClosing && (caught != null) ) { |
|
313 |
+ |
System.err.println( "ClientEventBusApp.TossingAsyncCallback.onFailure" ); |
|
314 |
+ |
caught.printStackTrace(); |
|
315 |
+ |
} |
|
316 |
+ |
} |
|
317 |
+ |
|
|
318 |
+ |
public void onSuccess( Object result ) { |
|
319 |
+ |
} |
|
320 |
+ |
} |
|
321 |
+ |
} |