| DNSLookupWebCacheItem.java |
1 /**
2 * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3 *
4 * The contents of this file are subject to the terms of the Liferay Enterprise
5 * Subscription License ("License"). You may not use this file except in
6 * compliance with the License. You can obtain a copy of the License by
7 * contacting Liferay, Inc. See the License for the specific language governing
8 * permissions and limitations under the License, including but not limited to
9 * distribution rights of the Software.
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17 * SOFTWARE.
18 */
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 /**
30 * <a href="DNSLookupWebCacheItem.java.html"><b><i>View Source</i></b></a>
31 *
32 * @author Brian Wing Shun Chan
33 *
34 */
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 }