Changeset: 2d880f90be2a for monetdb-java
URL: https://dev.monetdb.org/hg/monetdb-java/rev/2d880f90be2a
Modified Files:
        src/main/java/org/monetdb/mcl/net/ClientInfo.java
        src/main/java/org/monetdb/mcl/net/MapiSocket.java
Branch: default
Log Message:

Try to use reasonable defaults for ClientInfo


diffs (97 lines):

diff --git a/src/main/java/org/monetdb/mcl/net/ClientInfo.java 
b/src/main/java/org/monetdb/mcl/net/ClientInfo.java
--- a/src/main/java/org/monetdb/mcl/net/ClientInfo.java
+++ b/src/main/java/org/monetdb/mcl/net/ClientInfo.java
@@ -2,6 +2,11 @@ package org.monetdb.mcl.net;
 
 import org.monetdb.jdbc.MonetDriver;
 
+import java.io.File;
+import java.lang.management.ManagementFactory;
+import java.lang.management.RuntimeMXBean;
+import java.net.InetAddress;
+import java.net.UnknownHostException;
 import java.sql.ClientInfoStatus;
 import java.sql.SQLClientInfoException;
 import java.util.Collections;
@@ -13,7 +18,9 @@ public class ClientInfo {
 
        private static final String defaultClientLibrary = findClientLibrary();
 
-       private static final int defaultPid = findPid();
+       private static final String defaultApplicationName = 
findApplicationName();
+
+       private static final String defaultPid = findPid();
 
        private final Properties props;
 
@@ -21,17 +28,47 @@ public class ClientInfo {
                props = new Properties();
                props.setProperty("ClientHostname", defaultHostname);
                props.setProperty("ClientLibrary", defaultClientLibrary);
-               props.setProperty("ClientPid", "" + defaultPid);
-               props.setProperty("ApplicationName", "");
+               props.setProperty("ClientPid", defaultPid);
+               props.setProperty("ApplicationName", defaultApplicationName);
                props.setProperty("ClientRemark", "");
        }
 
        private static String findHostname() {
-               return "my host";
+               try {
+                       return InetAddress.getLocalHost().getHostName();
+               } catch (UnknownHostException e) {
+                       return "";
+               }
        }
 
-       private static int findPid() {
-               return 42;
+       private static String findApplicationName() {
+               String appName = "";
+               try {
+                       String prop = System.getProperty("sun.java.command");
+                       if (prop != null) {
+                               // we want only the command, and not the 
arguments
+                               prop = prop.split("\\s", 2)[0];
+                               // keep only the basename5
+                               int idx = prop.lastIndexOf(File.separatorChar);
+                               if (idx >= 0)
+                                       prop = prop.substring(idx + 1);
+                               appName = prop;
+                       }
+               } catch (SecurityException e) {
+                       // ignore
+               }
+
+               return appName;
+       }
+
+       private static String findPid() {
+               try {
+                       RuntimeMXBean mxbean = 
ManagementFactory.getRuntimeMXBean();
+                       String pidAtHostname = mxbean.getName();
+                       return pidAtHostname.split("@", 2)[0];
+               } catch (RuntimeException e) {
+                       return "";
+               }
        }
 
        private static String findClientLibrary() {
diff --git a/src/main/java/org/monetdb/mcl/net/MapiSocket.java 
b/src/main/java/org/monetdb/mcl/net/MapiSocket.java
--- a/src/main/java/org/monetdb/mcl/net/MapiSocket.java
+++ b/src/main/java/org/monetdb/mcl/net/MapiSocket.java
@@ -502,8 +502,12 @@ public final class MapiSocket {
                if (parts.length > 9 && target.isClientInfo()) {
                        clientInfo = new ClientInfo();
                        try {
-                               clientInfo.set("ApplicationName", 
target.getClientApplication());
-                               clientInfo.set("ClientRemark", 
target.getClientRemark());
+                               String clientApplication = 
target.getClientApplication();
+                               String clientRemark = target.getClientRemark();
+                               if (!clientApplication.isEmpty())
+                                       clientInfo.set("ApplicationName", 
clientApplication);
+                               if (!clientRemark.isEmpty())
+                                       clientInfo.set("ClientRemark", 
clientRemark);
                        } catch (SQLClientInfoException e) {
                                String keys = String.join(", ", 
e.getFailedProperties().keySet());
                                throw new MCLException("Could not set 
ClientInfo properties: " + keys, e);
_______________________________________________
checkin-list mailing list -- checkin-list@monetdb.org
To unsubscribe send an email to checkin-list-le...@monetdb.org

Reply via email to