diff options
author | nash <nash@13f79535-47bb-0310-9956-ffa450edef68> | 2010-11-22 09:49:22 +0000 |
---|---|---|
committer | nash <nash@13f79535-47bb-0310-9956-ffa450edef68> | 2010-11-22 09:49:22 +0000 |
commit | f0797f0026f729fa612930fbd0acefc5343a0fe6 (patch) | |
tree | e5f5736d13b815c6164bca3740bb8763b5a7e4db /sca-java-1.x/tags/1.6.1/demos/xml-bigbank/src/main/java/bigbank/ExchangeRateImpl.java | |
parent | cdd4a3ad287c73bb6d756c812bc61917f6ddf344 (diff) |
Copy 1.6.1-RC2 tag as 1.6.1
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@1037648 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'sca-java-1.x/tags/1.6.1/demos/xml-bigbank/src/main/java/bigbank/ExchangeRateImpl.java')
-rw-r--r-- | sca-java-1.x/tags/1.6.1/demos/xml-bigbank/src/main/java/bigbank/ExchangeRateImpl.java | 96 |
1 files changed, 96 insertions, 0 deletions
diff --git a/sca-java-1.x/tags/1.6.1/demos/xml-bigbank/src/main/java/bigbank/ExchangeRateImpl.java b/sca-java-1.x/tags/1.6.1/demos/xml-bigbank/src/main/java/bigbank/ExchangeRateImpl.java new file mode 100644 index 0000000000..11c300cd3c --- /dev/null +++ b/sca-java-1.x/tags/1.6.1/demos/xml-bigbank/src/main/java/bigbank/ExchangeRateImpl.java @@ -0,0 +1,96 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package bigbank; + +import java.io.ByteArrayInputStream; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.xpath.XPath; +import javax.xml.xpath.XPathConstants; +import javax.xml.xpath.XPathExpression; +import javax.xml.xpath.XPathFactory; + +import org.osoa.sca.ServiceRuntimeException; +import org.osoa.sca.annotations.Reference; +import org.osoa.sca.annotations.Service; +import org.w3c.dom.Document; +import org.w3c.dom.Node; + +import com.sun.syndication.feed.synd.SyndEntry; +import com.sun.syndication.feed.synd.SyndFeed; + +/** + * @version $Rev$ $Date$ + */ +@Service(ExchangeRate.class) +public class ExchangeRateImpl { + @Reference + protected CurrencyExchange exchangeRate; + + private final DocumentBuilder builder; + + public ExchangeRateImpl() { + try { + builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); + } catch (ParserConfigurationException e) { + throw new IllegalArgumentException(e); + } + } + + /** + * Retrieve the live currency exchange rate from a live feed and extract the data for a given + * currecy using XPath + * @param currency The currency + * @return The exchange rate + */ + public double getExchangeRate(String currency) { + try { + System.out.println("Retrieving exchange rate..."); + + // first try to get a live exchange rate quote from the RSS feed + SyndFeed feed = null; + try { + feed = exchangeRate.getRates(); + + // if the RSS feed isn't responding, continue with the demo using historical data + } catch (Exception e) { + System.out.println("Exchange rate live quote not available, using historical data"); + return 0.74107; + } + + // extract the exchange rate from the feed data + SyndEntry entry = (SyndEntry)feed.getEntries().get(0); + String rateTable = entry.getDescription().getValue(); + + Document doc = builder.parse(new ByteArrayInputStream(rateTable.getBytes())); + Node node = doc.getDocumentElement(); + XPath path = XPathFactory.newInstance().newXPath(); + XPathExpression exp = path.compile("/TABLE/TR[TD[1]='" + currency.toUpperCase() + "']/TD[2]"); + Node rateNode = (Node)exp.evaluate(node, XPathConstants.NODE); + double rate = Double.valueOf(rateNode.getTextContent().trim()); + System.out.println("Exchange rate: USD 1.0=" + currency + " " + rate); + return rate; + } catch (Exception e) { + throw new ServiceRuntimeException(e); + } + } +} |