Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check whether code is executed on server or client side in GWT?

Tags:

java

gwt

I've got one class which is used in both server and client side. How can I do checking in its constructor that it has been called either from client or server class?

I've done it in dirty way - just try if GWT.create() method throws an exception an if it does, run server side code. But how can I avoid this?

    public PrintManager() {
    try {
        factory = GWT.create(MapConfigFactory.class);   //clientsiede factory creation
    } catch (Exception ex) {
        factory = AutoBeanFactorySource.create(MapConfigFactory.class); //serverside factory creator
    }
}
like image 646
denu Avatar asked Nov 29 '11 12:11

denu


2 Answers

com.google.gwt.core.client.GWT.isScript() returns true when the code is running as JavaScript on the client.

com.google.gwt.core.client.GWT.isClient() returns false when the code is running on the server JVM (shared code).

like image 71
Witek Avatar answered Nov 03 '22 09:11

Witek


AutoBeanFactorySource is not shared code, so you cannot use this code on the client. in this case, you need to either:

  • use dependency-injection, so that the instance of MapConfigFactory can be provided differently on client-side and server-side
  • use super-source to have two files for the same class: one for the client-side, and another one for the server-side. super-source is explained in the Overriding one package implementation with another section of http://code.google.com/webtoolkit/doc/latest/DevGuideOrganizingProjects.html#DevGuideModuleXml, it is used by GWT for the Java runtime emulation and, for instance, for the com.google.gwt.regexp and com.google.gwt.safehtml packages, to provide a unified API that can run in both client and server side.
like image 34
Thomas Broyer Avatar answered Nov 03 '22 09:11

Thomas Broyer