001    /**
002     * Copyright (c) 2000-2011 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.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    }