Skip to content

Commit

Permalink
Merge pull request #98 from urbancamo/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
urbancamo authored Jun 18, 2024
2 parents 1a583f9 + 37de18c commit 3e1d9cb
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 5 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

<groupId>uk.m0nom</groupId>
<artifactId>adif-processor</artifactId>
<version>1.4.3</version>
<version>1.4.4</version>

<name>ADIF Processor</name>
<url>https://github.com/urbancamo/adif-processor</url>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;
Expand Down Expand Up @@ -56,16 +57,35 @@ public ActivityDatabaseService() {
*/
public void loadData() {
readers.values().forEach(reader -> {
InputStream inputStream = getClass().getClassLoader().getResourceAsStream(reader.getSourceFile());
if (inputStream == null) {
logger.severe(String.format("Can't load %s using classloader %s", reader.getSourceFile(), getClass().getClassLoader().toString()));
boolean readingFromRemote = false;
InputStream inputStream = null;
if (reader instanceof RemoteActivitySource remoteActivitySource) {
// Attempt to download the file from the remote source
try {
inputStream = URI.create(remoteActivitySource.getRemoteUrl()).toURL().openStream();
if (inputStream != null) {
logger.info(String.format("Loading %s from %s", reader.getType().getActivityName(), remoteActivitySource.getRemoteUrl()));
readingFromRemote = true;
}
} catch (IOException e) {
// problem downloading the file, so load the local copy instead
logger.warning(String.format("Problem downloading %s from %s", reader.getType().getActivityName(), remoteActivitySource.getRemoteUrl()));
}
}
if (!readingFromRemote) {
inputStream = getClass().getClassLoader().getResourceAsStream(reader.getSourceFile());
if (inputStream == null) {
logger.severe(String.format("Can't load %s using classloader %s", reader.getSourceFile(), getClass().getClassLoader().toString()));
}
}

try {
ActivityDatabase database = reader.read(inputStream);
databases.put(reader.getType().getActivityName(), database);
} catch (IOException e) {
logger.severe(String.format("Exception thrown reading activity databases: %s", e.getMessage()));
}

//logger.info(String.format("%d %s records loaded", database.size(), reader.getType().getActivityDescription()));
});
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package uk.m0nom.adifproc.activity;

public interface RemoteActivitySource {
String getRemoteUrl();
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,18 @@
import uk.m0nom.adifproc.activity.Activity;
import uk.m0nom.adifproc.activity.ActivityType;
import uk.m0nom.adifproc.activity.CsvActivityReader;
import uk.m0nom.adifproc.activity.RemoteActivitySource;

/**
* Reader for the Parks on the Air CSV extract
*/
public class PotaCsvReader extends CsvActivityReader {
public class PotaCsvReader extends CsvActivityReader implements RemoteActivitySource {

public PotaCsvReader(String sourceFile) {
super(ActivityType.POTA, sourceFile);
}


@Override
protected Activity readRecord(CSVRecord record) throws IllegalArgumentException {
PotaInfo info = new PotaInfo();
Expand All @@ -31,4 +33,9 @@ protected Activity readRecord(CSVRecord record) throws IllegalArgumentException
info.setGrid(record.get("grid"));
return info;
}

@Override
public String getRemoteUrl() {
return "https://pota.app/all_parks_ext.csv";
}
}

0 comments on commit 3e1d9cb

Please sign in to comment.