Skip to content

Commit 4d5e239

Browse files
committed
Merge pull request #2 from ankushs92/dev
Dev
2 parents a301a3e + dca495c commit 4d5e239

27 files changed

+571
-497
lines changed

README.md

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,62 @@
1-
Java wrapper for the DB-IP dataset
1+
# Java DB-IP
2+
A simple to use Java library for the freely available DB-IP [IP address to city dataset](https://db-ip.com/db/download/city).
3+
**Requires Java 8**
4+
5+
#Before you begin
6+
The entire dataset is loaded into a [TreeMap](https://docs.oracle.com/javase/8/docs/api/allclasses-noframe.html) . Make sure that you have about **3 GB of Heap space** available to load it.
7+
8+
#Get
9+
10+
With maven :
11+
12+
```xml
13+
14+
<dependency>
15+
<groupId>in.ankushs</groupId>
16+
<artifactId>Java-DB-IP</artifactId>
17+
<version>1.0</version>
18+
</dependency>
19+
```
20+
21+
Or gradle:
22+
23+
```groovy
24+
25+
compile('in.ankushs:Java-DB-IP:1.0')
26+
27+
```
28+
29+
The Javadocs for the latest release can be found [here](http://www.javadoc.io/doc/in.ankushs/Java-DB-IP/1.0)
30+
31+
32+
#Instructions
33+
In order to get geographical information for an ip address, just pass the `dbip-city-latest.csv.gz` as a File object to `DbIpClient` as follows:
34+
35+
```java
36+
File gzip = new File(PATH_TO_dbip-city-latest.csv.gz);
37+
DbIpClient client = new DbIpClient(gzip);
38+
```
39+
40+
Once the data is loaded from the file into memory , any subsequent invocation of the above code **would not** re-load the data .
41+
42+
Next,just fetch the data for a ip ,like so :
43+
44+
```java
45+
DbIpClient client = new DbIpClient(gzip);
46+
GeoEntity geoEntity = client.lookup("31.45.127.255");
47+
String city = geoEntity.getCity();
48+
String country = geoEntity.getCountry();
49+
String province = geoEntity.getProvince();
50+
51+
System.out.println("city : " + city);
52+
System.out.println("province : " + province);
53+
System.out.println("country : " + country);
54+
```
55+
56+
This prints :
57+
```
58+
city : Oslo
59+
province : Oslo
60+
country : Norway
61+
```
62+
That's pretty much it.

build.gradle

Lines changed: 79 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
apply plugin: 'groovy'
22
apply plugin: 'eclipse'
33
apply plugin : 'idea'
4-
4+
apply plugin: 'maven'
5+
apply plugin: 'signing'
6+
7+
58
sourceCompatibility = 1.8
9+
group ="in.ankushs"
10+
archivesBaseName = "dbip"
611
version = '1.0'
712

813
jar {
9-
baseName = "Java DbIp"
14+
baseName = "Java-DB-IP"
1015
manifest {
1116
attributes 'Implementation-Title': 'Gradle Quickstart',
1217
'Implementation-Version': version
@@ -26,13 +31,13 @@ dependencies {
2631
testCompile('org.codehaus.groovy:groovy-all:2.4.5')
2732
}
2833

29-
sourceSets {
30-
main {
31-
java { srcDirs = ["src/main/java"] } // no source dirs for the java compiler
32-
groovy { srcDirs = ["src/test/groovy"] } // compile everything in src/ with groovy
33-
}
34-
}
35-
34+
//sourceSets {
35+
// main {
36+
// java { srcDirs = ["src/main/java"] } // no source dirs for the java compiler
37+
// groovy { srcDirs = ["src/test/groovy"] } // compile everything in src/ with groovy
38+
// }
39+
// }
40+
//
3641
test {
3742
systemProperties 'property': 'value'
3843
}
@@ -44,11 +49,73 @@ eclipse {
4449
}
4550
}
4651

52+
53+
task javadocJar(type: Jar) {
54+
classifier = 'javadoc'
55+
from javadoc
56+
}
57+
58+
task sourcesJar(type: Jar) {
59+
classifier = 'sources'
60+
from sourceSets.main.allSource
61+
}
62+
63+
artifacts {
64+
archives javadocJar, sourcesJar
65+
}
66+
67+
signing {
68+
sign configurations.archives
69+
}
70+
4771

4872
uploadArchives {
4973
repositories {
50-
flatDir {
51-
dirs 'repos'
52-
}
74+
mavenDeployer{
75+
beforeDeployment{MavenDeployment deployment->
76+
signing.signPom(deployment)
77+
}
78+
79+
repository(url: "https://oss.sonatype.org/service/local/staging/deploy/maven2") {
80+
authentication(userName: ossrhUsername, password: ossrhPassword)
81+
}
82+
83+
pom{
84+
project{
85+
name 'Java DbIP'
86+
description 'A Java library for the DB-IP IP address to city dataset '
87+
packaging 'jar'
88+
url 'https://github.com/ankushs92/Java-DB-IP'
89+
inceptionYear '2016'
90+
91+
scm {
92+
connection 'scm:git:git@github.com:ankushs92/Java-DB-IP.git'
93+
developerConnection 'scm:git:git@github.com:ankushs92/Java-DB-IP.git'
94+
url 'git@github.com:ankushs92/Java-DB-IP.git'
95+
}
96+
97+
licenses {
98+
license {
99+
name 'The MIT License'
100+
url 'https://github.com/ankushs92/Java-DB-IP/blob/master/LICENSE.md'
101+
}
102+
}
103+
developers {
104+
developer {
105+
name 'Ankush Sharma'
106+
email 'ankush20058@gmail.com'
107+
}
108+
}
109+
issueManagement{
110+
system 'Github'
111+
url 'https://github.com/ankushs92/Java-DB-IP/issues'
112+
113+
}
114+
}
115+
}
116+
flatDir {
117+
dirs 'repos'
118+
}
53119
}
120+
}
54121
}

gradle.properties

Whitespace-only changes.

src/main/java/in/ankushs/dbip/api/DbIpClient.java

Lines changed: 48 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,53 +2,90 @@
22

33
import java.io.File;
44
import java.net.InetAddress;
5+
import java.util.concurrent.Executor;
6+
import java.util.concurrent.Executors;
57

68
import org.slf4j.Logger;
79
import org.slf4j.LoggerFactory;
810

911
import com.google.common.net.InetAddresses;
1012

13+
import in.ankushs.dbip.exceptions.InvalidIPException;
1114
import in.ankushs.dbip.importer.ResourceImporter;
1215
import in.ankushs.dbip.lookup.GeoEntityLookupService;
1316
import in.ankushs.dbip.lookup.GeoEntityLookupServiceImpl;
1417
import in.ankushs.dbip.utils.PreConditions;
15-
18+
/**
19+
*
20+
* Class responsible for loading data into the JVM and also an API for resolving ip.
21+
* @author Ankush Sharma
22+
*/
1623
public final class DbIpClient {
1724

1825
private static final Logger logger = LoggerFactory.getLogger(DbIpClient.class);
19-
26+
27+
/*
28+
* The dbip-city-latest.csv.gz file
29+
* */
2030
private final File file ;
21-
private final GeoEntityLookupService lookupService = new GeoEntityLookupServiceImpl();
31+
//Singleton
32+
private final GeoEntityLookupService lookupService = GeoEntityLookupServiceImpl.getInstance();
2233

34+
/*
35+
* Indicates whether the file has been loaded into the JVM.
36+
* */
2337
private static boolean flag = false;
2438

25-
public DbIpClient(final File csvFile){
26-
PreConditions.checkExpression(!csvFile.exists(), "file " + csvFile.getName() + " does not exist");
27-
this.file = csvFile;
39+
40+
/**
41+
* Create a new DbIpClient .
42+
* Once an instance has been created, the allLoaded flag is set to true.
43+
* Any futher initializations of the DbIpClient will not load data into memory again.
44+
* @param gzip The dbip-city-latest.csv.gz file as a File object.
45+
* @throws IllegalArgumentException if {@code gzip} does not exist.
46+
*/
47+
public DbIpClient(final File gzip){
48+
PreConditions.checkExpression(!gzip.exists(), "file " + gzip.getName() + " does not exist");
49+
this.file = gzip;
2850
if(!flag){
2951
flag = true;
3052
logger.info("Loading db ip into repository ");
31-
new ResourceImporter().load(csvFile);
53+
ResourceImporter.getInstance().load(gzip);
3254
logger.info("Loading finished");
3355
}
3456
else{
3557
logger.info(" DbIp csv file has already been loaded ");
36-
3758
}
3859
}
39-
60+
61+
62+
/**
63+
* Returns a loaded GeoEntity object for a given {@code ip}
64+
* If nothing can be resolved for an {@code ip} , then the city,state and country
65+
* for the GeoEntity will be set to 'Unknown'
66+
* Any futher initializations of the DbIpClient will not load data into memory again.
67+
* @param ip The ip (as String) to be resolved.
68+
* @return a GeoEntity object representing city,state and province info
69+
*/
4070
public GeoEntity lookup(final String ip){
4171
InetAddress inetAddress = null;
4272
try{
4373
inetAddress = InetAddresses.forString(ip);
4474
}
4575
catch(final IllegalArgumentException ex){
46-
logger.error("",ex);
47-
throw ex;
76+
logger.error("Invalid IP given",ex);
77+
throw new InvalidIPException("Invalid IP passed");
4878
}
4979
return lookup(inetAddress);
5080
}
5181

82+
/**
83+
* Returns a loaded GeoEntity object for a given {@code inetAddress}
84+
* If nothing can be resolved for an {@code inetAddress} , then the city,state and country
85+
* for the GeoEntity will be set to 'Unknown'
86+
* @param inetAddress The inetAddress (as InetAddress) to be resolved.
87+
* @return a GeoEntity object representing city,state and province info
88+
*/
5289
public GeoEntity lookup(final InetAddress inetAddress){
5390
PreConditions.checkNull(inetAddress, "inetAddress cannot be null");
5491
return lookupService.lookup(inetAddress);

src/main/java/in/ankushs/dbip/api/GeoEntity.java

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,26 @@
11
package in.ankushs.dbip.api;
22

33
import in.ankushs.dbip.utils.PreConditions;
4-
4+
/**
5+
*
6+
*
7+
* @author Ankush Sharma
8+
*/
59
public final class GeoEntity {
610
private final String city;
711
private final String country;
8-
private final String state;
12+
private final String province;
913

1014
public GeoEntity(final Builder builder){
1115
this.city = builder.city;
1216
this.country = builder.country;
13-
this.state = builder.state;
17+
this.province = builder.province;
1418
}
1519

1620
public static class Builder{
1721
private String city;
1822
private String country;
19-
private String state;
23+
private String province;
2024

2125
public Builder withCity(final String city ){
2226
this.city = city;
@@ -28,8 +32,8 @@ public Builder withCountry(final String country ){
2832
return this;
2933
}
3034

31-
public Builder withState(final String state ){
32-
this.state = state;
35+
public Builder withProvince(final String province ){
36+
this.province = province;
3337
return this;
3438
}
3539

@@ -46,13 +50,13 @@ public String getCountry() {
4650
return country;
4751
}
4852

49-
public String getState() {
50-
return state;
53+
public String getProvince() {
54+
return province;
5155
}
5256

5357
@Override
5458
public String toString() {
55-
return "GeoEntity [city=" + city + ", country=" + country + ", state=" + state + "]";
59+
return "GeoEntity [city=" + city + ", country=" + country + ", province=" + province + "]";
5660
}
5761

5862

0 commit comments

Comments
 (0)