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.util;
016    
017    import com.liferay.portal.kernel.util.GetterUtil;
018    import com.liferay.portal.kernel.util.StringUtil;
019    import com.liferay.portal.kernel.util.Validator;
020    
021    import java.net.InetAddress;
022    import java.net.UnknownHostException;
023    
024    import org.apache.tools.ant.BuildException;
025    import org.apache.tools.ant.Task;
026    
027    /**
028     * @author Brian Wing Shun Chan
029     */
030    public class InetAddressTask extends Task {
031    
032            @Override
033            public void execute() throws BuildException {
034                    try {
035                            InetAddress localHost = InetAddress.getLocalHost();
036    
037                            if (Validator.isNotNull(_hostAddressProperty)) {
038                                    getProject().setUserProperty(
039                                            _hostAddressProperty, localHost.getHostAddress());
040                            }
041    
042                            if (Validator.isNotNull(_hostNameProperty)) {
043                                    getProject().setUserProperty(
044                                            _hostNameProperty, localHost.getHostName());
045                            }
046    
047                            if (Validator.isNotNull(_vmId1Property)) {
048                                    int id = GetterUtil.getInteger(
049                                            StringUtil.extractDigits(localHost.getHostName()));
050    
051                                    getProject().setUserProperty(
052                                            _vmId1Property, String.valueOf((id * 2) - 1));
053                            }
054    
055                            if (Validator.isNotNull(_vmId2Property)) {
056                                    int id = GetterUtil.getInteger(
057                                            StringUtil.extractDigits(localHost.getHostName()));
058    
059                                    getProject().setUserProperty(
060                                            _vmId2Property, String.valueOf((id * 2)));
061                            }
062                    }
063                    catch (UnknownHostException uhe) {
064                            throw new BuildException(uhe);
065                    }
066            }
067    
068            public void setHostAddressProperty(String hostAddressProperty) {
069                    _hostAddressProperty = hostAddressProperty;
070            }
071    
072            public void setHostNameProperty(String hostNameProperty) {
073                    _hostNameProperty = hostNameProperty;
074            }
075    
076            public void setVmId1Property(String vmId1Property) {
077                    _vmId1Property = vmId1Property;
078            }
079    
080            public void setVmId2Property(String vmId2Property) {
081                    _vmId2Property = vmId2Property;
082            }
083    
084            private String _hostAddressProperty;
085            private String _hostNameProperty;
086            private String _vmId1Property;
087            private String _vmId2Property;
088    
089    }