1   /**
2    * Copyright (c) 2000-2010 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   *
12   *
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  }