1
14
15 package com.liferay.portlet.network.util;
16
17 import com.liferay.portal.kernel.util.StringBundler;
18 import com.liferay.portal.kernel.util.StringPool;
19 import com.liferay.portal.kernel.util.Time;
20 import com.liferay.portal.kernel.webcache.WebCacheException;
21 import com.liferay.portal.kernel.webcache.WebCacheItem;
22 import com.liferay.portlet.network.model.DNSLookup;
23
24 import java.net.InetAddress;
25
26
31 public class DNSLookupWebCacheItem implements WebCacheItem {
32
33 public DNSLookupWebCacheItem(String domain) {
34 _domain = domain;
35 }
36
37 public Object convert(String key) throws WebCacheException {
38 DNSLookup dnsLookup = null;
39
40 try {
41 String results = null;
42
43 char[] array = _domain.trim().toCharArray();
44
45 for (int i = 0; i < array.length; i++) {
46 if ((array[i] != '.') && !Character.isDigit(array[i])) {
47 InetAddress ia = InetAddress.getByName(_domain);
48
49 results = ia.getHostAddress();
50
51 break;
52 }
53 }
54
55 if (results == null) {
56 InetAddress[] ia = InetAddress.getAllByName(_domain);
57
58 if (ia.length == 0) {
59 results = StringPool.BLANK;
60 }
61 else {
62 StringBundler sb = new StringBundler(ia.length * 2 - 1);
63
64 for (int i = 0; i < ia.length; i++) {
65 sb.append(ia[i].getHostName());
66
67 if (i + 1 <= ia.length) {
68 sb.append(",");
69 }
70 }
71
72 results = sb.toString();
73 }
74 }
75
76 dnsLookup = new DNSLookup(_domain, results);
77 }
78 catch (Exception e) {
79 throw new WebCacheException(_domain + " " + e.toString());
80 }
81
82 return dnsLookup;
83 }
84
85 public long getRefreshTime() {
86 return _REFRESH_TIME;
87 }
88
89 private static final long _REFRESH_TIME = Time.DAY;
90
91 private String _domain;
92
93 }