This section explains how to use the FraSCAti remote API into your application.
You need to add a dependency to the
frascati-introspection-api
module to be able to load service
interfaces (RemoteScaDomain
,
Reconfiguration
and
Deployment
) and REST resources.
<dependency> <groupId>org.ow2.frascati</groupId> <artifactId>frascati-introspection-api</artifactId> <version>1.4</version> </dependency>
Of course, the easiest way to connect to the FraSCAti remote API is to declare an SCA reference to the targeted service on the remote component. In your composite file, it will lool like:
<component name="mycomponent"> <implementation.java class="org.myorg.myapp.MyCompImpl"/> <service name="myservice"> ... </service> <reference name="introspection"> <interface.java interface="org.ow2.frascati.remote.introspection.RemoteScaDomain"/> <frascati:binding.rest uri="http://<frascati-hostname[:portnumber]>/introspection"/> </reference> <reference name="reconfiguration"> <interface.java interface="org.ow2.frascati.remote.introspection.Reconfiguration"/> <frascati:binding.rest uri="http://<frascati-hostname[:portnumber]>/reconfig"/> </reference> <reference name="deployment"> <interface.java interface="org.ow2.frascati.remote.introspection.Deployment"/> <frascati:binding.rest uri="http://<frascati-hostname[:portnumber]>/deploy"/> </reference> </component>
You just need to define a reference to services you intend to use.
Of course, <frascati-hostname[:portnumber]>
has
to be replaced by something like myserver.mydomain.org:8080
,
depending on your infrastructure.
In the component implementation, you just have to declare these references:
import org.ow2.frascati.remote.introspection.RemoteScaDomain; import org.ow2.frascati.remote.introspection.Reconfiguration; import org.ow2.frascati.remote.introspection.Deployment; @Reference protected RemoteScaDomain introspection; @Reference protected Reconfiguration reconfiguration; @Reference protected Deployment deployment;
That's all! Have a look at the general usage section to learn about remote API.
From the Java world, there are several ways to send remote calls
to FraSCAti. For instance, you can use the URLConnection object of the
SDK or use third-party libraries like Jersey, Apache CXF, etc. We will
describe you how to set up and use CXF because it is really simple and
it offers you automatic marshalling/unmarshalling of parameters and
response. You need to add the cxf-rt-frontend-jaxrs
dependency (and its transitive dependencies if you don't use Maven) in
your project classpath.
<dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-frontend-jaxrs</artifactId> <version>2.4.0</version> </dependency>
Then, you need to create a dedicated JAX-RS client for the targeted service.
import org.ow2.frascati.remote.introspection.RemoteScaDomain; import org.ow2.frascati.remote.introspection.Reconfiguration; import org.ow2.frascati.remote.introspection.Deployment; private static final String BINDING_URI = "http://localhost:8090"; RemoteScaDomain introspection = JAXRSClientFactory.create(BINDING_URI+"/introspection", RemoteScaDomain.class); Reconfiguration reconfiguration = JAXRSClientFactory.create(BINDING_URI+"/reconfig", Reconfiguration.class); Deployment deployment = JAXRSClientFactory.create(BINDING_URI+"/deploy", Deployment.class);
The BINDING_URI
constant should point to the
targeted FraSCAti runtime, i.e. the hostname and the port number on
which FraSCAti services are running (ex:
http://myserver.mydomain.org:8080
).
Once references to FraSCAti remote services set-up, we are now able to do remote calls transparently through a simple Java interface. Here are some examples:
List composites loaded into the domain
import org.ow2.frascati.remote.introspection.resources.Component; for (Component c : introspection.getDomainComposites()) { System.out.println(c.toString()); }
Get information on a specific component
import org.ow2.frascati.remote.introspection.resources.Component; Component comp = introspection.getComponent("/org.ow2.frascati.FraSCAti/component-factory/factory"); System.out.println(comp);
Query with FraSCAti FScript
import org.ow2.frascati.remote.introspection.resources.Node; Collection<Node> result = reconfiguration.eval("$domain/scadescendant::component-factory/scaservice::*;"); System.out.println(result);
Reconfigure with FraSCAti FScript
import org.ow2.frascati.remote.introspection.resources.Node; Collection<Node> result; System.out.println("Before reconfiguration:"); result = reconfiguration.eval("$domain/scadescendant::services;"); System.out.println(result); reconfiguration.eval("set-state($domain/scadescendant::services, \"STOPPED\");"); System.out.println("After reconfiguration:"); result = reconfiguration.eval("$domain/scadescendant::services;"); System.out.println(result);
Deploy remotely an SCA contribution
import java.io.File; import java.io.IOException; import org.ow2.frascati.remote.introspection.FileUtil; String contrib = null; try { contrib = FileUtil.getStringFromFile(new File("src/test/resources/counter-contribution.zip")); } catch (IOException e) { System.err.println("Cannot read the contribution!"); } System.out.println("** Trying to deploy a contribution ..."); deployment.deploy(contrib); System.out.println("** Contribution deployed!");
Here, we first read a Contribution file on the file system, then convert the file content to a base64 encoded String to give it as argument to the deploy method.
The deploy method only takes SCA contribution files encoded as Base64 Strings. Have a look at the next section to know how to generate a contribution.