Thursday, July 22, 2010

Java Based Command Example

Here is the code for a shell style command written in Java. The command uses args4j which uses @Option parameterization to input the command flag and usage argument along side the Java Object field name that will store the information. By using the args4j CmdLineParser class and the parseArguments method, all command line parameters are neatly stored in their corresponding field names.

This particular command is for getting files from the TapForms program on an iphone and downloading the file to a macbook. TapForms runs a web server so when the iphone is connected over wifi that webserver can accessed and files downloaded in essentially a webdav access.


/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package webdavaccess;

/**
*
* @author tmcguire
*/
import com.googlecode.sardine.Sardine;
import com.googlecode.sardine.SardineFactory;
import com.googlecode.sardine.util.SardineException;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
//import org.kohsuke.args4j.Argument;
import org.apache.log4j.Appender;
import org.apache.log4j.ConsoleAppender;
import org.apache.log4j.SimpleLayout;

import org.kohsuke.args4j.CmdLineException;
import org.kohsuke.args4j.CmdLineParser;
import org.kohsuke.args4j.Option;
//import org.kohsuke.args4j.spi.BooleanOptionHandler;

public class GetTFFileCmd {

@Option(name = "-ip", usage = "sets ip address")
String ipaddr;
@Option(name = "-port", usage = "sets ip port number")
String port;
@Option(name = "-file", usage = "sets the full file path of Tapform file")
String filename;
@Option(name = "-?", usage = "prints usage")
Boolean help = false;
static Logger myLogger = Logger.getLogger(GetTFFileCmd.class.getName());
static Appender myAppender;

public GetTFFileCmd() {
this.filename = "/Exports/ProgressNote.csv";
this.port = "8080";
this.ipaddr = "10.0.1.46";

}

private void copy(InputStream in, OutputStream out) throws IOException {
byte[] b = new byte[8192];

int read;
while ((read = in.read(b)) != -1) {
out.write(b, 0, read);
}
}

public void run() throws SardineException, FileNotFoundException, IOException {
BufferedOutputStream os = null;
PrintStream xmls = null;
File f = null, fxml = null;
Sardine sardine = null;
InputStream srdis = null;

sardine = SardineFactory.begin("anonymous", "");

System.out.println("sardine factory intialized");
System.out.println("InputStream from: "+"http://" + this.ipaddr
+ ":" + this.port + this.filename);

srdis = sardine.getInputStream("http://" + this.ipaddr
+ ":" + this.port + this.filename);

os = new BufferedOutputStream(new FileOutputStream("ProgressNote.csv"));

this.copy(srdis, os);
os.close();
}

public static void main(String[] args) {
myLogger.setLevel(Level.ALL);

// Define Appender
myAppender = new ConsoleAppender(new SimpleLayout());

//myAppender.setLayout(new SimpleLayout());
myLogger.addAppender(myAppender);
GetTFFileCmd gfile = new GetTFFileCmd();

CmdLineParser parser = new CmdLineParser(gfile);
try {
parser.parseArgument(args);
if (gfile.help) {
parser.printUsage(System.out);
System.exit(0);
}
System.out.println(gfile.ipaddr+":"+gfile.port+gfile.filename);
gfile.run();
} catch (SardineException ex) {
Logger.getLogger(GetTFFileCmd.class.getName()).log(Level.FATAL, null, ex);
} catch (FileNotFoundException ex) {
java.util.logging.Logger.getLogger(GetTFFileCmd.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IOException ex) {
java.util.logging.Logger.getLogger(GetTFFileCmd.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (CmdLineException ex) {
Logger.getLogger(GetTFFileCmd.class.getName()).log(Level.FATAL, null, ex);
parser.printUsage(System.err);
}

}
}

No comments:

Post a Comment