提交 389cb809 编写于 作者: J jialinsun

refactor inservice path

上级 9d8ae77f
package com.dianping.cat.broker.api.page;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedHashMap;
import java.util.Map;
import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable;
import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException;
import com.dianping.cat.Cat;
public class IpService implements Initializable {
private int[] m_areaIds;
private Map<Integer, Area> m_areas;
private int[] m_corpIds;
private Map<Integer, Corporation> m_corps;
private long[] m_ends;
private long[] m_starts;
private int[] m_foreignAreaIds;
private Map<Integer, Area> m_foreignAreas;
private long[] m_foreignEnds;
private long[] m_foreignStarts;
private IpInfo findIpInfo(long ip) {
IpInfo ipInfo = findChinaIp(ip);
if (ipInfo == null) {
ipInfo = findForeignIp(ip);
}
return ipInfo;
}
private IpInfo findForeignIp(long ip) {
int low = 0, high = m_foreignStarts.length - 1, mid;
while (low <= high) {
mid = (low + high) / 2;
if (ip >= m_foreignStarts[mid] && ip <= m_foreignEnds[mid]) {
IpInfo ipInfo = new IpInfo();
Area area = m_foreignAreas.get(m_foreignAreaIds[mid]);
if (area != null) {
ipInfo.setNation(area.getNation());
ipInfo.setProvince(area.getProvince());
ipInfo.setCity(area.getCity());
} else {
ipInfo.setNation("国外");
ipInfo.setProvince("国外其他");
ipInfo.setCity("国外其他");
}
ipInfo.setChannel("国外其他");
return ipInfo;
} else if (ip < m_foreignStarts[mid]) {
high = mid - 1;
} else {
low = mid + 1;
}
}
return buildDefaultIpInfo("国外", "国外其他");
}
private IpInfo buildDefaultIpInfo(String nation, String other) {
IpInfo ipInfo = new IpInfo();
ipInfo.setNation(nation);
ipInfo.setProvince(other);
ipInfo.setCity(other);
ipInfo.setChannel(other);
return ipInfo;
}
private IpInfo findChinaIp(long ip) {
int low = 0, high = m_starts.length - 1, mid;
while (low <= high) {
mid = (low + high) / 2;
if (ip >= m_starts[mid] && ip <= m_ends[mid]) {
IpInfo ipInfo = new IpInfo();
Area area = m_areas.get(m_areaIds[mid]);
if (area != null) {
ipInfo.setNation(area.getNation());
ipInfo.setProvince(area.getProvince());
ipInfo.setCity(area.getCity());
} else {
ipInfo.setNation("其他");
ipInfo.setProvince("其他");
ipInfo.setCity("其他");
}
Corporation corp = m_corps.get(m_corpIds[mid]);
if (corp != null) {
ipInfo.setChannel(corp.getName());
} else {
ipInfo.setChannel("其他");
}
return ipInfo;
} else if (ip < m_starts[mid]) {
high = mid - 1;
} else {
low = mid + 1;
}
}
return null;
}
public IpInfo findIpInfoByString(String ip) {
try {
String[] segments = ip.split("\\.");
if (segments.length != 4) {
return null;
}
long ip_num = Long.parseLong(segments[0]) << 24;
ip_num += Integer.parseInt(segments[1]) << 16;
ip_num += Integer.parseInt(segments[2]) << 8;
ip_num += Integer.parseInt(segments[3]);
return findIpInfo(ip_num);
} catch (Exception e) {
return null;
}
}
private void initAreaMap(String areaFile) {
try {
BufferedReader areaReader = new BufferedReader(new InputStreamReader(new FileInputStream(areaFile)));
String line;
String[] strs;
int id;
m_areas = new LinkedHashMap<Integer, Area>();
while ((line = areaReader.readLine()) != null) {
strs = line.split(":");
id = Integer.parseInt(strs[1]);
String[] areaStr = strs[0].split("\\|");
Area area = new Area();
area.setAreaId(id);
area.setNation(areaStr[0]);
area.setProvince(areaStr[1]);
area.setCity(areaStr[2]);
m_areas.put(id, area);
}
areaReader.close();
} catch (Exception e) {
Cat.logError(e);
}
}
private void initForeignAreaMap(String areaFile) {
try {
BufferedReader areaReader = new BufferedReader(new InputStreamReader(new FileInputStream(areaFile)));
String line;
String[] strs;
String[] ids;
m_foreignAreas = new LinkedHashMap<Integer, Area>();
while ((line = areaReader.readLine()) != null) {
strs = line.split(":");
ids = strs[1].split(",");
Area area = new Area();
area.setNation("");
area.setProvince("");
area.setCity(strs[0]);
for (String id : ids) {
m_foreignAreas.put(Integer.valueOf(id), area);
}
}
areaReader.close();
} catch (Exception e) {
Cat.logError(e);
}
}
private void initCorpMap(String corpFile) {
try {
BufferedReader corpReader = new BufferedReader(new InputStreamReader(new FileInputStream(corpFile)));
String line;
String[] strs;
int id;
m_corps = new LinkedHashMap<Integer, Corporation>();
while ((line = corpReader.readLine()) != null) {
strs = line.split(":");
Corporation corp = new Corporation();
id = Integer.parseInt(strs[1]);
corp.setCorporationId(id);
corp.setName(strs[0]);
m_corps.put(id, corp);
}
corpReader.close();
} catch (Exception e) {
Cat.logError(e);
}
}
@Override
public void initialize() throws InitializationException {
String areaFile = IpService.class.getResource("/config/area_china").getFile();
String corpFile = IpService.class.getResource("/config/corp_china").getFile();
String ipFile = IpService.class.getResource("/config/iptable_china").getFile();
initAreaMap(areaFile);
initCorpMap(corpFile);
initIpTable(ipFile);
String foreignAreaFile = IpService.class.getResource("/config/area_foreign").getFile();
String foreignIpFile = IpService.class.getResource("/config/iptable_foreign").getFile();
initForeignAreaMap(foreignAreaFile);
initForeignIpTable(foreignIpFile);
}
public void initIpTable(String ipFile) {
BufferedReader reader = null;
try {
reader = new BufferedReader(new InputStreamReader(new FileInputStream(ipFile)));
int size = Integer.parseInt(reader.readLine());
String line;
String[] strs;
m_starts = new long[size];
m_ends = new long[size];
m_areaIds = new int[size];
m_corpIds = new int[size];
for (int i = 0; i < size; i++) {
line = reader.readLine();
strs = line.split(":");
m_starts[i] = Long.parseLong(strs[0]);
m_ends[i] = Long.parseLong(strs[1]);
m_areaIds[i] = Integer.parseInt(strs[2]);
m_corpIds[i] = Integer.parseInt(strs[3]);
}
} catch (IOException e) {
Cat.logError(e);
} finally {
try {
reader.close();
} catch (Exception e) {
Cat.logError(e);
}
}
}
public void initForeignIpTable(String ipFile) {
BufferedReader reader = null;
try {
reader = new BufferedReader(new InputStreamReader(new FileInputStream(ipFile)));
int size = Integer.parseInt(reader.readLine());
String line;
String[] strs;
m_foreignStarts = new long[size];
m_foreignEnds = new long[size];
m_foreignAreaIds = new int[size];
for (int i = 0; i < size; i++) {
line = reader.readLine();
strs = line.split(":");
m_foreignStarts[i] = Long.parseLong(strs[0]);
m_foreignEnds[i] = Long.parseLong(strs[1]);
m_foreignAreaIds[i] = Integer.parseInt(strs[2]);
}
} catch (IOException e) {
Cat.logError(e);
} finally {
try {
reader.close();
} catch (Exception e) {
Cat.logError(e);
}
}
}
public static class Area {
private Integer m_areaId;
private String m_city;
private String m_nation;
private String m_province;
public Integer getAreaId() {
return m_areaId;
}
public String getCity() {
return m_city;
}
public String getNation() {
return m_nation;
}
public String getProvince() {
return m_province;
}
public void setAreaId(Integer areaId) {
m_areaId = areaId;
}
public void setCity(String city) {
m_city = city;
}
public void setNation(String nation) {
m_nation = nation;
}
public void setProvince(String province) {
m_province = province;
}
}
public static class Corporation {
private Integer m_corporationId;
private String m_name;
public Integer getCorporationId() {
return m_corporationId;
}
public String getName() {
return m_name;
}
public void setCorporationId(Integer corporationId) {
m_corporationId = corporationId;
}
public void setName(String name) {
m_name = name;
}
}
public static class IpInfo {
private String m_channel;
private String m_city;
private String m_nation;
private String m_province;
public String getChannel() {
return m_channel;
}
public String getCity() {
return m_city;
}
public String getNation() {
return m_nation;
}
public String getProvince() {
return m_province;
}
public void setChannel(String name) {
m_channel = name;
}
public void setCity(String city) {
m_city = city;
}
public void setNation(String nation) {
m_nation = nation;
}
public void setProvince(String province) {
m_province = province;
}
}
}
......@@ -17,12 +17,13 @@ import org.unidal.lookup.annotation.Inject;
import com.dianping.cat.Cat;
import com.dianping.cat.CatConstants;
import com.dianping.cat.Monitor;
import com.dianping.cat.broker.api.page.IpService.IpInfo;
import com.dianping.cat.config.url.UrlPatternConfigManager;
import com.dianping.cat.message.Event;
import com.dianping.cat.message.Metric;
import com.dianping.cat.message.Transaction;
import com.dianping.cat.message.internal.DefaultMetric;
import com.dianping.cat.service.IpService;
import com.dianping.cat.service.IpService.IpInfo;
import com.site.lookup.util.StringUtils;
public class MonitorManager implements Initializable, LogEnabled {
......
......@@ -20,13 +20,13 @@ import com.dianping.cat.Cat;
import com.dianping.cat.broker.api.app.AppData;
import com.dianping.cat.broker.api.app.AppDataConsumer;
import com.dianping.cat.broker.api.page.Constrants;
import com.dianping.cat.broker.api.page.IpService;
import com.dianping.cat.broker.api.page.IpService.IpInfo;
import com.dianping.cat.broker.api.page.MonitorEntity;
import com.dianping.cat.broker.api.page.MonitorManager;
import com.dianping.cat.broker.api.page.RequestUtils;
import com.dianping.cat.config.app.AppConfigManager;
import com.dianping.cat.message.Event;
import com.dianping.cat.service.IpService;
import com.dianping.cat.service.IpService.IpInfo;
public class Handler implements PageHandler<Context>, LogEnabled {
......
......@@ -9,25 +9,24 @@ import org.unidal.lookup.configuration.AbstractResourceConfigurator;
import org.unidal.lookup.configuration.Component;
import com.dianping.cat.broker.api.app.AppDataConsumer;
import com.dianping.cat.broker.api.page.IpService;
import com.dianping.cat.broker.api.page.MonitorManager;
import com.dianping.cat.broker.api.page.RequestUtils;
import com.dianping.cat.build.AppDatabaseConfigurator;
import com.dianping.cat.config.app.AppDataCommandTableProvider;
import com.dianping.cat.config.app.AppDataService;
import com.dianping.cat.config.url.UrlPatternConfigManager;
import com.dianping.cat.service.IpService;
public class ComponentsConfigurator extends AbstractResourceConfigurator {
@Override
public List<Component> defineComponents() {
List<Component> all = new ArrayList<Component>();
all.add(C(IpService.class));
all.add(C(RequestUtils.class));
all.add(C(MonitorManager.class).req(UrlPatternConfigManager.class, IpService.class));
all.add(C(AppDataConsumer.class).req(AppDataService.class));
all.add(C(TableProvider.class,"app-data-command",AppDataCommandTableProvider.class));
all.add(C(TableProvider.class, "app-data-command", AppDataCommandTableProvider.class));
// database
all.add(C(JdbcDataSourceDescriptorManager.class) //
......
<plexus>
<components>
<component>
<role>com.dianping.cat.broker.api.page.IpService</role>
<implementation>com.dianping.cat.broker.api.page.IpService</implementation>
</component>
<component>
<role>com.dianping.cat.broker.api.page.RequestUtils</role>
<implementation>com.dianping.cat.broker.api.page.RequestUtils</implementation>
......@@ -16,7 +12,7 @@
<role>com.dianping.cat.config.url.UrlPatternConfigManager</role>
</requirement>
<requirement>
<role>com.dianping.cat.broker.api.page.IpService</role>
<role>com.dianping.cat.service.IpService</role>
</requirement>
</requirements>
</component>
......@@ -90,7 +86,7 @@
<implementation>com.dianping.cat.broker.api.page.MonitorManager</implementation>
<requirements>
<requirement>
<role>com.dianping.cat.broker.api.page.IpService</role>
<role>com.dianping.cat.service.IpService</role>
</requirement>
<requirement>
<role>com.dianping.cat.config.url.UrlPatternConfigManager</role>
......@@ -98,8 +94,8 @@
</requirements>
</component>
<component>
<role>com.dianping.cat.broker.api.page.IpService</role>
<implementation>com.dianping.cat.broker.api.page.IpService</implementation>
<role>com.dianping.cat.service.IpService</role>
<implementation>com.dianping.cat.service.IpService</implementation>
</component>
<component>
<role>com.dianping.cat.config.url.UrlPatternConfigManager</role>
......@@ -134,7 +130,7 @@
<role>com.dianping.cat.broker.api.app.AppDataConsumer</role>
</requirement>
<requirement>
<role>com.dianping.cat.broker.api.page.IpService</role>
<role>com.dianping.cat.service.IpService</role>
</requirement>
<requirement>
<role>com.dianping.cat.config.app.AppConfigManager</role>
......
中国|福建省|福州市:2
中国|广东省|其他:6
中国|广东省|广州市:8
中国|福建省|其他:47
中国|北京市|北京市:48
中国|北京市|其他:78
中国|内蒙古自治区|呼和浩特市:155
中国|内蒙古自治区|包头市:156
中国|内蒙古自治区|锡林郭勒盟:157
中国|内蒙古自治区|乌兰察布市:158
中国|内蒙古自治区|阿拉善盟:159
中国|内蒙古自治区|巴彦淖尔市:160
中国|内蒙古自治区|鄂尔多斯市:161
中国|内蒙古自治区|乌海市:162
中国|内蒙古自治区|呼伦贝尔市:163
中国|内蒙古自治区|赤峰市:164
中国|内蒙古自治区|兴安盟:165
中国|内蒙古自治区|通辽市:166
中国|贵州省|贵阳市:333
中国|宁夏回族自治区|银川市:334
中国|江苏省|其他:335
中国|江苏省|南京市:336
中国|安徽省|滁州市:337
中国|山东省|济南市:338
中国|山东省|德州市:339
中国|黑龙江省|鹤岗市:359
中国|黑龙江省|哈尔滨市:360
中国|黑龙江省|牡丹江市:361
中国|黑龙江省|绥化市:362
中国|黑龙江省|齐齐哈尔市:363
中国|黑龙江省|双鸭山市:364
中国|黑龙江省|鸡西市:365
中国|黑龙江省|大庆市:366
中国|黑龙江省|佳木斯市:367
中国|黑龙江省|黑河市:368
中国|黑龙江省|七台河市:369
中国|黑龙江省|伊春市:370
中国|黑龙江省|大兴安岭地区:371
中国|山西省|太原市:377
中国|陕西省|西安市:409
中国|陕西省|渭南市:410
中国|陕西省|汉中市:411
中国|陕西省|商洛市:412
中国|陕西省|安康市:413
中国|陕西省|榆林市:414
中国|陕西省|宝鸡市:415
中国|陕西省|延安市:416
中国|陕西省|咸阳市:417
中国|广东省|深圳市:915
中国|广西壮族自治区|南宁市:916
中国|广西壮族自治区|桂林市:917
中国|河南省|郑州市:934
中国|河南省|洛阳市:935
中国|河南省|平顶山市:936
中国|河南省|开封市:937
中国|河南省|安阳市:938
中国|河南省|新乡市:939
中国|河南省|焦作市:940
中国|河南省|濮阳市:941
中国|河南省|三门峡市:942
中国|河南省|许昌市:943
中国|河南省|商丘市:944
中国|河南省|信阳市:945
中国|河南省|鹤壁市:946
中国|河南省|漯河市:947
中国|河南省|周口市:948
中国|河南省|驻马店市:949
中国|河南省|南阳市:950
中国|贵州省|遵义市:952
中国|贵州省|毕节地区:953
中国|贵州省|六盘水市:954
中国|贵州省|黔西南布依族苗族自治州:955
中国|贵州省|黔南布依族苗族自治州:956
中国|贵州省|黔东南苗族侗族自治州:957
中国|贵州省|安顺市:958
中国|贵州省|铜仁地区:959
中国|广东省|东莞市:14906
中国|广东省|佛山市:14907
中国|广东省|茂名市:14908
中国|上海市|上海市:14975
中国|重庆市|重庆市:14977
中国|广东省|惠州市:14978
中国|广东省|阳江市:14979
中国|广东省|湛江市:14980
中国|广东省|中山市:14981
中国|广东省|珠海市:14982
中国|广东省|江门市:14983
中国|广东省|河源市:14984
中国|广东省|韶关市:14985
中国|广东省|汕头市:14986
中国|广东省|梅州市:14987
中国|广东省|揭阳市:14988
中国|宁夏回族自治区|石嘴山市:15006
中国|宁夏回族自治区|吴忠市:15007
中国|宁夏回族自治区|固原市:15008
中国|宁夏回族自治区|中卫市:15009
中国|广东省|潮州市:15061
中国|广东省|清远市:15062
中国|广东省|汕尾市:15063
中国|四川省|成都市:15088
中国|云南省|昆明市:15217
中国|云南省|玉溪市:15218
中国|云南省|曲靖市:15219
中国|广东省|肇庆市:15228
中国|广东省|云浮市:15229
中国|湖北省|武汉市:17901
中国|湖北省|荆州市:17902
中国|湖北省|宜昌市:17903
中国|湖北省|襄樊市:17904
中国|湖北省|孝感市:17905
中国|湖北省|黄冈市:17906
中国|湖北省|黄石市:17907
中国|湖北省|咸宁市:17908
中国|湖北省|荆门市:17909
中国|湖北省|十堰市:17910
中国|湖北省|鄂州市:17911
中国|西藏自治区|拉萨市:18175
中国|西藏自治区|日喀则地区:18176
中国|西藏自治区|山南地区:18177
中国|西藏自治区|林芝地区:18178
中国|天津市|天津市:18266
中国|河北省|石家庄市:18359
中国|河北省|衡水市:18360
中国|河北省|其他:18361
中国|河北省|保定市:18362
中国|河北省|邯郸市:18363
中国|河北省|廊坊市:18364
中国|河北省|邢台市:18365
中国|福建省|厦门市:18459
中国|福建省|南平市:18460
中国|福建省|泉州市:18461
中国|福建省|莆田市:18462
中国|福建省|宁德市:18463
中国|福建省|漳州市:18464
中国|福建省|三明市:18465
中国|福建省|龙岩市:18466
中国|河北省|唐山市:18467
中国|山东省|潍坊市:18468
中国|山东省|青岛市:18469
中国|山东省|烟台市:18470
中国|山东省|淄博市:18471
中国|山东省|聊城市:18472
中国|山东省|临沂市:18473
中国|山东省|济宁市:18474
中国|山东省|东营市:18475
中国|山东省|威海市:18476
中国|山东省|菏泽市:18477
中国|山东省|滨州市:18478
中国|甘肃省|兰州市:18479
中国|甘肃省|天水市:18480
中国|甘肃省|庆阳市:18481
中国|甘肃省|陇南市:18482
中国|甘肃省|临夏回族自治州:18483
中国|甘肃省|定西市:18484
中国|甘肃省|白银市:18485
中国|甘肃省|平凉市:18486
中国|甘肃省|金昌市:18487
中国|甘肃省|武威市:18488
中国|甘肃省|酒泉市:18489
中国|甘肃省|嘉峪关市:18490
中国|甘肃省|张掖市:18491
中国|海南省|海口市:21394
中国|海南省|文昌市:21395
中国|海南省|三亚市:21396
中国|海南省|其他:21397
中国|海南省|儋州市:21398
中国|安徽省|合肥市:21406
中国|安徽省|淮南市:21407
中国|安徽省|蚌埠市:21408
中国|浙江省|杭州市:21409
中国|浙江省|宁波市:21410
中国|浙江省|其他:21411
中国|浙江省|温州市:21412
中国|安徽省|芜湖市:21413
中国|吉林省|长春市:21418
中国|吉林省|吉林市:21419
中国|山西省|其他:21542
中国|内蒙古自治区|其他:21543
中国|辽宁省|其他:21544
中国|辽宁省|沈阳市:21545
中国|吉林省|其他:21546
中国|黑龙江省|其他:21547
中国|安徽省|其他:21548
中国|江西省|其他:21549
中国|江西省|南昌市:21550
中国|山东省|其他:21551
中国|河南省|其他:21552
中国|湖北省|其他:21553
中国|湖南省|其他:21554
中国|广西壮族自治区|其他:21555
中国|四川省|其他:21556
中国|贵州省|其他:21557
中国|云南省|其他:21558
中国|陕西省|其他:21559
中国|甘肃省|其他:21560
中国|青海省|其他:21561
中国|宁夏回族自治区|其他:21562
中国|新疆维吾尔自治区|其他:21563
中国|浙江省|丽水市:23310
中国|浙江省|台州市:23311
中国|浙江省|金华市:23312
中国|浙江省|衢州市:23313
中国|浙江省|绍兴市:23314
中国|浙江省|湖州市:23315
中国|浙江省|舟山市:23316
中国|浙江省|嘉兴市:23317
中国|湖南省|长沙市:24373
中国|湖南省|衡阳市:24374
中国|湖南省|株洲市:24375
中国|湖南省|岳阳市:24376
中国|湖南省|邵阳市:24377
中国|湖南省|娄底市:24378
中国|湖南省|永州市:24379
中国|湖南省|湘潭市:24380
中国|湖南省|常德市:24381
中国|湖南省|怀化市:24382
中国|湖南省|郴州市:24383
中国|湖南省|张家界市:24384
中国|湖南省|益阳市:24385
中国|辽宁省|大连市:24451
中国|辽宁省|葫芦岛市:24452
中国|云南省|红河哈尼族彝族自治州:24462
中国|江苏省|苏州市:24463
中国|江苏省|徐州市:24464
中国|江苏省|常州市:26837
中国|江苏省|无锡市:26882
中国|江苏省|南通市:26883
中国|江苏省|盐城市:26884
中国|江苏省|宿迁市:26885
中国|江苏省|淮安市:26886
中国|江苏省|泰州市:26887
中国|江苏省|镇江市:26888
中国|江苏省|扬州市:26889
中国|江苏省|连云港市:26890
中国|新疆维吾尔自治区|乌鲁木齐市:26895
中国|新疆维吾尔自治区|吐鲁番地区:26896
中国|新疆维吾尔自治区|和田地区:26897
中国|新疆维吾尔自治区|喀什地区:26898
中国|新疆维吾尔自治区|伊犁哈萨克自治州:26899
中国|新疆维吾尔自治区|巴音郭楞州:26900
中国|新疆维吾尔自治区|伊犁州:26901
中国|新疆维吾尔自治区|阿克苏地区:26902
中国|新疆维吾尔自治区|昌吉回族自治州:26903
中国|新疆维吾尔自治区|昌吉州:26904
中国|新疆维吾尔自治区|博尔塔拉州:26905
中国|新疆维吾尔自治区|阿勒泰地区:26906
中国|新疆维吾尔自治区|塔城地区:26907
中国|新疆维吾尔自治区|克拉玛依市:26908
中国|新疆维吾尔自治区|克孜勒苏柯州:26909
中国|新疆维吾尔自治区|自治区直辖县级行政区划:27189
中国|青海省|西宁市:27190
中国|贵州省|毕节市:28367
中国|江西省|九江市:28368
中国|江西省|上饶市:28369
中国|江西省|景德镇市:28370
中国|江西省|萍乡市:28371
中国|江西省|新余市:28372
中国|江西省|宜春市:28373
中国|江西省|吉安市:28374
中国|江西省|鹰潭市:28375
中国|江西省|抚州市:28376
中国|江西省|赣州市:28377
中国|湖北省|仙桃市:28378
中国|湖北省|随州市:28379
中国|湖北省|潜江市:28380
中国|湖北省|天门市:28381
中国|湖南省|湘西土家族苗族自治州:28410
中国|湖北省|恩施土家族苗族自治州:28411
中国|山东省|泰安市:28412
中国|山东省|枣庄市:28413
中国|山东省|日照市:28414
中国|广西壮族自治区|柳州市:28415
中国|广西壮族自治区|玉林市:28416
中国|广西壮族自治区|贺州市:28417
中国|广西壮族自治区|贵港市:28418
中国|广西壮族自治区|北海市:28475
中国|广西壮族自治区|钦州市:28476
中国|广西壮族自治区|防城港市:28477
中国|广西壮族自治区|百色市:28478
中国|广西壮族自治区|河池市:28479
中国|广西壮族自治区|梧州市:28480
中国|辽宁省|盘锦市:28641
中国|辽宁省|锦州市:28642
中国|吉林省|四平市:28643
中国|安徽省|铜陵市:28789
中国|安徽省|黄山市:28790
中国|安徽省|池州市:28791
中国|安徽省|宣城市:28792
中国|安徽省|宿州市:28793
中国|安徽省|亳州市:28794
中国|安徽省|六安市:28795
中国|安徽省|淮北市:28796
中国|安徽省|阜阳市:28797
中国|安徽省|马鞍山市:28798
中国|安徽省|安庆市:28799
中国|吉林省|辽源市:28800
中国|吉林省|通化市:28801
中国|吉林省|松原市:28802
中国|辽宁省|鞍山市:28818
中国|辽宁省|朝阳市:28819
中国|辽宁省|铁岭市:28820
中国|辽宁省|丹东市:28821
中国|辽宁省|本溪市:28822
中国|辽宁省|营口市:28823
中国|辽宁省|抚顺市:28824
中国|辽宁省|阜新市:28825
中国|辽宁省|辽阳市:28826
中国|山西省|大同市:28827
中国|山西省|长治市:28828
中国|山西省|忻州市:28829
中国|山西省|晋中市:28830
中国|山西省|临汾市:28831
中国|山西省|运城市:28832
中国|山西省|晋城市:28833
中国|山西省|朔州市:28834
中国|山西省|阳泉市:28835
中国|山西省|吕梁市:28836
中国|海南省|万宁市:28837
中国|海南省|五指山市:28838
中国|河北省|张家口市:28839
中国|河北省|沧州市:28840
中国|陕西省|铜川市:28841
中国|上海市|其他:29111
中国|广西壮族自治区|来宾市:29112
中国|河北省|秦皇岛市:29145
中国|河北省|承德市:29146
中国|海南省|琼海市:29147
中国|新疆维吾尔自治区|哈密地区:29148
中国|新疆维吾尔自治区|巴音郭楞蒙古自治州:29149
中国|新疆维吾尔自治区|博尔塔拉蒙古自治州:29150
中国|云南省|昭通市:29271
中国|云南省|临沧市:29272
中国|云南省|丽江市:29273
中国|甘肃省|甘南藏族自治州:29274
中国|山东省|莱芜市:29275
中国|四川省|自贡市:29354
中国|四川省|绵阳市:29355
中国|四川省|攀枝花市:29356
中国|四川省|宜宾市:29357
中国|四川省|阿坝藏族羌族自治州:29358
中国|四川省|遂宁市:29359
中国|四川省|资阳市:29360
中国|四川省|广安市:29361
中国|青海省|海东地区:29531
中国|青海省|海西蒙古族藏族自治州:29532
中国|青海省|海南藏族自治州:29533
中国|青海省|黄南藏族自治州:29534
中国|吉林省|白山市:29535
中国|吉林省|白城市:29536
中国|四川省|内江市:29537
中国|四川省|德阳市:29538
中国|四川省|南充市:29539
中国|四川省|泸州市:29540
中国|四川省|乐山市:29541
中国|四川省|雅安市:29542
中国|四川省|达州市:29543
中国|四川省|巴中市:29544
中国|四川省|广元市:29545
中国|四川省|眉山市:29546
中国|云南省|楚雄彝族自治州:29547
中国|云南省|文山壮族苗族自治州:29548
中国|云南省|保山市:29549
中国|湖北省|襄阳市:29582
中国|吉林省|延边朝鲜族自治州:29583
中国|其他|其他:66158
中国|西藏自治区|其他:76979
中国|天津市|其他:77122
中国|香港特别行政区|其他:77210
中国|湖北省|恩施州:78885
中国|四川省|甘孜藏族自治州:78973
中国|广西壮族自治区|崇左市:78997
中国|贵州省|铜仁市:79015
中国|云南省|德宏傣族景颇族自治州:79111
中国|云南省|西双版纳傣族自治州:79112
中国|云南省|大理白族自治州:79113
中国|云南省|普洱市:79676
中国|云南省|怒江傈僳族自治州:79708
中国|青海省|海西州:79829
中国|重庆市|其他:79844
中国|四川省|凉山彝族自治州:80241
中国|新疆维吾尔自治区|克孜勒苏柯尔克孜自治州:80431
中国|四川省|凉山州:80480
中国|云南省|思茅市:80481
中国|海南省|东方市:80495
中国|河南省|济源市:80826
中国|西藏自治区|昌都地区:80884
中国|西藏自治区|那曲地区:80885
中国|西藏自治区|阿里地区:80886
中国|青海省|海北藏族自治州:81075
中国|海南省|省直辖县级行政区划:81511
中国|贵州省|黔东南州:99448
中国|新疆维吾尔自治区|石河子市:99449
中国|吉林省|延边州:99476
中国电信:2
中国移动:3256
中国铁通:3257
中国联通:24
......@@ -3,8 +3,8 @@ package com.dianping.cat.broker;
import org.junit.Test;
import org.unidal.lookup.ComponentTestCase;
import com.dianping.cat.broker.api.page.IpService;
import com.dianping.cat.broker.api.page.IpService.IpInfo;
import com.dianping.cat.service.IpService;
import com.dianping.cat.service.IpService.IpInfo;
public class IpServiceTest extends ComponentTestCase {
@Test
......
......@@ -13,52 +13,109 @@ import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationExce
import com.dianping.cat.Cat;
public class IpService implements Initializable {
private Map<Integer, Area> m_areas;
private Map<Integer, Corporation> m_corps;
private int[] m_areaIds;
private Map<Integer, Area> m_areas;
private int[] m_corpIds;
private Map<Integer, Corporation> m_corps;
private long[] m_ends;
private long[] m_starts;
private int[] m_foreignAreaIds;
private Map<Integer, Area> m_foreignAreas;
private long[] m_foreignEnds;
private long[] m_foreignStarts;
private IpInfo findIpInfo(long ip) {
int low = 0, high = m_starts.length - 1, mid;
IpInfo ipInfo = findChinaIp(ip);
if (ipInfo == null) {
ipInfo = findForeignIp(ip);
}
return ipInfo;
}
private IpInfo findForeignIp(long ip) {
int low = 0, high = m_foreignStarts.length - 1, mid;
while (low <= high) {
mid = (low + high) / 2;
if (ip >= m_foreignStarts[mid] && ip <= m_foreignEnds[mid]) {
IpInfo ipInfo = new IpInfo();
Area area = m_foreignAreas.get(m_foreignAreaIds[mid]);
if (area != null) {
ipInfo.setNation(area.getNation());
ipInfo.setProvince(area.getProvince());
ipInfo.setCity(area.getCity());
} else {
ipInfo.setNation("国外");
ipInfo.setProvince("国外其他");
ipInfo.setCity("国外其他");
}
ipInfo.setChannel("国外其他");
return ipInfo;
} else if (ip < m_foreignStarts[mid]) {
high = mid - 1;
} else {
low = mid + 1;
}
}
return buildDefaultIpInfo("国外", "国外其他");
}
private IpInfo buildDefaultIpInfo(String nation, String other) {
IpInfo ipInfo = new IpInfo();
ipInfo.setNation("未知");
ipInfo.setProvince("未知");
ipInfo.setCity("未知");
ipInfo.setChannel("其他");
ipInfo.setNation(nation);
ipInfo.setProvince(other);
ipInfo.setCity(other);
ipInfo.setChannel(other);
return ipInfo;
}
private IpInfo findChinaIp(long ip) {
int low = 0, high = m_starts.length - 1, mid;
while (low <= high) {
mid = (low + high) / 2;
if (ip >= m_starts[mid] && ip <= m_ends[mid]) {
IpInfo ipInfo = new IpInfo();
Area area = m_areas.get(m_areaIds[mid]);
if (area != null) {
ipInfo.setNation(area.getNation());
ipInfo.setProvince(area.getProvince());
ipInfo.setCity(area.getCity());
} else {
ipInfo.setNation("其他");
ipInfo.setProvince("其他");
ipInfo.setCity("其他");
}
Corporation corp = m_corps.get(m_corpIds[mid]);
if (corp != null) {
ipInfo.setChannel(corp.getName());
} else {
ipInfo.setChannel("其他");
}
break;
return ipInfo;
} else if (ip < m_starts[mid]) {
high = mid - 1;
} else {
low = mid + 1;
}
}
return ipInfo;
return null;
}
public IpInfo findIpInfoByString(String ip) {
try {
String[] segments = ip.split("\\.");
......@@ -103,6 +160,32 @@ public class IpService implements Initializable {
}
}
private void initForeignAreaMap(String areaFile) {
try {
BufferedReader areaReader = new BufferedReader(new InputStreamReader(new FileInputStream(areaFile)));
String line;
String[] strs;
String[] ids;
m_foreignAreas = new LinkedHashMap<Integer, Area>();
while ((line = areaReader.readLine()) != null) {
strs = line.split(":");
ids = strs[1].split(",");
Area area = new Area();
area.setNation("");
area.setProvince("");
area.setCity(strs[0]);
for (String id : ids) {
m_foreignAreas.put(Integer.valueOf(id), area);
}
}
areaReader.close();
} catch (Exception e) {
Cat.logError(e);
}
}
private void initCorpMap(String corpFile) {
try {
BufferedReader corpReader = new BufferedReader(new InputStreamReader(new FileInputStream(corpFile)));
......@@ -135,6 +218,13 @@ public class IpService implements Initializable {
initAreaMap(areaFile);
initCorpMap(corpFile);
initIpTable(ipFile);
String foreignAreaFile = IpService.class.getResource("/config/area_foreign").getFile();
String foreignIpFile = IpService.class.getResource("/config/iptable_foreign").getFile();
initForeignAreaMap(foreignAreaFile);
initForeignIpTable(foreignIpFile);
}
public void initIpTable(String ipFile) {
......@@ -158,6 +248,38 @@ public class IpService implements Initializable {
m_areaIds[i] = Integer.parseInt(strs[2]);
m_corpIds[i] = Integer.parseInt(strs[3]);
}
} catch (IOException e) {
Cat.logError(e);
} finally {
try {
reader.close();
} catch (Exception e) {
Cat.logError(e);
}
}
}
public void initForeignIpTable(String ipFile) {
BufferedReader reader = null;
try {
reader = new BufferedReader(new InputStreamReader(new FileInputStream(ipFile)));
int size = Integer.parseInt(reader.readLine());
String line;
String[] strs;
m_foreignStarts = new long[size];
m_foreignEnds = new long[size];
m_foreignAreaIds = new int[size];
for (int i = 0; i < size; i++) {
line = reader.readLine();
strs = line.split(":");
m_foreignStarts[i] = Long.parseLong(strs[0]);
m_foreignEnds[i] = Long.parseLong(strs[1]);
m_foreignAreaIds[i] = Integer.parseInt(strs[2]);
}
} catch (IOException e) {
Cat.logError(e);
} finally {
......@@ -172,12 +294,12 @@ public class IpService implements Initializable {
public static class Area {
private Integer m_areaId;
private String m_city;
private String m_nation;
private String m_province;
private String m_city;
public Integer getAreaId() {
return m_areaId;
}
......@@ -236,13 +358,13 @@ public class IpService implements Initializable {
}
public static class IpInfo {
private String m_nation;
private String m_province;
private String m_channel;
private String m_city;
private String m_channel;
private String m_nation;
private String m_province;
public String getChannel() {
return m_channel;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册