ratehelper.java
来自「JAVA Servlet2.3外文书籍源码」· Java 代码 · 共 93 行
JAVA
93 行
//== save as RateHelper.java
import java.io.*;
import java.net.*;
import java.util.*;
import org.apache.soap.*;
import org.apache.soap.rpc.*;
public class RateHelper {
private static Hashtable rate_table = new Hashtable(11);
public static String getExchangeRate(String country) {
Double rate = (Double)rate_table.get(country);
if( rate != null ) {
return rate.toString() + "(old)";
} else {
try {
rate = callExchangeRateService( country );
return rate.toString() + "(new)";
} catch( Exception e ) {
return e.getMessage();
}
}
}
public static double getRate(String country) {
Double rate = (Double)rate_table.get(country);
if(rate == null) {
try {
rate = callExchangeRateService( country );
} catch(Exception e) {
rate = new Double(0.0);
}
}
return rate.doubleValue();
}
public static double getRate(String country1, String country2) {
double rval;
double currency1_per_usdollar = getRate( country1 );
double currency2_per_usdollar = getRate( country2 );
if(currency2_per_usdollar > 0.0) {
rval = currency1_per_usdollar/currency2_per_usdollar;
} else {
rval = 0.0;
}
return rval;
}
public static String getExchangeRate(String country1, String country2) {
double rate = getRate(country1, country2);
return Double.toString(rate);
}
public static synchronized Double callExchangeRateService(String country)
throws Exception {
//== just in case the last guy got it while we were waiting
Double rate = (Double)rate_table.get(country);
if(rate != null) {
return rate;
}
try{
URL url = new URL( "http://localhost:8088/soap/servlet/rpcrouter" );
String encodingStyleURI = Constants.NS_URI_SOAP_ENC;
URLConnection connection = url.openConnection();
// Build the call.
Call call = new Call ();
call.setTargetObjectURI ("SoapExchangeRate");
call.setMethodName ("getExchangeRate");
Vector params = new Vector ();
call.setEncodingStyleURI(encodingStyleURI);
params.addElement(new Parameter("country", String.class, country, null));
call.setParams (params);
Response resp = call.invoke (url, "");
if (resp.generatedFault ()) {
Fault fault = resp.getFault ();
String faultString = fault.getFaultString ();
System.out.println( faultString );
} else {
Parameter result = resp.getReturnValue ();
rate = (Double)result.getValue();
rate_table.put(country, rate);
return rate;
}
} catch(Exception e) {
throw(e);
}
return rate;
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?