Skip to content
This repository has been archived by the owner on Nov 16, 2023. It is now read-only.

Commit

Permalink
Fix refresh handling in error conditions (#49)
Browse files Browse the repository at this point in the history
* Fix refresh handling in error conditions

* Fix indentation in documentation

* Implement validation for Maxmind data adapter config
  • Loading branch information
bernd authored and kroepke committed Jun 14, 2017
1 parent 0deb843 commit 8e1d74d
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
Expand Up @@ -5,7 +5,9 @@
import com.fasterxml.jackson.annotation.JsonTypeName;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.google.auto.value.AutoValue;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Multimap;
import com.google.common.net.InetAddresses;
import com.google.inject.assistedinject.Assisted;
import com.maxmind.geoip2.DatabaseReader;
Expand Down Expand Up @@ -33,6 +35,7 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Optional;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;

Expand Down Expand Up @@ -93,9 +96,8 @@ public Duration refreshInterval() {
@Override
protected void doRefresh(LookupCachePurge cachePurge) throws Exception {
try {
clearError();
final FileInfo.Change databaseFileCheck = fileInfo.checkForChange();
if (!databaseFileCheck.isChanged()) {
if (!databaseFileCheck.isChanged() && !getError().isPresent()) {
return;
}

Expand All @@ -109,6 +111,7 @@ protected void doRefresh(LookupCachePurge cachePurge) throws Exception {
oldReader.close();
}
fileInfo = databaseFileCheck.fileInfo();
clearError();
} catch (IOException e) {
LOG.warn("Unable to load changed database file, leaving old one intact. Error message: {}", e.getMessage());
setError(e);
Expand Down Expand Up @@ -245,6 +248,20 @@ public static Config.Builder builder() {
return new AutoValue_MaxmindDataAdapter_Config.Builder();
}

@Override
public Optional<Multimap<String, String>> validate() {
final ArrayListMultimap<String, String> errors = ArrayListMultimap.create();

final Path path = Paths.get(path());
if (!Files.exists(path)) {
errors.put("path", "The file does not exist.");
} else if (!Files.isReadable(path)) {
errors.put("path", "The file cannot be read.");
}

return errors.isEmpty() ? Optional.empty() : Optional.of(errors);
}

@AutoValue.Builder
public abstract static class Builder {
@JsonProperty(TYPE_FIELD)
Expand Down
4 changes: 2 additions & 2 deletions src/web/components/adapter/MaxmindAdapterDocumentation.jsx
Expand Up @@ -17,7 +17,7 @@ const MaxmindAdapterDocumentation = React.createClass({
},
"postal": { "code": "94035" },
"subdivisions": [ { "geoname_id": 5332921, "iso_code": "CA", "names": { "en": "California" } } ],
}`;
}`;

const countryFields = `{
"continent": { "code": "NA", "geoname_id": 6255149, "names": { "en": "North America" } },
Expand All @@ -32,7 +32,7 @@ const MaxmindAdapterDocumentation = React.createClass({
"isp": null,
"organization": null,
}
}`;
}`;

return (<div>
<p>The GeoIP data adapter supports reading MaxMind's GeoIP2 databases.</p>
Expand Down
5 changes: 4 additions & 1 deletion src/web/components/adapter/MaxmindAdapterFieldSet.jsx
Expand Up @@ -10,6 +10,8 @@ const MaxmindAdapterFieldSet = React.createClass({
// eslint-disable-next-line react/no-unused-prop-types
updateConfig: PropTypes.func.isRequired,
handleFormEvent: PropTypes.func.isRequired,
validationState: PropTypes.func.isRequired,
validationMessage: PropTypes.func.isRequired,
},

_update(value, unit, enabled, name) {
Expand Down Expand Up @@ -43,7 +45,8 @@ const MaxmindAdapterFieldSet = React.createClass({
autoFocus
required
onChange={this.props.handleFormEvent}
help={'The path to the Maxmind\u2122 database file.'}
help={this.props.validationMessage('path', 'The path to the Maxmind\u2122 database file.')}
bsStyle={this.props.validationState('path')}
value={config.path}
labelClassName="col-sm-3"
wrapperClassName="col-sm-9" />
Expand Down

0 comments on commit 8e1d74d

Please sign in to comment.