javarestlet-2.0

Restlet obtaining Application


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?


Solution

  • 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