Search in sources :

Example 1 with MvnCoordinateToFileConverter

use of org.talend.sdk.component.dependencies.maven.MvnCoordinateToFileConverter in project component-runtime by Talend.

the class ComponentManagerService method init.

@PostConstruct
private void init() {
    ofNullable(configuration.mavenRepository()).ifPresent(repo -> System.setProperty("talend.component.manager.m2.repository", repo));
    mvnCoordinateToFileConverter = new MvnCoordinateToFileConverter();
    instance = ComponentManager.instance();
    deploymentListener = new DeploymentListener(componentDao, componentFamilyDao, actionDao, configurationDao);
    instance.getContainer().registerListener(deploymentListener);
    // note: we don't want to download anything from the manager, if we need to download any artifact we need
    // to ensure it is controlled (secured) and allowed so don't make it implicit but enforce a first phase
    // where it is cached locally (provisioning solution)
    ofNullable(configuration.componentCoordinates()).orElse(emptyList()).forEach(this::deploy);
    ofNullable(configuration.componentRegistry()).map(File::new).filter(File::exists).ifPresent(registry -> {
        final Properties properties = new Properties();
        try (final InputStream is = new FileInputStream(registry)) {
            properties.load(is);
        } catch (final IOException e) {
            throw new IllegalArgumentException(e);
        }
        properties.stringPropertyNames().stream().map(properties::getProperty).filter(gav -> !configuration.componentCoordinates().contains(gav)).forEach(this::deploy);
    });
}
Also used : Stream.empty(java.util.stream.Stream.empty) MvnCoordinateToFileConverter(org.talend.sdk.component.dependencies.maven.MvnCoordinateToFileConverter) Produces(javax.enterprise.inject.Produces) ComponentFamilyMeta(org.talend.sdk.component.runtime.manager.ComponentFamilyMeta) Inject(javax.inject.Inject) PreDestroy(javax.annotation.PreDestroy) ComponentServerConfiguration(org.talend.sdk.component.server.configuration.ComponentServerConfiguration) ConfigurationDao(org.talend.sdk.component.server.dao.ConfigurationDao) Observes(javax.enterprise.event.Observes) Config(org.talend.sdk.component.design.extension.repository.Config) Collectors.toSet(java.util.stream.Collectors.toSet) ComponentFamilyDao(org.talend.sdk.component.server.dao.ComponentFamilyDao) Properties(java.util.Properties) ContainerListener(org.talend.sdk.component.container.ContainerListener) ContainerComponentRegistry(org.talend.sdk.component.runtime.manager.ContainerComponentRegistry) Container(org.talend.sdk.component.container.Container) Collections.emptyList(java.util.Collections.emptyList) Optional.ofNullable(java.util.Optional.ofNullable) Artifact(org.talend.sdk.component.dependencies.maven.Artifact) Collection(java.util.Collection) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) File(java.io.File) RepositoryModel(org.talend.sdk.component.design.extension.RepositoryModel) ComponentActionDao(org.talend.sdk.component.server.dao.ComponentActionDao) Collectors.toList(java.util.stream.Collectors.toList) Slf4j(lombok.extern.slf4j.Slf4j) Stream(java.util.stream.Stream) ComponentDao(org.talend.sdk.component.server.dao.ComponentDao) PostConstruct(javax.annotation.PostConstruct) Data(lombok.Data) ApplicationScoped(javax.enterprise.context.ApplicationScoped) ComponentManager(org.talend.sdk.component.runtime.manager.ComponentManager) AllArgsConstructor(lombok.AllArgsConstructor) InputStream(java.io.InputStream) Initialized(javax.enterprise.context.Initialized) MvnCoordinateToFileConverter(org.talend.sdk.component.dependencies.maven.MvnCoordinateToFileConverter) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) Properties(java.util.Properties) File(java.io.File) FileInputStream(java.io.FileInputStream) PostConstruct(javax.annotation.PostConstruct)

Example 2 with MvnCoordinateToFileConverter

use of org.talend.sdk.component.dependencies.maven.MvnCoordinateToFileConverter in project component-runtime by Talend.

the class StudioInstaller method run.

@Override
public void run() {
    log.info("Installing development version of " + mainGav + " in " + studioHome);
    final List<String> artifacts = this.artifacts.values().stream().map(File::getName).collect(toList());
    // 0. remove staled libs from the cache (configuration/org.eclipse.osgi)
    final File osgiCache = new File(studioHome, "configuration/org.eclipse.osgi");
    if (osgiCache.isDirectory()) {
        ofNullable(osgiCache.listFiles(child -> {
            try {
                return child.isDirectory() && Integer.parseInt(child.getName()) > 0;
            } catch (final NumberFormatException nfe) {
                return false;
            }
        })).map(Stream::of).orElseGet(Stream::empty).map(id -> new File(id, ".cp")).filter(File::exists).flatMap(cp -> ofNullable(cp.listFiles((dir, name) -> name.endsWith(".jar"))).map(Stream::of).orElseGet(Stream::empty)).filter(jar -> artifacts.contains(jar.getName())).forEach(this::tryDelete);
    }
    // 1. install the runtime dependency tree (scope compile+runtime) in the studio m2 repo
    final File configIni = new File(studioHome, "configuration/config.ini");
    final Properties config = new Properties();
    if (configIni.exists()) {
        try (final InputStream is = new FileInputStream(configIni)) {
            config.load(is);
        } catch (final IOException e) {
            throw new IllegalStateException(e);
        }
    }
    final String repoType = config.getProperty("maven.repository");
    if (!"global".equals(repoType)) {
        final MvnCoordinateToFileConverter converter = new MvnCoordinateToFileConverter();
        this.artifacts.forEach((gav, file) -> {
            final Artifact artifact = converter.toArtifact(gav);
            try {
                final File target = new File(studioHome, "configuration/.m2/repository/" + artifact.toPath());
                if (target.exists() && !artifact.getVersion().endsWith("-SNAPSHOT")) {
                    log.info(gav + " already exists, skipping");
                    return;
                }
                mkdirP(target.getParentFile());
                Files.copy(file.toPath(), target.toPath(), StandardCopyOption.REPLACE_EXISTING);
                log.info("Installed " + gav + " at " + target.getAbsolutePath());
            } catch (final IOException e) {
                throw new IllegalStateException(e);
            }
        });
    } else {
        log.info("Studio " + studioHome + " configured to use global maven repository, skipping artifact installation");
    }
    // 2. register component adding them into the registry
    String registry = config.getProperty("component.java.registry", config.getProperty("talend.component.server.component.registry"));
    if (registry == null) {
        final File registryLocation = new File(configIni.getParentFile(), "components-registration.properties");
        registryLocation.getParentFile().mkdirs();
        registry = registryLocation.getAbsolutePath();
        config.setProperty("component.java.registry", registry);
        try {
            final File backup = new File(configIni.getParentFile(), "backup/" + configIni.getName() + "_" + LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-mm-dd_HH-mm-ss")));
            mkdirP(backup.getParentFile());
            log.info("Saving configuration in " + backup);
            Files.copy(configIni.toPath(), backup.toPath(), StandardCopyOption.REPLACE_EXISTING);
        } catch (final IOException e) {
            throw new IllegalStateException(e);
        }
        try (final Writer writer = new FileWriter(configIni)) {
            config.store(writer, "File rewritten by " + getClass().getName() + " utility to add component.java.registry entry");
            log.info("Updated " + configIni + " to add the component registry entry");
        } catch (final IOException e) {
            throw new IllegalStateException(e);
        }
        try {
            Files.write(registryLocation.toPath(), new byte[0], StandardOpenOption.CREATE_NEW);
        } catch (final IOException e) {
            throw new IllegalStateException(e);
        }
    }
    final Properties components = new Properties();
    try (final Reader reader = new FileReader(registry)) {
        components.load(reader);
    } catch (final IOException e) {
        throw new IllegalStateException(e);
    }
    final String key = mainGav.split(":")[1];
    if (!components.containsKey(key)) {
        components.setProperty(key, mainGav);
        try (final Writer writer = new FileWriter(registry)) {
            components.store(writer, "File rewritten to add " + mainGav);
            log.info("Updated " + registry + " with '" + mainGav + "'");
        } catch (final IOException e) {
            throw new IllegalStateException(e);
        }
    }
}
Also used : MvnCoordinateToFileConverter(org.talend.sdk.component.dependencies.maven.MvnCoordinateToFileConverter) Properties(java.util.Properties) Files(java.nio.file.Files) Optional.ofNullable(java.util.Optional.ofNullable) Artifact(org.talend.sdk.component.dependencies.maven.Artifact) FileWriter(java.io.FileWriter) StandardOpenOption(java.nio.file.StandardOpenOption) LocalDateTime(java.time.LocalDateTime) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) Reader(java.io.Reader) File(java.io.File) StandardCopyOption(java.nio.file.StandardCopyOption) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) Stream(java.util.stream.Stream) DateTimeFormatter(java.time.format.DateTimeFormatter) Map(java.util.Map) Writer(java.io.Writer) FileReader(java.io.FileReader) InputStream(java.io.InputStream) MvnCoordinateToFileConverter(org.talend.sdk.component.dependencies.maven.MvnCoordinateToFileConverter) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileWriter(java.io.FileWriter) Reader(java.io.Reader) FileReader(java.io.FileReader) IOException(java.io.IOException) Properties(java.util.Properties) FileInputStream(java.io.FileInputStream) Artifact(org.talend.sdk.component.dependencies.maven.Artifact) FileInputStream(java.io.FileInputStream) Stream(java.util.stream.Stream) InputStream(java.io.InputStream) FileReader(java.io.FileReader) File(java.io.File) FileWriter(java.io.FileWriter) Writer(java.io.Writer)

Example 3 with MvnCoordinateToFileConverter

use of org.talend.sdk.component.dependencies.maven.MvnCoordinateToFileConverter in project component-runtime by Talend.

the class CarBundler method run.

@Override
public void run() {
    final String date = DateTimeFormatter.ISO_LOCAL_DATE_TIME.format(LocalDateTime.now(ZoneId.of("UTC")));
    final Properties metadata = new Properties();
    metadata.put("date", date);
    metadata.put("version", ofNullable(configuration.version).orElse("NC"));
    metadata.put("component_coordinates", ofNullable(configuration.mainGav).orElseThrow(() -> new IllegalArgumentException("No component coordinates specified")));
    if (configuration.getCustomMetadata() != null) {
        configuration.getCustomMetadata().forEach(metadata::setProperty);
    }
    configuration.getOutput().getParentFile().mkdirs();
    final Manifest manifest = new Manifest();
    manifest.getMainAttributes().putValue("Main-Class", CarMain.class.getName());
    manifest.getMainAttributes().putValue("Created-By", "Talend Component Kit Tooling");
    manifest.getMainAttributes().putValue("Manifest-Version", "1.0");
    manifest.getMainAttributes().putValue("Build-Date", date);
    try (final JarOutputStream zos = new JarOutputStream(new BufferedOutputStream(new FileOutputStream(configuration.getOutput())), manifest)) {
        final Collection<String> created = new HashSet<>();
        // folders
        Stream.of("TALEND-INF/", "META-INF/", "MAVEN-INF/", "MAVEN-INF/repository/").forEach(folder -> {
            try {
                zos.putNextEntry(new JarEntry(folder));
                zos.closeEntry();
            } catch (final IOException e) {
                throw new IllegalStateException(e);
            }
            created.add(folder.substring(0, folder.length() - 1));
        });
        // libs
        final MvnCoordinateToFileConverter converter = new MvnCoordinateToFileConverter();
        configuration.getArtifacts().forEach((gav, file) -> {
            final String path = "MAVEN-INF/repository/" + converter.toArtifact(gav).toPath();
            try {
                createFolders(zos, created, path.split("/"));
                zos.putNextEntry(new JarEntry(path));
                Files.copy(file.toPath(), zos);
                zos.closeEntry();
            } catch (final IOException ioe) {
                throw new IllegalStateException(ioe);
            }
        });
        // meta
        zos.putNextEntry(new JarEntry("TALEND-INF/metadata.properties"));
        metadata.store(zos, "Generated metadata by Talend Component Kit Car Bundle");
        zos.closeEntry();
        // executable
        final String main = CarMain.class.getName().replace('.', '/') + ".class";
        createFolders(zos, created, main.split("/"));
        zos.putNextEntry(new JarEntry(main));
        try (final InputStream stream = CarBundler.class.getClassLoader().getResourceAsStream(main)) {
            byte[] buffer = new byte[1024];
            int read;
            while ((read = stream.read(buffer)) >= 0) {
                zos.write(buffer, 0, read);
            }
        }
        zos.closeEntry();
    } catch (final IOException e) {
        throw new IllegalStateException(e.getMessage(), e);
    }
    log.info("Created " + configuration.getOutput());
}
Also used : MvnCoordinateToFileConverter(org.talend.sdk.component.dependencies.maven.MvnCoordinateToFileConverter) InputStream(java.io.InputStream) JarOutputStream(java.util.jar.JarOutputStream) IOException(java.io.IOException) Properties(java.util.Properties) Manifest(java.util.jar.Manifest) JarEntry(java.util.jar.JarEntry) FileOutputStream(java.io.FileOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) CarMain(org.talend.sdk.component.tools.exec.CarMain) HashSet(java.util.HashSet)

Aggregations

IOException (java.io.IOException)3 InputStream (java.io.InputStream)3 Properties (java.util.Properties)3 MvnCoordinateToFileConverter (org.talend.sdk.component.dependencies.maven.MvnCoordinateToFileConverter)3 File (java.io.File)2 FileInputStream (java.io.FileInputStream)2 Optional.ofNullable (java.util.Optional.ofNullable)2 Collectors.toList (java.util.stream.Collectors.toList)2 Stream (java.util.stream.Stream)2 Artifact (org.talend.sdk.component.dependencies.maven.Artifact)2 BufferedOutputStream (java.io.BufferedOutputStream)1 FileOutputStream (java.io.FileOutputStream)1 FileReader (java.io.FileReader)1 FileWriter (java.io.FileWriter)1 Reader (java.io.Reader)1 Writer (java.io.Writer)1 Files (java.nio.file.Files)1 StandardCopyOption (java.nio.file.StandardCopyOption)1 StandardOpenOption (java.nio.file.StandardOpenOption)1 LocalDateTime (java.time.LocalDateTime)1