## Apache Synapse

## Sample 8: Introduction to Static and Dynamic Registry Resources, and Using XSLT Transformations

<definitions xmlns="http://ws.apache.org/ns/synapse"> <!-- the SimpleURLRegistry allows access to a URL based registry (e.g. file:/// or http://) --> <registry provider="org.apache.synapse.registry.url.SimpleURLRegistry"> <!-- the root property of the simple URL registry helps resolve a resource URL as root + key --> <parameter name="root">file:repository/conf/sample/resources/</parameter> <!-- all resources loaded from the URL registry would be cached for this number of milli seconds --> <parameter name="cachableDuration">15000</parameter> </registry> <!-- define the request processing XSLT resource as a static URL source --> <localEntry key="xslt-key-req" src="file:repository/conf/sample/resources/transform/transform.xslt"/> <sequence name="main"> <in> <!-- transform the custom quote request into a standard quote requst expected by the service --> <xslt key="xslt-key-req"/> </in> <out> <!-- transform the standard response back into the custom format the client expects --> <!-- the key is looked up in the remote registry and loaded as a 'dynamic' registry resource --> <xslt key="transform/transform_back.xslt"/> </out> <send/> </sequence> </definitions>

### Objective

Demonstrating the usage of the XSLT mediator for transforming message content and using local registry and remote registry for storing configuration metadata.

### Pre-requisites

- Deploy the SimpleStockQuoteService in the sample Axis2 server and start Axis2
- Start Synapse using the configuration numbered 8 (repository/conf/sample/synapse_sample_8.xml)
  Unix/Linux: sh synapse.sh -sample 8
  Windows: synapse.bat -sample 8

### Executing the Client

This example uses the XSLT mediator to perform transformations, and the xslt transformations are specified as registry resources. The first resource 'xslt-key-req' is specified as a 'local' registry entry. Local entries do not place the resource on the registry, but simply make it available to the local configuration. If a local entry is defined with a key that already exists in the remote registry, the local entry will get higher precedence over the remote resource.

In this example you will notice the new 'registry' definition. Synapse comes with a simple URL based registry implementation (SimpleURLRegistry). During initialization of the registry, the SimpleURLRegistry expects to find a property named 'root', which specifies a prefix for the registry keys used later. When the SimpleURLRegistry is used, this root is prefixed to the entry keys to form the complete URL of the resource being looked up. The registry caches a resource once requested, and stores it internally for a specified duration. Once this period expires, it will reload the meta information about the resource and reloads its cached copy if necessary, the next time the resource is requested.

Hence the second XSLT resource key 'transform/transform_back.xslt' concatenated with the 'root' of the SimpleURLRegistry 'file:repository/conf/sample/resources/' forms the complete URL of the resource as 'file:repository/conf/sample/resources/transform/transform_back.xslt' and caches its value for a period of 15000 ms.

Execute the custom quote client as follows and analyze the the Synapse debug log output.

ant stockquote -Daddurl=http://localhost:9000/services/SimpleStockQuoteService -Dtrpurl=http://localhost:8280/ -Dmode=customquote

The incoming message is transformed into a standard stock quote request by the XSLT mediator. The XSLT mediator uses Xalan-J to perform the transformations. It is possible to configure the underlying transformation engine using properties when necessary. The response from the SimpleStockQuoteService is converted back into the custom format as expected by the client during the out message processing.

During the response processing you could see the SimpleURLRegistry fetching the resource as shown by the log message below.

[HttpClientWorker-1] DEBUG SimpleURLRegistry ==> Repository fetch of resource with key : transform/transform_back.xslt

If you run the client again immediately (i.e within 15 seconds of the first request) you will not see the resource being reloaded by the registry as the cached value would be still valid.

However if you leave the system idle for 15 seconds or more and then retry the same request, you will now notice that the registry notices the cached resource has expired and will reload the meta information about the resource to check if the resource has changed and will require a fresh fetch from the source URL. If the meta data / version number indicates that a reload of the cached resource is not necessary (i.e. unless the resource itself actually changed) the updated meta information is used and the cache lease extended as appropriate.

[HttpClientWorker-1] DEBUG AbstractRegistry - Cached object has expired for key : transform/transform_back.xslt [HttpClientWorker-1] DEBUG SimpleURLRegistry - Perform RegistryEntry lookup for key : transform/transform_back.xslt [HttpClientWorker-1] DEBUG AbstractRegistry - Expired version number is same as current version in registry [HttpClientWorker-1] DEBUG AbstractRegistry - Renew cache lease for another 15s

Thus the SimpleURLRegistry allows resource to be cached, and updates are detected so that the configuration changes could be reloaded without restarting the Synapse instance.

Back to Catalog