Skip to content

Commit

Permalink
Add NativeDesktop#getHostName (and use it)
Browse files Browse the repository at this point in the history
  • Loading branch information
koppor committed Jun 7, 2023
1 parent 84899aa commit 151ea22
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 11 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ Note that this project **does not** adhere to [Semantic Versioning](http://semve
- `log.txt` now contains an entry if a BibTeX entry could not be parsed.
- `log.txt` now contains debug messages. Debugging needs to be enabled explicitly. [#9678](https://github.com/JabRef/jabref/pull/9678)
- `log.txt` does not contain entries for non-found files during PDF indexing. [#9678](https://github.com/JabRef/jabref/pull/9678)
- The hostname is now determined using environment variables (`COMPUTERNAME`/`HOSTNAME`) first. [#9910](https://github.com/JabRef/jabref/pull/9910)
- We improved the Medline importer to correctly import ISO dates for `revised`. [#9536](https://github.com/JabRef/jabref/issues/9536)
- To avoid cluttering of the directory, We always delete the `.sav` file upon successful write. [#9675](https://github.com/JabRef/jabref/pull/9675)
- We improved the unlinking/deletion of multiple linked files of an entry using the <kbd>Delete</kbd> key. [#9473](https://github.com/JabRef/jabref/issues/9473)
Expand Down
19 changes: 19 additions & 0 deletions src/main/java/org/jabref/gui/desktop/os/NativeDesktop.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,23 @@ public Path getSslDirectory() {
"ssl",
OS.APP_DIR_APP_AUTHOR));
}

public String getHostName() {
String hostName;
// Following code inspired by https://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/SystemUtils.html#getHostName--
// See also https://stackoverflow.com/a/20793241/873282
hostName = System.getenv("HOSTNAME");
if (StringUtil.isBlank(hostName)) {
hostName = System.getenv("COMPUTERNAME");
}
if (StringUtil.isBlank(hostName)) {
try {
hostName = InetAddress.getLocalHost().getHostName();
} catch (UnknownHostException e) {
LoggerFactory.getLogger(OS.class).info("Hostname not found. Using \"localhost\" as fallback.", e);
hostName = "localhost";
}
}
return hostName;
}
}
16 changes: 5 additions & 11 deletions src/main/java/org/jabref/preferences/JabRefPreferences.java
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ public class JabRefPreferences implements PreferencesService {
* Cache variables
*/
private Map<String, Set<Field>> entryEditorTabList;
private String userName;
private String userAndHost;

private LibraryPreferences libraryPreferences;
private TelemetryPreferences telemetryPreferences;
Expand Down Expand Up @@ -2019,17 +2019,11 @@ public InternalPreferences getInternalPreferences() {
}

private String getUserAndHost() {
if (StringUtil.isNotBlank(userName)) {
return userName;
}

try {
userName = get(DEFAULT_OWNER) + '-' + InetAddress.getLocalHost().getHostName();
return userName;
} catch (UnknownHostException ex) {
LOGGER.error("Hostname not found. Please go to https://docs.jabref.org/ to find possible problem resolution", ex);
return get(DEFAULT_OWNER);
if (StringUtil.isNotBlank(userAndHost)) {
return userAndHost;
}
userAndHost = get(DEFAULT_OWNER) + '-' + OS.getNativeDesktop().getHostName();
return userAndHost;
}

//*************************************************************************************************************
Expand Down

0 comments on commit 151ea22

Please sign in to comment.