Dynamic EntityManagerFactory creation and RequestFactory service stubs problem

238 views
Skip to first unread message

Jack

unread,
Nov 6, 2010, 2:26:26 PM11/6/10
to Google Web Toolkit
Hi,

we would like to integrate GWT 2.1 RequestFactory in our application.
Our application's backend uses JPA and has one database per customer.
So we have to tell our EntityManagerFactory which database it should
use, depending on the customer who did the server request.
Currently we have http://our-app.com/<unique-customername> as
application URL and we send <unique-customername> to our backend on
each request. That way we can construct an EntityManagerFactory with a
custom properties map which contains a datasource name similar to
"appdatasource-<unique-customername>". So every time we got a new
customer, we just have to add a new JDBC resource and connection pool
which points to the new customers database to our J2EE server.

So we tried this approach with RequestFactory and defined our service
interface like this:

@Service(Person.class)
public interface PersonRequest extends RequestContext {

Request<List<PersonProxy>> findAllPersons(String
uniqueCustomerName);

Request<List<PersonProxy>> findPersonsByAge(int min, int max, String
uniqueCustomerName);

InstanceRequest<PersonProxy, Void> persist(String
uniqueCustomerName);

InstanceRequest<PersonProxy, Void> remove(String
uniqueCustomerName);



Request<PersonProxy> findPerson(Long id);

}

So everything is fine except the method "Request<PersonProxy>
findPerson(Long id)" which is required by the GWT RequestFactory
servlet. So we can't add a second parameter to this method. But this
second parameter is required for our dynamic EntityManagerFactory
configuration.

Are there any plans for a GWT 2.1.x release which supports such a
scenario? It would be nice if the required findEntity method would
support additional custom parameters (maybe via Java5 VarArgs) or if
there is a way to transport additional information from client to
server during a RequestFactory request which can then be accessed
during server side method execution.

Does anybody have an idea for a nasty workaround for that problem that
is possible with the current GWT 2.1 implementation?

Thomas Broyer

unread,
Nov 6, 2010, 4:35:47 PM11/6/10
to Google Web Toolkit


On 6 nov, 19:26, Jack <mlsubscri...@gmail.com> wrote:
> Hi,
>
> we would like to integrate GWT 2.1 RequestFactory in our application.
> Our application's backend uses JPA and has one database per customer.
> So we have to tell our EntityManagerFactory which database it should
> use, depending on the customer who did the server request.
> Currently we havehttp://our-app.com/<unique-customername> as
The changes to expect are listed in http://code.google.com/p/google-web-toolkit/wiki/RequestFactory_2_1_1

> It would be nice if the required findEntity method would
> support additional custom parameters (maybe via Java5 VarArgs) or if
> there is a way to transport additional information from client to
> server during a RequestFactory request which can then be accessed
> during server side method execution.
>
> Does anybody have an idea for a nasty workaround for that problem that
> is possible with the current GWT 2.1 implementation?

One that I wouldn't call "nasty":
1. extend DefaultRequestTransport to send the uniqueCustomerName (say,
within an HTTP header)
2. initialize() your RequestFactory with an instance of your extended
RequestTransport
3. on the server-side, get the HttpServletRequest using
RequestFactoryServlet.getThreadLocalRequest(), to extract the
uniqueCustomerName from it and use it to create the
EntityManagerFactory.
That way, you don't have to send the uniqueCustomerName explicitly in
each and every request, it'll be done automatically and transparently
by your RequestTransport.

Jack

unread,
Nov 6, 2010, 7:46:59 PM11/6/10
to Google Web Toolkit
Oh that sounds pretty nice. Haven't realized that its possible that
way.. hehe..and its not nasty at all :)
I'll give it a try. Thanks!

And a second question that comes to my mind:
When using RequestFactory our domain objects are only defined by
interfaces. Is there a class in the framework that posts some sort of
PropertyChangeEvent on the provided EventBus whenever a property is
changed on client-side (This event should be fired whenever a setter
method is called on client side). If not is there a way to integrate
such a feature?
I asks this because currently we have an own simple binding system
which synchronizes UI and domain objects using PropertyChangeEvents
fired by the domain object. Of cause we could use the new Editor
Framework but it would be nice to do a step by step transition.
> The changes to expect are listed inhttp://code.google.com/p/google-web-toolkit/wiki/RequestFactory_2_1_1

Jack

unread,
Nov 6, 2010, 9:56:49 PM11/6/10
to Google Web Toolkit
Ok it works like a charm with a custom RequestTransport and a custom
header. Thanks for that tip!

But RequestFactory docs should really mention what to add to web.xml
and that you need a json implementation from json.org and a
javax.validation implementation in your server classpath to make
things work with Eclipse + GWT Plugin + Jetty. Otherwise you got some
nice java.lang.NoClassDefFoundError from jetty.



On 6 Nov., 21:35, Thomas Broyer <t.bro...@gmail.com> wrote:
> The changes to expect are listed inhttp://code.google.com/p/google-web-toolkit/wiki/RequestFactory_2_1_1

Thomas Broyer

unread,
Nov 7, 2010, 6:43:48 AM11/7/10
to Google Web Toolkit


On 7 nov, 02:56, Jack <mlsubscri...@gmail.com> wrote:
> Ok it works like a charm with a custom RequestTransport and a custom
> header. Thanks for that tip!
>
> But RequestFactory docs should really mention what to add to web.xml
> and that you need a json implementation from json.org and a
> javax.validation implementation in your server classpath to make
> things work with Eclipse + GWT Plugin + Jetty. Otherwise you got some
> nice java.lang.NoClassDefFoundError from jetty.

You can use the gwt-servlet-deps.jar which includes those required
dependencies (unless you're using Maven, in which case you'd add
dependencies to javax.validation and json in your pom)

Thomas Broyer

unread,
Nov 7, 2010, 6:47:22 AM11/7/10
to Google Web Toolkit


On 7 nov, 00:46, Jack <mlsubscri...@gmail.com> wrote:
> Oh that sounds pretty nice. Haven't realized that its possible that
> way.. hehe..and its not nasty at all :)
> I'll give it a try. Thanks!
>
> And a second question that comes to my mind:
> When using RequestFactory our domain objects are only defined by
> interfaces. Is there a class in the framework that posts some sort of
> PropertyChangeEvent on the provided EventBus whenever a property is
> changed on client-side (This event should be fired whenever a setter
> method is called on client side). If not is there a way to integrate
> such a feature?

On each setter call, no (this is because a RequestContext is a kind of
"transaction": it accumulates changes to entity proxies and method
calls on the RequestContext –currently limited to a single method call
AFAIK– to later send them all in one go to the server when you call
fire()).
When the server responds, it tells the client to fire
EntityProxyChange events on the event bus, those are the ones you'll
listen to to update the rest of your app.
At least this is how I understand how it's supposed to be!

Reply all
Reply to author
Forward
0 new messages