helipilot50
helipilot50

Reputation: 101

GWT client and server implementations of the same class

Is there any way to have the same class implemented differently on the client vs the server?

To avoid the "Why do you want to do that?" question.. I will elaborate

I am converting a very large Java client/server application. Currently it uses a Swing GUI client and talks to the server via Spring remoting (RPC). Using GWT RPC with Spring services is not a problem, there are several excellent examples available and the all seem to work well.

Several classes that are common to both the client and the server contain data that is passed back and forth. These classes also contain some behavior that is implemented by using the standard JRE classes. For example, one class contains, parses and formats date and time, including time zone, DST, etc. in a locale specific way. I could rewrite/refactor it but the application is over 10 million SLOC, resulting in literally millions of references to this class alone, so a major rewrite is not cost effective.

To use this as an example, GWT provides excellent i18n support for parsing and formatting dates. But the implementation is different to the way the JRE does it.

So I'm looking for a cleaver way where by I can inject an implementation into the shell of my DateTime class, depending on whether it is in the client (using GWT and native JS) or in the server (using the JRE). Is there a cunning way to do this? Perhaps using the module file XXXXX.gwt.xml. I'm looking for a generic solution.

Upvotes: 10

Views: 2990

Answers (2)

Thomas Broyer
Thomas Broyer

Reputation: 64551

You'd want to use the <super-source> for overriding one package implementation with another. This is what GWT uses to emulate the Java Runtime classes, and (among others) provide different implementations for client and server of the com.google.gwt.regexp.shared.* classes.

Upvotes: 5

jjczopek
jjczopek

Reputation: 3379

I think what You are looking for, is this: <source path="client" /> in your project gwt.xml file. It tells the GWT generator where to look for client side code to convert to JS. In my project I have it set up this way:

<source path="client" />
<source path="shared" />

Basically client code is in the client directory, and in shared we keep beans and some data wrappers for client and server side.

What you could do, is to add the packages you want to convert to client with the source path like above. But you must remember, that the classes you are going to convert, can be composed only of objects and properties which GWT generator can convert into client-side java script. I'm not sure also if more accurate path can be put in the source path, like:

<source path="shared/beans/whatever" />

Another drawback is, that if you use GWT i18n support, it handles different locale in time of compilation by its own - which is good. If you decide to use your own mechanism, your classes must contain some logic to be aware of locale being used currently, which must be compatible with GWT.

Upvotes: 0

Related Questions