Hi,

I previously sent something like this to ant-user, but Stefan told me this is 
the place it should go.

In order to be able to support using the hostname in build files, attached 2 
files for 2 different approaches:
a) Project.java.diff: Sets the property "ant.hostname" during the Ant 
bootstrap process. This patch is made against Project.java 1.109.
b) HostnameTask.java: A separate task for setting the hostname.

Personally, I prefer the first approach, but I don't make decisions :-)


Ernst

-- 
Ernst de Haan
EuroNet Internet B.V.

    "Come to me all who are weary and burdened
        and I will give you rest" -- Jesus Christ
/*
 * The Apache Software License, Version 1.1
 *
 * Copyright (c) 2000-2002 The Apache Software Foundation.  All rights
 * reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * 3. The end-user documentation included with the redistribution, if
 *    any, must include the following acknowlegement:
 *       "This product includes software developed by the
 *        Apache Software Foundation (http://www.apache.org/)."
 *    Alternately, this acknowlegement may appear in the software itself,
 *    if and wherever such third-party acknowlegements normally appear.
 *
 * 4. The names "The Jakarta Project", "Ant", and "Apache Software
 *    Foundation" must not be used to endorse or promote products derived
 *    from this software without prior written permission. For written
 *    permission, please contact [EMAIL PROTECTED]
 *
 * 5. Products derived from this software may not be called "Apache"
 *    nor may "Apache" appear in their names without prior written
 *    permission of the Apache Group.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 * ====================================================================
 *
 * This software consists of voluntary contributions made by many
 * individuals on behalf of the Apache Software Foundation.  For more
 * information on the Apache Software Foundation, please see
 * <http://www.apache.org/>.
 */

package org.apache.tools.ant.taskdefs;

import java.net.InetAddress;
import java.net.UnknownHostException;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.Task;

/**
 * Task that puts the hostname in a property. The name of the property can be
 * specified in the <code>property</code> attribute. If it is not specified,
 * then the default name of the property will be <code>hostname</code>.
 *
 * @author <a href="mailto:[EMAIL PROTECTED]">Ernst de Haan</a>
 */
public class HostnameTask extends Task {

    /**
     * The default name of the property. The default property name is
     * <code>hostname</code>.
     */
    private final static String DEFAULT_PROPERTY_NAME = "hostname";

    /**
     * The name of the property to put the hostname in. By default it is set
     * to [EMAIL PROTECTED] #DEFAULT_PROPERTY_NAME}.
     */
    private String propertyName = DEFAULT_PROPERTY_NAME;

    /**
     * Sets the property name. If the argument is <code>null</code>, then the
     * property name will be reset to the default.
     *
     * @param newPropertyName
     *    the property name or <code>null</code>.
     */
    public void setProperty(String newPropertyName) {

        // If the argument is null or empty, then fallback to the default
        if (newPropertyName == null) {
            propertyName = DEFAULT_PROPERTY_NAME;
        } else {
            newPropertyName = newPropertyName.trim();
            if ("".equals(newPropertyName)) {
                propertyName = DEFAULT_PROPERTY_NAME;

            // ...otherwise use the trimmed name
            } else {
                propertyName = newPropertyName;
            }
        }
    }

    public void execute() throws BuildException {

        // Do not override an existing property with the same name
        if (project.getUserProperty(propertyName) != null) {
            log("Override ignored for property \"" + propertyName + "\".", Project.MSG_VERBOSE);
        }

        // Determine the IP address of the current host
        InetAddress localhost;
        try {
            localhost = InetAddress.getLocalHost();
        } catch (UnknownHostException unknownHostException) {
            log("Unable to determine internet address of localhost. Not setting property \"" + propertyName + "\".", Project.MSG_ERR);
            return;
        } catch (SecurityException securityException) {
            log("Determining internet address of localhost is disallowed by security manager. Not setting property \"" + propertyName + "\".", Project.MSG_ERR);
            return;
        }

        // Determine the hostname
        String hostname;
        try {
            hostname = localhost.getHostName();
        } catch (SecurityException securityException) {
            log("Determining hostname of localhost is disallowed by security manager. Not setting property \"" + propertyName + "\".", Project.MSG_ERR);
            return;
        }

        // Make *sure* that we have the hostname
        if (hostname == null || "".equals(hostname.trim()) || "localhost".equals(hostname.trim())) {
            log("Determining hostname of localhost failed. Not setting property \"" + propertyName + "\".", Project.MSG_ERR);
            return;
        }

        // Set the property to contain the hostname
        project.setUserProperty(propertyName, hostname);
    }
}
Index: Project.java
===================================================================
RCS file: /home/cvspublic/jakarta-ant/src/main/org/apache/tools/ant/Project.java,v
retrieving revision 1.109
diff -d -u -r1.109 Project.java
--- Project.java	1 Jun 2002 12:26:37 -0000	1.109
+++ Project.java	3 Jun 2002 11:16:23 -0000
@@ -57,6 +57,8 @@
 import java.io.File;
 import java.io.InputStream;
 import java.io.IOException;
+import java.net.InetAddress;
+import java.net.UnknownHostException;
 import java.util.Hashtable;
 import java.util.Vector;
 import java.util.Properties;
@@ -242,6 +244,7 @@
      */
     public void init() throws BuildException {
         setJavaVersionProperty();
+        setHostnameProperty();
 
         String defs = "/org/apache/tools/ant/taskdefs/defaults.properties";
 
@@ -772,6 +775,28 @@
             + System.getProperty("java.home"), MSG_VERBOSE);
 
         log("Detected OS: " + System.getProperty("os.name"), MSG_VERBOSE);
+    }
+
+    /**
+     * Sets the <code>ant.hostname</code> property. A verbose log message is
+     * generated to record the hostname and IP address. If the hostname cannot
+     * be determined, then the <code>hostname</code> property is not set.
+     */
+    public void setHostnameProperty() {
+        try {
+            InetAddress localhost = InetAddress.getLocalHost();
+            if (localhost == null) {
+                log("Unable to determine IP address of this host.", MSG_ERR);
+                return;
+            }
+            String hostname = localhost.getHostName();
+            setPropertyInternal("ant.hostname", hostname);
+            log("Detected hostname: " + hostname, MSG_VERBOSE);
+        } catch (UnknownHostException unknownHostException) {
+            log("Unable to determine IP address of this host.", MSG_ERR);
+        } catch (SecurityException securityException) {
+            log("Not allowed to determine IP address of this host.", MSG_ERR);
+        }
     }
 
     /**

--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to