reliable initialization for permanentID (IDEA-256058)

GitOrigin-RevId: 76ea2689217a478b332b3e8d48a2482e8fe6b8be
This commit is contained in:
Eugene Zhuravlev
2020-12-17 17:08:45 +00:00
committed by intellij-monorepo-bot
parent 2d2f729f4d
commit 14b905bcc5
@@ -29,47 +29,50 @@ public final class PermanentInstallationID {
}
private static String calculateInstallationId() {
final ApplicationInfoEx appInfo = ApplicationInfoImpl.getShadowInstance();
final Preferences oldPrefs = Preferences.userRoot();
final String oldValue = appInfo.isVendorJetBrains()? oldPrefs.get(OLD_USER_ON_MACHINE_ID_KEY, null) : null; // compatibility with previous versions
String installationId = null;
try {
final ApplicationInfoEx appInfo = ApplicationInfoImpl.getShadowInstance();
final Preferences oldPrefs = Preferences.userRoot();
final String oldValue = appInfo.isVendorJetBrains()? oldPrefs.get(OLD_USER_ON_MACHINE_ID_KEY, null) : null; // compatibility with previous versions
final String companyName = appInfo.getShortCompanyName();
final Preferences prefs = Preferences.userRoot().node(StringUtil.isEmptyOrSpaces(companyName)? "jetbrains" : StringUtil.toLowerCase(companyName));
final String companyName = appInfo.getShortCompanyName();
final Preferences prefs = Preferences.userRoot().node(StringUtil.isEmptyOrSpaces(companyName)? "jetbrains" : StringUtil.toLowerCase(companyName));
String installationId = prefs.get(INSTALLATION_ID_KEY, null);
if (StringUtil.isEmptyOrSpaces(installationId)) {
installationId = !StringUtil.isEmptyOrSpaces(oldValue) ? oldValue : UUID.randomUUID().toString();
prefs.put(INSTALLATION_ID_KEY, installationId);
installationId = prefs.get(INSTALLATION_ID_KEY, null);
if (StringUtil.isEmptyOrSpaces(installationId)) {
installationId = !StringUtil.isEmptyOrSpaces(oldValue) ? oldValue : UUID.randomUUID().toString();
prefs.put(INSTALLATION_ID_KEY, installationId);
}
if (!appInfo.isVendorJetBrains()) {
return installationId;
}
// for Windows attempt to use PermanentUserId, so that DotNet products and IDEA would use the same ID.
if (SystemInfo.isWindows) {
installationId = syncWithSharedFile("PermanentUserId", installationId, prefs, INSTALLATION_ID_KEY);
}
// make sure values in older location and in the new location are the same
if (!installationId.equals(oldValue)) {
oldPrefs.put(OLD_USER_ON_MACHINE_ID_KEY, installationId);
}
}
catch (Throwable ignored) {
// should not happen
}
if (!appInfo.isVendorJetBrains()) {
return installationId;
}
// for Windows attempt to use PermanentUserId, so that DotNet products and IDEA would use the same ID.
if (SystemInfo.isWindows) {
installationId = syncWithSharedFile("PermanentUserId", installationId, prefs, INSTALLATION_ID_KEY);
}
// make sure values in older location and in the new location are the same
if (!installationId.equals(oldValue)) {
oldPrefs.put(OLD_USER_ON_MACHINE_ID_KEY, installationId);
}
return installationId;
return installationId == null? UUID.randomUUID().toString() : installationId;
}
@NotNull
public static String syncWithSharedFile(@NotNull String fileName,
@NotNull String installationId,
@NotNull Preferences prefs,
@NotNull String prefsKey) {
final String appdata = System.getenv("APPDATA");
if (appdata != null) {
final File dir = new File(appdata, "JetBrains");
if (dir.exists() || dir.mkdirs()) {
final File permanentIdFile = new File(dir, fileName);
try {
public static String syncWithSharedFile(@NotNull String fileName, @NotNull String installationId, @NotNull Preferences prefs, @NotNull String prefsKey) {
try {
final String appdata = System.getenv("APPDATA");
if (appdata != null) {
final File dir = new File(appdata, "JetBrains");
if (dir.exists() || dir.mkdirs()) {
final File permanentIdFile = new File(dir, fileName);
String fromFile = "";
if (permanentIdFile.exists()) {
fromFile = loadFromFile(permanentIdFile).trim();
@@ -84,9 +87,10 @@ public final class PermanentInstallationID {
writeToFile(permanentIdFile, installationId);
}
}
catch (IOException ignored) { }
}
}
catch (Throwable ignored) {
}
return installationId;
}