001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
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    }