Subversion Repository Public Repository

litesoft

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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
package org.litesoft.GWT.eventbus.client;

import org.litesoft.GWT.client.*;
import org.litesoft.GWT.client.logging.*;
import org.litesoft.GWT.eventbus.client.eventpackages.*;
import org.litesoft.GWT.eventbus.client.nonpublic.*;
import org.litesoft.GWT.eventbus.client.rpc.*;
import org.litesoft.core.simpletypes.temporal.*;
import org.litesoft.logger.*;
import org.litesoft.loggerconfig.*;

import com.google.gwt.user.client.*;
import com.google.gwt.user.client.rpc.*;
import com.google.gwt.user.client.ui.*;
import com.google.gwt.event.logical.shared.*;

public abstract class AbstractClientEventBusApp extends AbstractClientMainAppEntryPoint
{
    private static String sVersion = "N/A";
    private static ClientEventBus sEventBus = null;
    private static int sClientIdNumber;
    protected static String sEncodedClientId = null;

    private WindowNameClientIdCodec mWindowNameClientIdCodec;
    protected DecendantManager mDecendantManager;

    private EventBusRemoteServiceAsync mRemoteServiceAsync = null;

    public static String getVersion()
    {
        return sVersion;
    }

    public static ClientEventBus getEventBus()
    {
        return sEventBus;
    }

    public static void initializeEventBus( String pEncodedClientId )
    {
        System.out.println( "initializeEventBus: " + pEncodedClientId );
        // todo: ...
    }

    public static void pollEventBus()
    {
        ClientEventBus zBus = getEventBus();
        if ( zBus != null )
        {
            zBus.pollNow();
        }
    }

    public static void streamClosed()
    {
        System.out.println( "streamClosed" );
        Window.alert( "streamClosed" );
        // todo: ...
    }

    public static int getClientIdNumber()
    {
        ClientEventBus zBus = getEventBus();
        return (zBus != null) ? zBus.getClientIdNumber() : 0;
    }

    protected AbstractClientEventBusApp( AppNames pAppNames )
    {
        super( pAppNames );
        mWindowNameClientIdCodec = new WindowNameClientIdCodec( pAppNames );
        mDecendantManager = new DecendantManager( mWindowNameClientIdCodec );
    }

    @Override
    public void onAppLoad()
    {
        super.onAppLoad();

        mRemoteServiceAsync = createRemoteServiceProxy();

        String zEncodedClientId = mWindowNameClientIdCodec.extractEncodedClientId( WindowName.get() );

        initializeEventBusConnection( zEncodedClientId );
    }

    @Override
    public void onClose( CloseEvent<Window> pEvent )
    {
        super.onClose( pEvent );
        sEventBus.dispose( "WindowClosed" );
        if ( (mRemoteServiceAsync != null) && (sEncodedClientId != null) )
        {
            try
            {
                mRemoteServiceAsync.clientDead( sClientIdNumber, new TossingAsyncCallback() );
            }
            catch ( RuntimeException e )
            {
                System.err.println( "ClientEventBusApp.WindowClosed" );
                e.printStackTrace();
            }
        }
    }

    @Override
    public void nativeCodeInjection()
    {
        super.nativeCodeInjection();

        NativeClientEventBusSupport.export();
    }

    @Override
    public void nativeCodeDispose()
    {
        super.nativeCodeDispose();

        NativeClientEventBusSupport.dispose();
    }

    protected void initializeLoggerFactory()
    {
        //noinspection NonJREEmulationClassesInClientCode
        LoggerFactory.init( new ConfigurationLoggerLevel(), BrowserAsLogSyncFactory.INSTANCE );
    }

    protected void initializeEventBusConnection( String pEncodedClientId )
    {
        ((ServiceDefTarget) mRemoteServiceAsync).setServiceEntryPoint( extractEntryName() );

        mRemoteServiceAsync.initialize( pEncodedClientId, new AppInitializationCallback() );
    }

    private static final String ASYNC_NAME_PROXY_TAIL = "_Proxy";

    private String extractEntryName()
    {
        String zTypeName = mRemoteServiceAsync.getClass().getName();

        if ( !zTypeName.endsWith( ASYNC_NAME_PROXY_TAIL ) )
        {
            throw new IllegalStateException( zTypeName + " did not end with '" + ASYNC_NAME_PROXY_TAIL + "'" );
        }
        int lastDot = zTypeName.lastIndexOf( '.' );
        if ( lastDot == -1 )
        {
            throw new IllegalStateException( zTypeName + " may not live in the default package" );
        }
        return zTypeName.substring( lastDot + 1, zTypeName.length() - ASYNC_NAME_PROXY_TAIL.length() );
    }

    protected int getPollSecs()
    {
        return 5;
    }

    abstract protected EventBusRemoteServiceAsync createRemoteServiceProxy();

    /**
     * @noinspection NonJREEmulationClassesInClientCode
     */
    protected Logger createClientEventBusLogger( int pClientIdNumber )
    {
        return null;
    }

    abstract protected void appReady( AuthenticationData pAuthenticationData );

    protected void sessionLost()
    {
        UtilsGwt.closeWindowNoConfirm( getSessionLostWindowAlertText() );
    }

    protected String getSessionLostWindowAlertText()
    {
        return "Your session with the server has been lost.\nYou will need to reconnect.\nThe application window will now attempt to close.";
    }

    protected void addEventListenersForNewClient( EventSubscriptionCollector pCollector )
    {
    }

    private void initialSubscriptions( EventBus pEventBus )
    {
        EventSubscriptionCollector collector = new EventSubscriptionCollector();
        addEventListenersForNewClient( collector );
        collector.subscribe( new OpenNewClientPackageListener() );
        collector.subscribe( new CloseClientPackageListener() );
        collector.subscribe( new NoServerEventBusPackageListener() );
        collector.subscribe( new RelatedClientToFrontPackageListener() );
        mDecendantManager.addEventListenersForNewClient( collector );
        pEventBus.subscribe( collector );
    }

    protected void finalPrep( ChannelEventPackage pInitialChannelEvent, AuthenticationData pAuthenticationData )
    {
        System.err.println( new UtilDateAdaptor() + " | Client AppReady" );
        appReady( pAuthenticationData );
    }

    protected void closeClientRequested()
    {
        UtilsGwt.closeWindowNoConfirm( 1 );
    }

    private class AppInitializationCallback implements AsyncCallback<AppInitializationResult>
    {
        public void onFailure( Throwable pThrowable )
        {
            String message;

            if ( pThrowable instanceof InvocationException )
            {
                // see javadoc for InvocationException
                message = "A connection could not be established with the server";
            }
            else if ( pThrowable instanceof IncompatibleRemoteServiceException )
            {
                /*
                 * This _should_ not happen to users, so long as the .nocache 
                 * file isn't cached by the client. 
                 */
                message =
                        "Client and server versions are incompatible. Try ctrl-clicking " + "or shift-clicking the browser refresh button, or clearing your " +
                        "browser's cache in 'Internet Options' / your browser's preferences. ";
            }
            else
            {
                // probably an application-specific execption on the server
                message = "An application error occured on the server";
            }

            reportFailure( pThrowable, "onFailure()", message );
        }

        public void onSuccess( AppInitializationResult result )
        {
            System.err.println( new UtilDateAdaptor() + " | Client AppInitialization" );

            if ( result == null )
            {
                UtilsGwt.closeWindowNoConfirm( 5 );
                return;
            }

            ClientEventBus ceb = null;
            try
            {
                new ClientConfiguration( result.getClientConfig() ); // This line must preceed the next
                initializeLoggerFactory(); // ......................... This line must follow the previous

                AlertManager.setVersion( sVersion = result.getApplicationVersion() );

                sClientIdNumber = result.getClientIdNumber();

                sEncodedClientId = result.getEncodedClientId();
                WindowName.set( mWindowNameClientIdCodec.createWindowName( sEncodedClientId ) );

                ceb = new ClientEventBus( sClientIdNumber );
                ceb.initialize( mRemoteServiceAsync, getPollSecs(), createClientEventBusLogger( sClientIdNumber ) );

                if ( ceb.getLogger().debug.isEnabled() )
                {
                    UtilsGwt.addWindowNameToWindowTitle();
                }

                ChannelServicePackage zPackage = result.getChannelServicePackage();
                if ( zPackage != null )
                {
                    ceb.propagateFromRemotePeerService( zPackage );
                }
                initialSubscriptions( ceb );
                sEventBus = ceb;
                finalPrep( result.getInitialChannelEvent(), result.getAuthenticationData() );
                ceb.release();
            }
            catch ( RedirectClientException e )
            {
                if ( ceb != null )
                {
                    ceb.dispose( "Redirect" );
                }
                UtilsGwt.redirect( e.getRedirectTo() );
            }
            catch ( RuntimeException e )
            {
                reportFailure( e, "onSuccess()", null );
            }
        }

        private void reportFailure( Throwable pThrowable, String method, String message )
        {
            pThrowable.printStackTrace(); // prints the trace in the GWT hosted shell
            String html = "<h1>Application Initialization Failed</h1>" + "<p>" + message + "</p>" + "<p>at " + getClass().getName() + "." + method + "</p>" +
                          "<p>Exception: </p>" + "<p><pre>" + pThrowable + "</pre></p>";
            DOM.setInnerHTML( RootPanel.getBodyElement(), html );
        }
    }

    private class NoServerEventBusPackageListener implements EventPackageSubscriber
    {
        public String subscribeWith()
        {
            return NoServerEventBusEventPackage.SubscribeWith;
        }

        public void packageReceivedVia( EventPackage pEventPackage, EventBus pEventBus )
        {
            sessionLost();
        }
    }

    private class RelatedClientToFrontPackageListener implements EventPackageSubscriber
    {
        public String subscribeWith()
        {
            return RelatedClientToFrontEventPackage.SubscribeWith;
        }

        public void packageReceivedVia( EventPackage pEventPackage, EventBus pEventBus )
        {
            int id = ((RelatedClientToFrontEventPackage) pEventPackage).getClientId();
            mDecendantManager.decendantToFront( id );
        }
    }

    private class CloseClientPackageListener implements EventPackageSubscriber
    {
        public String subscribeWith()
        {
            return CloseClientEventPackage.SubscribeWith;
        }

        public void packageReceivedVia( EventPackage pEventPackage, EventBus pEventBus )
        {
            closeClientRequested();
        }
    }

    private class OpenNewClientPackageListener implements EventPackageSubscriber
    {
        public String subscribeWith()
        {
            return OpenNewClientEventPackage.SubscribeWith;
        }

        public void packageReceivedVia( EventPackage pEventPackage, EventBus pEventBus )
        {
            OpenNewClientEventPackage onc = (OpenNewClientEventPackage) pEventPackage;
            String url = UtilsGwt.getURL();
            String windowName = mWindowNameClientIdCodec.createWindowName( onc.getNewEncodedClientId() );

            openChildWindow( windowName, url );
        }
    }

    protected void openChildWindow( String pWinName, String pUrl )
    {
        UtilsGwt.windowOpenChromeless( pUrl, pWinName );
        // Other Features:
        //      "hotkeys=0" + // Netscape!, FireFox?, IE?
        //      "screenX=nnnn,screenY=nnnn," + // Netscape!, FireFox?,
        //          IE?
        //      width=100,height=100," +
    }

    private static class TossingAsyncCallback implements AsyncCallback<Object>
    {
        public void onFailure( Throwable caught )
        {
            if ( !sWindowClosing && (caught != null) )
            {
                System.err.println( "ClientEventBusApp.TossingAsyncCallback.onFailure" );
                caught.printStackTrace();
            }
        }

        public void onSuccess( Object result )
        {
        }
    }
}

Commits for litesoft/trunk/Java/GWT/OldClient/src/org/litesoft/GWT/eventbus/client/AbstractClientEventBusApp.java

Diff revisions: vs.
Revision Author Commited Message
2 GeorgeS picture GeorgeS Sun 07 Feb, 2010 12:50:58 +0000