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

camel-jbang - Auto detect vault dependencies #16293

Merged
merged 3 commits into from
Nov 16, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import org.apache.camel.main.download.DependencyDownloaderDataFormatResolver;
import org.apache.camel.main.download.DependencyDownloaderKamelet;
import org.apache.camel.main.download.DependencyDownloaderLanguageResolver;
import org.apache.camel.main.download.DependencyDownloaderPeriodTaskResolver;
import org.apache.camel.main.download.DependencyDownloaderPropertiesComponent;
import org.apache.camel.main.download.DependencyDownloaderPropertiesFunctionResolver;
import org.apache.camel.main.download.DependencyDownloaderPropertyBindingListener;
Expand Down Expand Up @@ -612,6 +613,14 @@ protected CamelContext createCamelContext() {
ff = ffr.resolveDefaultFactoryFinder(classResolver);
answer.getCamelContextExtension().setDefaultFactoryFinder(ff);

// period task resolver that can download needed dependencies
if (!export) {
Object camelVersion = getInitialProperties().get(getInstanceType() + ".camelVersion");
PeriodTaskResolver ptr = new DependencyDownloaderPeriodTaskResolver(
ff, answer, Optional.ofNullable(camelVersion).map(Object::toString).orElse(null));
answer.getCamelContextExtension().addContextPlugin(PeriodTaskResolver.class, ptr);
}

answer.getCamelContextExtension().addContextPlugin(ComponentResolver.class,
new DependencyDownloaderComponentResolver(answer, stubPattern, silent));
answer.getCamelContextExtension().addContextPlugin(DataFormatResolver.class,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
* 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.
*/
package org.apache.camel.main.download;

import java.util.Optional;

import org.apache.camel.CamelContext;
import org.apache.camel.impl.engine.DefaultPeriodTaskResolver;
import org.apache.camel.spi.FactoryFinder;
import org.apache.camel.spi.PeriodTaskResolver;
import org.apache.camel.support.ResolverHelper;
import org.apache.camel.util.ObjectHelper;

public class DependencyDownloaderPeriodTaskResolver extends DefaultPeriodTaskResolver {

private final DependencyDownloader downloader;
private final CamelContext camelContext;
private final String camelVersion;

public DependencyDownloaderPeriodTaskResolver(FactoryFinder finder, CamelContext camelContext, String camelVersion) {
super(finder);
this.camelContext = camelContext;
this.camelVersion = camelVersion;
this.downloader = camelContext.hasService(DependencyDownloader.class);
}

@Override
public Optional<Object> newInstance(String key) {
maybeDownload(key);

Optional<Object> answer = super.newInstance(key);
if (answer.isEmpty()) {
// need to use regular factory finder as bootstrap has already marked as a miss
final FactoryFinder finder
= camelContext.getCamelContextExtension().getFactoryFinder(PeriodTaskResolver.RESOURCE_PATH);
Object obj = ResolverHelper.resolveService(camelContext, finder, key, Object.class).orElse(null);
return Optional.ofNullable(obj);
}
return answer;
}

@Override
public <T> Optional<T> newInstance(String key, Class<T> type) {
maybeDownload(key);

Optional<T> answer = super.newInstance(key, type);
if (answer.isEmpty()) {
// need to use regular factory finder as bootstrap has already marked as a miss
final FactoryFinder finder
= camelContext.getCamelContextExtension().getFactoryFinder(PeriodTaskResolver.RESOURCE_PATH);
T obj = ResolverHelper.resolveService(camelContext, finder, key, type).orElse(null);
return Optional.ofNullable(obj);
}
return answer;
}

private void maybeDownload(String key) {
if ("aws-secret-refresh".equals(key)) {
downloadLoader("camel-aws-secrets-manager");
} else if ("gcp-secret-refresh".equals(key)) {
downloadLoader("camel-google-secret-manager");
} else if ("azure-secret-refresh".equals(key)) {
downloadLoader("camel-azure-key-vault");
} else if ("kubernetes-secret-refresh".equals(key)) {
downloadLoader("camel-kubernetes");
} else if ("kubernetes-configmaps-refresh".equals(key)) {
downloadLoader("camel-kubernetes");
}
}

private void downloadLoader(String artifactId) {
String resolvedCamelVersion = camelContext.getVersion();
if (ObjectHelper.isEmpty(resolvedCamelVersion)) {
resolvedCamelVersion = camelVersion;
}

if (!downloader.alreadyOnClasspath("org.apache.camel", artifactId, resolvedCamelVersion)) {
downloader.downloadDependency("org.apache.camel", artifactId, resolvedCamelVersion);
}
}

}