001/*
002  Copyright (c) 2012, 2014, Credit Suisse (Anatole Tresch), Werner Keil and others by the @author tag.
003
004  Licensed under the Apache License, Version 2.0 (the "License"); you may not
005  use this file except in compliance with the License. You may obtain a copy of
006  the License at
007
008  http://www.apache.org/licenses/LICENSE-2.0
009
010  Unless required by applicable law or agreed to in writing, software
011  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013  License for the specific language governing permissions and limitations under
014  the License.
015 */
016package org.javamoney.moneta.convert.imf;
017
018import java.io.IOException;
019import java.util.logging.Level;
020import java.util.logging.Logger;
021
022import javax.money.convert.ConversionContext;
023import javax.money.convert.ExchangeRateProvider;
024import javax.money.convert.ProviderContext;
025import javax.money.convert.ProviderContextBuilder;
026import javax.money.convert.RateType;
027import javax.money.spi.Bootstrap;
028
029import org.javamoney.moneta.spi.LoaderService;
030
031/**
032 * Implements a {@link ExchangeRateProvider} that loads the IMF conversion data.
033 * In most cases this provider will provide chained rates, since IMF always is
034 * converting from/to the IMF <i>SDR</i> currency unit.
035 *
036 * @author Anatole Tresch
037 * @author Werner Keil
038 */
039public class IMFRateProvider extends IMFAbstractRateProvider {
040
041        private static final Logger LOG = Logger
042                        .getLogger(IMFRateProvider.class.getName());
043
044    /**
045     * The data id used for the LoaderService.
046     */
047    private static final String DATA_ID = IMFRateProvider.class.getSimpleName();
048    /**
049     * The {@link ConversionContext} of this provider.
050     */
051    private static final ProviderContext CONTEXT = ProviderContextBuilder.of("IMF", RateType.DEFERRED)
052            .set("providerDescription", "International Monetary Fond")
053            .set("days", 1)
054            .set("User-Agent", "Chrome/51.0.2704.103")
055            .build();
056
057    public IMFRateProvider() {
058        super(CONTEXT);
059        LoaderService loader = Bootstrap.getService(LoaderService.class);
060        loader.addLoaderListener(this, DATA_ID);
061        try {
062            loader.loadData(DATA_ID);
063        } catch (IOException e) {
064                LOG.log(Level.WARNING, "Error loading initial data from IMF provider...", e);
065        }
066    }
067
068}