1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.util;
16  
17  import com.liferay.portal.kernel.util.GetterUtil;
18  import com.liferay.portal.kernel.util.StringUtil;
19  import com.liferay.portal.kernel.util.Validator;
20  
21  import java.net.InetAddress;
22  import java.net.UnknownHostException;
23  
24  import org.apache.tools.ant.BuildException;
25  import org.apache.tools.ant.Task;
26  
27  /**
28   * <a href="InetAddressTask.java.html"><b><i>View Source</i></b></a>
29   *
30   * @author Brian Wing Shun Chan
31   */
32  public class InetAddressTask extends Task {
33  
34      public void execute() throws BuildException {
35          try {
36              InetAddress localHost = InetAddress.getLocalHost();
37  
38              if (Validator.isNotNull(_hostAddressProperty)) {
39                  getProject().setUserProperty(
40                      _hostAddressProperty, localHost.getHostAddress());
41              }
42  
43              if (Validator.isNotNull(_hostNameProperty)) {
44                  getProject().setUserProperty(
45                      _hostNameProperty, localHost.getHostName());
46              }
47  
48              if (Validator.isNotNull(_vmId1Property)) {
49                  int id = GetterUtil.getInteger(
50                      StringUtil.extractDigits(localHost.getHostName()));
51  
52                  getProject().setUserProperty(
53                      _vmId1Property, String.valueOf((id * 2) - 1));
54              }
55  
56              if (Validator.isNotNull(_vmId2Property)) {
57                  int id = GetterUtil.getInteger(
58                      StringUtil.extractDigits(localHost.getHostName()));
59  
60                  getProject().setUserProperty(
61                      _vmId2Property, String.valueOf((id * 2)));
62              }
63          }
64          catch (UnknownHostException uhe) {
65              throw new BuildException(uhe);
66          }
67      }
68  
69      public void setHostAddressProperty(String hostAddressProperty) {
70          _hostAddressProperty = hostAddressProperty;
71      }
72  
73      public void setHostNameProperty(String hostNameProperty) {
74          _hostNameProperty = hostNameProperty;
75      }
76  
77      public void setVmId1Property(String vmId1Property) {
78          _vmId1Property = vmId1Property;
79      }
80  
81      public void setVmId2Property(String vmId2Property) {
82          _vmId2Property = vmId2Property;
83      }
84  
85      private String _hostAddressProperty;
86      private String _hostNameProperty;
87      private String _vmId1Property;
88      private String _vmId2Property;
89  
90  }