|  | 
| 6 | 6 | 
 | 
| 7 | 7 | import com.microsoft.azure.toolkit.lib.Azure; | 
| 8 | 8 | import com.microsoft.azure.toolkit.lib.AzureConfiguration; | 
|  | 9 | +import com.microsoft.azure.toolkit.lib.common.messager.AzureMessager; | 
| 9 | 10 | import com.microsoft.azure.toolkit.lib.common.utils.InstallationIdUtils; | 
|  | 11 | +import com.microsoft.azure.toolkit.lib.common.utils.JsonUtils; | 
|  | 12 | +import com.microsoft.azure.toolkit.lib.common.utils.Utils; | 
|  | 13 | +import org.apache.commons.lang3.BooleanUtils; | 
| 10 | 14 | import org.apache.commons.lang3.StringUtils; | 
| 11 | 15 | 
 | 
| 12 | 16 | import java.nio.file.Files; | 
| 13 | 17 | import java.nio.file.Paths; | 
| 14 | 18 | import java.util.Arrays; | 
|  | 19 | +import java.util.Optional; | 
| 15 | 20 | import java.util.stream.Collectors; | 
| 16 | 21 | 
 | 
| 17 | 22 | import static com.microsoft.azure.toolkit.ide.common.dotnet.DotnetRuntimeHandler.getDotnetRuntimePath; | 
| @@ -44,18 +49,39 @@ public class AzureConfigInitializer { | 
| 44 | 49 |     public static final String AZURITE_PATH = "azurite_path"; | 
| 45 | 50 |     public static final String AZURITE_WORKSPACE = "azurite_workspace"; | 
| 46 | 51 |     public static final String ENABLE_LEASE_MODE = "enable_lease_mode"; | 
|  | 52 | +    public static final String SYSTEM = "system"; | 
|  | 53 | +    public static final String AZURE_CONFIGURATION = "azure_configuration"; | 
| 47 | 54 | 
 | 
| 48 | 55 |     public static void initialize(String defaultMachineId, String pluginName, String pluginVersion) { | 
| 49 |  | -        String machineId = AzureStoreManager.getInstance().getMachineStore().getProperty(TELEMETRY, | 
| 50 |  | -            TELEMETRY_INSTALLATION_ID); | 
| 51 |  | -        if (StringUtils.isBlank(machineId) || !InstallationIdUtils.isValidHashMac(machineId)) { | 
| 52 |  | -            machineId = defaultMachineId; | 
| 53 |  | -        } | 
| 54 |  | - | 
|  | 56 | +        final String machineId = Optional.ofNullable(AzureStoreManager.getInstance().getMachineStore().getProperty(TELEMETRY, TELEMETRY_INSTALLATION_ID)) | 
|  | 57 | +                .filter(StringUtils::isNotBlank) | 
|  | 58 | +                .filter(InstallationIdUtils::isValidHashMac) | 
|  | 59 | +                .orElse(defaultMachineId); | 
| 55 | 60 |         final AzureConfiguration config = Azure.az().config(); | 
| 56 | 61 |         config.setMachineId(machineId); | 
|  | 62 | +        config.setProduct(pluginName); | 
|  | 63 | +        config.setVersion(pluginVersion); | 
|  | 64 | +        config.setLogLevel("NONE"); | 
|  | 65 | +        final String userAgent = String.format("%s, v%s, machineid:%s", pluginName, pluginVersion, | 
|  | 66 | +                BooleanUtils.isNotFalse(config.getTelemetryEnabled()) ? config.getMachineId() : StringUtils.EMPTY); | 
|  | 67 | +        config.setUserAgent(userAgent); | 
| 57 | 68 | 
 | 
| 58 | 69 |         final IIdeStore ideStore = AzureStoreManager.getInstance().getIdeStore(); | 
|  | 70 | +        final String property = ideStore.getProperty(SYSTEM, AZURE_CONFIGURATION); | 
|  | 71 | +        if (StringUtils.isBlank(property)) { | 
|  | 72 | +            loadLegacyData(ideStore, config); | 
|  | 73 | +        } else { | 
|  | 74 | +            final AzureConfiguration azureConfiguration = JsonUtils.fromJson(property, AzureConfiguration.class); | 
|  | 75 | +            try { | 
|  | 76 | +                Utils.copyProperties(config, azureConfiguration, false); | 
|  | 77 | +            } catch (IllegalAccessException e) { | 
|  | 78 | +                AzureMessager.getMessager().warning("Failed to load azure configuration from store.", e); | 
|  | 79 | +            } | 
|  | 80 | +        } | 
|  | 81 | +        saveAzConfig(); | 
|  | 82 | +    } | 
|  | 83 | + | 
|  | 84 | +    private static void loadLegacyData(final IIdeStore ideStore, final AzureConfiguration config) { | 
| 59 | 85 |         final String allowTelemetry = ideStore.getProperty(TELEMETRY, TELEMETRY_ALLOW_TELEMETRY, "true"); | 
| 60 | 86 |         config.setTelemetryEnabled(Boolean.parseBoolean(allowTelemetry)); | 
| 61 | 87 |         final String enableAuthPersistence = ideStore.getProperty(OTHER, ENABLE_AUTH_PERSISTENCE, "true"); | 
| @@ -113,38 +139,12 @@ public static void initialize(String defaultMachineId, String pluginName, String | 
| 113 | 139 | 
 | 
| 114 | 140 |         final Boolean enableLeaseMode = Boolean.valueOf(ideStore.getProperty(AZURITE, ENABLE_LEASE_MODE, "false")); | 
| 115 | 141 |         config.setEnableLeaseMode(enableLeaseMode); | 
| 116 |  | - | 
| 117 |  | -        ideStore.getProperty(TELEMETRY, TELEMETRY_PLUGIN_VERSION, ""); | 
| 118 |  | - | 
| 119 |  | -        final String userAgent = String.format("%s, v%s, machineid:%s", pluginName, pluginVersion, | 
| 120 |  | -            config.getTelemetryEnabled() ? config.getMachineId() : StringUtils.EMPTY); | 
| 121 |  | -        config.setUserAgent(userAgent); | 
| 122 |  | -        config.setProduct(pluginName); | 
| 123 |  | -        config.setLogLevel("NONE"); | 
| 124 |  | -        config.setVersion(pluginVersion); | 
| 125 |  | -        saveAzConfig(); | 
| 126 | 142 |     } | 
| 127 | 143 | 
 | 
| 128 | 144 |     public static void saveAzConfig() { | 
| 129 | 145 |         final AzureConfiguration config = Azure.az().config(); | 
| 130 |  | -        final IIdeStore ideStore = AzureStoreManager.getInstance().getIdeStore(); | 
| 131 |  | - | 
| 132 |  | -        AzureStoreManager.getInstance().getMachineStore().setProperty(TELEMETRY, TELEMETRY_INSTALLATION_ID, | 
| 133 |  | -            config.getMachineId()); | 
| 134 |  | - | 
| 135 |  | -        ideStore.setProperty(TELEMETRY, TELEMETRY_ALLOW_TELEMETRY, Boolean.toString(config.getTelemetryEnabled())); | 
| 136 |  | -        ideStore.setProperty(OTHER, ENABLE_AUTH_PERSISTENCE, Boolean.toString(config.isAuthPersistenceEnabled())); | 
| 137 |  | -        ideStore.setProperty(MONITOR, MONITOR_TABLE_ROWS, String.valueOf(config.getMonitorQueryRowNumber())); | 
| 138 |  | -        ideStore.setProperty(ACCOUNT, AZURE_ENVIRONMENT_KEY, config.getCloud()); | 
| 139 |  | -        ideStore.setProperty(FUNCTION, FUNCTION_CORE_TOOLS_PATH, config.getFunctionCoreToolsPath()); | 
| 140 |  | -        ideStore.setProperty(STORAGE, STORAGE_EXPLORER_PATH, config.getStorageExplorerPath()); | 
| 141 |  | -        ideStore.setProperty(COMMON, PAGE_SIZE, String.valueOf(config.getPageSize())); | 
| 142 |  | -        ideStore.setProperty(COSMOS, DOCUMENTS_LABEL_FIELDS, String.join(";", config.getDocumentsLabelFields())); | 
| 143 |  | -        // don't save pluginVersion, it is saved in AzurePlugin class | 
| 144 |  | -        ideStore.setProperty(BICEP, DOTNET_RUNTIME_PATH, config.getDotnetRuntimePath()); | 
| 145 |  | -        ideStore.setProperty(EVENT_HUBS, CONSUMER_GROUP_NAME, config.getEventHubsConsumerGroup()); | 
| 146 |  | -        ideStore.setProperty(AZURITE, AZURITE_PATH, config.getAzuritePath()); | 
| 147 |  | -        ideStore.setProperty(AZURITE, AZURITE_WORKSPACE, config.getAzuriteWorkspace()); | 
| 148 |  | -        ideStore.setProperty(AZURITE, ENABLE_LEASE_MODE, String.valueOf(config.getEnableLeaseMode())); | 
|  | 146 | +        final AzureStoreManager storeManager = AzureStoreManager.getInstance(); | 
|  | 147 | +        storeManager.getIdeStore().setProperty(SYSTEM, AZURE_CONFIGURATION, JsonUtils.toJson(config)); | 
|  | 148 | +        storeManager.getMachineStore().setProperty(TELEMETRY, TELEMETRY_INSTALLATION_ID, config.getMachineId()); | 
| 149 | 149 |     } | 
| 150 | 150 | } | 
0 commit comments