Search in sources :

Example 1 with Cacheable

use of com.microsoft.azure.toolkit.lib.common.cache.Cacheable in project azure-tools-for-java by Microsoft.

the class AzureSdkCategoryService method loadAzureSDKCategories.

@Cacheable(value = "azure-sdk-category-entities")
@AzureOperation(name = "sdk.load_category_data", type = AzureOperation.Type.TASK)
public static Map<String, List<AzureSdkCategoryEntity>> loadAzureSDKCategories() {
    try (final InputStream stream = AzureSdkCategoryService.class.getResourceAsStream(SERVICE_CATEGORY_CSV)) {
        // read
        final ObjectReader reader = CSV_MAPPER.readerFor(AzureSdkCategoryEntity.class).with(CsvSchema.emptySchema().withHeader());
        final MappingIterator<AzureSdkCategoryEntity> data = reader.readValues(stream);
        final List<AzureSdkCategoryEntity> categories = data.readAll();
        // default category & description suffix.
        categories.stream().filter(c -> StringUtils.isNotBlank(c.getServiceName())).forEach(c -> {
            if (StringUtils.isBlank(c.getCategory())) {
                c.setCategory("Others");
            }
            final String trimDescription = StringUtils.trim(c.getDescription());
            if (StringUtils.isNotBlank(trimDescription) && !StringUtils.endsWith(trimDescription, ".")) {
                c.setDescription(trimDescription + ".");
            }
        });
        // unique
        final List<AzureSdkCategoryEntity> uniqueCategories = categories.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(o -> o.getCategory() + "," + o.getServiceName()))), ArrayList::new));
        // group
        return uniqueCategories.stream().collect(Collectors.groupingBy(AzureSdkCategoryEntity::getCategory));
    } catch (final IOException e) {
        final String message = String.format("failed to load Azure SDK categories from \"%s\"", SERVICE_CATEGORY_CSV);
        throw new AzureToolkitRuntimeException(message, e);
    }
}
Also used : AzureToolkitRuntimeException(com.microsoft.azure.toolkit.lib.common.exception.AzureToolkitRuntimeException) AzureOperation(com.microsoft.azure.toolkit.lib.common.operation.AzureOperation) AzureSdkCategoryEntity(com.microsoft.azure.toolkit.intellij.azuresdk.model.AzureSdkCategoryEntity) MappingIterator(com.fasterxml.jackson.databind.MappingIterator) CsvMapper(com.fasterxml.jackson.dataformat.csv.CsvMapper) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) CsvSchema(com.fasterxml.jackson.dataformat.csv.CsvSchema) IOException(java.io.IOException) StringUtils(org.apache.commons.lang3.StringUtils) Collectors(java.util.stream.Collectors) DeserializationFeature(com.fasterxml.jackson.databind.DeserializationFeature) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) ObjectReader(com.fasterxml.jackson.databind.ObjectReader) List(java.util.List) Map(java.util.Map) Cacheable(com.microsoft.azure.toolkit.lib.common.cache.Cacheable) Comparator(java.util.Comparator) InputStream(java.io.InputStream) AzureSdkCategoryEntity(com.microsoft.azure.toolkit.intellij.azuresdk.model.AzureSdkCategoryEntity) InputStream(java.io.InputStream) ObjectReader(com.fasterxml.jackson.databind.ObjectReader) AzureToolkitRuntimeException(com.microsoft.azure.toolkit.lib.common.exception.AzureToolkitRuntimeException) IOException(java.io.IOException) Cacheable(com.microsoft.azure.toolkit.lib.common.cache.Cacheable) AzureOperation(com.microsoft.azure.toolkit.lib.common.operation.AzureOperation)

Example 2 with Cacheable

use of com.microsoft.azure.toolkit.lib.common.cache.Cacheable in project azure-tools-for-java by Microsoft.

the class AzureSdkLibraryService method loadAzureSDKWhitelist.

@Cacheable("sdk/packages/whitelist")
@AzureOperation(name = "sdk.load_meta_data.whitelist", type = AzureOperation.Type.TASK)
private static Set<String> loadAzureSDKWhitelist() {
    try {
        final URL destination = AzureSdkLibraryService.class.getResource(SDK_ALLOW_LIST_CSV);
        final ObjectReader reader = CSV_MAPPER.readerFor(AzureSdkAllowListEntity.class).with(CsvSchema.emptySchema().withHeader());
        final MappingIterator<AzureSdkAllowListEntity> data = reader.readValues(destination);
        return data.readAll().stream().filter(e -> StringUtils.isNoneBlank(e.getArtifactId(), e.getGroupId())).map(AzureSdkAllowListEntity::getPackageName).collect(Collectors.toSet());
    } catch (final IOException e) {
        log.warn(String.format("failed to load Azure SDK allow list from \"%s\"", SDK_ALLOW_LIST_CSV), e);
    }
    return Collections.emptySet();
}
Also used : ObjectReader(com.fasterxml.jackson.databind.ObjectReader) IOException(java.io.IOException) AzureSdkAllowListEntity(com.microsoft.azure.toolkit.intellij.azuresdk.model.AzureSdkAllowListEntity) URL(java.net.URL) Cacheable(com.microsoft.azure.toolkit.lib.common.cache.Cacheable) AzureOperation(com.microsoft.azure.toolkit.lib.common.operation.AzureOperation)

Aggregations

ObjectReader (com.fasterxml.jackson.databind.ObjectReader)2 Cacheable (com.microsoft.azure.toolkit.lib.common.cache.Cacheable)2 AzureOperation (com.microsoft.azure.toolkit.lib.common.operation.AzureOperation)2 IOException (java.io.IOException)2 DeserializationFeature (com.fasterxml.jackson.databind.DeserializationFeature)1 MappingIterator (com.fasterxml.jackson.databind.MappingIterator)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 CsvMapper (com.fasterxml.jackson.dataformat.csv.CsvMapper)1 CsvSchema (com.fasterxml.jackson.dataformat.csv.CsvSchema)1 AzureSdkAllowListEntity (com.microsoft.azure.toolkit.intellij.azuresdk.model.AzureSdkAllowListEntity)1 AzureSdkCategoryEntity (com.microsoft.azure.toolkit.intellij.azuresdk.model.AzureSdkCategoryEntity)1 AzureToolkitRuntimeException (com.microsoft.azure.toolkit.lib.common.exception.AzureToolkitRuntimeException)1 InputStream (java.io.InputStream)1 URL (java.net.URL)1 ArrayList (java.util.ArrayList)1 Comparator (java.util.Comparator)1 List (java.util.List)1 Map (java.util.Map)1 TreeSet (java.util.TreeSet)1 Collectors (java.util.stream.Collectors)1