Coding a Sample Client application

Run this client to get a Connection by an implementer of one of the supported protocols (SOAP, FTP) and process any files that need to move across the connection. Elements stored in an XML file control details of the Connection as well as details in the file systems on the computers exchanging files. When run, the client program does the following:
  1. parses the XML input file
  2. gets Network connection of the type specified by an entry in the property file
  3. locates file system directories on the Source Computer and on Target Computer
  4. inspects files on the Source Computer
  5. identifies a Collection of files matching a name pattern and younger than LastFile
  6. copies the files in the Collection from Source to Target
  7. updates XML output file with properties of LastFile that was copied
  8. repeat steps 3 thru 7 on the next directories if indicated by the XML content
  9. repeat steps 2 thru 8 for the next computer connection if necessary

Listing 1: A Java Client to move files on an FTP Connection


  1	public static void main(java.lang.String[] args) {
 	
  2      FileExchangeConnection conn;       

  3	        System.out.println("prop reset : "+ System.getProperty("user.dir"));
  4	        try{
  5             XmlMgr xmlmgr = new XmlMgr();
  6             InputStream in = FtpProperty.class.getResource(
  7                  FtpProperty.get("ftpInFile")).openStream();    
  8	        xmlmgr.buildTrees(in);
  9	        List exSessionList = xmlmgr.getFtpwork().getExchangeSession();
  10	        ExchangeSession entry;
  11	        Connectable mgr;
  12		for (ListIterator i = exSessionList.listIterator(); i.hasNext();) {
  13		    entry = (ExchangeSession)i.next();
  14	            Class veriable = Class.forName(FtpProperty.get("connectionClass"));
  15	            mgr = (Connectable) veriable.newInstance();
  16 	            conn = mgr.newConnection(entry);
  17	            mgr.fileAssociationEntries(conn);
  18	            conn.disconnect();
	        }     // end forloop
  19	        xmlmgr.validateTrees();
  20	        FileOutputStream fMOut = new FileOutputStream(
  21                  (new File(
  22                      System.getProperty( "user.dir"),                           
  23                      FtpProperty.get("ftpOutFile")
                )));
  24            xmlmgr.marshalTrees(fMOut);
  25            fMOut.close();  
	        }      
	        catch (SecurityException se){
	            System.out.println("error in Client cls " +se);}
	        catch (Exception e){
	                System.out.println("error in Client cls " +e);
	            }

Detailed Explanation of Sample Client Code

  1. Runs as application with no arguments
  2. Class Declaration
  3. Simple checking
  4. Main try block for Client
  5. xmlmgr New instance. This class wraps the JAXB API and handles responsibility for parsing XML, instantiating run-time objects from the XML elements, updating the run-time tree with properties and state that may change as the file exchange progresses. At the end, it wraps the creation of a new version of the XML file that reflects what happened in the run (LastFile Element is updated).
  6. Collaborate with an entry in the properties file and Create an input stream on the file
  7. FtpProperty provides "getters" to specific entries in the properties file
  8. BuildTrees is standard Java XML operation according to the rules for the JAXB API. This is how JAXB parses XML. The byproduct of the parse is instantiation of all classes represented by XML elements.
  9. Retrieve from parsed objects the ExchangeSession as List.
  10. Declare the type that each list entry should be "cast" to.
  11. Interface Declaration ( Connectable )
  12. Iterator for each ExchangeSession instance in the List.
  13. Get Next from the List.
  14. From the properties file, get the plug-in Class used for an implementation of the Connectable Interface. SOAP/HTTP or FTP are the 2 options currently. To the client, both protocols are incapsulated inside different implementations of the same interface and the client sees no difference. FTP option is implemented in class com.borneo.ftp.FtpConnectionMgr. However, anything implementing "Connectable" can be used without recompiling any code.
  15. Get a new instance of the class described above an Caste it as the interface in use for connections.
  16. There is alot here. This one gets a new connection, over the protocol implemented in the class chosen in the properties file, passing in the entry of ExchangeSession instance whose properties describe both the attributes used to connect (ip, user, password) and the work that will need to be done over the connection. The connection that is returned, is an implementation of another interface covering the operations that occur over the connection. At this point, the connection knows the work that it has to process. SOAP connection properties are currently hard-coded in AxisCallMgr.java .
  17. This covers the files that get exchanged over the connection. Every FileAssociation object in the Exchange Session object consists of Source and Target objects covering the FTP endpoints. Using the processFiles method, files are moved from each Source directory to each corresponding Target directory on the computer at the other end of the Connection. Note that the parsedTrees from the XML are updated in the scope of this method and that the "updated" state will require re-validation on the trees.
  18. At the end of processing for a given ExchangeSession, Disconnect cleans up the network connection. Note that the XML structure allows for a Collection of ExchangeSession nodes in a single XML file. If this were the case, then the loop would get the next Network Connection based on the XML "exchangeSession" element and would process all the Source and Target information contained in the XML's fileAssociation elements.
  19. According to the rules for JAXB a validation step needs to preceed the marshall step.
  20. Create new instance of OutputStream for the call to marshallTrees method.
  21. Get the new file that the OutputStream instance will wrap
  22. Sys property=User.dir is set earlier from the properties file entry=workingDirectory
  23. The output XML file's Name comes from this entry in the properties file.
  24. Marshal is a JAXB term for writing the XML back out to the file system. The name of the file to be written is in the properties file and was used above to create the "stream" parameter for the marshall method. The prototype version of the properties file has separate names of XML in and XML out. In production, these should both be the same name. In that case each time the job runs, the XML node LastFile is appended with the youngest files transmitted for each FileAssociation node.
  25. Cleanup the stream resources.