X

An Oracle SOA Suite - Team Blog

Customizing Oracle MFT File Rename Java Callout

Review and Use Case

So now that you have all digested the previous Overview blog in this Customizing MFT series, you fully understand the components of Custom Actions(callouts) and have built the Newline Conversion sample from the Oracle MFT Documentation section Processing Transfers with Custom Callouts you are ready to build another real world scenario that implements a file rename inside of the MFT engine. I say inside because that differentiates it from the use case where a file is written to the file system or remote FTP server and then renamed after it is delivered. This use case is a file comes in from a Source that has extra characters in the name such a time-stamp that you wish to remove using standard Java regular expressions. I.E. order20151011.xml would be renamed to order.xml or customer12345.csv is renamed to customer.csv.

Components

XML Config File

As you know, each of the callout actions needs an xml config file that defines the interface. Below is the config file for this sample that defines things like action name, library name, help text and parameters that are configured in the designer. This sample is used in Target Pre-Processing and takes 2 parameters, SourceExp and ReplaceWith. Details for configuring callout config files were covered in the last blog entry so go reread that if this isn't clear, To proceed, just download the file from the link below or copy the bolded text into your favorite text editor and save the file RenameRegexp.xml it into a new folder named RenameRegexp.

RenameRegexp.xml

<?xml version="1.0" encoding="UTF-8"?>
  <mft:Callouts xmlns:mft="http://xmlns.oracle.com/mft"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.oracle.com/mft callout.xsd ">
    <mft:Callout description="Rename Regexp"
      helpText="File Rename with Regular Expressions"
      groupName="Target-pre" timeout="300"
      implementationClass="com.oracle.callout.sample.RenameRegexp"
      libraryName="RenameRegexp.jar" name="RenameRegexp">
    <mft:Parameter description="SourceExp" mandatory="true"
      helpText="Source Regular Expression Pattern"
      name="SourceExp" parameterType="string"/>
    <mft:Parameter description="ReplaceWith" mandatory="true"
      helpText="ReplaceWith Regular Expression Pattern"
      name="ReplaceWith" parameterType="string"/>
   </mft:Callout>
</mft:Callouts>

Java Source Code

As you know, there are different API's for Source, Pre and Post Target callouts. This example illustrates the Pre Target interface that is not modifying the file contents like the Newline Conversion example did. To review this topic visit the MFT Documentation page titled Java Code Requirements and Tips. The java class receives 2 strings at runtime for regular expression substitution that are configured in the Designer Transfer artifact. The  new and interesting code being used here to get and set the new file name are the methods PluginContext.getTransformedInputFileName and PluginOutput.setNewFileName. The regular expression substitution is implemented with an old reliable single line of code using String.replaceAll. Below are a code snippets illustrating this.

   

PluginOutput out = new PluginOutput();

oldfname = pluginContext.getTransformedInputFileName() ;

newfname = getNewname(oldfname, SourceExp, ReplaceWith);

if (newfname != null && newfname.length() > 0) {

out.setNewFileName(newfname);

}

...
   public String getNewname(String oldfname, String srcexp, String repexp) {

return oldfname.replaceAll(srcexp, repexp);
}

Download the full RenameRegexp.java file and place it in a newly created child directory of your RenameRegexp folder called com/oracle/callout/sample.

Compile, Package and Install

This section assumes the following environment variables have been set.

  $MW_HOME where your MFT WebLogic is installed

  $JAVA_HOME where your JDK 1.7 is installed

Compile

From the command prompt, run the following command. The command is split to multiple lines.

  $ $JAVA_HOME/bin/javac

   -classpath $MW_HOME/mft/modules/oracle.mft_12.1.3.0/core-12.1.1.0.jar

   com/oracle/callout/sample/RenameRegexp.java

Package

Now use the JDK "jar" command to build a java jar file as shown below. The cmd is split to multiple lines.

  $ $JAVA_HOME/bin/jar

     cvf RenameRegexp.jar com/oracle/callout/sample/RenameRegexp.class

Install

This involves 2 steps, first copy the jar file to the MFT server callout folder and another to import the config xml file. Use the copy command shown below. Note the domain name below may be different in your environment.

  $ cp RenameRegexp.jar $MW_HOME/user_projects/domains/base_domain/mft/callouts/

NOTE: If you have an MFT export of a file transfer that uses this callout, you can import it and skip this WLST step.

Next, run a WLST command to import the RenameRegexp.xml file

  $ $MW_HOME/mft/common/bin/wlst.sh

Connect to your WebLogic Server using your host, port and admin credentials

  connect("weblogic", "<PASSWORD>, "t3://localhost:7901")

Enter the WLST CreateCallout command substituting the location of your files.

  createCallouts('/home/oracle/mft/callouts/RenameRegexp/RenameRegexp.xml')

Do a listCallouts() command to verify it worked.

  listCallouts()

Enter the exit() command to quit WLST.

  exit()

Deploy a Transfer and Test

This section requires some knowledge of Java Regular Expressions. This implementation uses the java String.replaceAll method. It is recommended to use an external tool test test your expressions in advance. There are many available but www.regexplanet.com has a nice online tool for testing your expressions before entering them into the MFT Designer. For this example, you will use following parameter values to rename a file order20151011.xml to order.xml.

  SourceExpr:  ([A-z])\d+(.*)

  ReplaceWith: $1$2

Design and Deploy

At this point, the RenameRegexp callout action will appear in your Transfers Target Pre-Processing UI and is ready to use in any Transfer. Go to the Designer and build a Transfer "Rename Transfer" with a Source and Target of "Rename FTP Source" and "Rename FTP Target". The FTP Embedded Source  uses a location of "/rename/in" and the FTP Remote Target uses a location of "/rename/out". The Transfer should look like the following screenshot.


You need to configure the Pre-Processing Action in the Transfer with the below parameters as shown in the screens below.

  SourceExpr:  ([A-z])\d+(.*)

  ReplaceWith: $1$2

Now Save and Deploy this Transfer as you would any other Oracle MFT Transfer.

Test

To test use any FTP Client to connect to the MFT embedded FTP server and
copying a file named  "order20151011.xml" to the "/rename/in" folder.
When completed, go to the MFT Monitoring console and view the transfer
report as shown below.


Congratulations, you have implemented a very handy, configurable and
reusable rename custom callout that you can use in any Transfer
Pre-Target Processor Action and it only took 76 lines of code! A
completed export of this Transfer is also  available for download

Be the first to comment

Comments ( 0 )
Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.Captcha