001    /**
002     * Copyright (c) 2000-2010 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            public Object convert(String key) throws WebCacheException {
036                    DNSLookup dnsLookup = null;
037    
038                    try {
039                            String results = null;
040    
041                            char[] array = _domain.trim().toCharArray();
042    
043                            for (int i = 0; i < array.length; i++) {
044                                    if ((array[i] != '.') && !Character.isDigit(array[i])) {
045                                            InetAddress ia = InetAddress.getByName(_domain);
046    
047                                            results = ia.getHostAddress();
048    
049                                            break;
050                                    }
051                            }
052    
053                            if (results == null) {
054                                    InetAddress[] ia = InetAddress.getAllByName(_domain);
055    
056                                    if (ia.length == 0) {
057                                            results = StringPool.BLANK;
058                                    }
059                                    else {
060                                            StringBundler sb = new StringBundler(ia.length * 2 - 1);
061    
062                                            for (int i = 0; i < ia.length; i++) {
063                                                    sb.append(ia[i].getHostName());
064    
065                                                    if (i + 1 <= ia.length) {
066                                                            sb.append(",");
067                                                    }
068                                            }
069    
070                                            results = sb.toString();
071                                    }
072                            }
073    
074                            dnsLookup = new DNSLookup(_domain, results);
075                    }
076                    catch (Exception e) {
077                            throw new WebCacheException(_domain + " " + e.toString());
078                    }
079    
080                    return dnsLookup;
081            }
082    
083            public long getRefreshTime() {
084                    return _REFRESH_TIME;
085            }
086    
087            private static final long _REFRESH_TIME = Time.DAY;
088    
089            private String _domain;
090    
091    }