1616import java .io .IOException ;
1717import java .lang .reflect .Proxy ;
1818import java .util .ArrayList ;
19+ import java .util .Arrays ;
1920import java .util .Iterator ;
2021import java .util .List ;
2122import java .util .Map .Entry ;
6364 * </p>
6465 */
6566public final class DBusConnection extends AbstractConnection {
66- private final Logger logger = LoggerFactory .getLogger (getClass ());
67+
68+ private final Logger logger = LoggerFactory .getLogger (getClass ());
6769
6870 public static final String DEFAULT_SYSTEM_BUS_ADDRESS =
6971 "unix:path=/var/run/dbus/system_bus_socket" ;
72+ private static final String DBUS_MACHINE_ID_SYS_VAR = "DBUS_MACHINE_ID_LOCATION" ;
7073
7174 private List <String > busnames ;
7275
@@ -256,22 +259,13 @@ private AtomicInteger getConcurrentConnections() {
256259
257260 /**
258261 * Extracts the machine-id usually found in /var/lib/dbus/machine-id.
262+ * Use system variable DBUS_MACHINE_ID_LOCATION to use other location
259263 *
260264 * @return machine-id string, never null
261265 * @throws DBusException if machine-id could not be found
262266 */
263267 public static String getDbusMachineId () throws DBusException {
264- File uuidfile = new File ("/var/lib/dbus/machine-id" );
265- if (!uuidfile .exists ()) {
266- uuidfile = new File ("/usr/local/var/lib/dbus/machine-id" );
267- }
268- if (!uuidfile .exists ()) {
269- uuidfile = new File ("/etc/machine-id" );
270- }
271- if (!uuidfile .exists ()) {
272- throw new DBusException ("Cannot Resolve Session Bus Address" );
273- }
274-
268+ File uuidfile = determineMachineIdFile ();
275269 String uuid = FileIoUtil .readFileToString (uuidfile );
276270 if (StringUtil .isEmpty (uuid )) {
277271 throw new DBusException ("Cannot Resolve Session Bus Address: MachineId file is empty." );
@@ -280,6 +274,17 @@ public static String getDbusMachineId() throws DBusException {
280274 return uuid ;
281275 }
282276
277+ private static File determineMachineIdFile () throws DBusException {
278+ List <String > locationPriorityList = Arrays .asList (System .getenv (DBUS_MACHINE_ID_SYS_VAR ),
279+ "/var/lib/dbus/machine-id" , "/usr/local/var/lib/dbus/machine-id" , "/etc/machine-id" );
280+ return locationPriorityList .stream ()
281+ .filter (s -> s != null )
282+ .map (s -> new File (s ))
283+ .filter (f -> f .exists ())
284+ .findFirst ()
285+ .orElseThrow (() -> new DBusException ("Cannot Resolve Session Bus Address: MachineId file can not be found" ));
286+ }
287+
283288 private DBusConnection (String _address , boolean _shared , boolean _registerSelf , String _machineId ) throws DBusException {
284289 super (_address );
285290 busnames = new ArrayList <>();
0 commit comments