use of org.apache.camel.tooling.model.ComponentModel in project camel-idea-plugin by camel-tooling.
the class CamelDocumentationProvider method externalUrl.
private static String externalUrl(Project project, String val) {
String url = null;
String name = asComponentName(val);
CamelCatalog camelCatalog = ServiceManager.getService(project, CamelCatalogService.class).get();
if (name != null && camelCatalog.findComponentNames().contains(name)) {
String json = camelCatalog.componentJSonSchema(name);
ComponentModel component = JsonMapper.generateComponentModel(json);
String version = component.getVersion();
if (version.startsWith("2")) {
version = "2.x";
} else if (version.startsWith("3.4")) {
// LTS
version = "3.4.x";
} else if (version.startsWith("3.7")) {
// LTS
version = "3.7.x";
} else {
version = "latest";
}
if ("other".equals(component.getKind())) {
url = String.format("https://camel.apache.org/components/%s/others/%s.html", version, component.getName());
} else if ("component".equals(component.getKind())) {
url = String.format("https://camel.apache.org/components/%s/%s-component.html", version, component.getScheme());
} else {
url = String.format("https://camel.apache.org/components/%s/%ss/%s-%s.html", version, component.getKind(), component.getName(), component.getKind());
}
}
return url;
}
use of org.apache.camel.tooling.model.ComponentModel in project camel-idea-plugin by camel-tooling.
the class CamelSmartCompletionEndpointOptions method addSmartCompletionContextPathEnumSuggestions.
private static List<LookupElement> addSmartCompletionContextPathEnumSuggestions(final String val, final ComponentModel component, final Map<String, String> existing) {
final List<LookupElement> answer = new ArrayList<>();
double priority = 100.0d;
// lets help the suggestion list if we are editing the context-path and only have 1 enum type option
// and the option has not been in use yet, then we can populate the list with the enum values.
final long enums = component.getEndpointOptions().stream().filter(o -> "path".equals(o.getKind()) && o.getEnums() != null).count();
if (enums == 1) {
for (final ComponentModel.EndpointOptionModel option : component.getEndpointOptions()) {
// only add support for enum in the context-path smart completion
if ("path".equals(option.getKind()) && option.getEnums() != null) {
final String name = option.getName();
// only add if not already used
final String old = existing != null ? existing.get(name) : "";
if (existing == null || old == null || old.isEmpty()) {
// add all enum as choices
for (final String choice : option.getEnums()) {
final String key = choice;
final String lookup = val + key;
LookupElementBuilder builder = LookupElementBuilder.create(lookup);
// only show the option in the UI
builder = builder.withPresentableText(choice);
// lets use the option name as the type so its visible
builder = builder.withTypeText(name, true);
builder = builder.withIcon(AllIcons.Nodes.Enum);
if (option.isDeprecated()) {
// mark as deprecated
builder = builder.withStrikeoutness(true);
}
// its an enum so always auto complete the choices
LookupElement element = builder.withAutoCompletionPolicy(AutoCompletionPolicy.ALWAYS_AUTOCOMPLETE);
// they should be in the exact order
element = PrioritizedLookupElement.withPriority(element, priority);
priority -= 1.0d;
answer.add(element);
}
}
}
}
}
return answer;
}
use of org.apache.camel.tooling.model.ComponentModel in project camel-idea-plugin by camel-tooling.
the class CamelEndpointSmartCompletionExtension method addCompletions.
@Override
public void addCompletions(@NotNull CompletionParameters parameters, @NotNull ProcessingContext context, @NotNull CompletionResultSet resultSet, @NotNull String[] query) {
boolean endsWithAmpQuestionMark = false;
// it is a known Camel component
String componentName = StringUtils.asComponentName(query[0]);
// it is a known Camel component
Project project = parameters.getOriginalFile().getManager().getProject();
CamelCatalog camelCatalog = ServiceManager.getService(project, CamelCatalogService.class).get();
String json = camelCatalog.componentJSonSchema(componentName);
ComponentModel componentModel = JsonMapper.generateComponentModel(json);
final PsiElement element = parameters.getPosition();
// grab all existing parameters
String concatQuery = query[0];
String suffix = query[1];
String queryAtPosition = query[2];
String prefixValue = query[2];
// camel catalog expects & as & when it parses so replace all & as &
concatQuery = concatQuery.replaceAll("&", "&");
boolean editQueryParameters = concatQuery.contains("?");
// strip up ending incomplete parameter
if (queryAtPosition.endsWith("&") || queryAtPosition.endsWith("?")) {
endsWithAmpQuestionMark = true;
queryAtPosition = queryAtPosition.substring(0, queryAtPosition.length() - 1);
}
// strip up ending incomplete parameter
if (concatQuery.endsWith("&") || concatQuery.endsWith("?")) {
concatQuery = concatQuery.substring(0, concatQuery.length() - 1);
}
Map<String, String> existing = null;
try {
existing = camelCatalog.endpointProperties(concatQuery);
} catch (Exception e) {
LOG.warn("Error parsing Camel endpoint properties with url: " + queryAtPosition, e);
}
// are we editing an existing parameter value
// or are we having a list of suggested parameters to choose among
boolean caretAtEndOfLine = getIdeaUtils().isCaretAtEndOfLine(element);
LOG.trace("Caret at end of line: " + caretAtEndOfLine);
String[] queryParameter = getIdeaUtils().getQueryParameterAtCursorPosition(element);
String optionValue = queryParameter[1];
// a bit complex to figure out whether to edit the endpoint value or not
boolean editOptionValue = false;
if (endsWithAmpQuestionMark) {
// should not edit value but suggest a new option instead
editOptionValue = false;
} else {
if ("".equals(optionValue)) {
// empty value so must edit
editOptionValue = true;
} else if (StringUtils.isNotEmpty(optionValue) && !caretAtEndOfLine) {
// has value and cursor not at end of line so must edit
editOptionValue = true;
}
}
LOG.trace("Add new option: " + !editOptionValue);
LOG.trace("Edit option value: " + editOptionValue);
List<LookupElement> answer = null;
if (editOptionValue) {
String name = queryParameter[0].substring(1);
ComponentModel.EndpointOptionModel endpointOption = componentModel.getEndpointOptions().stream().filter(o -> name.equals(o.getName())).findFirst().orElse(null);
if (endpointOption != null) {
answer = addSmartCompletionForEndpointValue(parameters.getEditor(), queryAtPosition, suffix, endpointOption, element, xmlMode);
}
}
if (answer == null) {
if (editQueryParameters) {
// suggest a list of options for query parameters
answer = addSmartCompletionSuggestionsQueryParameters(query, componentModel, existing, xmlMode, element, parameters.getEditor());
} else {
if (!resultSet.isStopped()) {
// suggest a list of options for context-path
answer = addSmartCompletionSuggestionsContextPath(queryAtPosition, componentModel, existing, xmlMode, element);
}
}
}
// are there any results then add them
if (answer != null && !answer.isEmpty()) {
resultSet.withPrefixMatcher(prefixValue).addAllElements(answer);
resultSet.stopHere();
}
}
use of org.apache.camel.tooling.model.ComponentModel in project camel-spring-boot by apache.
the class SpringBootAutoConfigurationMojo method executeComponents.
private void executeComponents(JarFile componentJar, Map<String, Supplier<String>> jsonFiles) throws MojoFailureException {
// find the component names
List<String> componentNames = findComponentNames(componentJar);
// create auto configuration for the components
if (!componentNames.isEmpty()) {
getLog().debug("Found " + componentNames.size() + " components");
List<ComponentModel> allModels = new LinkedList<>();
for (String componentName : componentNames) {
String json = loadComponentJson(jsonFiles, componentName);
if (json != null) {
ComponentModel model = JsonMapper.generateComponentModel(json);
allModels.add(model);
}
}
// Group the models by implementing classes
Map<String, List<ComponentModel>> grModels = allModels.stream().collect(Collectors.groupingBy(ComponentModel::getJavaType));
for (String componentClass : grModels.keySet()) {
List<ComponentModel> compModels = grModels.get(componentClass);
// They should be
ComponentModel model = compModels.get(0);
// equivalent
List<String> aliases = compModels.stream().map(ComponentModel::getScheme).sorted().collect(Collectors.toList());
// use springboot as sub package name so the code is not in normal
// package so the Spring Boot JARs can be optional at runtime
int pos = model.getJavaType().lastIndexOf(".");
String pkg = model.getJavaType().substring(0, pos) + ".springboot";
String overrideComponentName = null;
if (aliases.size() > 1) {
// determine component name when there are multiple ones
overrideComponentName = model.getArtifactId().replace("camel-", "");
}
beforeGenerateComponentSource(model);
boolean complexOptions = model.getOptions().stream().anyMatch(this::isComplexType);
createComponentConfigurationSource(pkg, model, overrideComponentName);
createComponentAutoConfigurationSource(pkg, model, overrideComponentName, complexOptions);
if (complexOptions) {
createComponentConverterSource(pkg, model);
}
createComponentSpringFactorySource(pkg, model);
}
}
}
use of org.apache.camel.tooling.model.ComponentModel in project camel-kafka-connector by apache.
the class CamelKafkaConnectorUpdateMojo method updateConnector.
private void updateConnector() throws Exception {
String sanitizedName = sanitizeMavenArtifactId(name);
// create the connector directory
File connectorDir = new File(projectDir, "camel-" + sanitizedName + KAFKA_CONNECTORS_SUFFIX);
if (!connectorDir.exists() || !connectorDir.isDirectory()) {
getLog().info("Connector " + name + " can not be updated since directory " + connectorDir.getAbsolutePath() + " dose not exist.");
throw new MojoFailureException("Directory already exists: " + connectorDir);
}
// create the base pom.xml
Document pom = createBasePom(connectorDir);
// Apply changes to the connector pom
fixExcludedDependencies(pom);
fixAdditionalDependencies(pom, additionalDependencies);
fixAdditionalRepositories(pom);
// Write the connector pom
File pomFile = new File(connectorDir, "pom.xml");
writeXmlFormatted(pom, pomFile, getLog());
// write package
Document pkg = createPackageFile();
File pkgFile = new File(connectorDir, "src/main/assembly/package.xml");
writeXmlFormatted(pkg, pkgFile, getLog());
// write LICENSE, USAGE
writeStaticFiles(connectorDir);
// generate classes
ComponentModel model = JsonMapper.generateComponentModel(componentJson);
if (model.isConsumerOnly()) {
createClasses(sanitizedName, connectorDir, model, ConnectorType.SOURCE);
} else if (model.isProducerOnly()) {
createClasses(sanitizedName, connectorDir, model, ConnectorType.SINK);
} else {
createClasses(sanitizedName, connectorDir, model, ConnectorType.SOURCE);
createClasses(sanitizedName, connectorDir, model, ConnectorType.SINK);
}
}
Aggregations