public GeoIpResolverEngine(GeoIpResolverConfig config, MetricRegistry metricRegistry) { this.resolveTime = metricRegistry.timer(name(GeoIpResolverEngine.class, "resolveTime")); try { final File database = new File(config.dbPath()); if (Files.exists(database.toPath())) { this.databaseReader = new DatabaseReader.Builder(database).build(); this.enabled = config.enabled(); } else { LOG.warn("GeoIP database file does not exist: {}", config.dbPath()); this.enabled = false; } } catch (IOException e) { LOG.error("Could not open GeoIP database {}", config.dbPath(), e); this.enabled = false; } }
try { java.io.InputStream inputFile = GeoLocationImpl.class.getClassLoader().getResourceAsStream("reference/GeoLite2-City.mmdb"); reader = new DatabaseReader.Builder(inputFile).build(); } catch(Exception e) { LOGGER.error("Cannot instantiate IP database",e);
public static void main(String[] args) throws Exception { if (args.length != 2) { System.err.println("Usage: DatabaseReader geoip2-file.mmdb ip"); System.exit(1); } File f = new File(args[0]); Builder b = new Builder(f); b.withCache(new CHMCache(256)); DatabaseReader r = b.build(); System.out.println("Database Metadata: " + r.getMetadata()); String c = r.country(args[1]); System.out.println("IP: " + args[1] + " country: " + c); } }
@PostConstruct public void postConstruct() { // A File object pointing to your GeoIP2 or GeoLite2 database if (pathToGeoLocationDatabase == null) { return; } File database = new File(pathToGeoLocationDatabase); if (!database.exists()) { return; } // This creates the DatabaseReader object, which should be reused across // lookups. try { this.databaseReader = new DatabaseReader.Builder(database).build(); } catch (IOException e) { logger.error("Cannot read IP database", e); } }
@Override public void setConf(AbstractConfig config) { this.config = (GeoIpOperationConfig) config; AmazonS3Client client = this.s3Factory.newInstance(); AmazonS3URI uri = new AmazonS3URI(this.config.getGeoLiteDb()); GetObjectRequest req = new GetObjectRequest(uri.getBucket(), uri.getKey()); S3Object obj = client.getObject(req); try { this.databaseReader = new DatabaseReader.Builder(obj.getObjectContent()).withCache(new CHMCache()).build(); } catch (IOException e) { throw new ConfigurationException("Unable to read " + this.config.getGeoLiteDb(), e); } } }
@Override public void setConf(AbstractConfig config) { this.config = (GeoIpOperationConfig) config; AmazonS3Client client = this.s3Factory.newInstance(); AmazonS3URI uri = new AmazonS3URI(this.config.getGeoLiteDb()); GetObjectRequest req = new GetObjectRequest(uri.getBucket(), uri.getKey()); S3Object obj = client.getObject(req); try { this.databaseReader = new DatabaseReader.Builder(obj.getObjectContent()).withCache(new CHMCache()).build(); } catch (IOException e) { throw new ConfigurationException("Unable to read " + this.config.getGeoLiteDb(), e); } } }
@Override public void prepareForRun() throws InvalidDissectorException { // This creates the DatabaseReader object, which should be reused across lookups. try { reader = new DatabaseReader .Builder(openDatabaseFile(databaseFileName)) .fileMode(Reader.FileMode.MEMORY) .withCache(new CHMCache()) .build(); } catch (IOException e) { throw new InvalidDissectorException(this.getClass().getCanonicalName() + ":" + e.getMessage()); } }
public static synchronized DatabaseReader getDatabaseReader() { if (databaseReader!=null) return databaseReader; try { File f = new File(MAXMIND_DB_PATH); databaseReader = new DatabaseReader.Builder(f).build(); } catch (IOException e) { lookupFailed = true; log.debug("MaxMind geo lookup unavailable; either download and unpack the latest "+ "binary from "+MAXMIND_DB_URL+" into "+MAXMIND_DB_PATH+", "+ "or specify a different HostGeoLookup implementation with the key "+ BrooklynSystemProperties.HOST_GEO_LOOKUP_IMPL.getPropertyName()+" (error trying to read: "+e+")"); } return databaseReader; }
public MMDB(String fileName) throws Exception { File f = new File("data/GeoIP2-ISP.mmdb"); reader = new DatabaseReader.Builder(f).build(); }
private void initializeDataset() { if(serverConfiguration.isGeolocateIpAddresses()) { logger.info("Geolocation enabled: attempting to load database from " + serverConfiguration.getGeolocationDatabasePath()); try { File database = new File(serverConfiguration.getGeolocationDatabasePath()); reader = new DatabaseReader.Builder(database).build(); } catch(IOException e) { if(logger != null) { logger.warn("Couldn't load IP address <--> geolocation DB", e); } } } }
public GeoIP2Location(String dbPath) { try { File database = new File(dbPath); reader = new DatabaseReader.Builder(database).build(); } catch (IOException ex) { LOG.error("Unable to load MaxMind database", ex); throw new RuntimeException("Unable to load MaxMind database"); } }
@PostConstruct public void init() { try { db = getClass().getResourceAsStream("/GeoLite2-Country.mmdb"); reader = new DatabaseReader.Builder(db).build(); } catch (IOException ex) { LOGGER.error("Error initializing GeoIP database.", ex); } }
/** * Common setup. * @throws Exception on I/O errors. */ public static void setup() throws Exception { File f = new File(file); if (!f.exists()) throw new Exception("No such file: " + file); reader = new DatabaseReader.Builder(f).build(); }
private static void loadDatabaseReader() { try (InputStream gzipInputStream = new GZIPInputStream(Resources.getResource("GeoLite2-City.tar.gz").openStream())) { try (TarArchiveInputStream tarArchiveInputStream = new TarArchiveInputStream(gzipInputStream)) { databaseReader = new DatabaseReader.Builder(seekToDbFile(tarArchiveInputStream)).withCache(new CHMCache()).build(); } } catch (Exception e) { throw new RuntimeException("failed to load geoip database", e); } }
.withCache(new CHMCache()).build()) { CountryResponse response = reader.country(InetAddress.getByName(rp.getRemoteAddr()));
/** * Reads a new Database from a given HDFS file * @param reader The DatabaseReader to use to read the file * @param hdfsFile The HDFS file to read * @param is An InputStream for use with the reader * @return The DatabaseReader that is set up with the new file * @throws IOException If there is an issue reading the file. */ public static DatabaseReader readNewDatabase(DatabaseReader reader, String hdfsFile, InputStream is) throws IOException { LOG.info("Update to GeoIP data started with {}", hdfsFile); // InputStream based DatabaseReaders are always in memory. DatabaseReader newReader = new DatabaseReader.Builder(is).withCache(new CHMCache()).build(); // If we've never set a reader, don't close the old one if (reader != null) { reader.close(); } LOG.info("Finished update to GeoIP data started with {}", hdfsFile); return newReader; }
@Override public void afterPropertiesSet() throws Exception { File database = new File(geoDBFilePath); reader = new DatabaseReader.Builder(database).build(); }
/** * Open a GeoIP2 database * @since 0.9.38 */ private DatabaseReader openGeoIP2(File geoFile) throws IOException { DatabaseReader.Builder b = new DatabaseReader.Builder(geoFile); b.withCache(new CHMCache(256)); DatabaseReader rv = b.build(); if (_log.shouldDebug()) _log.debug("Opened GeoIP2 Database, Metadata: " + rv.getMetadata()); return rv; }
private DatabaseReader getReader(URL url) { try { FileInputStream cityDatabase = new FileInputStream(MaxmindGeoIPModule.downloadOrGetFile(url)); return new DatabaseReader.Builder(cityDatabase).fileMode(MEMORY).build(); } catch (Exception e) { throw new RuntimeException(e); } }
private DatabaseReader loadReader(File file) throws IOException { return new DatabaseReader.Builder(file).build(); }