ppalaga commented on a change in pull request #226: Use MainSupport as base for 
running Camel
URL: https://github.com/apache/camel-quarkus/pull/226#discussion_r331416255
 
 

 ##########
 File path: 
extensions/core/runtime/src/main/java/org/apache/camel/quarkus/core/runtime/CamelRecorder.java
 ##########
 @@ -16,88 +16,98 @@
  */
 package org.apache.camel.quarkus.core.runtime;
 
-import io.quarkus.arc.runtime.BeanContainerListener;
+import java.util.function.Supplier;
+
+import io.quarkus.arc.runtime.BeanContainer;
 import io.quarkus.runtime.RuntimeValue;
-import io.quarkus.runtime.ShutdownContext;
 import io.quarkus.runtime.annotations.Recorder;
-import org.apache.camel.RoutesBuilder;
-import org.apache.camel.quarkus.core.runtime.support.FastCamelRuntime;
+import org.apache.camel.CamelContext;
+import org.apache.camel.ExtendedCamelContext;
+import org.apache.camel.RuntimeCamelException;
+import org.apache.camel.quarkus.core.runtime.support.FastCamelContext;
+import org.apache.camel.quarkus.core.runtime.support.FastModel;
+import org.apache.camel.quarkus.core.runtime.support.RuntimeRegistry;
 import org.apache.camel.spi.Registry;
+import org.graalvm.nativeimage.ImageInfo;
 
 @Recorder
 public class CamelRecorder {
 
-    public RuntimeValue<CamelRuntime> create(Registry registry) {
-
-        FastCamelRuntime fcr = new FastCamelRuntime();
-        fcr.setRegistry(registry);
-
-        return new RuntimeValue<>(fcr);
-    }
-
-    public void init(
-            RuntimeValue<CamelRuntime> runtime,
-            CamelConfig.BuildTime buildTimeConfig) {
-
-        FastCamelRuntime fcr = (FastCamelRuntime) runtime.getValue();
-        fcr.init(buildTimeConfig);
+    public RuntimeValue<Registry> createRegistry() {
+        return new RuntimeValue<>(new RuntimeRegistry());
     }
 
-    public void start(
-            ShutdownContext shutdown,
-            RuntimeValue<CamelRuntime> runtime,
-            CamelConfig.Runtime runtimeConfig) throws Exception {
+    @SuppressWarnings("unchecked")
+    public RuntimeValue<CamelContext> createContext(RuntimeValue<Registry> 
registry, BeanContainer beanContainer, CamelConfig.BuildTime buildTimeConfig) {
+        FastCamelContext context = new FastCamelContext();
+        context.setRegistry(registry.getValue());
+        context.setLoadTypeConverters(false);
+        context.getTypeConverterRegistry().setInjector(context.getInjector());
 
-        runtime.getValue().start(runtimeConfig);
-
-        //in development mode undertow is started eagerly
-        shutdown.addShutdownTask(new Runnable() {
-            @Override
-            public void run() {
-                try {
-                    runtime.getValue().stop();
-                } catch (Exception e) {
-                    throw new RuntimeException(e);
+        try {
+            if (buildTimeConfig.disableJaxb) {
+                
context.adapt(ExtendedCamelContext.class).setModelJAXBContextFactory(() -> {
+                    throw new UnsupportedOperationException();
+                });
+            } else {
+                // The creation of the JAXB context is very time consuming, so 
always prepare it
+                // when running in native mode, but lazy create it in java 
mode so that we don't
+                // waste time if using java routes
+                if (ImageInfo.inImageBuildtimeCode()) {
+                    
context.adapt(ExtendedCamelContext.class).getModelJAXBContextFactory().newJAXBContext();
                 }
             }
-        });
-    }
+        } catch (Exception e) {
+            throw RuntimeCamelException.wrapRuntimeCamelException(e);
+        }
 
-    public void addBuilder(
-        RuntimeValue<CamelRuntime> runtime,
-        String className) {
+        FastModel model = new FastModel(context);
 
-        FastCamelRuntime fcr = (FastCamelRuntime) runtime.getValue();
+        context.setModel(model);
+        context.init();
 
-        try {
-            fcr.getBuilders().add((RoutesBuilder) 
Class.forName(className).newInstance());
-        } catch (Exception e) {
-            throw new RuntimeException(e);
-        }
+        // register to the container
+        beanContainer.instance(CamelProducers.class).setContext(context);
+
+        return new RuntimeValue<>(context);
     }
 
     public void bind(
-        RuntimeValue<CamelRuntime> runtime,
+        RuntimeValue<Registry> runtime,
         String name,
         Class<?> type,
         Object instance) {
 
-        runtime.getValue().getRegistry().bind(name, type, instance);
+        runtime.getValue().bind(name, type, instance);
     }
 
     public void bind(
-        RuntimeValue<CamelRuntime> runtime,
+        RuntimeValue<Registry> runtime,
         String name,
         Class<?> type) {
 
         try {
-            runtime.getValue().getRegistry().bind(name, type, 
type.newInstance());
+            runtime.getValue().bind(name, type, type.newInstance());
         } catch (Exception e) {
             throw new RuntimeException(e);
         }
     }
 
-    public BeanContainerListener 
initRuntimeInjection(RuntimeValue<CamelRuntime> runtime) {
-        return container -> 
container.instance(CamelProducers.class).setCamelRuntime(runtime.getValue());
+    public Supplier<CamelConfig.BuildTime> 
buildTimeSupplier(CamelConfig.BuildTime config) {
 
 Review comment:
   I vote for renaming to `buildTimeConfigSupplier`

----------------------------------------------------------------
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.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to