Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Split kamelet catalog and utilities #319 #420

Merged
merged 1 commit into from
Jul 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 19 additions & 40 deletions library/camel-kamelets-catalog/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,17 @@

<dependencies>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.apache.camel.kamelets</groupId>
<artifactId>camel-kamelets</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-yaml</artifactId>
Expand All @@ -50,26 +61,31 @@
<artifactId>jackson-datatype-jsr310</artifactId>
<version>${jackson.version}</version>
</dependency>

<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>${commons.io.version}</version>
<groupId>io.github.classgraph</groupId>
<artifactId>classgraph</artifactId>
<version>${classgraph.version}</version>
</dependency>

<!-- logging -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>${log4j.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>${log4j.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>${log4j.version}</version>
<scope>test</scope>
</dependency>

<dependency>
Expand All @@ -84,43 +100,6 @@
<scope>test</scope>
<version>${junit.jupiter.version}</version>
</dependency>
<dependency>
<groupId>io.github.classgraph</groupId>
<artifactId>classgraph</artifactId>
<version>${classgraph.version}</version>
</dependency>


</dependencies>

<build>
<plugins>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<id>copy-resource-one</id>
<phase>install</phase>
<goals>
<goal>copy-resources</goal>
</goals>

<configuration>
<overwrite>true</overwrite>
<outputDirectory>${project.basedir}/src/main/resources/kamelets</outputDirectory>
<resources>
<resource>
<directory>./../../</directory>
<includes>
<include>*.kamelet.yaml</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -16,65 +16,75 @@
*/
package org.apache.camel.kamelets.catalog;

import java.io.IOException;
import java.io.InputStream;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import io.fabric8.camelk.v1alpha1.Kamelet;
import io.fabric8.kubernetes.api.model.apiextensions.v1.JSONSchemaProps;
import io.github.classgraph.ClassGraph;
import io.github.classgraph.Resource;
import io.github.classgraph.ScanResult;
import org.apache.camel.kamelets.catalog.model.KameletAnnotationsNames;
import org.apache.camel.kamelets.catalog.model.KameletLabelNames;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class KameletsCatalog {

private static final Logger LOG = LoggerFactory.getLogger(KameletsCatalog.class);
static final String KAMELETS_DIR = "kamelets";
private static final Logger LOG = LoggerFactory.getLogger(KameletsCatalog.class);
private static final String KAMELETS_FILE_SUFFIX = ".kamelet.yaml";
private static ObjectMapper mapper = new ObjectMapper(new YAMLFactory()).configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
private Map<String, Kamelet> kameletModels = new HashMap<>();
private List<String> kameletNames = new ArrayList<>();
private static final ObjectMapper MAPPER = new ObjectMapper(new YAMLFactory()).configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

private final Map<String, Kamelet> kameletModels;
private final List<String> kameletNames;

public KameletsCatalog() {
initCatalog();
kameletNames = kameletModels.keySet().stream().sorted(Comparator.naturalOrder()).map(x -> x).collect(Collectors.toList());
kameletModels = initCatalog();
kameletNames = kameletModels.keySet().stream().sorted(Comparator.naturalOrder()).collect(Collectors.toList());
}

private void initCatalog() {
List<String> resourceNames;
private static Map<String, Kamelet> initCatalog() {
Map<String, Kamelet> kameletModels = new HashMap<>();

try (ScanResult scanResult = new ClassGraph().acceptPaths("/" + KAMELETS_DIR + "/").scan()) {
resourceNames = scanResult.getAllResources().getPaths();
}
for (String fileName: resourceNames) {
String pathInJar = "/" + fileName;
try {
Kamelet kamelet = mapper.readValue(KameletsCatalog.class.getResourceAsStream(pathInJar), Kamelet.class);
kameletModels.put(sanitizeFileName(fileName), kamelet);
} catch (IOException e) {
LOG.warn("Cannot init Kamelet Catalog with content of " + pathInJar, e);
for (Resource resource : scanResult.getAllResources()) {

try (InputStream is = resource.open()) {
String name = sanitizeFileName(resource.getPath());
Kamelet kamelet = MAPPER.readValue(is, Kamelet.class);

LOG.debug("Loading kamelet from: {}, path: {}, name: {}",
resource.getClasspathElementFile(),
resource.getPath(),
name);

kameletModels.put(name, kamelet);
} catch (IOException e) {
LOG.warn("Cannot init Kamelet Catalog with content of " + resource.getPath(), e);
}
}
}

return Collections.unmodifiableMap(kameletModels);
}

private String sanitizeFileName(String fileName) {
private static String sanitizeFileName(String fileName) {
int index = fileName.lastIndexOf(KAMELETS_FILE_SUFFIX);
if (index > 0) {
fileName = fileName.substring(0, index);
}
String finalName = fileName.substring(9);
return finalName;
return fileName.substring(9);
}


Expand Down
19 changes: 19 additions & 0 deletions library/camel-kamelets-catalog/src/test/resources/log4j2-test.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="INFO">
<Appenders>
<Console name="console" target="SYSTEM_OUT">
<PatternLayout
pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [%-5level] [%t] %c{1} - %msg%n" />
</Console>
<File name="file" fileName="target//camel-kamelets-test.log">
<PatternLayout>
<Pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%-5level] [%t] %c{1} - %msg%n</Pattern>
</PatternLayout>
</File>
</Appenders>
<Loggers>
<Root level="debug" additivity="false">
<AppenderRef ref="file" />
</Root>
</Loggers>
</Configuration>
66 changes: 66 additions & 0 deletions library/camel-kamelets/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--

Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.apache.camel.kamelets</groupId>
<artifactId>camel-kamelets-parent</artifactId>
<version>main-SNAPSHOT</version>
<relativePath>../../pom.xml</relativePath>
</parent>

<artifactId>camel-kamelets</artifactId>
<packaging>jar</packaging>

<name>Camel Kamelets</name>
<description>Camel Kamelets</description>

<build>
<plugins>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.1.0</version>
<executions>
<execution>
<id>copy-resource-one</id>
<phase>install</phase>
<goals>
<goal>copy-resources</goal>
</goals>

<configuration>
<overwrite>true</overwrite>
<outputDirectory>${project.basedir}/src/main/resources/kamelets</outputDirectory>
<resources>
<resource>
<directory>./../../</directory>
<includes>
<include>*.kamelet.yaml</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
2 changes: 2 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
<description>Camel Kamelets Library Parent</description>

<modules>
<module>library/camel-kamelets</module>
<module>library/camel-kamelets-catalog</module>
<module>library/camel-kamelets-utils</module>
</modules>
Expand All @@ -50,6 +51,7 @@
<maven-checksum-maven-plugin.version>1.7</maven-checksum-maven-plugin.version>

<camel.version>3.10.0</camel.version>
<slf4j.version>1.7.30</slf4j.version>
<log4j.version>2.13.3</log4j.version>
<jackson.version>2.11.4</jackson.version>
<camel.k.extension.version>5.3.1</camel.k.extension.version>
Expand Down