001: /*
002: * $Id: Main.java,v 1.3 2007/03/16 10:05:46 agoubard Exp $
003: */
004: package org.xins.demos.caller;
005:
006: import java.io.FileInputStream;
007: import java.io.FileReader;
008: import java.io.InputStream;
009: import java.io.Reader;
010: import java.util.Properties;
011: import org.apache.log4j.Logger;
012: import org.apache.log4j.PropertyConfigurator;
013: import org.xins.client.CallRequest;
014: import org.xins.client.CallRequestParser;
015: import org.xins.client.XINSServiceCaller;
016: import org.xins.common.collections.PropertiesPropertyReader;
017: import org.xins.common.collections.PropertyReader;
018: import org.xins.common.service.Descriptor;
019: import org.xins.common.service.DescriptorBuilder;
020:
021: /**
022: * Executes a call to a XINS API.
023: *
024: * <p>This program expects 2 or 3 arguments:
025: *
026: * <ol>
027: * <li>Function caller configuration file (e.g. <code>caller.properties</code>)
028: * <li>Request file (e.g. <code>request.xml</code>)
029: * <li>Count (optional, e.g. 3, default is 1)
030: * </ol>
031: *
032: * @version $Revision: 1.3 $ $Date: 2007/03/16 10:05:46 $
033: * @author Ernst de Haan (<a href="mailto:znerd@FreeBSD.org">znerd@FreeBSD.org</a>)
034: *
035: * @since XINS 0.46
036: */
037: public final class Main extends Object {
038:
039: /**
040: * Main method.
041: *
042: * @param args
043: * the arguments passed to this program, not <code>null</code>.
044: *
045: * @throws Throwable
046: * if anything goes wrong.
047: */
048: public static void main(String[] args) throws Throwable {
049:
050: int argCount = (args != null) ? args.length : 0;
051:
052: if (argCount < 2 || argCount > 3) {
053: System.err.println("Usage: java " + Main.class.getName()
054: + " <config> <request> <count>");
055: System.err
056: .println(" <config> -- Name of config file (required).");
057: System.err
058: .println(" <request> -- Name of request file (required).");
059: System.err
060: .println(" <count> -- Number of times to execute the request (optional).");
061: System.exit(1);
062: }
063:
064: // Initialize Log4J
065: Properties settings = new Properties();
066: settings.setProperty("log4j.rootLogger", "DEBUG, console");
067: settings.setProperty("log4j.appender.console",
068: "org.apache.log4j.ConsoleAppender");
069: settings.setProperty("log4j.appender.console.layout",
070: "org.apache.log4j.PatternLayout");
071: settings.setProperty(
072: "log4j.appender.console.layout.ConversionPattern",
073: "%d %-5p [%c] %m%n");
074: settings.setProperty("log4j.logger.httpclient.wire", "WARN");
075: settings.setProperty(
076: "log4j.logger.org.apache.commons.httpclient", "WARN");
077: PropertyConfigurator.configure(settings);
078: Logger log = Logger.getLogger(Main.class.getName());
079:
080: // Get all parameters
081: String configFileName = args[0];
082: String requestFileName = args[1];
083: int count = (argCount > 2) ? Integer.parseInt(args[2]) : 1;
084:
085: // Read the config file
086: InputStream configFile = new FileInputStream(configFileName);
087: Properties p = new Properties();
088: p.load(configFile);
089: PropertyReader properties = new PropertiesPropertyReader(p);
090: Descriptor descriptor = DescriptorBuilder.build(properties,
091: "caller");
092: XINSServiceCaller caller = new XINSServiceCaller(descriptor);
093:
094: // Read the request file
095: Reader requestFile = new FileReader(requestFileName);
096: CallRequestParser callRequestParser = new CallRequestParser();
097: CallRequest request = callRequestParser.parse(requestFile);
098:
099: // Execute the call(s)
100: XINSServiceCaller.Result result;
101: try {
102: for (int i = 0; i < count; i++) {
103: result = caller.call(request);
104: log.info("Call " + i + " performed (success="
105: + result.isSuccess() + ").");
106: }
107: } catch (Throwable exception) {
108: log.error("Failed to execute call.", exception);
109: }
110: }
111: }
|