Example usage for org.springframework.boot.configurationmetadata ConfigurationMetadataRepositoryJsonBuilder withJsonResource

List of usage examples for org.springframework.boot.configurationmetadata ConfigurationMetadataRepositoryJsonBuilder withJsonResource

Introduction

In this page you can find the example usage for org.springframework.boot.configurationmetadata ConfigurationMetadataRepositoryJsonBuilder withJsonResource.

Prototype

public ConfigurationMetadataRepositoryJsonBuilder withJsonResource(InputStream inputStream) throws IOException 

Source Link

Document

Add the content of a ConfigurationMetadataRepository defined by the specified InputStream json document using the default charset.

Usage

From source file:demo.config.diff.support.ConfigurationMetadataRepositoryLoader.java

private Resource load(ConfigurationMetadataRepositoryJsonBuilder builder, String moduleId, String version,
        boolean mandatory) throws IOException {
    String coordinates = moduleId + ":" + version;
    try {//from   ww  w . j  a v a2s.  c om
        ArtifactResult artifactResult = dependencyResolver.resolveDependency(coordinates);
        File file = artifactResult.getArtifact().getFile();
        JarFile jarFile = new JarFile(file);
        ZipEntry entry = jarFile.getEntry("META-INF/spring-configuration-metadata.json");
        if (entry != null) {
            logger.info("Adding meta-data from '" + coordinates + "'");
            try (InputStream stream = jarFile.getInputStream(entry)) {
                builder.withJsonResource(stream);
            }
        } else {
            logger.info("No meta-data found for '" + coordinates + "'");
        }
    } catch (ArtifactResolutionException e) {
        if (mandatory) {
            throw new UnknownSpringBootVersion("Could not load '" + coordinates + "'", version);
        }
        logger.info("Ignoring '" + coordinates + " (not found)");
    }
    return null;
}

From source file:com.github.alexfalappa.nbspringboot.projects.service.spi.SpringBootServiceImpl.java

private void updateConfigRepo() {
    logger.fine("Updating config metadata repo");
    repo = new SimpleConfigurationMetadataRepository();
    final List<FileObject> cfgMetaFiles = cpExec.findAllResources(METADATA_JSON);
    for (FileObject fo : cfgMetaFiles) {
        try {//from  w  w w  .jav a 2  s  . c om
            ConfigurationMetadataRepositoryJsonBuilder builder = ConfigurationMetadataRepositoryJsonBuilder
                    .create();
            ConfigurationMetadataRepository currRepo;
            FileObject archiveFo = FileUtil.getArchiveFile(fo);
            if (archiveFo != null) {
                // parse and cache configuration metadata from JSON file in jar
                String archivePath = archiveFo.getPath();
                if (!reposInJars.containsKey(archivePath)) {
                    logger.log(INFO, "Unmarshalling configuration metadata from {0}",
                            FileUtil.getFileDisplayName(fo));
                    ConfigurationMetadataRepository jarRepo = builder.withJsonResource(fo.getInputStream())
                            .build();
                    reposInJars.put(archivePath, jarRepo);
                }
                currRepo = reposInJars.get(archivePath);
            } else {
                // parse configuration metadata from standalone JSON file (usually produced by spring configuration processor)
                logger.log(INFO, "Unmarshalling configuration metadata from {0}",
                        FileUtil.getFileDisplayName(fo));
                currRepo = builder.withJsonResource(fo.getInputStream()).build();
            }
            repo.include(currRepo);
        } catch (Exception ex) {
            Exceptions.printStackTrace(ex);
        }
    }
    // update cached values
    cachedProperties = repo.getAllProperties();
    // extract collection/map properties names based on heuristics
    for (Map.Entry<String, ConfigurationMetadataProperty> entry : cachedProperties.entrySet()) {
        final String type = entry.getValue().getType();
        if (type != null) {
            final String key = entry.getKey();
            if (type.contains("Map<")) {
                mapProperties.add(key);
            }
            if (type.contains("List<") || type.contains("Set<") || type.contains("Collection<")) {
                collectionProperties.add(key);
            }
        }
    }
    System.out.printf("Collections: %s%n", collectionProperties);
    System.out.printf("       Maps: %s%n", mapProperties);
}

From source file:org.springframework.boot.context.properties.migrator.PropertiesMigrationListener.java

private ConfigurationMetadataRepository loadRepository(ConfigurationMetadataRepositoryJsonBuilder builder)
        throws IOException {
    Resource[] resources = new PathMatchingResourcePatternResolver()
            .getResources("classpath*:/META-INF/spring-configuration-metadata.json");
    for (Resource resource : resources) {
        try (InputStream inputStream = resource.getInputStream()) {
            builder.withJsonResource(inputStream);
        }/*from  w  w  w  .j  a  va 2 s .c  o  m*/
    }
    return builder.build();
}