You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by GitBox <gi...@apache.org> on 2020/12/03 13:48:12 UTC

[GitHub] [camel-quarkus] lburgazzoli commented on a change in pull request #2048: CSimple language support

lburgazzoli commented on a change in pull request #2048:
URL: https://github.com/apache/camel-quarkus/pull/2048#discussion_r535240497



##########
File path: extensions-core/core/deployment/src/main/java/org/apache/camel/quarkus/core/deployment/CSimpleLanguageProcessor.java
##########
@@ -0,0 +1,455 @@
+/*
+ * 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.quarkus.core.deployment;
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.net.URISyntaxException;
+import java.net.URL;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.AbstractMap.SimpleImmutableEntry;
+import java.util.ArrayDeque;
+import java.util.Collections;
+import java.util.Deque;
+import java.util.Enumeration;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.LinkedHashMap;
+import java.util.LinkedHashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.Set;
+import java.util.TreeSet;
+import java.util.function.Function;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+
+import javax.xml.bind.JAXBContext;
+import javax.xml.bind.JAXBException;
+import javax.xml.bind.Marshaller;
+import javax.xml.bind.Marshaller.Listener;
+
+import org.xml.sax.Attributes;
+import org.xml.sax.ContentHandler;
+import org.xml.sax.Locator;
+import org.xml.sax.SAXException;
+
+import io.quarkus.bootstrap.classloading.ClassPathElement;
+import io.quarkus.bootstrap.classloading.QuarkusClassLoader;
+import io.quarkus.deployment.annotations.BuildProducer;
+import io.quarkus.deployment.annotations.BuildStep;
+import io.quarkus.deployment.annotations.ExecutionTime;
+import io.quarkus.deployment.annotations.Record;
+import io.quarkus.deployment.builditem.GeneratedClassBuildItem;
+import io.quarkus.deployment.dev.CompilationProvider;
+import io.quarkus.deployment.dev.CompilationProvider.Context;
+import io.quarkus.deployment.dev.JavaCompilationProvider;
+import io.quarkus.runtime.RuntimeValue;
+import org.apache.camel.Exchange;
+import org.apache.camel.NamedNode;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.language.csimple.CSimpleCodeGenerator;
+import org.apache.camel.language.csimple.CSimpleGeneratedCode;
+import org.apache.camel.language.csimple.CSimpleHelper;
+import org.apache.camel.language.csimple.CSimpleLanguage;
+import org.apache.camel.model.Constants;
+import org.apache.camel.model.ExpressionNode;
+import org.apache.camel.quarkus.core.CSimpleLanguageRecorder;
+import org.apache.camel.quarkus.core.deployment.spi.CSimpleExpressionSourceBuildItem;
+import org.apache.camel.quarkus.core.deployment.spi.CamelBeanBuildItem;
+import org.apache.camel.quarkus.core.deployment.spi.CamelContextBuildItem;
+import org.apache.camel.quarkus.core.deployment.spi.CamelRoutesBuilderClassBuildItem;
+import org.apache.camel.quarkus.core.deployment.spi.CompiledCSimpleExpressionBuildItem;
+import org.apache.camel.util.PropertiesHelper;
+
+class CSimpleLanguageProcessor {
+    static final String CLASS_EXT = ".class";
+
+    @BuildStep
+    void collectCSimpleExpresions(
+            List<CamelRoutesBuilderClassBuildItem> routesBuilderClasses,
+            BuildProducer<CSimpleExpressionSourceBuildItem> csimpleExpressions)
+            throws IOException, ClassNotFoundException, URISyntaxException, JAXBException {
+
+        if (!routesBuilderClasses.isEmpty()) {
+            final ClassLoader loader = Thread.currentThread().getContextClassLoader();
+            if (!(loader instanceof QuarkusClassLoader)) {
+                throw new IllegalStateException(
+                        QuarkusClassLoader.class.getSimpleName() + " expected as the context class loader");
+            }
+
+            final ExpressionCollector collector = new ExpressionCollector(loader);
+
+            for (CamelRoutesBuilderClassBuildItem routesBuilderClass : routesBuilderClasses) {
+                final String className = routesBuilderClass.getDotName().toString();
+                final Class<?> cl = loader.loadClass(className);
+
+                if (RouteBuilder.class.isAssignableFrom(cl)) {
+                    try {
+                        final RouteBuilder rb = (RouteBuilder) cl.newInstance();
+                        rb.configure();

Review comment:
       I'm not very sure if this is always enough or if you need to set a camel context, as example if the route invokes [this method](https://github.com/apache/camel/blob/master/core/camel-core-model/src/main/java/org/apache/camel/builder/RouteBuilder.java#L286), then you'll get an NPE. In general the builder assumes that the camel context is set when `configure()`.




----------------------------------------------------------------
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:
users@infra.apache.org