Search in sources :

Example 1 with LayerConfiguration

use of com.google.cloud.tools.jib.api.LayerConfiguration in project component-runtime by Talend.

the class ConnectorLoader method createConnectorLayer.

public ConnectorLayer createConnectorLayer(final AbsoluteUnixPath rootContainerPath, final Path workDir, final Path car) {
    String gav = null;
    final Map<String, Path> toCopy = new HashMap<>();
    try (final JarInputStream jarInputStream = new JarInputStream(Files.newInputStream(car))) {
        JarEntry entry;
        while ((entry = jarInputStream.getNextJarEntry()) != null) {
            if ("TALEND-INF/metadata.properties".equals(entry.getName())) {
                // don't close, it is the jar!
                gav = IO.loadProperties(new BufferedReader(new InputStreamReader(jarInputStream)).lines().collect(joining("\n"))).getProperty("component_coordinates");
                continue;
            }
            if (!entry.getName().startsWith("MAVEN-INF/repository/")) {
                continue;
            }
            final String relativeName = entry.getName().substring("MAVEN-INF/repository/".length());
            final Path local = workDir.resolve(relativeName);
            if (entry.isDirectory()) {
                if (!Files.exists(local)) {
                    Files.createDirectories(local);
                }
            } else {
                if (local.getParent() != null && !Files.exists(local.getParent())) {
                    Files.createDirectories(local.getParent());
                }
                Files.copy(jarInputStream, local);
                if (entry.getLastModifiedTime() != null) {
                    Files.setLastModifiedTime(local, entry.getLastModifiedTime());
                }
                toCopy.put(relativeName, local);
            }
        }
    } catch (final IOException e) {
        throw new IllegalStateException(e);
    }
    return new ConnectorLayer(toCopy.entrySet().stream().collect(Collector.of(LayerConfiguration::builder, (builder, entry) -> {
        try {
            builder.addEntry(new LayerEntry(entry.getValue(), rootContainerPath.resolve(entry.getKey()), FilePermissions.DEFAULT_FILE_PERMISSIONS, Instant.ofEpochMilli(Files.getLastModifiedTime(entry.getValue()).toMillis())));
        } catch (final IOException e) {
            throw new IllegalStateException(e);
        }
    }, (builder1, builder2) -> {
        builder2.build().getLayerEntries().forEach(builder1::addEntry);
        return builder1;
    })).build(), toCopy, requireNonNull(gav, "GAV was not found in '" + car + "', ensure it is a valid component archive."));
}
Also used : AbsoluteUnixPath(com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath) Path(java.nio.file.Path) LayerEntry(com.google.cloud.tools.jib.api.LayerEntry) AbsoluteUnixPath(com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath) Files(java.nio.file.Files) FilePermissions(com.google.cloud.tools.jib.api.buildplan.FilePermissions) IO(org.talend.sdk.component.remoteengine.customizer.lang.IO) IOException(java.io.IOException) HashMap(java.util.HashMap) Instant(java.time.Instant) InputStreamReader(java.io.InputStreamReader) Collectors.joining(java.util.stream.Collectors.joining) JarEntry(java.util.jar.JarEntry) LayerConfiguration(com.google.cloud.tools.jib.api.LayerConfiguration) Objects.requireNonNull(java.util.Objects.requireNonNull) Map(java.util.Map) JarInputStream(java.util.jar.JarInputStream) Data(lombok.Data) BufferedReader(java.io.BufferedReader) Collector(java.util.stream.Collector) Path(java.nio.file.Path) InputStreamReader(java.io.InputStreamReader) HashMap(java.util.HashMap) JarInputStream(java.util.jar.JarInputStream) LayerEntry(com.google.cloud.tools.jib.api.LayerEntry) IOException(java.io.IOException) JarEntry(java.util.jar.JarEntry) BufferedReader(java.io.BufferedReader)

Aggregations

LayerConfiguration (com.google.cloud.tools.jib.api.LayerConfiguration)1 LayerEntry (com.google.cloud.tools.jib.api.LayerEntry)1 AbsoluteUnixPath (com.google.cloud.tools.jib.api.buildplan.AbsoluteUnixPath)1 FilePermissions (com.google.cloud.tools.jib.api.buildplan.FilePermissions)1 BufferedReader (java.io.BufferedReader)1 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 Files (java.nio.file.Files)1 Path (java.nio.file.Path)1 Instant (java.time.Instant)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Objects.requireNonNull (java.util.Objects.requireNonNull)1 JarEntry (java.util.jar.JarEntry)1 JarInputStream (java.util.jar.JarInputStream)1 Collector (java.util.stream.Collector)1 Collectors.joining (java.util.stream.Collectors.joining)1 Data (lombok.Data)1 IO (org.talend.sdk.component.remoteengine.customizer.lang.IO)1