Croway commented on code in PR #16773:
URL: https://github.com/apache/camel/pull/16773#discussion_r1910364837


##########
dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/infra/InfraRun.java:
##########
@@ -0,0 +1,135 @@
+/*
+ * 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.core.commands.infra;
+
+import java.io.InputStream;
+import java.lang.reflect.Method;
+import java.nio.charset.StandardCharsets;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Scanner;
+
+import com.fasterxml.jackson.core.type.TypeReference;
+import org.apache.camel.dsl.jbang.core.commands.CamelCommand;
+import org.apache.camel.dsl.jbang.core.commands.CamelJBangMain;
+import org.apache.camel.test.infra.common.services.InfrastructureService;
+import picocli.CommandLine;
+
[email protected](name = "run",
+                     description = "Run an external service")
+public class InfraRun extends CamelCommand {
+
+    @CommandLine.Parameters(description = "Service name", arity = "1")
+    private List<String> serviceName;
+
+    public InfraRun(CamelJBangMain main) {
+        super(main);
+    }
+
+    @Override
+    public Integer doCall() throws Exception {
+        if (serviceName == null || serviceName.isEmpty()) {
+            return 0;
+        }
+
+        String service = serviceName.get(0);
+        String serviceImplementation = serviceName.size() > 1 ? 
serviceName.get(1) : null;
+
+        run(service, serviceImplementation);
+
+        return 0;
+    }
+
+    private void run(String testService, String testServiceImplementation) 
throws Exception {
+        List<InfraCommand.TestInfraService> metadata;
+
+        try (InputStream is
+                = 
this.getClass().getClassLoader().getResourceAsStream("META-INF/test-infra-metadata.json"))
 {
+            String json = new String(is.readAllBytes(), 
StandardCharsets.UTF_8);
+
+            metadata = InfraCommand.JSON_MAPPER.readValue(json, new 
TypeReference<List<InfraCommand.TestInfraService>>() {
+            });
+        }
+
+        List<InfraCommand.TestInfraService> services = metadata;
+
+        InfraCommand.TestInfraService testInfraService = services
+                .stream()
+                .filter(service -> {
+                    if (testServiceImplementation != null && 
!testServiceImplementation.isEmpty()
+                            && service.aliasImplementation() != null) {
+                        return service.alias().contains(testService)
+                                && 
service.aliasImplementation().contains(testServiceImplementation);
+                    } else if (testServiceImplementation == null) {
+                        return service.alias().contains(testService)
+                                && (service.aliasImplementation() == null || 
service.aliasImplementation().isEmpty());
+                    }
+
+                    return false;
+                })
+                .findFirst()
+                .orElseThrow(() -> {
+                    String message = ", use the list command for the available 
services";
+                    if (testServiceImplementation != null) {
+                        return new IllegalArgumentException(
+                                "service " + testService + " with 
implementation " + testServiceImplementation + " not found"
+                                                            + message);
+                    }
+
+                    return new IllegalArgumentException("service " + 
testService + " not found" + message);
+                });
+
+        String serviceInterface = testInfraService.service();
+        String serviceImpl = testInfraService.implementation();
+
+        printer().println("Starting service " + testService);
+
+        InfrastructureService actualService = (InfrastructureService) 
Class.forName(serviceImpl)
+                .getDeclaredConstructor(null)
+                .newInstance(null);

Review Comment:
   yeah, that would be nice, so far most of the Services have an empty 
constructor with a default configuration, but some of them, sftp for example, 
need a security configuration.
   
   I guess that we can add some information into the annotation, or the Service 
itself



-- 
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]

Reply via email to