Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Remote Application Platform (RAP) » registering handlers
registering handlers [message #1031924] Tue, 02 April 2013 07:28 Go to next message
Phill Perryman is currently offline Phill Perryman
Messages: 214
Registered: July 2009
Senior Member
I am writing a pure rwt application (no workbench) and have the following code in a handler

RWT.getServiceManager().registerServiceHandler("download", new DownloadServiceHandler());
String url = RWT.getServiceManager().getServiceHandlerUrl("download");
UrlLauncher launcher = RWT.getClient().getService(UrlLauncher.class);
launcher.openURL(url);

It works fine the first time but then fails with an already registered the next time.

However if I move the registration line of code to the BasicApplication or Activator classes then I get an invalid thread access so I am assuming that is too early in the life cycle.

There does not appear to be any other place where I can put the registration code. I can't find a post start up method anywhere and if I put it in an entryPoint class then it fails on a page reload.
Re: registering handlers [message #1032024 is a reply to message #1031924] Tue, 02 April 2013 09:36 Go to previous messageGo to next message
Cole Markham is currently offline Cole Markham
Messages: 133
Registered: July 2009
Senior Member
Phill,

The key used to register the service handler must be unique across the entire application, but each handler is associated with a particular user session (Display). You can use an integer counter, UUID, or some other mechanism to ensure the id is unique. You should also unregister the handler when it is no longer needed. For instance, if your download corresponds the contents of some table, then it probably makes sense to unregister it when the table is disposed. To make it available for an entire user session, you could put the registration your EntryPoint.createUI() method. I believe RAP will cleanup the registration automatically when the user session is disposed.

Hope that helps,

Cole
Re: registering handlers [message #1032078 is a reply to message #1031924] Tue, 02 April 2013 10:43 Go to previous messageGo to next message
Phill Perryman is currently offline Phill Perryman
Messages: 214
Registered: July 2009
Senior Member
Thanks, I was thinking of having a single handler registered at application start and then passing it parameters in the query string but it makes sense to create a new handler for each instance using a unique id and parameterized through the constructor. I can pass in the UUID in the constructor and each handler can unregister itself. i.e. one shot handlers.

Yes that change in thinking helps a lot.
Re: registering handlers [message #1037965 is a reply to message #1032024] Wed, 10 April 2013 04:28 Go to previous messageGo to next message
Ralf Sternberg is currently offline Ralf Sternberg
Messages: 1290
Registered: July 2009
Senior Member

Hi Cole,

your statement about the scope of ServiceHandlers is not correct, let me
clarify. ServiceHandlers are like simple servlets, they are registered
with the (application-scoped) ServiceManager and have application scope
itself, not session scope.

> ... each handler is associated with a particular user session (Display)

No, code in a ServiceHandler has access to the user's Display, but the
ServiceHandler does not have session scope.

> I believe RAP will cleanup the
> registration automatically when the user session is disposed.

Since they have application scope, that's not the case. If you don't
unregister a service handler, it will remain in place as long as the
application keeps running.

Therefore I would not recommend to create new service handlers for every
user session. I would rather create one service handler in the
application configuration and use it for all sessions.

If you have an idea how we can make this more clear in the documentation
or API, your input is very appreciated.

Best regards,
Ralf

--
Ralf Sternberg

Twitter: @EclipseRAP
Blog: http://eclipsesource.com/blogs/

Professional services for RAP and RCP?
http://eclipsesource.com/services/rap/
Re: registering handlers [message #1038118 is a reply to message #1037965] Wed, 10 April 2013 08:30 Go to previous messageGo to next message
Phill Perryman is currently offline Phill Perryman
Messages: 214
Registered: July 2009
Senior Member
Thanks for the clarification but that now puts me back to square one. I have no where in a standalone RWT application that I can actually register the handler.

You can't register it in the BasicApplication (configure(Application application)) or activator (start(BundleContext context)) as it gets a thread error and you can't register it in the EntryPoint as you get an already loaded error on page refresh. So if it is application wide where does it get registered.

So where exactly do i put this line of code: RWT.getServiceManager().registerServiceHandler("download", new DownloadServiceHandler());

I tried Cole's approach and create new handlers each time and let the handler un-register itself.

I created a simple abstract class, and my handler calls super.service to unregister itself before it does anything.

Also this way I can pass objects to the constructor of the handler before launching it eg new SomeHandler(Object o).launch()

public abstract class AbstractServiceHandler implements ServiceHandler {

private static long ids = 0;

private String id;

private synchronized long getNextId() {
return ids++;
}

@Override
public void service(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
ServiceManager manager = RWT.getServiceManager();
manager.unregisterServiceHandler(id);
}

/**
* Creates a unique id and registers this handler with that id and then uses
* URLLauncher to open it in a new page.
*/
@SuppressWarnings("nls")
public void launch() {
UrlLauncher launcher = RWT.getClient().getService(UrlLauncher.class);
ServiceManager manager = RWT.getServiceManager();
id = "ash" + Long.toString(getNextId());
manager.registerServiceHandler(id, this);
String url = manager.getServiceHandlerUrl(id);
launcher.openURL(url);
}

}
Re: registering handlers [message #1038782 is a reply to message #1038118] Thu, 11 April 2013 06:05 Go to previous message
Ralf Sternberg is currently offline Ralf Sternberg
Messages: 1290
Registered: July 2009
Senior Member

Hi Phill,

I don't have the one ideal solution for you, but some suggestions:

1) You can create a separate ServiceHandler for every single UISession,
but you have to make sure to unregister the handlers correctly. For me
it just doesn't feel right to create so many service handlers in the
application scope and there is the risk to create a memory leak.

2) The custom component could provide an initialize() method that is
called from the application configuration. In this initialize method,
the component could register resources, service handlers, etc. However,
this involves a dependency from the application to the custom widget.
Most of the times you'll prefer loose coupling instead. Moreover, when
the component needs a reference to the application, you end up with a
circular dependency.

2) If the application does not know about the custom component, the
component can only be initialized when it is first accessed, i.e.
lazily. You can do so by storing the service handler in the
ApplicationContext using setAttribute() and create it only if doesn't
exist yet. You'll have to make sure that this code is thread safe. RAP
does not yet provide support for ApplicationSingletons, which would
probably be helpful for those cases.

3) Depending on your environment, you could also consider to register a
servlet with the HttpService or the servlet API. In a servlet you can't
access the UISession, but you can access the HttpSession, where all
user-related information should be kept. So if you store whatever the
servlet needs in the HttpSession (UISession.getHttpSession()), a servlet
could be an alternative.

HTH, Ralf

--
Ralf Sternberg

Twitter: @EclipseRAP
Blog: http://eclipsesource.com/blogs/

Professional services for RAP and RCP?
http://eclipsesource.com/services/rap/
Previous Topic:Could anyone give me a tip for paint graphic into table cell?
Next Topic:Focus first Text in Dialog
Goto Forum:
  


Current Time: Mon Sep 01 03:43:45 EDT 2014

Powered by FUDForum. Page generated in 0.01876 seconds