gnodet-bot commented on code in PR #28675: URL: https://github.com/apache/camel/pull/28675#discussion_r4418107215
########## dsl/camel-jbang/camel-jbang-export/src/main/java/org/apache/camel/dsl/jbang/export/ExportContext.java: ########## @@ -0,0 +1,104 @@ +/* + * 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.dsl.jbang.export; + +import java.util.Collection; +import java.util.Iterator; +import java.util.List; +import java.util.ServiceLoader; +import java.util.function.Function; +import java.util.function.Predicate; + +import org.apache.camel.dsl.jbang.export.common.PluginExporter; +import org.apache.camel.dsl.jbang.export.common.Printer; + +/** + * What an export uses from its caller: where it prints, how it resolves the dependencies, the exporters of the active + * plugins, and how it asks for confirmation (such as before it deletes the files of the export directory). + */ +public class ExportContext { + + private Printer printer = new Printer.SystemOutPrinter(); + private DependencyResolver dependencyResolver; + private Function<String, Collection<PluginExporter>> pluginExporters = repos -> List.of(); + private Predicate<String> confirm = message -> true; + + public Printer getPrinter() { + return printer; + } + + /** + * Where the export prints (default the console). + */ + public ExportContext withPrinter(Printer printer) { + this.printer = printer; + return this; + } + + /** + * The resolver of the dependencies; when none is set, the one registered with the {@link ServiceLoader} is used. + */ + public DependencyResolver getDependencyResolver() { + if (dependencyResolver == null) { Review Comment: ⚠️ **Thread safety:** `getDependencyResolver()` does a lazy `ServiceLoader.load()` with a plain `if (dependencyResolver == null)` check — no `volatile` field, no synchronization. If two threads call this concurrently (e.g. in a multi-threaded embedding scenario), both may load the SPI and the second assignment silently overwrites the first. Either add `volatile` + double-checked locking, or document that `ExportContext` is not thread-safe and must not be shared across threads. ########## docs/user-manual/modules/ROOT/pages/camel-jbang-projects.adoc: ########## @@ -178,6 +178,39 @@ TIP: Export logs are stored in `~/.camel/camel-export.log` by default. Export loads configuration from `application.properties`, where you can set runtime, Java version, and other parameters. See xref:camel-jbang-configuration.adoc[Configuration] for all `camel.jbang.*` options. +=== Exporting from Java + +*Since Camel 4.24 (preview)* + +Tools that create Camel projects, such as an IDE or a visual designer, can export from Java instead of running the `camel` command. +The `camel-jbang-export` module has the same options as `camel export`, as public fields of an `ExportRequest`: + +[source,java] +---- +ExportRequest request = new ExportRequest(); +request.files.add("hello.camel.yaml"); +request.runtime = RuntimeType.main; // or springBoot, quarkus +request.exportDir = "target/hello"; + +int exit = new ProjectExporter().export(request); +---- Review Comment: 💡 **Nit — doc sample:** The first code block calls `new ProjectExporter().export(request)` without a `DependencyResolver`. The fact that this requires `camel-jbang-core` on the classpath is only explained two paragraphs later. Consider adding a brief note here (e.g. `// camel-jbang-core must be on the classpath`) so readers don't hit an `IllegalStateException` before reaching the explanation. ########## dsl/camel-jbang/camel-jbang-export/src/main/java/org/apache/camel/dsl/jbang/export/ExportRequest.java: ########## @@ -0,0 +1,376 @@ +/* + * 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.dsl.jbang.export; + +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.List; + +import org.apache.camel.dsl.jbang.export.common.MavenResolver; +import org.apache.camel.dsl.jbang.export.common.QuarkusPlatform; +import org.apache.camel.dsl.jbang.export.common.RuntimeType; + +/** + * What to export: the Camel files and the options of the Maven project to create, the same as the options of camel + * export. + * <p> + * The fields are public and mutable, as the request is filled in by the caller and then completed by + * {@link ProjectExporter#prepare(ExportRequest)} with the values of application.properties and the profile. The copy + * constructor is shallow: the copy shares the lists of the original. + */ +public class ExportRequest { + Review Comment: 💡 **Nit — missing `@since`:** `ExportRequest`, `ExportContext`, `DependencyResolver`, and `ProjectExporter` are new public API classes. Consider adding `@since 4.24` to their class-level Javadoc so API consumers know when these were introduced. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
