Wednesday 23 November 2011

Dynamically passing file name,directory and file content to a file adapter


In this post, I am going to show the steps for passing name,directory and content of a file at run time to a file adapter.

The first approach is implemented using a mediator.The steps are discussed below:-

First,create a new application and a project.
Now add a file adapter service to your composite.Press next and specify a name for the file adapter service and press next.



In the next screen, select Define from operation and schema(specified later) option.Press next.



In the next screen, select Write file operation.




In the next screen ,choose physical path.Alternatively you could have chosen logical path also.If you specify a logical path, then the process will differ slightly, which is discussed in the section "Using a logical path in the File Adapter Configuration" below.In case of physical path, this value is going to be overridden by a value.
In case of a logical path, a property(a variable) will be created for the file adapter which we will have to populate before invoking the file adapter service.We can specify a static value or an expression for populating the property.Notice that we can't specify logical file name.Just input a name.We are going to override the file name too by the value specified by us.





Next choose the schema element.Click the magnifying glass icon on the next screen that appears.





Choose the element that you want to use as file content.The source for the schema file that I used is given below.

FileDetails.xsd
---------------------------------------------------

<?xml version="1.0" encoding="windows-1252" ?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            xmlns:tns="http://www.dynamicfilewriting/filedetail"
            targetNamespace="http://www.dynamicfilewriting/filedetail"
            elementFormDefault="qualified">

  <xsd:element name="file_details" type="tns:file_details_type"/>
  <xsd:element name="file_content" type="xsd:string"/>
  <xsd:complexType name="file_details_type">
    <xsd:sequence>
      <xsd:element name="file_name" type="xsd:string"/>
      <xsd:element name="file_directory" type="xsd:string"/>
      <xsd:element name="content" type="xsd:string"/>
    </xsd:sequence>
  </xsd:complexType>
</xsd:schema>


Take a moment to investigate the source.I have two root level elements:- file_details which is based on a complex type and the file_content which is a string element.The file_content element is used by the file adapter as the content of the file.The file_details element comprises three elements:- file_name i.e. the name of the file, file_directory i.e the the name of the directory where the file is to be written and the contents of the file.The mediator we expose uses the file_details element.

We shall choose the file_content element from the FileDetails.xsd




Hit ok and then hit next.Click finish to close the file adapter configuration wizard.Now drag and drop a mediator to your composite.Choose a name and select one way interface.Make sure to expose the mediator through a web service binding.Select the file_details element as the input.




Wire the mediator service to the File Adapter Service.Your composite should look like the following figure:-


Now we shall assign the file name and file paths to the file adapter service.Double click to open the mplan.We should have only one routing rule.Click on the assign button(marked by the red circle in the follwing figure).




In the window that comes up, click on the green plus icon to add an assign activity.






In the from action of the assign dialog, select expression from the drop down dialog.Click on the button that is highlighted in the image below to bring up the expression builder.



In the expression builder, select the file_name element under the request part of the 'in' variable and then hit the 'Insert into Expression' button.


Hit Ok.Then on the To side of the assign dialog, select property from the drop down list if not already selected and then choose the jca.file.FileName property.




Hit ok.Again add another assign activity.Follow the same steps.On the From side of the assign dialog, select the file_directory element and on the To side, select jca.file.Directory property.







Hit Ok twice to come back to the mediator mplan.Now add a transform activity to populate the file contents.Click on the transform button as highlighted in the following image.




In the new window that pops up, select "Create new mapper file" radio button and provide a name for the xsl file.Hit ok.Create the follwing mapping as shown in the following figure.Connect the content from the left hand side to the file_content on the right hand side.


So we are done.Deploy your composite, go the EM console and test.Provide a file name, a valid location and the contents of the file and invoke the mediator.If all goes well, the file with the given name,content will be written.at the provided location.

Using a logical path in the File Adapter Configuration:


   If you choose a logical path, then a property will be created for the adapter.You can see the property when you select the database adapter and go to properties inspector;the property should be visible in the property section;you should provide a value which we shall override in the same way as explained above.


The rest of the steps are the same.Go ahead and deploy the service.
We can achieve this same thing using BPEL. However, I guess you can figure that out once you're done with this tutorial.Hope this helps.

No comments:

Post a Comment

xslt padding with characters call template for left pad and right pad

  Could a call-template be written that took two parameters ?   a string, and a   number) return the string with empty spaces appended t...