001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portlet.network.util;
016    
017    import com.liferay.portal.kernel.util.StringBundler;
018    import com.liferay.portal.kernel.util.StringPool;
019    import com.liferay.portal.kernel.util.Time;
020    import com.liferay.portal.kernel.webcache.WebCacheException;
021    import com.liferay.portal.kernel.webcache.WebCacheItem;
022    import com.liferay.portlet.network.model.DNSLookup;
023    
024    import java.net.InetAddress;
025    
026    /**
027     * @author Brian Wing Shun Chan
028     */
029    public class DNSLookupWebCacheItem implements WebCacheItem {
030    
031            public DNSLookupWebCacheItem(String domain) {
032                    _domain = domain;
033            }
034    
035            @Override
036            public Object convert(String key) throws WebCacheException {
037                    DNSLookup dnsLookup = null;
038    
039                    try {
040                            String results = null;
041    
042                            char[] array = _domain.trim().toCharArray();
043    
044                            for (int i = 0; i < array.length; i++) {
045                                    if ((array[i] != '.') && !Character.isDigit(array[i])) {
046                                            InetAddress ia = InetAddress.getByName(_domain);
047    
048                                            results = ia.getHostAddress();
049    
050                                            break;
051                                    }
052                            }
053    
054                            if (results == null) {
055                                    InetAddress[] ia = InetAddress.getAllByName(_domain);
056    
057                                    if (ia.length == 0) {
058                                            results = StringPool.BLANK;
059                                    }
060                                    else {
061                                            StringBundler sb = new StringBundler(ia.length * 2 - 1);
062    
063                                            for (int i = 0; i < ia.length; i++) {
064                                                    sb.append(ia[i].getHostName());
065    
066                                                    if ((i + 1) <= ia.length) {
067                                                            sb.append(StringPool.COMMA);
068                                                    }
069                                            }
070    
071                                            results = sb.toString();
072                                    }
073                            }
074    
075                            dnsLookup = new DNSLookup(_domain, results);
076                    }
077                    catch (Exception e) {
078                            throw new WebCacheException(_domain + " " + e.toString());
079                    }
080    
081                    return dnsLookup;
082            }
083    
084            @Override
085            public long getRefreshTime() {
086                    return _REFRESH_TIME;
087            }
088    
089            private static final long _REFRESH_TIME = Time.DAY;
090    
091            private String _domain;
092    
093    }