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);
});
}
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);
}
}
}
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);
}
}
};
}
Aggregations