package se.kodapan.osm.sweden;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import se.kodapan.osm.services.nominatim.FileSystemCachedNominatim;
import se.kodapan.osm.services.overpass.FileSystemCachedOverpass;
import se.kodapan.osm.sweden.ext.se.posten.postnummer.CachedPostenPostnummerService;
import se.kodapan.osm.sweden.ext.se.posten.postnummer.local.LocalPostenPostnummerService;
import se.kodapan.osm.sweden.ext.se.posten.postnummer.local.Root;
import java.io.File;
import java.io.IOException;
/**
* @author kalle
* @since 2013-07-27 17:00
*/
public class OsmSweden {
private static Logger log = LoggerFactory.getLogger(OsmSweden.class);
private static OsmSweden instance = new OsmSweden();
public static OsmSweden getInstance() {
return instance;
}
private OsmSweden() {
}
private CachedPostenPostnummerService posten;
private FileSystemCachedNominatim nominatim;
private FileSystemCachedOverpass overpass;
private se.kodapan.osm.sweden.ext.se.posten.postnummer.local.Root localPosten;
private String userAgent = "http://github/karlwettin/osm-sweden";
private File path;
public void open() throws Exception {
File path;
if (System.getProperty("path") == null) {
path = new File("data");
log.warn("No -Dpath=value set, falling back on default value.");
} else {
path = new File(System.getProperty("path"));
}
open(path);
}
private boolean isOpened = false;
public synchronized void open(File path) throws Exception {
this.path = path;
if (isOpened) {
return;
}
if (!path.exists()) {
if (!path.mkdirs()) {
throw new IOException("Could not create data directory structure path " + path.getAbsolutePath());
}
log.info("mkdirs() " + path.getAbsolutePath());
}
log.info("Using data path " + path.getAbsolutePath());
overpass = new FileSystemCachedOverpass(new File(path, FileSystemCachedOverpass.class.getSimpleName()));
nominatim = new FileSystemCachedNominatim(new File(path, FileSystemCachedNominatim.class.getSimpleName()));
posten = new CachedPostenPostnummerService(new File(path, CachedPostenPostnummerService.class.getSimpleName()));
overpass.setUserAgent(userAgent);
nominatim.setUserAgent(userAgent);
posten.setUserAgent(userAgent);
posten.open();
nominatim.open();
overpass.open();
isOpened = true;
}
public void close() throws Exception {
posten.close();
nominatim.close();
overpass.close();
}
public CachedPostenPostnummerService getPosten() {
return posten;
}
public void setPosten(CachedPostenPostnummerService posten) {
this.posten = posten;
}
public FileSystemCachedNominatim getNominatim() {
return nominatim;
}
public void setNominatim(FileSystemCachedNominatim nominatim) {
this.nominatim = nominatim;
}
public FileSystemCachedOverpass getOverpass() {
return overpass;
}
public void setOverpass(FileSystemCachedOverpass overpass) {
this.overpass = overpass;
}
public String getUserAgent() {
return userAgent;
}
public void setUserAgent(String userAgent) {
this.userAgent = userAgent;
}
public synchronized Root getLocalPosten() throws Exception {
if (localPosten == null) {
File localPostenFile = new File(path, "Local Posten.Root.object");
if (localPostenFile.exists()) {
try {
localPosten = (Root)FileUtils.loadSerializable(localPostenFile);
} catch (Exception e) {
log.error("Could not load " + localPostenFile.getAbsolutePath(), e);
}
}
if (localPosten == null) {
localPosten = new LocalPostenPostnummerService().factory(getPosten(), getOverpass());
FileUtils.saveSerializable(localPosten, localPostenFile);
}
}
return localPosten;
}
public void setLocalPosten(Root localPosten) {
this.localPosten = localPosten;
}
}