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.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            public void execute() throws BuildException {
033                    try {
034                            InetAddress localHost = InetAddress.getLocalHost();
035    
036                            if (Validator.isNotNull(_hostAddressProperty)) {
037                                    getProject().setUserProperty(
038                                            _hostAddressProperty, localHost.getHostAddress());
039                            }
040    
041                            if (Validator.isNotNull(_hostNameProperty)) {
042                                    getProject().setUserProperty(
043                                            _hostNameProperty, localHost.getHostName());
044                            }
045    
046                            if (Validator.isNotNull(_vmId1Property)) {
047                                    int id = GetterUtil.getInteger(
048                                            StringUtil.extractDigits(localHost.getHostName()));
049    
050                                    getProject().setUserProperty(
051                                            _vmId1Property, String.valueOf((id * 2) - 1));
052                            }
053    
054                            if (Validator.isNotNull(_vmId2Property)) {
055                                    int id = GetterUtil.getInteger(
056                                            StringUtil.extractDigits(localHost.getHostName()));
057    
058                                    getProject().setUserProperty(
059                                            _vmId2Property, String.valueOf((id * 2)));
060                            }
061                    }
062                    catch (UnknownHostException uhe) {
063                            throw new BuildException(uhe);
064                    }
065            }
066    
067            public void setHostAddressProperty(String hostAddressProperty) {
068                    _hostAddressProperty = hostAddressProperty;
069            }
070    
071            public void setHostNameProperty(String hostNameProperty) {
072                    _hostNameProperty = hostNameProperty;
073            }
074    
075            public void setVmId1Property(String vmId1Property) {
076                    _vmId1Property = vmId1Property;
077            }
078    
079            public void setVmId2Property(String vmId2Property) {
080                    _vmId2Property = vmId2Property;
081            }
082    
083            private String _hostAddressProperty;
084            private String _hostNameProperty;
085            private String _vmId1Property;
086            private String _vmId2Property;
087    
088    }