Difference between revisions of "Lyo/TRSReferenceApplication"
From Eclipsepedia
Latest revision as of 15:52, 25 November 2013
The Guided Tour walks you through the following tasks:
[edit] Examine how the TRS toolkit helps create a TRS provider via JAX-RS
- Open the file org.eclipse.lyo.rio.trs.resources.BaseResource.java. Notice @Path("/trs/"+TRSConstants.TRS_TERM_BASE) at the top of the class. This is a JAX-RS annotation that assigns a relative path (from the jax-rs servlet context) for the Base Resource RESTful endpoint. There are various constants and vocabularies defined in the toolkit to abstract the terms and namespaces from TRS provider implementers.
- In the reference application, the Base Resource is paged so that, according to the specification, a GET on the base resources will do a redirect to the first page of base resources. This is what happens in the getBase() endpoint.
- The other method, getBasePage(page), returns the Base resource at the given page. The class org.eclipse.lyo.core.trs.Base (and org.eclipse.lyo.core.trs.Page in the 2.0 release) is provided by the TRS toolkit. It is basically a simple java bean that has been annotated with various OSLC4J annotations to allow the OSLC4J marshalling to create the proper RDF document for the JAX-RS configured serialization.
- Both methods have additional annotations such as @GET, which identifies the type of HTTP traffic it can handle, as well as an @Produces that identifies the return type the method can handle. In all cases, the REST endpoints can return Turtle, RDF XML, XML, or JSON. JAX-RS dynamically returns the correct format based on the HTTP request that is received.
- Open the file org.eclipse.lyo.rio.trs.resources.TRSResource.java
- In similar fashion it also has an @Path annotation that identifies the root of the RESTful service for the Tracked Resource Set.
- The reference application utilizes the segmented Changelog concept from the TRS Specification. The method getTrackedResourceSet() returns a tracked resource set with the most recent segment of the changelog.
- The other method in this class, getChangeLog(page), returns the segmented portion of the change as requested by the HTTP request
 org.eclipse.lyo.rio. All implementations of the Base end point handles RDF XML and the 2.0 version adds support for text/turtle. The reference application can be extended to support.
 link "Generic Base Resources" to the Base and "Generic Tracked Resource Set" to Tracked Resource Set as implemented by the BaseGeneric and TRSGeneric classes we observed earlier. Navigate to the Generic Base Resources link. You'll see the Base Resources document in XML form.
- Navigate to the Generic Tracked Resource Set link. The change log contained in the Tracked Resource Set should contain an rdf:nil entry] Change Log Considerations
[edit] Generating Change Log Events
There are several ways to generate the change events that populate the change log. If accessing the resource server's code is possible then it may be easiest to simply alter the code where resources are created, modified, and deleted. The reference application does this in certain cases. For example, open the ChangeRequestResource.java file in the org.eclipse.lyo.rio.trs.resources package and browse to the deleteChangeRequest() method. This method handles deleting a resource as well as updating the change log. The method call to insertEventTypeToChangeLog handles creating a deletion event and inserting it into the change log.
Another, perhaps cleaner way, to achieve the same result is to use the Java listener pattern. Open the ChangeRequestListener.java file in the org.eclipse.lyo.rio.trs.cm package. A new class can implement this ChangeRequestListener interface and override the changeRequestAltered method to be alerted to alterations of underlying resources. For example, open the Persistence.java class in the org.eclipse.lyo.rio.trs package and look at the addListner() method. The newly created listener can be registered with a method such as this. Then when alterations occur the listeners can be updated. Navigate to the addChangeRequest() method and notice that after several operations are performed to create a new resource and save it to disk that the notifyListener method is called. notifyListener will call all registered listeners and they can react how they wish to the resource alterations. In this case a change event could be created and inserted into the change log.
A final approach, which is often useful if direct access to the resource server's code is not available is a polling mechanism. The server with the TRS endpoint can periodically poll the resource server for changes and then generate change events based on what it discovers between polling intervals. It should be noted that this approach assumes the resource server has a query mechanism of some sort. Using a timestamp query parameter if available is recommended.
[edit] When to Generate Change Log Events
There are three types of change events: creation, modification, and deletion. Creation and deletion events are fairly straightforward, they correspond to when an underlying resource is created or deleted respectively. When to create modification events is not as clear. The server should not report unnecessary change events although it might happen, for example, if changes occur while the base is being computed. If a server knows that a resource will change twice, it can safely emit a single change event to cover both. If a server knows that it will be changing a set of resources, it can safely batch up the change events until it has changed all of them.
[edit] Persisting and Pruning the Change Log
The change log keeps track of recent modifications to change request resources. When a resource is created, modified, or deleted a change event is created. It is recommended that a resource server persist these events so that the change log may be restored if the resource server needs to be restarted. In order to keep the change log from growing without bounds, it should be pruned periodically.
Both persisting the change log, and pruning it, are considered best practices as they allow consumers to optimize updates. Instead of examining the entire base for details, consumers can processes a smaller set of data in the change log. Persisting the change log allows this optimization to take place even across server resets. Pruning the log allows a system administrator to control how far back in time consumers may read events in the change log before having to parse the base resource. A history of seven days is recommended as a starting point. However, the administrator should choose a prune time based on how frequently change events are created and how far back in time consumers typically need to search in order to stay in synch with the resource server.
Once both the Base and Change Log resources are initialized the cutoffEvent property should be set in the Base resource. The cutoffEvent property consists of a URL which points to the most recent change event that exists in both the Base and Change Log. Clients can examine this property to quickly determine what change events exist in the Change Log.
[edit] Example Implementation (2.0 only)
- Open the file org.eclipse.lyo.rio.trs.util.TRSObject.java file.
- Navigate to the loadChangeEvents method and read the method comment.
- The method starts by making a call to FileUtil.fileload which loads any existing change events from file into memory. It should be noted that in a production environment any calls to the FileUtil class would likely be replaced with calls to a true database.
- The first for loop examines all change events and sorts them according to their order property. The second for loop makes calls to isPrunningNecessary() to determine if the current change event being processed should be removed due to its age. The reference application only performs pruning at server startup. In a production system, where a server may remaining for multiple days or weeks without reset, pruning should take place more frequently.
- If events are pruned, the method saves the pruned list back to disk.
- Navigate to the initialize() method and jump to the bottom of the method. Note the call to setcutOffEventInner and its corresponding comment. This call updates the cutoffEvent property of the base resource, allowing existing clients a mechanism for quickly determing what data the change log contains. The server is now initialized.
- After initialization the reference application continues to maintain the change log. Navigate to the insertEventTypeToChangeLog method in the TRSResource.java file.
- This method is called whenever a new change event is created in order to insert it into the change log.
- Note at the bottom of the file how the call to FileUtil.save persists the new change event so it is available if the server is restarted.
[edit] Run the TRS compliance test suite to validate the TRS Reference Application's TRS provider
- In the Project Explorer view, expand the org.eclipse.lyo.testsuite.trs project and navigate to the src/main/resources/config.properties file. The configTrsEndpoint property specifies the location of the TRS resource to be validated by the compliance test suite. The tests also examine the base resource, which is discovered by examining the TRS' trs:base property.
- Edit the values to point to the location of the TRS Reference Application that you are running.
- In the org.eclipse.lyo.testsuite.trs project, right-click Launch All JUnits.launch and click Run As -> Launch All JUnits.
The TRS compliance JUnit test suite runs and validates the contents of the Base Resources and the Tracked Resource Set of the reference app. The results of the JUnit test are displayed in the JUnit view.
It should be noted that additional tests exist in the Rio repository. These can be examined by loading the org.eclipse.lyo.rio.trs.tests project in the same way the org.eclipse.lyo.rio.trs was loaded. To run these tests, start the reference application and then navigate to org.eclipse.lyo.rio.trs.tests/src/main/resources and open the config.properties file. This file controls the content and headers that are sent to the resource server, which is the reference application by default. To run these tests again your own server, alter the contents of this file using the comments in the file for reference. A launch file exists in the org.eclipse.lyo.rio.trs.tests project to assist in running the tests.
Return to the main TRS SDK page.