まとめ
JBuilder 2007を用いることで、既存のWebサービスを使って、JSFのWebクライアントから呼び出すことができました。JBuilder 2007が、Webサービスインフラコードをすべて生成してくれるので、必要な問題解決に集中することができました。
ソースコード
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%> <%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %> <%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd"> <f:view> <html> <head> <meta http-equiv="Content-Type" content="text/html;
charset=ISO-8859-1"> <title>Currency Converter</title> </head> <body> <h:form> <h:outputLabel id="lblFrom" value="From:"/> <h:selectOneMenu id="cboFrom"
value="#{converterBean.fromCurrency}"> <f:selectItems value="#{converterBean.currencyList}" /> </h:selectOneMenu> <h:inputText id="txtAmount" size="10"
title="Amount to be converted."
value="#{converterBean.amount}"> <f:validateDoubleRange minimum="0" maximum="100000000000" /> <f:convertNumber /> </h:inputText> <h:message for="txtAmount"/> <br/> <h:outputLabel id="lblTo" value="To:"/> <h:selectOneMenu id="cboTo" value="#{converterBean.toCurrency}"> <f:selectItems value="#{converterBean.currencyList}" /> </h:selectOneMenu> <br/> <h:commandButton id="cmdConvert" value="Convert"/><br/> <br/><br/> <h:outputLabel id="lblConvert"
value="#{converterBean.convertedAmount}" /> </h:form> </body> </html> </f:view>
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE faces-config PUBLIC
"-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.1//EN"
"http://java.sun.com/dtd/web-facesconfig_1_1.dtd"> <faces-config> <managed-bean> <managed-bean-name> converterBean</managed-bean-name> <managed-bean-class> converter.ConverterBean</managed-bean-class> <managed-bean-scope> request</managed-bean-scope> </managed-bean> </faces-config>
package converter; import java.rmi.RemoteException; import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.lang.reflect.*; import javax.faces.model.SelectItem; import NET.webserviceX.www.Currency; public class ConverterBean { /**
* Private variable declarations
* that are used to get and set currency and amount.
*/ private String fromCurrency = ""; private String toCurrency = ""; private double amount = 0.0; private double getCurrencyConversionRate(Currency fromCurrency, Currency toCurrency) throws RemoteException { NET.webserviceX.www.CurrencyConvertorSoapStub binding = null; try { binding = (NET.webserviceX.www.CurrencyConvertorSoapStub) new NET.webserviceX.www.CurrencyConvertorLocator() .getCurrencyConvertorSoap(); } catch (javax.xml.rpc.ServiceException jre) { if(jre.getLinkedCause()!=null) jre.getLinkedCause().printStackTrace(); } // Time out after a minute binding.setTimeout(60000); return binding.conversionRate(fromCurrency, toCurrency); } /**
* Do something not-so-fancy here to create a currency.
* @param currencyCode
* @return
*/ private Currency getCurrencyFromString(String currencyCode) { Currency c = Currency.fromString(currencyCode); return c; } public SelectItem[] getCurrencyList() { List<String> al = new ArrayList<String>(); Field f[]; try { Class c = Class.forName("NET.webserviceX.www.Currency"); f = c.getFields(); // Get the fields of the class that don't start with an "_", // because this is the list of currencies for(Field f1 : f) { if(!f1.getName().contains("_")) { al.add(f1.getName()); } } } catch (ClassNotFoundException e) { System.out.println("Error getting currency list."); } Collections.sort(al); SelectItem[] items = new SelectItem[al.size()]; for(int i=0; i < al.size(); i++) { items[i] = new SelectItem(al.get(i).toString(), al.get(i).toString()); } return items; } public String getFromCurrency() { return fromCurrency; } public void setFromCurrency(String fromCurrency) { this.fromCurrency = fromCurrency; } public String getToCurrency() { return toCurrency; } public void setToCurrency(String toCurrency) { this.toCurrency = toCurrency; } public double getAmount() { return amount; } public void setAmount(double amount) { this.amount = amount; } public double getConvertedAmount() throws RemoteException { Double convertedValue = 0.0; if(amount != 0) convertedValue = amount * getCurrencyConversionRate(getCurrencyFromString(fromCurrency), getCurrencyFromString(toCurrency)); return convertedValue; } }