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.internal.convert;
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").set("days", 1).build();
053
054    public IMFRateProvider() {
055        super(CONTEXT);
056        LoaderService loader = Bootstrap.getService(LoaderService.class);
057        loader.addLoaderListener(this, DATA_ID);
058        try {
059            loader.loadData(DATA_ID);
060        } catch (IOException e) {
061                LOG.log(Level.WARNING, "Error loading initial data from IMF provider...", e);
062        }
063    }
064
065}