summaryrefslogtreecommitdiffstats
path: root/sca-java-1.x/branches/sca-java-1.6.2/demos/xml-bigbank/src/main/java/bigbank/ExchangeRateImpl.java
blob: 11c300cd3cd3fd7b4d41489d62ff3fe9c6692459 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
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);
        }
    }
}