动态调用动态语言,第 2 部分: 在运行时寻找、执行和修改脚本
2009-11-19 00:00:00 来源:WEB开发网运行代码:ScriptMortgageQualifierRunner
为了测试 ScriptMortgageQualifier 类,将使用测试数据表示四个贷款人、贷款人打算购买的一项资产和一笔抵押贷款。我们将用一个贷款人、资产和贷款运行所有三个脚本,检查贷款人是否满足脚本所代表的抵押产品的业务规则。
清单 2 给出 ScriptMortgageQualifierRunner 程序的部分代码,我们将用这个程序创建测试对象、在一个目录中寻找脚本文件并通过 清单 1 中的 ScriptMortgageQualifier 类运行它们。为了节省篇幅,这里没有给出这个程序的 createGoodBorrower()、createAverageBorrower()、createInvestorBorrower()、createRiskyBorrower()、createProperty() 和 createLoan() helper 方法。这些方法的作用仅仅是创建实体对象并设置测试所需的值。在 下载 一节中可以获得所有方法的完整源代码。
清单 2. ScriptMortgageQualifierRunner 程序// Imports and some helper methods not shown.
public class ScriptMortgageQualifierRunner {
private static File scriptDirectory;
private static Borrower goodBorrower = createGoodBorrower();
private static Borrower averageBorrower = createAverageBorrower();
private static Borrower investorBorrower = createInvestorBorrower();
private static Borrower riskyBorrower = createRiskyBorrower();
private static Property property = createProperty();
private static Loan loan = createLoan();
/**
* Main method to create a File for the directory name on the command line,
* then call the run method if that directory exists.
*/
public static void main(String[] args) {
if (args.length > 0 && args[0].contains("-help")) {
printUsageAndExit();
}
String dirName;
if (args.length == 0) {
dirName = "."; // Current directory.
} else {
dirName = args[0];
}
scriptDirectory = new File(dirName);
if (!scriptDirectory.exists() || !scriptDirectory.isDirectory()) {
printUsageAndExit();
}
run();
}
/**
* Determines mortgage loan-qualification status for four test borrowers by
* processing all script files in the given directory. Each script will determine
* whether the given borrower is qualified for a particular mortgage type
*/
public static void run() {
ScriptMortgageQualifier mortgageQualifier = new ScriptMortgageQualifier();
for(;;) { // Requires Ctrl-C to exit
runQualifications(mortgageQualifier, goodBorrower, loan, property);
runQualifications(mortgageQualifier, averageBorrower, loan, property);
loan.setDownPayment(30000.0); // Reduce down payment to 10%
runQualifications(mortgageQualifier, investorBorrower, loan, property);
loan.setDownPayment(10000.0); // Reduce down payment to 3 1/3%
runQualifications(mortgageQualifier, riskyBorrower, loan, property);
waitOneMinute();
}
}
/**
* Reads all script files in the scriptDirectory and runs them with this borrower's
* information to see if he/she qualifies for each mortgage product.
*/
private static void runQualifications(
ScriptMortgageQualifier mortgageQualifier,
Borrower borrower,
Loan loan,
Property property
) {
for (File scriptFile : getScriptFiles(scriptDirectory)) {
// Print info about the borrower, loan and property.
System.out.println("Processing file: " + scriptFile.getName());
System.out.println(" Borrower: " + borrower.getName());
System.out.println(" Credit score: " + borrower.getCreditScore());
System.out.println(" Sales price: " + property.getSalesPrice());
System.out.println(" Down payment: " + loan.getDownPayment());
MortgageQualificationResult result = null;
try {
// Run the script rules for this borrower on the loan product.
result = mortgageQualifier.qualifyMortgage(
borrower, property, loan, scriptFile
);
} catch (FileNotFoundException fnfe) {
System.out.println(
"Can't read script file: " + fnfe.getMessage()
);
} catch (IllegalArgumentException e) {
System.out.println(
"No script engine available to handle file: " +
scriptFile.getName()
);
} catch (ScriptException e) {
System.out.println(
"Script '" + scriptFile.getName() +
"' encountered an error: " + e.getMessage()
);
}
if (result == null) continue; // Must have hit exception.
// Print results.
System.out.println(
"* Mortgage product: " + result.getProductName() +
", Qualified? " + result.isQualified() +
"\n* Interest rate: " + result.getInterestRate() +
"\n* Message: " + result.getMessage()
);
System.out.println();
}
}
/** Returns files with a '.' other than as the first or last character. */
private static File[] getScriptFiles(File directory) {
return directory.listFiles(new FilenameFilter() {
public boolean accept(File dir, String name) {
int indexOfDot = name.indexOf('.');
// Ignore files w/o a dot, or with dot as first or last char.
if (indexOfDot < 1 || indexOfDot == (name.length() - 1)) {
return false;
} else {
return true;
}
}
});
}
private static void waitOneMinute() {
System.out.println(
"\nSleeping for one minute before reprocessing files." +
"\nUse Ctrl-C to exit..."
);
System.out.flush();
try {
Thread.sleep(1000 * 60);
} catch (InterruptedException e) {
System.exit(1);
}
}
}
更多精彩
赞助商链接