changeset 565:706785db6c08

Get host name from gethostname review-thread: http://icedtea.classpath.org/pipermail/thermostat/2012-August/002913.html reviewed-by: vanaltj
author Mario Torre <neugens.limasoftware@gmail.com>
date Thu, 23 Aug 2012 20:02:48 +0200
parents 23f2ec2942c6
children b4360eae631a
files agent/core/Makefile agent/core/pom.xml agent/core/src/main/java/com/redhat/thermostat/backend/system/HostInfoBuilder.java agent/core/src/main/java/com/redhat/thermostat/utils/hostname/HostName.java agent/core/src/main/native/HostName.c distribution/pom.xml
diffstat 6 files changed, 241 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/agent/core/Makefile	Thu Aug 23 20:02:48 2012 +0200
@@ -0,0 +1,42 @@
+CC         = gcc
+JAVAH      = javah
+CFLAGS     = -c -Wall -fPIC
+LDFLAGS    = -fPIC -shared
+COPY       = cp -a
+
+CLASSPATH  = target/classes/
+TARGET_DIR = target
+
+INCLUDE    = -I $(TARGET_DIR) -I $(JAVA_HOME)/include/ -I $(JAVA_HOME)/include/linux
+SOURCES    = src/main/native/HostName.c
+TARGET     = $(TARGET_DIR)/HostName.c
+OBJECTS    = $(TARGET:.c=.o)
+
+EXECUTABLE = libHostNameWrapper.so
+
+.phoni:
+JNI_LIST = com.redhat.thermostat.utils.hostname.HostName
+
+$(JNI_LIST):
+	$(JAVAH) -force -classpath $(CLASSPATH) -d $(TARGET_DIR) $(JNI_LIST)
+
+all: $(JNI_LIST) init $(SOURCES) $(EXECUTABLE)
+
+.phoni:
+init:
+	$(COPY) $(SOURCES) $(TARGET)
+
+$(EXECUTABLE): $(OBJECTS)
+	$(CC) $(OBJECTS) -o  $(TARGET_DIR)/$@ $(LDFLAGS)
+
+.c.o:
+	$(CC) $(CFLAGS) $(INCLUDE)  $< -o $@
+
+clean-lib:
+	rm $(TARGET_DIR)/$(EXECUTABLE)
+	
+clean-obj:
+	rm $(OBJECTS) $(TARGET)
+	
+clean: clean-obj clean-lib
+	
\ No newline at end of file
--- a/agent/core/pom.xml	Wed Aug 22 16:29:35 2012 -0400
+++ b/agent/core/pom.xml	Thu Aug 23 20:02:48 2012 +0200
@@ -73,6 +73,11 @@
     </dependency>
     <dependency>
       <groupId>com.redhat.thermostat</groupId>
+      <artifactId>thermostat-hostname</artifactId>
+      <version>${project.version}</version>
+    </dependency>
+    <dependency>
+      <groupId>com.redhat.thermostat</groupId>
       <artifactId>thermostat-tools</artifactId>
       <version>${project.version}</version>
     </dependency>
@@ -104,6 +109,7 @@
               com.redhat.thermostat.backend,
             </Export-Package>
             <Private-Package>
+              com.redhat.thermostat.utils.hostname,
               com.redhat.thermostat.backend.sample,
               com.redhat.thermostat.backend.system,
             </Private-Package>
@@ -112,7 +118,70 @@
           </instructions>
         </configuration>
       </plugin>
+      
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+          <artifactId>maven-surefire-plugin</artifactId>
+          <configuration>
+            <argLine>-Djava.library.path=${project.build.directory}/</argLine>
+          </configuration>
+      </plugin>
+      
+      <plugin>
+        <groupId>org.codehaus.mojo</groupId>
+        <artifactId>exec-maven-plugin</artifactId>
+        <version>1.2.1</version>
+        <executions>  
+          <execution>
+            <phase>compile</phase>
+            <goals>
+              <goal>exec</goal>
+            </goals>
+          </execution>  
+        </executions>
+        <configuration>
+          <executable>make</executable>
+          <arguments>
+            <argument>all</argument>
+          </arguments>
+          <systemProperties>
+            <systemProperty>
+              <key>JAVA_HOME</key>
+              <value>${java.home}</value>
+            </systemProperty>
+          </systemProperties>
+        </configuration>
+      </plugin>
     </plugins>
+    <pluginManagement>
+      <plugins>
+        <!--This plugin's configuration is used to store Eclipse m2e settings only. It has no influence on the Maven build itself.-->
+        <plugin>
+          <groupId>org.eclipse.m2e</groupId>
+          <artifactId>lifecycle-mapping</artifactId>
+          <version>1.0.0</version>
+          <configuration>
+            <lifecycleMappingMetadata>
+              <pluginExecutions>
+                <pluginExecution>
+                  <pluginExecutionFilter>
+                    <groupId>org.codehaus.mojo</groupId>
+                    <artifactId>exec-maven-plugin</artifactId>
+                    <versionRange>[1.2.1,)</versionRange>
+                    <goals>
+                      <goal>exec</goal>
+                    </goals>
+                  </pluginExecutionFilter>
+                  <action>
+                    <ignore></ignore>
+                  </action>
+                </pluginExecution>
+              </pluginExecutions>
+            </lifecycleMappingMetadata>
+          </configuration>
+        </plugin>
+      </plugins>
+    </pluginManagement>   
   </build>
 
 </project>
--- a/agent/core/src/main/java/com/redhat/thermostat/backend/system/HostInfoBuilder.java	Wed Aug 22 16:29:35 2012 -0400
+++ b/agent/core/src/main/java/com/redhat/thermostat/backend/system/HostInfoBuilder.java	Thu Aug 23 20:02:48 2012 +0200
@@ -46,6 +46,7 @@
 import com.redhat.thermostat.common.Constants;
 import com.redhat.thermostat.common.model.HostInfo;
 import com.redhat.thermostat.common.utils.LoggingUtils;
+import com.redhat.thermostat.utils.hostname.HostName;
 
 public class HostInfoBuilder {
 
@@ -150,15 +151,27 @@
     }
 
     String getHostName() {
+        String hostname = null;
+        
         try {
             InetAddress localAddress = null;
             localAddress = InetAddress.getLocalHost();
-            return getHostName(localAddress);
+            hostname = getHostName(localAddress);
         } catch (UnknownHostException uhe) {
             logger.log(Level.WARNING, "unable to get hostname", uhe);
         }
-
-        return Constants.AGENT_LOCAL_HOSTNAME;
+        
+        // if fails, try to get hostname without dns lookup
+        if (hostname == null) {
+            hostname = HostName.getLocalHostName();
+        }
+        
+        // still null, use localhost
+        if (hostname == null) {
+            hostname = Constants.AGENT_LOCAL_HOSTNAME;
+        }
+        
+        return hostname;
     }
 
     String getHostName(InetAddress localAddress) {
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/agent/core/src/main/java/com/redhat/thermostat/utils/hostname/HostName.java	Thu Aug 23 20:02:48 2012 +0200
@@ -0,0 +1,50 @@
+/*
+ * Copyright 2012 Red Hat, Inc.
+ *
+ * This file is part of Thermostat.
+ *
+ * Thermostat is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published
+ * by the Free Software Foundation; either version 2, or (at your
+ * option) any later version.
+ *
+ * Thermostat is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Thermostat; see the file COPYING.  If not see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * Linking this code with other modules is making a combined work
+ * based on this code.  Thus, the terms and conditions of the GNU
+ * General Public License cover the whole combination.
+ *
+ * As a special exception, the copyright holders of this code give
+ * you permission to link this code with independent modules to
+ * produce an executable, regardless of the license terms of these
+ * independent modules, and to copy and distribute the resulting
+ * executable under terms of your choice, provided that you also
+ * meet, for each linked independent module, the terms and conditions
+ * of the license of that module.  An independent module is a module
+ * which is not derived from or based on this code.  If you modify
+ * this code, you may extend this exception to your version of the
+ * library, but you are not obligated to do so.  If you do not wish
+ * to do so, delete this exception statement from your version.
+ */
+
+package com.redhat.thermostat.utils.hostname;
+
+public class HostName {
+
+    static {
+        System.loadLibrary("HostNameWrapper");
+    }
+    
+    public static String getLocalHostName() {
+        return getHostName();
+    }
+    
+    private static native String getHostName();
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/agent/core/src/main/native/HostName.c	Thu Aug 23 20:02:48 2012 +0200
@@ -0,0 +1,60 @@
+/*
+ * Copyright 2012 Red Hat, Inc.
+ *
+ * This file is part of Thermostat.
+ *
+ * Thermostat is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published
+ * by the Free Software Foundation; either version 2, or (at your
+ * option) any later version.
+ *
+ * Thermostat is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Thermostat; see the file COPYING.  If not see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * Linking this code with other modules is making a combined work
+ * based on this code.  Thus, the terms and conditions of the GNU
+ * General Public License cover the whole combination.
+ *
+ * As a special exception, the copyright holders of this code give
+ * you permission to link this code with independent modules to
+ * produce an executable, regardless of the license terms of these
+ * independent modules, and to copy and distribute the resulting
+ * executable under terms of your choice, provided that you also
+ * meet, for each linked independent module, the terms and conditions
+ * of the license of that module.  An independent module is a module
+ * which is not derived from or based on this code.  If you modify
+ * this code, you may extend this exception to your version of the
+ * library, but you are not obligated to do so.  If you do not wish
+ * to do so, delete this exception statement from your version.
+ */
+
+#include "com_redhat_thermostat_utils_hostname_HostName.h"
+
+#include <jni.h>
+#include <unistd.h>
+#include <string.h>
+
+#include <netdb.h>
+
+#ifndef NI_MAXHOST
+#define NI_MAXHOST 1025
+#endif /* NI_MAXHOST */
+
+JNIEXPORT jstring JNICALL
+Java_com_redhat_thermostat_utils_hostname_HostName_getHostName
+  (JNIEnv *env, jclass HostNameClass)
+{
+    char hostname[NI_MAXHOST];
+    memset(hostname, 0, sizeof(hostname));
+
+    if (gethostname(hostname,  sizeof(hostname)) == 0) {
+        return (*env)->NewStringUTF(env, hostname);
+    }
+    return NULL;
+}
--- a/distribution/pom.xml	Wed Aug 22 16:29:35 2012 -0400
+++ b/distribution/pom.xml	Thu Aug 23 20:02:48 2012 +0200
@@ -173,7 +173,9 @@
                 <!--  also copy the native libraries -->
                 <copy file="${main.basedir}/keyring/target/libGnomeKeyringWrapper.so"
                       todir="${project.build.directory}/libs/native" />
-                
+                <copy file="${main.basedir}/agent/core/target/libHostNameWrapper.so"
+                      todir="${project.build.directory}/libs/native" />
+                      
               </target>
             </configuration>
             <goals>
@@ -323,7 +325,7 @@
         <artifactId>thermostat-keyring</artifactId>
         <version>${project.version}</version>
     </dependency>
-    
+
     <!--  thread plugin -->
     <dependency>
         <groupId>com.redhat.thermostat</groupId>