1
19
20 package com.liferay.portlet.network.util;
21
22 import com.liferay.portal.kernel.util.Time;
23 import com.liferay.portal.kernel.webcache.WebCacheException;
24 import com.liferay.portal.kernel.webcache.WebCacheItem;
25 import com.liferay.portlet.network.model.DNSLookup;
26
27 import java.net.InetAddress;
28
29
35 public class DNSLookupWebCacheItem implements WebCacheItem {
36
37 public DNSLookupWebCacheItem(String domain) {
38 _domain = domain;
39 }
40
41 public Object convert(String key) throws WebCacheException {
42 DNSLookup dnsLookup = null;
43
44 try {
45 String results = null;
46
47 char[] array = _domain.trim().toCharArray();
48
49 for (int i = 0; i < array.length; i++) {
50 if ((array[i] != '.') && !Character.isDigit(array[i])) {
51 InetAddress ia = InetAddress.getByName(_domain);
52
53 results = ia.getHostAddress();
54
55 break;
56 }
57 }
58
59 if (results == null) {
60 StringBuilder sb = new StringBuilder();
61
62 InetAddress[] ia = InetAddress.getAllByName(_domain);
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 dnsLookup = new DNSLookup(_domain, results);
76 }
77 catch (Exception e) {
78 throw new WebCacheException(_domain + " " + e.toString());
79 }
80
81 return dnsLookup;
82 }
83
84 public long getRefreshTime() {
85 return _REFRESH_TIME;
86 }
87
88 private static final long _REFRESH_TIME = Time.DAY;
89
90 private String _domain;
91
92 }