Entering content frame

This graphic is explained in the accompanying text File Transfer Service API Example Locate the document in its SAP Library structure

Purpose

This example is a class that uses the File Transfer Service API to create a remote file and upload it to a J2EE Engine server, and then to download a JAR file from a deployed application. The example also demonstrates the use of the com.sap.engine.services.deploy.DeployService interface from a standalone client. For more information, see Using DeployService Interface in Java Clients.

Creating the Example

The example consists several logical sections:

       1.      Import declarations.

The required imports for the File and the Deploy Service functions include the RemoteFile class and the FileTransfer interface, as well as the DeployService interface. 

Syntax documentation

import java.io.File;

import java.util.Properties;

 

import javax.naming.Context;

import javax.naming.InitialContext;

 

import com.sap.engine.services.deploy.DeployService;

import com.sap.engine.services.deploy.container.WarningException;

import com.sap.engine.services.file.FileTransfer;

import com.sap.engine.services.file.RemoteFile;

       2.      Variable declarations.

In the beginning of the class code, we declare any variables that are later initialized to particular values and passed as parameters to the methods.

Syntax documentation

public class FileTransferServiceExample {

 

  // provides path and file name for the server jar file to be downloaded

   private static String fileForDownload = null;

 

   private Context ctx;

 

  // provides upload() and download() methods

   private RemoteFile remoteFile;

 

  // sets protocol support for p4

   private final String support[] = { "p4" };

 

  // provides path and file name for user's EAR on local machine

   private final String earToDeploy = "D:\\TestEar.ear";

 

  // provides path and name for the downloaded jar file

   private final String downloadedFile = "D:\\result\\FileResult.jar";

 

   String serverEarName;

   String pathToTheService;

   FileTransfer transferer;

   DeployService deploy;

   Properties props;

   Properties ctxProp;

...

       3.      Constructor of the example class.

Here we need to initialize the declared variables, and to get references to the File Transfer Service. By retrieving the service interface, we can invoke their methods and implement the functions they provide.

Syntax documentation

  public FileTransferServiceExample() {

 

      ctx = null;

      remoteFile = null;

      serverEarName = "";

      pathToTheService = "./temp/deploy/work/deploying/";

      transferer = null;

      deploy = null;

      props = null;

 

    try {

         // properties for InitialContext

         ctxProp = new Properties();

         ctxProp.put(

            "java.naming.factory.initial",   "com.sap.engine.services.jndi.InitialContextFactoryImpl");

         ctxProp.put("java.naming.provider.url", "localhost:50004");

         ctxProp.put("java.naming.security.principal", "Administrator");

         ctxProp.put("java.naming.security.credentials", "sap");

         ctx = new InitialContext(ctxProp);

         props = new Properties();

 

         // set server EAR name

         serverEarName =

            pathToTheService + (new File("D:\\TestEar.ear")).getName();

 

         // get File Transfer Service reference

         transferer = (FileTransfer) ctx.lookup("file");

      } catch (Exception e) {

         System.out.println("Failed to look up the service: " + e);

         System.out.println("The example stops...");

         System.exit(0);

      }

   }

 

...

       4.      A process method.

This method first creates a remote file out of a local EAR file and uploads it to the server. Then it deploys the EAR using the DeployService interface. The method then downloads a client JAR file to the location specified as downloadedFilevariable.

Syntax documentation

public void process() {

    try {

     

      // creates the remote file

     remoteFile = transferer.createRemoteFile(earToDeploy, serverEarName);

 

      // uploads the file

     remoteFile.upload();

     

     // deployment

     deploy = (DeployService) ctx.lookup("deploy");

     

     try {

          // deploys file using DeployService

     deploy.deploy(serverEarName, support, props);

      } catch (WarningException we) {

        System.out.println("Warning: " + we.getMessage());

      }

     

     // download of the uploaded file

       fileForDownload = "./apps/sap.com/TestEar/EJBContainer/clientjars/clientTest.jar";

     try {    

        // creates remote file

        remoteFile = transferer.createRemoteFile(downloadedFile, fileForDownload);

        

       // downloads the file

       remoteFile.download();

     } catch (Exception e) {

     System.out.println("Cannot complete the download process: " + e);

     }

     

    } catch (Exception e) {

      System.out.println("Check if TestEar application has been started. The example failed.: " + e);

    }

  }

       5.      The main method of the class.

It initializes the example class and invokes the process method.

Syntax documentation

public static void main(String[] args) {

      FileTransferServiceExample ftsExample = new FileTransferServiceExample();

      ftsExample.process();

   }

}

 

 

Leaving content frame