Skip to content

Commit b8e9f4d

Browse files
committed
First commit
0 parents  commit b8e9f4d

20 files changed

+1405
-0
lines changed

.gitignore

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# Directories #
2+
/build/
3+
bin/
4+
repos/
5+
/repos/
6+
doc/
7+
/doc/
8+
.gradle/
9+
/bin/
10+
target/
11+
12+
# OS Files #
13+
.DS_Store
14+
15+
*.class
16+
17+
# Package Files #
18+
*.jar
19+
*.war
20+
*.ear
21+
*.db
22+
23+
######################
24+
# Windows
25+
######################
26+
27+
# Windows image file caches
28+
Thumbs.db
29+
30+
# Folder config file
31+
Desktop.ini
32+
33+
######################
34+
# OSX
35+
######################
36+
37+
.DS_Store
38+
.svn
39+
40+
# Thumbnails
41+
._*
42+
43+
# Files that might appear on external disk
44+
.Spotlight-V100
45+
.Trashes
46+
47+
48+
######################
49+
# Eclipse
50+
######################
51+
52+
*.pydevproject
53+
.project
54+
.metadata
55+
bin/**
56+
tmp/**
57+
tmp/**/*
58+
*.tmp
59+
*.bak
60+
*.swp
61+
*~.nib
62+
local.properties
63+
.classpath
64+
.settings/
65+
.loadpath
66+
/src/main/resources/rebel.xml
67+
# External tool builders
68+
.externalToolBuilders/
69+
70+
# Locally stored "Eclipse launch configurations"
71+
*.launch
72+
73+
# CDT-specific
74+
.cproject
75+
76+
# PDT-specific
77+
.buildpath

build.gradle

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
apply plugin: 'groovy'
2+
apply plugin: 'eclipse'
3+
apply plugin : 'idea'
4+
5+
sourceCompatibility = 1.8
6+
version = '1.0'
7+
8+
jar {
9+
baseName = "Java DbIp"
10+
manifest {
11+
attributes 'Implementation-Title': 'Gradle Quickstart',
12+
'Implementation-Version': version
13+
}
14+
}
15+
16+
repositories {
17+
mavenCentral()
18+
}
19+
20+
dependencies {
21+
compile('com.google.guava:guava:19.0')
22+
compile('ch.qos.logback:logback-classic:1.1.4')
23+
compile('ch.qos.logback:logback-core:1.1.4')
24+
compile('org.slf4j:slf4j-api:1.7.18')
25+
testCompile('org.spockframework:spock-core:1.0-groovy-2.4')
26+
testCompile('org.codehaus.groovy:groovy-all:2.4.5')
27+
}
28+
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+
36+
test {
37+
systemProperties 'property': 'value'
38+
}
39+
40+
eclipse {
41+
classpath {
42+
containers.remove('org.eclipse.jdt.launching.JRE_CONTAINER')
43+
containers 'org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8'
44+
}
45+
}
46+
47+
48+
uploadArchives {
49+
repositories {
50+
flatDir {
51+
dirs 'repos'
52+
}
53+
}
54+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package in.ankushs.dbip.api;
2+
3+
import java.io.File;
4+
import java.net.InetAddress;
5+
6+
import org.slf4j.Logger;
7+
import org.slf4j.LoggerFactory;
8+
9+
import com.google.common.net.InetAddresses;
10+
11+
import in.ankushs.dbip.importer.ResourceImporter;
12+
import in.ankushs.dbip.lookup.GeoEntityLookupService;
13+
import in.ankushs.dbip.lookup.GeoEntityLookupServiceImpl;
14+
import in.ankushs.dbip.utils.PreConditions;
15+
16+
public final class DbIpClient {
17+
18+
private static final Logger logger = LoggerFactory.getLogger(DbIpClient.class);
19+
20+
private final File file ;
21+
private final GeoEntityLookupService lookupService = new GeoEntityLookupServiceImpl();
22+
23+
private static boolean flag = false;
24+
25+
public DbIpClient(final File csvFile){
26+
PreConditions.checkExpression(!csvFile.exists(), "file " + csvFile.getName() + " does not exist");
27+
this.file = csvFile;
28+
if(!flag){
29+
flag = true;
30+
logger.info("Loading db ip into repository ");
31+
new ResourceImporter().load(csvFile);
32+
logger.info("Loading finished");
33+
}
34+
else{
35+
logger.info(" DbIp csv file has already been loaded ");
36+
37+
}
38+
}
39+
40+
public GeoEntity lookup(final String ip){
41+
InetAddress inetAddress = null;
42+
try{
43+
inetAddress = InetAddresses.forString(ip);
44+
}
45+
catch(final IllegalArgumentException ex){
46+
logger.error("",ex);
47+
throw ex;
48+
}
49+
return lookup(inetAddress);
50+
}
51+
52+
public GeoEntity lookup(final InetAddress inetAddress){
53+
PreConditions.checkNull(inetAddress, "inetAddress cannot be null");
54+
return lookupService.lookup(inetAddress);
55+
}
56+
57+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package in.ankushs.dbip.api;
2+
3+
import in.ankushs.dbip.utils.PreConditions;
4+
5+
public final class GeoEntity {
6+
private final String city;
7+
private final String country;
8+
private final String state;
9+
10+
public GeoEntity(final Builder builder){
11+
this.city = builder.city;
12+
this.country = builder.country;
13+
this.state = builder.state;
14+
}
15+
16+
public static class Builder{
17+
private String city;
18+
private String country;
19+
private String state;
20+
21+
public Builder withCity(final String city ){
22+
this.city = city;
23+
return this;
24+
}
25+
26+
public Builder withCountry(final String country ){
27+
this.country = country;
28+
return this;
29+
}
30+
31+
public Builder withState(final String state ){
32+
this.state = state;
33+
return this;
34+
}
35+
36+
public GeoEntity build(){
37+
return new GeoEntity(this);
38+
}
39+
}
40+
41+
public String getCity() {
42+
return city;
43+
}
44+
45+
public String getCountry() {
46+
return country;
47+
}
48+
49+
public String getState() {
50+
return state;
51+
}
52+
53+
@Override
54+
public String toString() {
55+
return "GeoEntity [city=" + city + ", country=" + country + ", state=" + state + "]";
56+
}
57+
58+
59+
}

0 commit comments

Comments
 (0)