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
}
}
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).
AutoBeanFactorySource
is not shared code, so you cannot use this code on the client. in this case, you need to either:
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.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With