Java源码示例:com.maxmind.geoip.LookupService
示例1
public GeoIp(String dataDirPath, boolean inMemory) {
try {
File dir = new File(dataDirPath);
if( ! dir.exists()) throw new FileNotFoundException("Can't find GeoIP dir: "+dataDirPath);
File ip4File = new File(dir, "GeoIP.dat");
File ip6File = new File(dir, "GeoIPv6.dat");
if( ! ip4File.exists() && ! ip6File.exists()){
File zipFile = new File(dir, "GeoIP-db.zip");
if( ! zipFile.exists()) throw new FileNotFoundException("Can't find GeoIP db: "+zipFile);
unzip(zipFile, dir);
}
int readMode = inMemory? LookupService.GEOIP_MEMORY_CACHE : LookupService.GEOIP_STANDARD;
if( ip4File.exists()) ip4Mapper = new LookupService(ip4File, readMode);
if( ip6File.exists()) ip6Mapper = new LookupService(ip6File, readMode);
}catch(IOException e){
log.error("can't load get ip files: "+e);
}
}
示例2
private static LookupService getLookupServiceV4()
{
synchronized (LOOKUP_SERVICE_LOCK)
{
if (lookupServiceV4Failure)
return null;
if (lookupServiceV4 != null)
return lookupServiceV4;
try
{
return lookupServiceV4 = getLookupService("/usr/share/GeoIP/GeoIP.dat");
}
catch (Exception e)
{
lookupServiceV4Failure = true;
return null;
}
}
}
示例3
private static LookupService getLookupServiceV6()
{
synchronized (LOOKUP_SERVICE_LOCK)
{
if (lookupServiceV6Failure)
return null;
if (lookupServiceV6 != null)
return lookupServiceV6;
try
{
return lookupServiceV6 = getLookupService("/usr/share/GeoIP/GeoIPv6.dat");
}
catch (Exception e)
{
lookupServiceV6Failure = true;
return null;
}
}
}
示例4
public FeedEntry GetLocationServices() {
FeedEntry entry = null;
try {
if (this.database == null) {
return entry;
}
LookupService lookup = new LookupService(this.database, LookupService.GEOIP_MEMORY_CACHE);
Location locationServices = lookup.getLocation(LocationInfo.getIp());
if (locationServices == null) {
return entry;
}
String City = locationServices.city;
String CountryCode = locationServices.countryCode;
entry = new FeedEntry();
entry.setLocation(City);
entry.setCountryCode(CountryCode);
entry.setDays(7);
} catch (Exception ex) {
ex.printStackTrace();
return entry;
}
return entry;
}
示例5
public Location getLocation(String ipAddress) {
try {
LookupService maxmind = new LookupService(MAXMINDDB, LookupService.GEOIP_MEMORY_CACHE);
Location location = maxmind.getLocation(ipAddress);
return location;
} catch (IOException ex) {
Logger.getLogger(GeoIP.class.getName()).log(Level.SEVERE, null, ex);
}
return null;
}
示例6
@Override
public void setup()
{
try {
reader = new LookupService(databasePath, LookupService.GEOIP_MEMORY_CACHE | LookupService.GEOIP_CHECK_CACHE);
} catch (IOException ex) {
throw new RuntimeException(ex);
}
}
示例7
private static LookupService getLookupService(String path) throws IOException
{
final File file = new File(path);
if (! (file.exists() && file.isFile() && file.canRead()))
throw new FileNotFoundException();
return new LookupService(file);
}
示例8
private void init(TypedProperties props) throws MegatronException {
String dbFile = props.getString(AppProperties.GEO_IP_CITY_DATABASE_FILE_KEY, GEO_IP_CITY_DATABASE_FILE);
try {
geoIpLookup = new LookupService(dbFile, LookupService.GEOIP_MEMORY_CACHE);
} catch (IOException e) {
String msg = "Cannot initialize GeoIP-service. Database need to be downloaded (see conf/geoip-db/readme.txt) Database file: " + dbFile;
throw new MegatronException(msg, e);
}
}
示例9
private void init(TypedProperties props) throws MegatronException {
String dbFile = props.getString(AppProperties.GEO_IP_ASN_DATABASE_FILE_KEY, GEO_IP_ASN_DATABASE_FILE);
try {
geoIpLookup = new LookupService(dbFile, LookupService.GEOIP_MEMORY_CACHE);
} catch (IOException e) {
String msg = "Cannot initialize GeoIP-service. Database need to be downloaded (see conf/geoip-db/readme.txt) Database file: " + dbFile;
throw new MegatronException(msg, e);
}
}
示例10
private void initCountry(TypedProperties props) throws MegatronException {
String dbFile = props.getString(AppProperties.GEO_IP_COUNTRY_DATABASE_FILE_KEY, null);
if (dbFile == null) {
// Use the old deprecated property name
dbFile = props.getString(AppProperties.GEO_IP_DATABASE_FILE_KEY, GEO_IP_DATABASE_FILE);
}
try {
geoIpLookup = new LookupService(dbFile, LookupService.GEOIP_MEMORY_CACHE);
} catch (IOException e) {
String msg = "Cannot initialize GeoIP-service. Database file: " + dbFile;
throw new MegatronException(msg, e);
}
log.info("Country lookups will use the MaxMind country database.");
}
示例11
@Override
public void afterPropertiesSet() throws Exception {
lookupService = new LookupService(ip2CountryLookupDataFilename, LookupService.GEOIP_CHECK_CACHE);
}
示例12
public static void exportToKML() {
String filename = UtilFunctions.saveFile("*.kml");
if (filename.isEmpty()) {
return;
}
String kmlBody = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
+ "<kml xmlns=\"http://earth.google.com/kml/2.2\">\n"
+ "<Document>\n"
+ "%s"
+ "</Document>\n</kml>\n";
String kmlPlace = "<Placemark>\n"
+ "<name>%s</name>\n"
+ "<description>%s</description>\n"
+ "<open>true</open>\n"
+ "<Point>"
+ "<coordinates>%s,%s</coordinates></Point>\n"
+ "</Placemark>\n";
try {
LookupService cl = new LookupService(ConfigSettings.getGeoIPCityFile(),
LookupService.GEOIP_MEMORY_CACHE);
String kmlBodyPayload = "";
for (String hostName : DataStore.getHosts()) {
for (String ipAddress : DataStore.getIpAddressesForHost(hostName)) {
Location l = cl.getLocation(ipAddress);
if (l != null) {
kmlBodyPayload += String.format(kmlPlace, ipAddress,
hostName, String.valueOf(l.longitude), String.valueOf(l.latitude));
}
}
}
String kmlData = String.format(kmlBody, kmlBodyPayload);
try (BufferedWriter out = new BufferedWriter(new FileWriter(filename))) {
out.write(kmlData);
}
} catch (IOException ex) {
Logger.getLogger("networkMapview.btnExportToKMLActionPerformed").log(Level.SEVERE, null, ex);
}
}