1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
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  /**
27   * <a href="DNSLookupWebCacheItem.java.html"><b><i>View Source</i></b></a>
28   *
29   * @author Brian Wing Shun Chan
30   */
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  }