1

In Restlet 2.3 what is the recommended way of obtaining an Application?

The docs suggest there is a static method Application.getCurrent() that implies that it is possible to obtain the executing Application. However, this requires the call to be made from the executing Application.

Say I had several applications and they are all running:

public class ApplicationA extends Application {...}

public class ApplicationB extends Application {...}

Is it possible to obtain Application for ApplicationA from ApplicationB?

tarka
  • 5,289
  • 10
  • 51
  • 75

1 Answers1

0

It depends on the way you configured your Restlet "application" but since you define your routing based on application instances, you could inject one into other one, as described below:

Component c = new Component();
(...)

MyApplication1 app1 = new MyApplication1();
c.getDefaultHost().attach("/app1", app1);

MyApplication1 app2 = new MyApplication2(app1);
c.getDefaultHost().attach("/app1", app2);

(...)

You can notice that you can also introspect your application at runtime. The APISpark extension of Restlet does something like that. See class org.restlet.ext.apispark.internal.introspection.application.ComponentIntrospector and org.restlet.ext.apispark.internal.introspection.application.ComponentIntrospector.

Why do you need such feature in your application? Just to be sure to give the right answer ;-)

Edited

You can reach a sample application for your use case at the address https://github.com/templth/restlet-stackoverflow/tree/master/restlet/test-restlet-application-manager.

Hope it helps you, Thierry

Thierry Templier
  • 198,364
  • 44
  • 396
  • 360
  • Im trying to come up with an effective way of shutting down the application from the command line. The application is demonized and has a updater that performs a shutdown of the current version and then starts up the new one. `Application.stop()` seems to be the perfect solution but I am trying to establish the best way of obtaining it. Before I go down the road of setting up a shutdown hook from a specific route I wanted to see if it was possible to obtain the `Application` any other way. – tarka Jun 04 '15 at 12:46
  • Do you use OSGi or a plain Java application? – Thierry Templier Jun 04 '15 at 13:21
  • Plain java. I considered OSGi but its a lot of extra work and it is very invasive for the benefits. – tarka Jun 04 '15 at 13:27
  • Incidentally I have just tried calling `Application.getCurrent().stop()` but it didn't seem to shutdown the application. – tarka Jun 04 '15 at 13:42
  • In fact, to actually "stop" a Restlet application you need to detach it from its corresponding virtual host(s). Calling the method `stop` is just to free some resources. – Thierry Templier Jun 05 '15 at 09:59
  • With OSGi, you can tie the application to the bundle lifecycle to attach / detach contained application. With plain Java application, you need to do that by hand. I can provide you a simple example if you're interested in... – Thierry Templier Jun 05 '15 at 10:00
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/79761/discussion-between-tarka-and-thierry-templier). – tarka Jun 05 '15 at 10:30
  • Hi, I wonder why you need to have access to ApplicationA from ApplicationB? Why don't you gather all your resources into a single application? In addition, are you aware of the riap protocol that allows you to transmit call inside the components between applications (http://restlet.com/technical-resources/restlet-framework/guide/2.3/core/engine/internal-connectors/riap)? – Thierry Boileau Jun 05 '15 at 15:58
  • I uploaded a project for your use case. The link is within the answer (I updated it). Sorry for the delay and hoping this will answer your issue ;-) – Thierry Templier Jun 12 '15 at 09:02