SOLR-4515: CurrencyField's OpenExchangeRatesOrgProvider now requires a ratesFileLocation init param, since the previous global default no longer works

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1451818 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Chris M. Hostetter 2013-03-02 01:52:03 +00:00
parent aa22ceb97d
commit daa66dd5d0
6 changed files with 81 additions and 17 deletions

View File

@ -195,6 +195,10 @@ Bug Fixes
well as when nodes may switch master/slave roles.
(Mark Miller, Raúl Grande)
* SOLR-4515: CurrencyField's OpenExchangeRatesOrgProvider now requires
a ratesFileLocation init param, since the previous global default
no longer works (hossman)
Optimizations
----------------------

View File

@ -130,7 +130,7 @@ public class CurrencyField extends FieldType implements SchemaAware, ResourceLoa
provider = c.newInstance();
provider.init(args);
} catch (Exception e) {
throw new SolrException(ErrorCode.BAD_REQUEST, "Error instansiating exhange rate provider "+exchangeRateProviderClass+". Please check your FieldType configuration", e);
throw new SolrException(ErrorCode.BAD_REQUEST, "Error instantiating exhange rate provider "+exchangeRateProviderClass+": " + e.getMessage(), e);
}
}

View File

@ -33,19 +33,29 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Exchange Rates Provider for {@link CurrencyField} implementing the freely available
* exchange rates from openexchangerates.org
* <p>
* Exchange Rates Provider for {@link CurrencyField} capable of fetching &amp
* parsing the freely available exchange rates from openexchangerates.org
* </p>
* <p>
* Configuration Options:
* </p>
* <ul>
* <li><code>ratesFileLocation</code> - A file path or absolute URL specifying the JSON data to load (mandatory)</li>
* <li><coderefreshInterval></code> - How frequently (in minutes) to reload the exchange rate data (default: 1440)</li>
* </ul>
* <p>
* <b>Disclaimer:</b> This data is collected from various providers and provided free of charge
* for informational purposes only, with no guarantee whatsoever of accuracy, validity,
* availability or fitness for any purpose; use at your own risk. Other than that - have
* fun, and please share/watch/fork if you think data like this should be free!
* </p>
* @see <a href="https://openexchangerates.org/documentation">openexchangerates.org JSON Data Format</a>
*/
public class OpenExchangeRatesOrgProvider implements ExchangeRateProvider {
public static Logger log = LoggerFactory.getLogger(OpenExchangeRatesOrgProvider.class);
protected static final String PARAM_RATES_FILE_LOCATION = "ratesFileLocation";
protected static final String PARAM_REFRESH_INTERVAL = "refreshInterval";
protected static final String DEFAULT_RATES_FILE_LOCATION = "http://openexchangerates.org/latest.json";
protected static final String DEFAULT_REFRESH_INTERVAL = "1440";
protected String ratesFileLocation;
@ -145,7 +155,10 @@ public class OpenExchangeRatesOrgProvider implements ExchangeRateProvider {
@Override
public void init(Map<String,String> params) throws SolrException {
try {
ratesFileLocation = getParam(params.get(PARAM_RATES_FILE_LOCATION), DEFAULT_RATES_FILE_LOCATION);
ratesFileLocation = params.get(PARAM_RATES_FILE_LOCATION);
if (null == ratesFileLocation) {
throw new SolrException(ErrorCode.SERVER_ERROR, "Init param must be specified: " + PARAM_RATES_FILE_LOCATION);
}
refreshInterval = Integer.parseInt(getParam(params.get(PARAM_REFRESH_INTERVAL), DEFAULT_REFRESH_INTERVAL));
// Force a refresh interval of minimum one hour, since the API does not offer better resolution
if (refreshInterval < 60) {
@ -153,8 +166,11 @@ public class OpenExchangeRatesOrgProvider implements ExchangeRateProvider {
log.warn("Specified refreshInterval was too small. Setting to 60 minutes which is the update rate of openexchangerates.org");
}
log.info("Initialized with rates="+ratesFileLocation+", refreshInterval="+refreshInterval+".");
} catch (Exception e) {
throw new SolrException(ErrorCode.BAD_REQUEST, "Error initializing", e);
} catch (SolrException e1) {
throw e1;
} catch (Exception e2) {
throw new SolrException(ErrorCode.SERVER_ERROR, "Error initializing: " +
e2.getMessage(), e2);
} finally {
// Removing config params custom to us
params.remove(PARAM_RATES_FILE_LOCATION);

View File

@ -0,0 +1,37 @@
<?xml version="1.0" ?>
<!--
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.
-->
<schema name="bad-schema-currency-ft-oer-norates" version="1.4">
<types>
<fieldType name="string" class="solr.StrField" multiValued="true"/>
<!-- BEGIN BAD STUFF: multiValued -->
<fieldType name="currency"
class="solr.CurrencyField"
providerClass="solr.OpenExchangeRatesOrgProvider"
multiValued="false" />
</types>
<fields>
<field name="id" type="string" indexed="true" stored="true" multiValued="false"/>
<field name="money" type="currency" indexed="true" stored="true" />
</fields>
<defaultSearchField>id</defaultSearchField>
<uniqueKey>id</uniqueKey>
</schema>

View File

@ -73,6 +73,11 @@ public class BadIndexSchemaTest extends AbstractBadConfigTestBase {
"Fields can not be multiValued: *_c");
}
public void testCurrencyOERNoRates() throws Exception {
doTest("bad-schema-currency-ft-oer-norates.xml",
"ratesFileLocation");
}
public void testPerFieldtypeSimButNoSchemaSimFactory() throws Exception {
doTest("bad-schema-sim-global-vs-ft-mismatch.xml", "global similarity does not support it");
}

View File

@ -32,8 +32,7 @@ import java.util.Map;
public class OpenExchangeRatesOrgProviderTest extends SolrTestCaseJ4 {
OpenExchangeRatesOrgProvider oerp;
ResourceLoader loader;
private final Map<String,String> emptyParams = new HashMap<String,String>();
private Map<String,String> mockParams;
private final Map<String,String> mockParams = new HashMap<String,String>();
@Override
@Before
@ -42,24 +41,27 @@ public class OpenExchangeRatesOrgProviderTest extends SolrTestCaseJ4 {
("USD", "EUR", "MXN", "GBP", "JPY");
super.setUp();
mockParams = new HashMap<String,String>();;
mockParams.put(OpenExchangeRatesOrgProvider.PARAM_RATES_FILE_LOCATION, "open-exchange-rates.json");
mockParams.put(OpenExchangeRatesOrgProvider.PARAM_RATES_FILE_LOCATION,
"open-exchange-rates.json");
oerp = new OpenExchangeRatesOrgProvider();
loader = new SolrResourceLoader("solr/collection1");
}
@Test
public void testInit() throws Exception {
oerp.init(emptyParams);
assertTrue("Wrong default url", oerp.ratesFileLocation.toString().equals("http://openexchangerates.org/latest.json"));
assertTrue("Wrong default interval", oerp.refreshInterval == 1440);
oerp.init(mockParams);
assertEquals("Wrong url",
"open-exchange-rates.json", oerp.ratesFileLocation);
assertEquals("Wrong default interval", 1440, oerp.refreshInterval);
Map<String,String> params = new HashMap<String,String>();
params.put(OpenExchangeRatesOrgProvider.PARAM_RATES_FILE_LOCATION, "http://foo.bar/baz");
params.put(OpenExchangeRatesOrgProvider.PARAM_RATES_FILE_LOCATION,
"http://foo.bar/baz");
params.put(OpenExchangeRatesOrgProvider.PARAM_REFRESH_INTERVAL, "100");
oerp.init(params);
assertTrue("Wrong param set url", oerp.ratesFileLocation.equals("http://foo.bar/baz"));
assertTrue("Wrong param interval", oerp.refreshInterval == 100);
assertEquals("Wrong param set url",
"http://foo.bar/baz", oerp.ratesFileLocation);
assertEquals("Wrong param interval", 100, oerp.refreshInterval);
}
@Test