diff --git a/PluginsAndFeatures/azure-toolkit-for-intellij/src/main/java/com/microsoft/intellij/AzureInitializer.java b/PluginsAndFeatures/azure-toolkit-for-intellij/src/main/java/com/microsoft/intellij/AzureInitializer.java index 098f3f5e0e..53f17b39c8 100644 --- a/PluginsAndFeatures/azure-toolkit-for-intellij/src/main/java/com/microsoft/intellij/AzureInitializer.java +++ b/PluginsAndFeatures/azure-toolkit-for-intellij/src/main/java/com/microsoft/intellij/AzureInitializer.java @@ -25,10 +25,6 @@ public class AzureInitializer { public static void initialize() { ProxyUtils.initProxy(); - initializeAzureConfiguration(); - } - - private static void initializeAzureConfiguration() { IntellijConfigInitializer.initialize(); initializeTelemetry(); } diff --git a/PluginsAndFeatures/azure-toolkit-for-intellij/src/main/java/com/microsoft/intellij/IntellijConfigInitializer.java b/PluginsAndFeatures/azure-toolkit-for-intellij/src/main/java/com/microsoft/intellij/IntellijConfigInitializer.java index 6a362d7444..2575ae2ce6 100644 --- a/PluginsAndFeatures/azure-toolkit-for-intellij/src/main/java/com/microsoft/intellij/IntellijConfigInitializer.java +++ b/PluginsAndFeatures/azure-toolkit-for-intellij/src/main/java/com/microsoft/intellij/IntellijConfigInitializer.java @@ -51,6 +51,7 @@ public static void initialize() { AzureConfigInitializer.initialize(installId, "Azure Toolkit for IntelliJ", AzurePlugin.PLUGIN_VERSION); CommonSettings.setUserAgent(Azure.az().config().getUserAgent()); + Azure.az().setSaveConfigurationHandler(c -> AzureConfigInitializer.saveAzConfig()); if (StringUtils.isNotBlank(Azure.az().config().getCloud())) { Azure.az(AzureCloud.class).setByName(Azure.az().config().getCloud()); } diff --git a/PluginsAndFeatures/azure-toolkit-for-intellij/src/main/java/com/microsoft/intellij/ui/AzurePanel.java b/PluginsAndFeatures/azure-toolkit-for-intellij/src/main/java/com/microsoft/intellij/ui/AzurePanel.java index 5b7ab69422..0fe13ae97a 100644 --- a/PluginsAndFeatures/azure-toolkit-for-intellij/src/main/java/com/microsoft/intellij/ui/AzurePanel.java +++ b/PluginsAndFeatures/azure-toolkit-for-intellij/src/main/java/com/microsoft/intellij/ui/AzurePanel.java @@ -273,7 +273,7 @@ public boolean doOKAction() { if (StringUtils.isNotBlank(newConfig.getCloud())) { Azure.az(AzureCloud.class).setByName(newConfig.getCloud()); } - AzureConfigInitializer.saveAzConfig(); + Azure.az().saveConfiguration(); return true; } diff --git a/Utils/azure-toolkit-ide-libs/azure-toolkit-ide-appservice-lib/src/main/java/com/microsoft/azure/toolkit/ide/appservice/function/coretools/FunctionsCoreToolsManager.java b/Utils/azure-toolkit-ide-libs/azure-toolkit-ide-appservice-lib/src/main/java/com/microsoft/azure/toolkit/ide/appservice/function/coretools/FunctionsCoreToolsManager.java index b56792d22a..ed7a768f18 100644 --- a/Utils/azure-toolkit-ide-libs/azure-toolkit-ide-appservice-lib/src/main/java/com/microsoft/azure/toolkit/ide/appservice/function/coretools/FunctionsCoreToolsManager.java +++ b/Utils/azure-toolkit-ide-libs/azure-toolkit-ide-appservice-lib/src/main/java/com/microsoft/azure/toolkit/ide/appservice/function/coretools/FunctionsCoreToolsManager.java @@ -5,7 +5,6 @@ package com.microsoft.azure.toolkit.ide.appservice.function.coretools; -import com.microsoft.azure.toolkit.ide.common.store.AzureConfigInitializer; import com.microsoft.azure.toolkit.lib.Azure; import com.microsoft.azure.toolkit.lib.common.event.AzureEventBus; import com.microsoft.azure.toolkit.lib.common.exception.AzureToolkitRuntimeException; @@ -14,12 +13,18 @@ import org.apache.commons.lang3.StringUtils; import javax.annotation.Nullable; -import java.io.*; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.OutputStream; import java.net.URL; import java.nio.channels.Channels; import java.nio.file.Files; import java.nio.file.Paths; -import java.util.*; +import java.util.List; +import java.util.Locale; +import java.util.Objects; +import java.util.Optional; import java.util.stream.IntStream; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; @@ -98,7 +103,7 @@ private void doDownloadReleaseTools(@Nullable ReleaseInfo releaseInfo, String do throw new AzureToolkitRuntimeException(e); } Azure.az().config().setFunctionCoreToolsPath(executionFilePath); - AzureConfigInitializer.saveAzConfig(); + Azure.az().saveConfiguration(); AzureEventBus.emit("function.download_func_core_tools_succeed.version", releaseInfo.releaseVersion); } diff --git a/Utils/azure-toolkit-ide-libs/azure-toolkit-ide-common-lib/src/main/java/com/microsoft/azure/toolkit/ide/common/action/ResourceCommonActionsContributor.java b/Utils/azure-toolkit-ide-libs/azure-toolkit-ide-common-lib/src/main/java/com/microsoft/azure/toolkit/ide/common/action/ResourceCommonActionsContributor.java index 15853d249c..a11bcea0c7 100644 --- a/Utils/azure-toolkit-ide-libs/azure-toolkit-ide-common-lib/src/main/java/com/microsoft/azure/toolkit/ide/common/action/ResourceCommonActionsContributor.java +++ b/Utils/azure-toolkit-ide-libs/azure-toolkit-ide-common-lib/src/main/java/com/microsoft/azure/toolkit/ide/common/action/ResourceCommonActionsContributor.java @@ -9,7 +9,6 @@ import com.microsoft.azure.toolkit.ide.common.IActionsContributor; import com.microsoft.azure.toolkit.ide.common.favorite.Favorites; import com.microsoft.azure.toolkit.ide.common.icon.AzureIcons; -import com.microsoft.azure.toolkit.ide.common.store.AzureConfigInitializer; import com.microsoft.azure.toolkit.lib.AzService; import com.microsoft.azure.toolkit.lib.Azure; import com.microsoft.azure.toolkit.lib.account.IAccount; @@ -111,8 +110,7 @@ public void registerActions(AzureActionManager am) { .withShortcut(shortcuts.delete()) .visibleWhen((s, place) -> s instanceof AzResource && s instanceof Deletable) .enableWhen(s -> { - if (s instanceof AbstractAzResource) { - final AbstractAzResource r = (AbstractAzResource) s; + if (s instanceof AbstractAzResource r) { return !r.getFormalStatus().isDeleted() && !r.isDraftForCreating(); } return true; @@ -301,7 +299,7 @@ public void registerActions(AzureActionManager am) { .visibleWhen(s -> Azure.az().config().isAuthPersistenceEnabled()) .withHandler((s) -> { Azure.az().config().setAuthPersistenceEnabled(false); - AzureConfigInitializer.saveAzConfig(); + Azure.az().saveConfiguration(); final AzureAccount az = Azure.az(AzureAccount.class); if (az.isLoggedIn()) { az.logout(); diff --git a/Utils/azure-toolkit-ide-libs/azure-toolkit-ide-common-lib/src/main/java/com/microsoft/azure/toolkit/ide/common/dotnet/DotnetRuntimeHandler.java b/Utils/azure-toolkit-ide-libs/azure-toolkit-ide-common-lib/src/main/java/com/microsoft/azure/toolkit/ide/common/dotnet/DotnetRuntimeHandler.java index 6ea120a028..b9584d0b4b 100644 --- a/Utils/azure-toolkit-ide-libs/azure-toolkit-ide-common-lib/src/main/java/com/microsoft/azure/toolkit/ide/common/dotnet/DotnetRuntimeHandler.java +++ b/Utils/azure-toolkit-ide-libs/azure-toolkit-ide-common-lib/src/main/java/com/microsoft/azure/toolkit/ide/common/dotnet/DotnetRuntimeHandler.java @@ -6,7 +6,6 @@ package com.microsoft.azure.toolkit.ide.common.dotnet; import com.microsoft.azure.toolkit.ide.common.action.ResourceCommonActionsContributor; -import com.microsoft.azure.toolkit.ide.common.store.AzureConfigInitializer; import com.microsoft.azure.toolkit.lib.Azure; import com.microsoft.azure.toolkit.lib.common.action.Action; import com.microsoft.azure.toolkit.lib.common.action.AzureActionManager; @@ -97,7 +96,7 @@ public static void installDotnet(final String path) { CommandUtils.exec(INSTALL_COMMAND, path); } Azure.az().config().setDotnetRuntimePath(path); - AzureConfigInitializer.saveAzConfig(); + Azure.az().saveConfiguration(); AzureEventBus.emit("dotnet_runtime.updated"); final String INSTALL_SUCCEED_MESSAGE = ".NET runtime is installed and configured successfully."; AzureMessager.getMessager().success(INSTALL_SUCCEED_MESSAGE, null, openSettingsAction, ResourceCommonActionsContributor.RESTART_IDE); diff --git a/Utils/azure-toolkit-ide-libs/azure-toolkit-ide-common-lib/src/main/java/com/microsoft/azure/toolkit/ide/common/store/AzureConfigInitializer.java b/Utils/azure-toolkit-ide-libs/azure-toolkit-ide-common-lib/src/main/java/com/microsoft/azure/toolkit/ide/common/store/AzureConfigInitializer.java index 30a22a4bab..09df552e20 100644 --- a/Utils/azure-toolkit-ide-libs/azure-toolkit-ide-common-lib/src/main/java/com/microsoft/azure/toolkit/ide/common/store/AzureConfigInitializer.java +++ b/Utils/azure-toolkit-ide-libs/azure-toolkit-ide-common-lib/src/main/java/com/microsoft/azure/toolkit/ide/common/store/AzureConfigInitializer.java @@ -6,12 +6,17 @@ import com.microsoft.azure.toolkit.lib.Azure; import com.microsoft.azure.toolkit.lib.AzureConfiguration; +import com.microsoft.azure.toolkit.lib.common.messager.AzureMessager; import com.microsoft.azure.toolkit.lib.common.utils.InstallationIdUtils; +import com.microsoft.azure.toolkit.lib.common.utils.JsonUtils; +import com.microsoft.azure.toolkit.lib.common.utils.Utils; +import org.apache.commons.lang3.BooleanUtils; import org.apache.commons.lang3.StringUtils; import java.nio.file.Files; import java.nio.file.Paths; import java.util.Arrays; +import java.util.Optional; import java.util.stream.Collectors; import static com.microsoft.azure.toolkit.ide.common.dotnet.DotnetRuntimeHandler.getDotnetRuntimePath; @@ -44,18 +49,39 @@ public class AzureConfigInitializer { public static final String AZURITE_PATH = "azurite_path"; public static final String AZURITE_WORKSPACE = "azurite_workspace"; public static final String ENABLE_LEASE_MODE = "enable_lease_mode"; + public static final String SYSTEM = "system"; + public static final String AZURE_CONFIGURATION = "azure_configuration"; public static void initialize(String defaultMachineId, String pluginName, String pluginVersion) { - String machineId = AzureStoreManager.getInstance().getMachineStore().getProperty(TELEMETRY, - TELEMETRY_INSTALLATION_ID); - if (StringUtils.isBlank(machineId) || !InstallationIdUtils.isValidHashMac(machineId)) { - machineId = defaultMachineId; - } - + final String machineId = Optional.ofNullable(AzureStoreManager.getInstance().getMachineStore().getProperty(TELEMETRY, TELEMETRY_INSTALLATION_ID)) + .filter(StringUtils::isNotBlank) + .filter(InstallationIdUtils::isValidHashMac) + .orElse(defaultMachineId); final AzureConfiguration config = Azure.az().config(); config.setMachineId(machineId); + config.setProduct(pluginName); + config.setVersion(pluginVersion); + config.setLogLevel("NONE"); + final String userAgent = String.format("%s, v%s, machineid:%s", pluginName, pluginVersion, + BooleanUtils.isNotFalse(config.getTelemetryEnabled()) ? config.getMachineId() : StringUtils.EMPTY); + config.setUserAgent(userAgent); final IIdeStore ideStore = AzureStoreManager.getInstance().getIdeStore(); + final String property = ideStore.getProperty(SYSTEM, AZURE_CONFIGURATION); + if (StringUtils.isBlank(property)) { + loadLegacyData(ideStore, config); + } else { + final AzureConfiguration azureConfiguration = JsonUtils.fromJson(property, AzureConfiguration.class); + try { + Utils.copyProperties(config, azureConfiguration, false); + } catch (IllegalAccessException e) { + AzureMessager.getMessager().warning("Failed to load azure configuration from store.", e); + } + } + saveAzConfig(); + } + + private static void loadLegacyData(final IIdeStore ideStore, final AzureConfiguration config) { final String allowTelemetry = ideStore.getProperty(TELEMETRY, TELEMETRY_ALLOW_TELEMETRY, "true"); config.setTelemetryEnabled(Boolean.parseBoolean(allowTelemetry)); final String enableAuthPersistence = ideStore.getProperty(OTHER, ENABLE_AUTH_PERSISTENCE, "true"); @@ -113,38 +139,12 @@ public static void initialize(String defaultMachineId, String pluginName, String final Boolean enableLeaseMode = Boolean.valueOf(ideStore.getProperty(AZURITE, ENABLE_LEASE_MODE, "false")); config.setEnableLeaseMode(enableLeaseMode); - - ideStore.getProperty(TELEMETRY, TELEMETRY_PLUGIN_VERSION, ""); - - final String userAgent = String.format("%s, v%s, machineid:%s", pluginName, pluginVersion, - config.getTelemetryEnabled() ? config.getMachineId() : StringUtils.EMPTY); - config.setUserAgent(userAgent); - config.setProduct(pluginName); - config.setLogLevel("NONE"); - config.setVersion(pluginVersion); - saveAzConfig(); } public static void saveAzConfig() { final AzureConfiguration config = Azure.az().config(); - final IIdeStore ideStore = AzureStoreManager.getInstance().getIdeStore(); - - AzureStoreManager.getInstance().getMachineStore().setProperty(TELEMETRY, TELEMETRY_INSTALLATION_ID, - config.getMachineId()); - - ideStore.setProperty(TELEMETRY, TELEMETRY_ALLOW_TELEMETRY, Boolean.toString(config.getTelemetryEnabled())); - ideStore.setProperty(OTHER, ENABLE_AUTH_PERSISTENCE, Boolean.toString(config.isAuthPersistenceEnabled())); - ideStore.setProperty(MONITOR, MONITOR_TABLE_ROWS, String.valueOf(config.getMonitorQueryRowNumber())); - ideStore.setProperty(ACCOUNT, AZURE_ENVIRONMENT_KEY, config.getCloud()); - ideStore.setProperty(FUNCTION, FUNCTION_CORE_TOOLS_PATH, config.getFunctionCoreToolsPath()); - ideStore.setProperty(STORAGE, STORAGE_EXPLORER_PATH, config.getStorageExplorerPath()); - ideStore.setProperty(COMMON, PAGE_SIZE, String.valueOf(config.getPageSize())); - ideStore.setProperty(COSMOS, DOCUMENTS_LABEL_FIELDS, String.join(";", config.getDocumentsLabelFields())); - // don't save pluginVersion, it is saved in AzurePlugin class - ideStore.setProperty(BICEP, DOTNET_RUNTIME_PATH, config.getDotnetRuntimePath()); - ideStore.setProperty(EVENT_HUBS, CONSUMER_GROUP_NAME, config.getEventHubsConsumerGroup()); - ideStore.setProperty(AZURITE, AZURITE_PATH, config.getAzuritePath()); - ideStore.setProperty(AZURITE, AZURITE_WORKSPACE, config.getAzuriteWorkspace()); - ideStore.setProperty(AZURITE, ENABLE_LEASE_MODE, String.valueOf(config.getEnableLeaseMode())); + final AzureStoreManager storeManager = AzureStoreManager.getInstance(); + storeManager.getIdeStore().setProperty(SYSTEM, AZURE_CONFIGURATION, JsonUtils.toJson(config)); + storeManager.getMachineStore().setProperty(TELEMETRY, TELEMETRY_INSTALLATION_ID, config.getMachineId()); } }