Mercurial > hg > monetdb-java
comparison src/main/java/org/monetdb/mcl/net/ClientInfo.java @ 903:778959b2e0a4
Send ClientInfo on startup
Configurable through the client_info=bool, client_application=NAME,
client_remark=MESSAGE properties.
author | Joeri van Ruth <joeri.van.ruth@monetdbsolutions.com> |
---|---|
date | Fri, 14 Jun 2024 15:57:49 +0200 (10 months ago) |
parents | |
children | 2d880f90be2a |
comparison
equal
deleted
inserted
replaced
902:65d42db0c831 | 903:778959b2e0a4 |
---|---|
1 package org.monetdb.mcl.net; | |
2 | |
3 import org.monetdb.jdbc.MonetDriver; | |
4 | |
5 import java.sql.ClientInfoStatus; | |
6 import java.sql.SQLClientInfoException; | |
7 import java.util.Collections; | |
8 import java.util.Map; | |
9 import java.util.Properties; | |
10 | |
11 public class ClientInfo { | |
12 private static final String defaultHostname = findHostname(); | |
13 | |
14 private static final String defaultClientLibrary = findClientLibrary(); | |
15 | |
16 private static final int defaultPid = findPid(); | |
17 | |
18 private final Properties props; | |
19 | |
20 public ClientInfo() { | |
21 props = new Properties(); | |
22 props.setProperty("ClientHostname", defaultHostname); | |
23 props.setProperty("ClientLibrary", defaultClientLibrary); | |
24 props.setProperty("ClientPid", "" + defaultPid); | |
25 props.setProperty("ApplicationName", ""); | |
26 props.setProperty("ClientRemark", ""); | |
27 } | |
28 | |
29 private static String findHostname() { | |
30 return "my host"; | |
31 } | |
32 | |
33 private static int findPid() { | |
34 return 42; | |
35 } | |
36 | |
37 private static String findClientLibrary() { | |
38 return "monetdb-java " + MonetDriver.getDriverVersion(); | |
39 } | |
40 | |
41 public String format() { | |
42 StringBuilder builder = new StringBuilder(200); | |
43 for (String name : props.stringPropertyNames()) { | |
44 String value = props.getProperty(name); | |
45 builder.append(name); | |
46 builder.append('='); | |
47 builder.append(value); | |
48 builder.append('\n'); | |
49 } | |
50 return builder.toString(); | |
51 } | |
52 | |
53 public Properties get() { | |
54 Properties ret = new Properties(); | |
55 ret.putAll(props); | |
56 return ret; | |
57 } | |
58 | |
59 public boolean set(String name, String value) throws SQLClientInfoException { | |
60 if (value == null) | |
61 value = ""; | |
62 if (value.contains("\n")) { | |
63 Map<String, ClientInfoStatus> map = Collections.singletonMap(name, ClientInfoStatus.REASON_VALUE_INVALID); | |
64 throw new SQLClientInfoException(map); | |
65 } | |
66 if (props.containsKey(name)) { | |
67 props.setProperty(name, value); | |
68 return true; | |
69 } else { | |
70 return false; | |
71 } | |
72 } | |
73 | |
74 } |