Jump to: navigation, search

Difference between revisions of "Lyo/TRSReferenceApplication"

< Lyo
Line 59: Line 59:
 
<br>
 
<br>
  
=== Example Implementation (2.0 only)<br> ===
+
=== Example Implementation (2.0 only)<br> ===
  
#Open the file '''org.eclipse.lyo.rio.trs.resources.TRSResource.java''' file.<br>
+
#Open the file '''org.eclipse.lyo.rio.trs.util.TRSObject.java''' file.<br>  
#Navigate to the '''loadChangeEvents''' method and read the method comment.<br>
+
#Navigate to the '''loadChangeEvents''' method and read the method comment.<br>  
#The method starts by making a call to '''FileUtil.''''''fileload''' which loads any existing change events from file into memory.&nbsp; 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. <br>
+
#The method starts by making a call to '''FileUtil.'''''fileload''' which loads any existing change events from file into memory.&nbsp; 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. <br>  
#The first for loop examines all change events.&nbsp; Calls to '''isPrunningNecessary'''() determine if the current change event being processed should be removed due to its age.&nbsp; 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.<br>
+
#The first for loop examines all change events and sorts them according to their order property.&nbsp; The second for loop makes&nbsp; calls to '''isPrunningNecessary'''() to determine if the current change event being processed should be removed due to its age.&nbsp; 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.<br>  
#Once the events are pruned the method loads the remaining events into memory and saves the pruned list back to disk.<br>
+
#If events are pruned, the method saves the pruned list back to disk.<br>  
#Navigate to the '''insertEventTypeToChangeLog''' method in the '''TRSResource.java&nbsp;'''file.<br>
+
#Navigate to the '''initialize'''() method and jump to the bottom of the method.&nbsp; Note the call to '''setcutOffEventInner '''and its corresponding comment.&nbsp; This call updates the cutoff property of the base resource, allowing existing clients a mechanism for quickly determing what data the change log contains.&nbsp; The server is now initialized.
 +
#After initialization the reference application continues to maintain the change log.&nbsp; Navigate to the '''insertEventTypeToChangeLog''' method in the '''TRSResource.java&nbsp;'''file.<br>  
 
#This method is called whenever a new change event is created in order to insert it into the change log.&nbsp;  
 
#This method is called whenever a new change event is created in order to insert it into the change log.&nbsp;  
 
#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.
 
#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.

Revision as of 13:18, 23 July 2013

 The Guided Tour walks you through the following tasks:

Examine how the TRS toolkit helps create a TRS provider via JAX-RS

  1. 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.BaseResource.png
  2. 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.
  3. 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.
  4. 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.
  5. Open the file org.eclipse.lyo.rio.trs.resources.TRSResource.java
  6. In similar fashion it also has an @Path annotation that identifies the root of the RESTful service for the Tracked Resource Set.
  7. 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.
  8. The other method in this class, getChangeLog(page), returns the segmented portion of the change as requested by the HTTP request


Trying it out

  1. Ensure that the reference application is running. If it is not, in the Project Explorer view, in the org.eclipse.lyo.rio.trs project, right-click Launch TRS Reference App.launch and click Run As -> Launch TRS Reference App.
  2. The TRS Reference Application includes an index.html file that contains links to the various capabilities provided by the application. To open the page, point your browser at http://<hostname>:8082/org.eclipse.lyo.rio.trs (e.g. http://localhost:8082/org.eclipse.lyo.rio.trs)
  3. The first section of the page provides links to the Base and Tracked Resource Set as implemented by the BaseResource and TRSResource classes we observed earlier. Navigate to the Base Resources link. You'll see the Base Resources document in XML form. The format and syntax has been handled by the TRS toolkit's annotated beans and the OSLC4J serializers.
  4. Navigate to the Tracked Resource Set link. The change log contained in the Tracked Resource Set should contain a rdf:nil to represent no changes have been made to the resources.
  5. Return to the index page and click Create Change Request.
  6. Type in a title and a description, and then click Submit Bug. Make note of the change request number.
  7. Go back to the Tracked Resource Set page and refresh if necessary. Notice an entry in the change log section of the Tracked Resource Set containing a Creation event. If you repeat the creation event more than 3 times, you'll notice the change log being segmented with a previous element containing a URI to return the previous segment of the change log.


Examine how the TRS toolkit helps create a TRS provider via generic Servlet

  1. Open the file org.eclipse.lyo.rio.trs/src/main/webapp/WEB-INF/web.xml. Notice the servlet mapping for the relative path /restx/trs/base/. This maps the Base resource RESTful endpoint to the servlet class org.eclipse.lyo.rio.trs.servlet.BaseGeneric.
  2. Open the file org.eclipse.lyo.rio.trs.servlet.BaseGeneric. It has an overridden doGet method, which implements the root of the RESTful service for the Base Resource.
  3. 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. If the path for the Get request contains the page number, it returns the response corresponding to the Base resource at the given page. In a simple servlet implementation, we serialize the Base resource to the response stream using OSLC4JMarshaller. 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 various other media types based on the HTTP request.
  4. Similary, the file web.xml also contains the servlet mapping for two other endpoints for Tracked Resource Set and Change Log.
  5. The servlet mapping for the relative path /restx/trs/ maps the Tracked Resource Set RESTful endpoint to the servlet class org.eclipse.lyo.rio.trs.servlet.TRSGeneric.
  6. Open the file org.eclipse.lyo.rio.trs.servlet.TRSGeneric. It has an overridden doGet method, which implements the root of the RESTful service for the Tracked Resource Set.
  7. The reference application is utilizing the segmented Changelog concept from the TRS Specification. The method toGet() populates a TrackedResourceSet (a simple java bean provided by the toolkit for representing a tracked resource set) with the most recent segment of the changelog. It then serializes the TrackedResourceSet object to the response stream using OSLC4JMarshaller.
  8. In a similar fashion, the Changelog endpoint, which is mapped to the the relative path /restx/trs/changelog/, is implemented in the class org.eclipse.lyo.rio.trs.servlet.ChangeLogGeneric.java.


Trying it out

  1. Ensure that the reference application is running. If it is not, in the Project Explorer view, in the org.eclipse.lyo.rio.trs project, right-click Launch TRS Reference App.launch and click Run As -> Launch TRS Reference App.
  2. The TRS Reference Application includes an index.html file that contains links to the various capabilities provided by the application. To open the page, point your browser at http://<hostname>:8082/org.eclipse.lyo.rio.trs (e.g. http://localhost:8082/org.eclipse.lyo.rio.trs)
  3. The first section of the page provides 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.
  4. Navigate to the Generic Tracked Resource Set link. The change log contained in the Tracked Resource Set should contain an rdf:nil entry to represent no changes have been made to the resources.
  5. Return to the index page and click Create Change Request.
  6. Type in a title and a description, and then click Submit Bug. Make note of the change request number.
  7. Go back to the Tracked Resource Set page and refresh if necessary. Notice an entry in the change log section of the Tracked Resource Set containing a Creation event. If you repeat the creation event more than 3 times, you'll notice the change log being segmented with a previous element containing a URI to return the previous segment of the change log.


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.


Example Implementation (2.0 only)

  1. Open the file org.eclipse.lyo.rio.trs.util.TRSObject.java file.
  2. Navigate to the loadChangeEvents method and read the method comment.
  3. 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.
  4. 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.
  5. If events are pruned, the method saves the pruned list back to disk.
  6. 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 cutoff property of the base resource, allowing existing clients a mechanism for quickly determing what data the change log contains.  The server is now initialized.
  7. After initialization the reference application continues to maintain the change log.  Navigate to the insertEventTypeToChangeLog method in the TRSResource.java file.
  8. This method is called whenever a new change event is created in order to insert it into the change log. 
  9. 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.


Run the TRS compliance test suite to validate the TRS Reference Application's TRS provider

  1. 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.
  2. Edit the values to point to the location of the TRS Reference Application that you are running.
  3. 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.