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/14 06:11:06 UTC

[camel] 03/04: 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 a9276e9fef71fc8d1933115088a4e829663fe4d7
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Mon Mar 14 07:02:34 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        | 58 -------------------
 .../support/TypeConverterCompilePostProcessor.java | 47 ---------------
 .../apache/camel/main/CamelAnnotationSupport.java  | 66 +++++++++++++++++++++-
 3 files changed, 64 insertions(+), 107 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
deleted file mode 100644
index 041f45d..0000000
--- a/dsl/camel-dsl-support/src/main/java/org/apache/camel/dsl/support/BindToRegistryCompilePostProcessor.java
+++ /dev/null
@@ -1,58 +0,0 @@
-/*
- * 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 {
-
-    // TODO: move to camel-kamelet-main
-
-    @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/TypeConverterCompilePostProcessor.java b/dsl/camel-dsl-support/src/main/java/org/apache/camel/dsl/support/TypeConverterCompilePostProcessor.java
deleted file mode 100644
index 19028ee..0000000
--- a/dsl/camel-dsl-support/src/main/java/org/apache/camel/dsl/support/TypeConverterCompilePostProcessor.java
+++ /dev/null
@@ -1,47 +0,0 @@
-/*
- * 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 {
-
-    // TODO: move to camel-kamelet-main
-
-    @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-kamelet-main/src/main/java/org/apache/camel/main/CamelAnnotationSupport.java b/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/CamelAnnotationSupport.java
index b7c58cc..e5fb6ab 100644
--- a/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/CamelAnnotationSupport.java
+++ b/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/CamelAnnotationSupport.java
@@ -1,8 +1,17 @@
 package org.apache.camel.main;
 
+import org.apache.camel.BindToRegistry;
+import org.apache.camel.CamelConfiguration;
 import org.apache.camel.CamelContext;
-import org.apache.camel.dsl.support.BindToRegistryCompilePostProcessor;
-import org.apache.camel.dsl.support.TypeConverterCompilePostProcessor;
+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.dsl.support.CompilePostProcessor;
+import org.apache.camel.spi.CamelBeanPostProcessor;
+import org.apache.camel.spi.TypeConverterRegistry;
+import org.apache.camel.util.ObjectHelper;
 
 public class CamelAnnotationSupport {
 
@@ -11,4 +20,57 @@ public class CamelAnnotationSupport {
         context.getRegistry().bind("CamelBindToRegistryCompilePostProcessor", new BindToRegistryCompilePostProcessor());
     }
 
+    private static 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);
+                }
+            }
+        }
+
+    }
+
+    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);
+                }
+            }
+        }
+
+    }
+
 }