Skip to content

Instantly share code, notes, and snippets.

@cacoco
Last active December 17, 2015 00:09
Show Gist options
  • Save cacoco/5518218 to your computer and use it in GitHub Desktop.
Save cacoco/5518218 to your computer and use it in GitHub Desktop.
public class MyLookupServiceImpl implements ApplicationContextAware {
private com.maxmind.geoip.LookupService lookupService;
private String dataPath; // classpath location of .dat file, e.g GeoIPCity.dat
private ApplicationContext context;
// constructor that allows us to inject a LookupService, e.g. for testing
public MyLookupServiceImpl(com.maxmind.geoip.LookupService lookupService) {
this.lookupService = lookupService;
}
...
public void setApplicationContext(ApplicationContext applicationContext) {
this.context = applicationContext;
if (this.lookupService == null) {
if (this.dataPath == null) { LOG.warn("dataPath not set."); return; }
final Resource resource = this.context.getResource("classpath:" + this.dataPath);
if (resource != null) {
try {
final File f = resource.getFile();
// instantiate MaxMind LookupService
this.lookupService = new com.maxmind.geoip.LookupService(f, com.maxmind.geoip.LookupService.GEOIP_MEMORY_CACHE);
} catch (Exception e) {
LOG.error(String.format("Could not create service. %s", e.getMessage()), e);
}
} else {
LOG.warn(String.format("Could not find classpath resource: [%s]. ", path));
}
}
}
...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment