Search in sources :

Example 1 with Artifact

use of org.talend.sdk.component.dependencies.maven.Artifact 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 Artifact

use of org.talend.sdk.component.dependencies.maven.Artifact 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 Artifact

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

the class ComponentResource method getDependency.

/**
 * Return a binary of the dependency represented by `id`.
 * It can be maven coordinates for dependencies or a component id.
 *
 * @param id the dependency identifier.
 * @return the dependency binary (jar).
 */
@GET
@Path("dependency/{id}")
@Produces(MediaType.APPLICATION_OCTET_STREAM)
public StreamingOutput getDependency(@PathParam("id") final String id) {
    final ComponentFamilyMeta.BaseMeta<?> component = componentDao.findById(id);
    final File file;
    if (component != null) {
        // local dep
        file = componentManagerService.manager().findPlugin(component.getParent().getPlugin()).orElseThrow(() -> new WebApplicationException(Response.status(Response.Status.NOT_FOUND).type(APPLICATION_JSON_TYPE).entity(new ErrorPayload(PLUGIN_MISSING, "No plugin matching the id: " + id)).build())).getContainerFile().orElseThrow(() -> new WebApplicationException(Response.status(Response.Status.NOT_FOUND).type(APPLICATION_JSON_TYPE).entity(new ErrorPayload(PLUGIN_MISSING, "No dependency matching the id: " + id)).build()));
    } else {
        // just try to resolve it locally, note we would need to ensure some security here
        // .map(Artifact::toPath).map(localDependencyRelativeResolver
        final Artifact artifact = Artifact.from(id);
        file = componentManagerService.manager().getContainer().resolve(artifact.toPath());
    }
    if (!file.exists()) {
        throw new WebApplicationException(Response.status(Response.Status.NOT_FOUND).type(APPLICATION_JSON_TYPE).entity(new ErrorPayload(PLUGIN_MISSING, "No file found for: " + id)).build());
    }
    return output -> {
        // 5k
        final byte[] buffer = new byte[40960];
        try (final InputStream stream = new BufferedInputStream(new FileInputStream(file), buffer.length)) {
            int count;
            while ((count = stream.read(buffer)) >= 0) {
                if (count == 0) {
                    continue;
                }
                output.write(buffer, 0, count);
            }
        }
    };
}
Also used : BufferedInputStream(java.io.BufferedInputStream) Produces(javax.ws.rs.Produces) PropertiesService(org.talend.sdk.component.server.service.PropertiesService) ComponentFamilyMeta(org.talend.sdk.component.runtime.manager.ComponentFamilyMeta) Path(javax.ws.rs.Path) Icon(org.talend.sdk.component.server.front.model.Icon) Collections.singletonList(java.util.Collections.singletonList) ComponentDetail(org.talend.sdk.component.server.front.model.ComponentDetail) ComponentManagerService(org.talend.sdk.component.server.service.ComponentManagerService) MediaType(javax.ws.rs.core.MediaType) Link(org.talend.sdk.component.server.front.model.Link) QueryParam(javax.ws.rs.QueryParam) Collectors.toMap(java.util.stream.Collectors.toMap) Consumes(javax.ws.rs.Consumes) Locale(java.util.Locale) Map(java.util.Map) DefaultValue(javax.ws.rs.DefaultValue) ComponentId(org.talend.sdk.component.server.front.model.ComponentId) ComponentFamilyDao(org.talend.sdk.component.server.dao.ComponentFamilyDao) ContainerComponentRegistry(org.talend.sdk.component.runtime.manager.ContainerComponentRegistry) ComponentDetailList(org.talend.sdk.component.server.front.model.ComponentDetailList) Collections.emptyList(java.util.Collections.emptyList) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) StreamingOutput(javax.ws.rs.core.StreamingOutput) DependencyDefinition(org.talend.sdk.component.server.front.model.DependencyDefinition) ErrorDictionary(org.talend.sdk.component.server.front.model.ErrorDictionary) ErrorPayload(org.talend.sdk.component.server.front.model.error.ErrorPayload) Objects(java.util.Objects) List(java.util.List) Slf4j(lombok.extern.slf4j.Slf4j) Stream(java.util.stream.Stream) Response(javax.ws.rs.core.Response) PostConstruct(javax.annotation.PostConstruct) Optional(java.util.Optional) WebApplicationException(javax.ws.rs.WebApplicationException) ApplicationScoped(javax.enterprise.context.ApplicationScoped) PathParam(javax.ws.rs.PathParam) GET(javax.ws.rs.GET) Dependencies(org.talend.sdk.component.server.front.model.Dependencies) DESIGN_MODEL_MISSING(org.talend.sdk.component.server.front.model.ErrorDictionary.DESIGN_MODEL_MISSING) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) Inject(javax.inject.Inject) LocaleMapper(org.talend.sdk.component.server.service.LocaleMapper) PLUGIN_MISSING(org.talend.sdk.component.server.front.model.ErrorDictionary.PLUGIN_MISSING) RequestKey(org.talend.sdk.component.server.front.base.internal.RequestKey) ActionsService(org.talend.sdk.component.server.service.ActionsService) Collections.emptyMap(java.util.Collections.emptyMap) POST(javax.ws.rs.POST) Container(org.talend.sdk.component.container.Container) Optional.ofNullable(java.util.Optional.ofNullable) Artifact(org.talend.sdk.component.dependencies.maven.Artifact) COMPONENT_MISSING(org.talend.sdk.component.server.front.model.ErrorDictionary.COMPONENT_MISSING) DesignModel(org.talend.sdk.component.design.extension.DesignModel) ComponentIndex(org.talend.sdk.component.server.front.model.ComponentIndex) FileInputStream(java.io.FileInputStream) ComponentIndices(org.talend.sdk.component.server.front.model.ComponentIndices) IconResolver(org.talend.sdk.component.server.service.IconResolver) File(java.io.File) Collectors.toList(java.util.stream.Collectors.toList) ComponentDao(org.talend.sdk.component.server.dao.ComponentDao) APPLICATION_JSON_TYPE(javax.ws.rs.core.MediaType.APPLICATION_JSON_TYPE) ComponentManager(org.talend.sdk.component.runtime.manager.ComponentManager) InputStream(java.io.InputStream) ErrorPayload(org.talend.sdk.component.server.front.model.error.ErrorPayload) WebApplicationException(javax.ws.rs.WebApplicationException) BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ComponentFamilyMeta(org.talend.sdk.component.runtime.manager.ComponentFamilyMeta) File(java.io.File) Artifact(org.talend.sdk.component.dependencies.maven.Artifact) FileInputStream(java.io.FileInputStream) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Aggregations

File (java.io.File)3 FileInputStream (java.io.FileInputStream)3 InputStream (java.io.InputStream)3 Optional.ofNullable (java.util.Optional.ofNullable)3 Collectors.toList (java.util.stream.Collectors.toList)3 Stream (java.util.stream.Stream)3 Artifact (org.talend.sdk.component.dependencies.maven.Artifact)3 IOException (java.io.IOException)2 Collections.emptyList (java.util.Collections.emptyList)2 List (java.util.List)2 Map (java.util.Map)2 Properties (java.util.Properties)2 PostConstruct (javax.annotation.PostConstruct)2 ApplicationScoped (javax.enterprise.context.ApplicationScoped)2 Inject (javax.inject.Inject)2 Slf4j (lombok.extern.slf4j.Slf4j)2 Container (org.talend.sdk.component.container.Container)2 MvnCoordinateToFileConverter (org.talend.sdk.component.dependencies.maven.MvnCoordinateToFileConverter)2 ComponentFamilyMeta (org.talend.sdk.component.runtime.manager.ComponentFamilyMeta)2 BufferedInputStream (java.io.BufferedInputStream)1