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