You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2022/03/12 06:18:39 UTC

[camel] 04/11: CAMEL-17571: camel-dsl - Allow to register custom annotation processors that can do custom logic after a DSL has compiled source into Java object.

This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch CAMEL-17571
in repository https://gitbox.apache.org/repos/asf/camel.git

commit 8ba2dd851a553108d7acb8f898936fee9068a37a
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Fri Mar 11 11:11:54 2022 +0100

    CAMEL-17571: camel-dsl - Allow to register custom annotation processors that can do custom logic after a DSL has compiled source into Java object.
---
 .../BindToRegistryCompilePostProcessor.java        | 56 ++++++++++++++++++
 .../dsl/support/RouteBuilderLoaderSupport.java     |  7 ++-
 .../support/TypeConverterCompilePostProcessor.java | 45 +++++++++++++++
 .../dsl/java/joor/JavaRoutesBuilderLoader.java     | 66 ----------------------
 4 files changed, 106 insertions(+), 68 deletions(-)

diff --git a/dsl/camel-dsl-support/src/main/java/org/apache/camel/dsl/support/BindToRegistryCompilePostProcessor.java b/dsl/camel-dsl-support/src/main/java/org/apache/camel/dsl/support/BindToRegistryCompilePostProcessor.java
new file mode 100644
index 0000000..cd1271a
--- /dev/null
+++ b/dsl/camel-dsl-support/src/main/java/org/apache/camel/dsl/support/BindToRegistryCompilePostProcessor.java
@@ -0,0 +1,56 @@
+/*
+ * 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.support;
+
+import org.apache.camel.BindToRegistry;
+import org.apache.camel.CamelConfiguration;
+import org.apache.camel.CamelContext;
+import org.apache.camel.Configuration;
+import org.apache.camel.ExtendedCamelContext;
+import org.apache.camel.spi.CamelBeanPostProcessor;
+import org.apache.camel.util.ObjectHelper;
+
+public class BindToRegistryCompilePostProcessor implements CompilePostProcessor {
+
+    @Override
+    public void postCompile(CamelContext camelContext, String name, Class<?> clazz, Object instance) throws Exception {
+        BindToRegistry bir = instance.getClass().getAnnotation(BindToRegistry.class);
+        Configuration cfg = instance.getClass().getAnnotation(Configuration.class);
+        if (bir != null || cfg != null || instance instanceof CamelConfiguration) {
+            CamelBeanPostProcessor bpp = camelContext.adapt(ExtendedCamelContext.class).getBeanPostProcessor();
+            if (bir != null && ObjectHelper.isNotEmpty(bir.value())) {
+                name = bir.value();
+            } else if (cfg != null && ObjectHelper.isNotEmpty(cfg.value())) {
+                name = cfg.value();
+            }
+            // to support hot reloading of beans then we need to enable unbind mode in bean post processor
+            bpp.setUnbindEnabled(true);
+            try {
+                // this class is a bean service which needs to be post processed and registered which happens
+                // automatic by the bean post processor
+                bpp.postProcessBeforeInitialization(instance, name);
+                bpp.postProcessAfterInitialization(instance, name);
+            } finally {
+                bpp.setUnbindEnabled(false);
+            }
+            if (instance instanceof CamelConfiguration) {
+                ((CamelConfiguration) instance).configure(camelContext);
+            }
+        }
+    }
+
+}
diff --git a/dsl/camel-dsl-support/src/main/java/org/apache/camel/dsl/support/RouteBuilderLoaderSupport.java b/dsl/camel-dsl-support/src/main/java/org/apache/camel/dsl/support/RouteBuilderLoaderSupport.java
index 0b3dae8..eb0024b 100644
--- a/dsl/camel-dsl-support/src/main/java/org/apache/camel/dsl/support/RouteBuilderLoaderSupport.java
+++ b/dsl/camel-dsl-support/src/main/java/org/apache/camel/dsl/support/RouteBuilderLoaderSupport.java
@@ -37,12 +37,15 @@ import org.apache.camel.support.RoutesBuilderLoaderSupport;
  */
 public abstract class RouteBuilderLoaderSupport extends RoutesBuilderLoaderSupport {
     private final String extension;
-
-    private StartupStepRecorder recorder;
     private final List<CompilePostProcessor> compilePostProcessors = new ArrayList<>();
+    private StartupStepRecorder recorder;
 
     protected RouteBuilderLoaderSupport(String extension) {
         this.extension = extension;
+
+        // out of the box camel based compile post processors
+        addCompilePostProcessor(new TypeConverterCompilePostProcessor());
+        addCompilePostProcessor(new BindToRegistryCompilePostProcessor());
     }
 
     @ManagedAttribute(description = "Supported file extension")
diff --git a/dsl/camel-dsl-support/src/main/java/org/apache/camel/dsl/support/TypeConverterCompilePostProcessor.java b/dsl/camel-dsl-support/src/main/java/org/apache/camel/dsl/support/TypeConverterCompilePostProcessor.java
new file mode 100644
index 0000000..ba3de3b
--- /dev/null
+++ b/dsl/camel-dsl-support/src/main/java/org/apache/camel/dsl/support/TypeConverterCompilePostProcessor.java
@@ -0,0 +1,45 @@
+/*
+ * 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.support;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.Converter;
+import org.apache.camel.LoggingLevel;
+import org.apache.camel.TypeConverterExists;
+import org.apache.camel.spi.TypeConverterRegistry;
+
+public class TypeConverterCompilePostProcessor implements CompilePostProcessor {
+
+    @Override
+    public void postCompile(CamelContext camelContext, String name, Class<?> clazz, Object instance) throws Exception {
+        if (clazz.getAnnotation(Converter.class) != null) {
+            TypeConverterRegistry tcr = camelContext.getTypeConverterRegistry();
+            TypeConverterExists exists = tcr.getTypeConverterExists();
+            LoggingLevel level = tcr.getTypeConverterExistsLoggingLevel();
+            // force type converter to override as we could be re-loading
+            tcr.setTypeConverterExists(TypeConverterExists.Override);
+            tcr.setTypeConverterExistsLoggingLevel(LoggingLevel.OFF);
+            try {
+                tcr.addTypeConverters(clazz);
+            } finally {
+                tcr.setTypeConverterExists(exists);
+                tcr.setTypeConverterExistsLoggingLevel(level);
+            }
+        }
+    }
+
+}
diff --git a/dsl/camel-java-joor-dsl/src/main/java/org/apache/camel/dsl/java/joor/JavaRoutesBuilderLoader.java b/dsl/camel-java-joor-dsl/src/main/java/org/apache/camel/dsl/java/joor/JavaRoutesBuilderLoader.java
index a407559..b5d2815 100644
--- a/dsl/camel-java-joor-dsl/src/main/java/org/apache/camel/dsl/java/joor/JavaRoutesBuilderLoader.java
+++ b/dsl/camel-java-joor-dsl/src/main/java/org/apache/camel/dsl/java/joor/JavaRoutesBuilderLoader.java
@@ -21,26 +21,15 @@ import java.io.InputStream;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
-import org.apache.camel.BindToRegistry;
-import org.apache.camel.CamelConfiguration;
-import org.apache.camel.CamelContext;
-import org.apache.camel.Configuration;
-import org.apache.camel.Converter;
-import org.apache.camel.ExtendedCamelContext;
-import org.apache.camel.LoggingLevel;
-import org.apache.camel.TypeConverterExists;
 import org.apache.camel.api.management.ManagedResource;
 import org.apache.camel.builder.RouteBuilder;
 import org.apache.camel.dsl.support.CompilePostProcessor;
 import org.apache.camel.dsl.support.RouteBuilderLoaderSupport;
-import org.apache.camel.spi.CamelBeanPostProcessor;
 import org.apache.camel.spi.Resource;
-import org.apache.camel.spi.TypeConverterRegistry;
 import org.apache.camel.spi.annotations.RoutesLoader;
 import org.apache.camel.support.ResourceHelper;
 import org.apache.camel.util.FileUtil;
 import org.apache.camel.util.IOHelper;
-import org.apache.camel.util.ObjectHelper;
 import org.joor.Reflect;
 
 @ManagedResource(description = "Managed JavaRoutesBuilderLoader")
@@ -52,9 +41,6 @@ public class JavaRoutesBuilderLoader extends RouteBuilderLoaderSupport {
 
     public JavaRoutesBuilderLoader() {
         super(EXTENSION);
-
-        addCompilePostProcessor(new ConverterCompilePostProcessor());
-        addCompilePostProcessor(new BindToRegistryCompilePostProcessor());
     }
 
     @Override
@@ -98,56 +84,4 @@ public class JavaRoutesBuilderLoader extends RouteBuilderLoaderSupport {
                 : name;
     }
 
-    private static class ConverterCompilePostProcessor implements CompilePostProcessor {
-
-        @Override
-        public void postCompile(CamelContext camelContext, String name, Class<?> clazz, Object instance) throws Exception {
-            if (clazz.getAnnotation(Converter.class) != null) {
-                TypeConverterRegistry tcr = camelContext.getTypeConverterRegistry();
-                TypeConverterExists exists = tcr.getTypeConverterExists();
-                LoggingLevel level = tcr.getTypeConverterExistsLoggingLevel();
-                // force type converter to override as we could be re-loading
-                tcr.setTypeConverterExists(TypeConverterExists.Override);
-                tcr.setTypeConverterExistsLoggingLevel(LoggingLevel.OFF);
-                try {
-                    tcr.addTypeConverters(clazz);
-                } finally {
-                    tcr.setTypeConverterExists(exists);
-                    tcr.setTypeConverterExistsLoggingLevel(level);
-                }
-            }
-        }
-    }
-
-    private static class BindToRegistryCompilePostProcessor implements CompilePostProcessor {
-
-        @Override
-        public void postCompile(CamelContext camelContext, String name, Class<?> clazz, Object instance) throws Exception {
-            BindToRegistry bir = instance.getClass().getAnnotation(BindToRegistry.class);
-            Configuration cfg = instance.getClass().getAnnotation(Configuration.class);
-            if (bir != null || cfg != null || instance instanceof CamelConfiguration) {
-                CamelBeanPostProcessor bpp = camelContext.adapt(ExtendedCamelContext.class).getBeanPostProcessor();
-                if (bir != null && ObjectHelper.isNotEmpty(bir.value())) {
-                    name = bir.value();
-                } else if (cfg != null && ObjectHelper.isNotEmpty(cfg.value())) {
-                    name = cfg.value();
-                }
-                // to support hot reloading of beans then we need to enable unbind mode in bean post processor
-                bpp.setUnbindEnabled(true);
-                try {
-                    // this class is a bean service which needs to be post processed and registered which happens
-                    // automatic by the bean post processor
-                    bpp.postProcessBeforeInitialization(instance, name);
-                    bpp.postProcessAfterInitialization(instance, name);
-                } finally {
-                    bpp.setUnbindEnabled(false);
-                }
-                if (instance instanceof CamelConfiguration) {
-                    ((CamelConfiguration) instance).configure(camelContext);
-                }
-            }
-        }
-
-    }
-
 }