Notice: This Wiki is now read only and edits are no longer possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.
Difference between revisions of "Gyrex/Learning Material/Develop JAX-RS"
(→Requirements) |
|||
Line 6: | Line 6: | ||
OSGi Declarative Services requires a target plattform has been set up as well as Java 6 runtime environment. <br> | OSGi Declarative Services requires a target plattform has been set up as well as Java 6 runtime environment. <br> | ||
− | It is recommend to work through the tutorial [http://http://wiki.eclipse.org/Gyrex/Develop_OSGi_DS ''Develop OSGi Declarative Service''] first. | + | It is recommend to work through the tutorial [http://http://wiki.eclipse.org/Gyrex/Develop_OSGi_DS ''Develop OSGi Declarative Service''] first. Furthermore you need a few more bundles registeres in your target platform, in order to do this work through the tutorial [http://wiki.eclipse.org/Gyrex/Install_JAXRS_AddOn ''Install JAXRS AddOn for Gyrex'']. |
− | ''This tutorial was created using eclipse Indigo Service Release 2.'' <br> | + | ''This tutorial was created using eclipse Indigo Service Release 2.'' <br> |
=== Tutorial === | === Tutorial === |
Revision as of 10:10, 15 June 2012
Gyrex | |
Website | |
Download | |
Community | |
Mailing List • Forums • IRC • mattermost | |
Issues | |
Open • Help Wanted • Bug Day | |
Contribute | |
Browse Source • Project Set File |
This tutorial guides you throug the development of JAX-RS application using Gyrex. In the tutorial Develop OSGi Declarative Service a GreetingService was implemented which will be used now. As it was said in the tutorial Devlop OSGi DS an interface will be separated from the re-implementations. Therefore the interface GreetingService will be removed from the sample.service and now placed in a new bundle sample.jaxrs. Both bundles with all containing manifests and classes you can find here (zip-file).
Requirements
OSGi Declarative Services requires a target plattform has been set up as well as Java 6 runtime environment.
It is recommend to work through the tutorial Develop OSGi Declarative Service first. Furthermore you need a few more bundles registeres in your target platform, in order to do this work through the tutorial Install JAXRS AddOn for Gyrex.
This tutorial was created using eclipse Indigo Service Release 2.
Tutorial
Create a new plug-in project and name it like "sample.jaxrs". As target platform we will use an "Equinox" OSGi framework. The option for an Activator you can deselect, we don't need it. A template we don't need, either. To import required packages open the MANIFEST.MF and switch to the register MANIFEST.MF and add the following lines:
Import-Package: javax.inject;version="[1.0.0,2.0.0)", javax.ws.rs;version="[1.1.0,2.0.0)", javax.ws.rs.core;version="[1.1.0,2.0.0)", org.apache.commons.lang;version="[2.4.0,3.0.0)", org.apache.commons.lang.exception;version="[2.4.0,3.0.0)", org.eclipse.gyrex.http.application.provider;version="[1.0.0,2.0.0)", org.eclipse.gyrex.http.jaxrs;version="[1.0.0,2.0.0)"
This line has to be added, too:
Bundle-ActivationPolicy: lazy
Remember the space line at the bottom of the text.
- javax.ws.rs
- org.eclipse.gyrex.http
- org.eclipse.gyrex.http.jersey
- org.apache.commons.lang
It's time to create a new folder in your project with he name "OSGI-INF". In this folder add a new OSGi DS component (Plug-In Development -> Component Definition) called "jaxrs-application.xml" with this properties:
- parnet folder: sample.jaxrs/OSGI-INF
- filename: jaxrs-application.xml
- name: sample.jaxrs.application.component
- class: org.eclipse.gyrex.http.jaxrs.JaxRsApplicationProviderComponent
After creating this file open it and set following parameters:
- Activate: activate
- Deactivate: deactivate
- Provided Service: org.eclipse.gyrex.http.application.provider.ApplicationProvider
- Property: "service.description" type="String" value="JAX-RS Application"
At the end you have to say your MANIFEST.MF that there is a service-component. Open the MANIFEST.MF and switch to the register MANIFEST.MF and add following line if it isn't allready done:
Service Component: OSGI-INF/jaxrs-application.xml
In this bundle sample.jaxrs the provided interface GreetingService will be placed. The service you implemented in the tutorial Develop OSGi Declarative Service will use it. Therfore the package sample.service has to be created containig the GreetingService.java. You implemented it in the last tutorial, so just copy it from there and paste it in this package. To inform the framework that there is such a interface add in the MANIFEST.MF (register MANIFEST.MF) the line:
Export-Package: sample.service
Sample.service has to be the name of the package containing the interface.
Assuming you use the service bundle from last tutorial now you have to make changes there, too. First delete the interface GreetingService, we placed it allready in the sample.jaxrs bundle. Now open the MANIFEST.MF of this sample_imp.service bundle (the name was changed to sample_imp.service to higlight the different bundles). Add following lines:
Export-Package: sample_imp.service Require-Bundle: sample.jaxrs
Exporting the package you provide the implementation of the GreetingService interface. This is a re-implementation of an interface so the interface-implementation is required. Where it is placed tells the Require-Bundle line. Note that sample.jaxrs is the hole bundle you have implemented at the beginning.
In this example we still need another interface to get information about the client (node) who is running the application right now. Open the greeting-service.xml and switch to register Source. Between the tags <scr: ...> and </scr:component> add following line:
<reference bind="setEnvironment" cardinality="1..1" interface="org.gyrex.cloud.environment.INodeEnvironment" name="INodeEnvironment" policy="static"/>
This is a reference to a service with the interface INodeEnvironment. Only one instance of this service may be assigned to the sample.service component and the instance is mendatory (cardinality=1..1). With the methode setEnvironment, implemented in GreetingServiceComponent our component calls this interface. If you open the GreetingServiceComponent, you see that the setEnvironment mehtod calls the getNodeID() method of the interface, returning the current nodeid. By the way, this is how you call and use interfaces in a OSGi framework with declarative services.
In this tutorial we just want to use our interface in the same bundle. To do this create a new package in the sample.jaxrs bundle. We will name it sample.jaxrs. This application at the end shows "greetings". In order to do this we need the two classes HelloResource and GreetingResource. HelloResource will be started first when you start this application later caused by the @path parameter. It just says hello to you and tells you the current time. When you expand the url-path the GreetingResource will be startet to. There you have the possibility to "send" greetings. Of course with this tutorial it's enough just to show the greetings sorted by time of post. The implementation of this classes you will find in the uploaded file-system.
With finishing this implementation you are able to start the server and create an instance of this application to mount to an URL. Either you use the debug configurations from the tutorial Develope OSGi Declarative Services or you have to setup a new one. To setup a new one right-click on your project and choose Debug As -> Debug Configurations... . The next right-click on Eclipse Application -> New opens the page to set up the configuration. In the box Name type in a name. In the section Program to Run select Run an application and org.eclipse.gyrex.boot.server. In the register Argmuents add "-console" in the frame Program arguments. With clicks on Apply and Debug the server will start. In the console view the known commands to check the components states( 'ss'+name, 'start'+id, 'stop'+id, 'ls' ) are available.
Now you should be able to create a JAX-RS application. It should be possible to create new services an bind them with an interface, too.