|
1 | 1 | package com.publiccms.views.method.tools; |
2 | 2 |
|
| 3 | +import java.io.File; |
3 | 4 | import java.io.IOException; |
4 | | -import java.io.InputStream; |
5 | 5 | import java.util.List; |
6 | 6 |
|
7 | | -import org.apache.commons.io.IOUtils; |
| 7 | +import javax.annotation.PreDestroy; |
| 8 | + |
8 | 9 | import org.apache.commons.lang3.StringUtils; |
9 | | -import org.lionsoul.ip2region.xdb.LongByteArray; |
10 | | -import org.lionsoul.ip2region.xdb.Searcher; |
11 | | -import org.lionsoul.ip2region.xdb.Version; |
| 10 | +import org.lionsoul.ip2region.service.Config; |
| 11 | +import org.lionsoul.ip2region.service.InvalidConfigException; |
| 12 | +import org.lionsoul.ip2region.service.Ip2Region; |
| 13 | +import org.lionsoul.ip2region.xdb.XdbException; |
12 | 14 | import org.springframework.stereotype.Component; |
13 | 15 |
|
14 | 16 | import com.publiccms.common.base.BaseMethod; |
| 17 | +import com.publiccms.common.constants.CommonConstants; |
| 18 | +import com.publiccms.common.tools.AnalyzerDictUtils; |
15 | 19 | import com.publiccms.common.tools.CommonUtils; |
16 | | -import com.publiccms.common.tools.IpUtils; |
17 | 20 | import com.publiccms.views.pojo.entities.IpRegion; |
18 | 21 |
|
19 | 22 | import freemarker.template.TemplateModel; |
|
43 | 46 | */ |
44 | 47 | @Component |
45 | 48 | public class GetIpRegionMethod extends BaseMethod { |
46 | | - private static LongByteArray longByteArray; |
| 49 | + private static Ip2Region ip2Region = null; |
47 | 50 |
|
48 | | - public GetIpRegionMethod() { |
49 | | - try (InputStream inputStream = GetIpRegionMethod.class.getResourceAsStream("/ip2region_v4.xdb")) { |
50 | | - longByteArray = new LongByteArray(IOUtils.toByteArray(inputStream)); |
51 | | - } catch (IOException e) { |
52 | | - e.printStackTrace(); |
53 | | - longByteArray = null; |
| 51 | + private void init() { |
| 52 | + if (null == ip2Region) { |
| 53 | + synchronized (ip2Region) { |
| 54 | + try { |
| 55 | + if (null == ip2Region) { |
| 56 | + Config v4Config = null; |
| 57 | + File ipv4 = getFilePath("ip2region_v4.xdb"); |
| 58 | + File ipv6 = getFilePath("ip2region_v6.xdb"); |
| 59 | + if (ipv4.exists()) { |
| 60 | + Config.custom().setCachePolicy(Config.VIndexCache).setSearchers(20).setXdbFile(ipv4).asV4(); |
| 61 | + } else { |
| 62 | + Config.custom().setCachePolicy(Config.BufferCache) |
| 63 | + .setXdbInputStream(GetIpRegionMethod.class.getResourceAsStream("/ip2region_v4.xdb")).asV4(); |
| 64 | + } |
| 65 | + Config v6Config = null; |
| 66 | + if (ipv6.exists()) { |
| 67 | + v6Config = Config.custom().setCachePolicy(Config.VIndexCache).setSearchers(20).setXdbFile(ipv6) |
| 68 | + .asV6(); |
| 69 | + } |
| 70 | + ip2Region = Ip2Region.create(v4Config, v6Config); |
| 71 | + } |
| 72 | + } catch (IOException | XdbException | InvalidConfigException e) { |
| 73 | + } |
| 74 | + } |
54 | 75 | } |
55 | 76 | } |
56 | 77 |
|
| 78 | + private File getFilePath(String path) { |
| 79 | + File file = new File(CommonUtils.joinString(CommonConstants.CMS_FILEPATH, AnalyzerDictUtils.DIR_DICT, path)); |
| 80 | + return file; |
| 81 | + } |
| 82 | + |
57 | 83 | @Override |
58 | 84 | public Object execute(List<TemplateModel> arguments) throws TemplateModelException { |
59 | 85 | String ip = getString(0, arguments); |
60 | | - if (CommonUtils.notEmpty(ip) && IpUtils.isIpv4(ip) && null != longByteArray) { |
61 | | - Searcher searcher = null; |
| 86 | + if (CommonUtils.notEmpty(ip)) { |
62 | 87 | try { |
63 | | - searcher = Searcher.newWithBuffer(Version.IPv4, longByteArray); |
64 | | - return getIpRegion(searcher.search(ip)); |
| 88 | + init(); |
| 89 | + if (null != ip2Region) { |
| 90 | + return getIpRegion(ip2Region.search(ip)); |
| 91 | + } |
65 | 92 | } catch (Exception e) { |
66 | 93 | log.error(e.getMessage()); |
67 | | - } finally { |
68 | | - if (null != searcher) { |
69 | | - try { |
70 | | - searcher.close(); |
71 | | - } catch (IOException e) { |
72 | | - } |
73 | | - } |
74 | 94 | } |
75 | 95 | } |
76 | 96 | return null; |
@@ -104,4 +124,13 @@ private static IpRegion getIpRegion(String region) { |
104 | 124 | return ipRegion; |
105 | 125 | } |
106 | 126 |
|
| 127 | + @PreDestroy |
| 128 | + public void destroy() { |
| 129 | + if (null != ip2Region) { |
| 130 | + try { |
| 131 | + ip2Region.close(); |
| 132 | + } catch (InterruptedException e) { |
| 133 | + } |
| 134 | + } |
| 135 | + } |
107 | 136 | } |
0 commit comments