You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by fm...@apache.org on 2023/11/27 13:35:38 UTC

(camel-spring-boot) branch main updated: Upgrade to SB 3.2.0

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

fmariani pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel-spring-boot.git


The following commit(s) were added to refs/heads/main by this push:
     new fd0e7b6d31b Upgrade to SB 3.2.0
fd0e7b6d31b is described below

commit fd0e7b6d31b1e38ba18f71805333384fb29fb807
Author: Croway <fe...@gmail.com>
AuthorDate: Mon Nov 27 09:36:06 2023 +0100

    Upgrade to SB 3.2.0
---
 .../MicrometerTagsAutoConfiguration.java           | 128 +++++++++++++--------
 pom.xml                                            |   2 +-
 tests/camel-itest-spring-boot/pom.xml              |   7 ++
 .../arquillian/ArquillianSyncBootJarLauncher.java  |  41 +++----
 .../src/test/resources/application-pom-sb3.xml     |   4 +
 5 files changed, 112 insertions(+), 70 deletions(-)

diff --git a/components-starter/camel-micrometer-starter/src/main/java/org/apache/camel/component/micrometer/springboot/MicrometerTagsAutoConfiguration.java b/components-starter/camel-micrometer-starter/src/main/java/org/apache/camel/component/micrometer/springboot/MicrometerTagsAutoConfiguration.java
index b6ca1e5b04a..c5589a91b1f 100644
--- a/components-starter/camel-micrometer-starter/src/main/java/org/apache/camel/component/micrometer/springboot/MicrometerTagsAutoConfiguration.java
+++ b/components-starter/camel-micrometer-starter/src/main/java/org/apache/camel/component/micrometer/springboot/MicrometerTagsAutoConfiguration.java
@@ -16,10 +16,9 @@
  */
 package org.apache.camel.component.micrometer.springboot;
 
-import io.micrometer.core.instrument.Tag;
-import io.micrometer.core.instrument.Tags;
+import io.micrometer.common.KeyValue;
+import io.micrometer.common.KeyValues;
 import jakarta.servlet.http.HttpServletRequest;
-import jakarta.servlet.http.HttpServletResponse;
 import org.apache.camel.CamelContext;
 import org.apache.camel.component.micrometer.springboot.metrics.CamelMetricsConfiguration;
 import org.apache.camel.http.common.CamelServlet;
@@ -27,13 +26,14 @@ import org.apache.camel.http.common.HttpConsumer;
 import org.apache.camel.spring.boot.CamelAutoConfiguration;
 import org.apache.camel.spring.boot.util.ConditionalOnCamelContextAndAutoConfigurationBeans;
 import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.boot.actuate.metrics.web.servlet.DefaultWebMvcTagsProvider;
-import org.springframework.boot.actuate.metrics.web.servlet.WebMvcTagsProvider;
 import org.springframework.boot.autoconfigure.AutoConfigureAfter;
 import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
 import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.Conditional;
 import org.springframework.context.annotation.Configuration;
+import org.springframework.http.server.observation.DefaultServerRequestObservationConvention;
+import org.springframework.http.server.observation.ServerRequestObservationContext;
+import org.springframework.http.server.observation.ServerRequestObservationConvention;
 
 import java.util.Optional;
 
@@ -43,49 +43,83 @@ import java.util.Optional;
 @AutoConfigureAfter({CamelAutoConfiguration.class})
 public class MicrometerTagsAutoConfiguration {
 
-    @Autowired
-    CamelContext camelContext;
+	/**
+	 * To integrate with micrometer to include expanded uri in tags when for example using
+	 * camel rest-dsl with servlet.
+	 */
+	@Bean
+	ServerRequestObservationConvention serverRequestObservationConvention(
+			Optional<CamelServlet> servlet, CamelMetricsConfiguration configuration) {
+		return new DefaultServerRequestObservationConvention() {
 
-    /**
-     * To integrate with micrometer to include expanded uri in tags when for example using
-     * camel rest-dsl with servlet.
-     */
-    @Bean
-    WebMvcTagsProvider webMvcTagsProvider(Optional<CamelServlet> servlet, CamelMetricsConfiguration configuration) {
-        return new DefaultWebMvcTagsProvider() {
-            @Override
-            public Iterable<Tag> getTags(HttpServletRequest request, HttpServletResponse response,
-                                         Object handler, Throwable exception) {
+			@Override
+			public KeyValues getLowCardinalityKeyValues(ServerRequestObservationContext context) {
+				// here, we just want to have an additional KeyValue to the observation, keeping the default values
+				return super.getLowCardinalityKeyValues(context).and(custom(context));
+			}
 
-                String uri = null;
-                if (servlet.isPresent() && !configuration.isUriTagDynamic()) {
-                    HttpConsumer consumer = servlet.get().getServletResolveConsumerStrategy().resolve(request, servlet.get().getConsumers());
-                    if (consumer != null) {
-                        uri = consumer.getPath();
-                    }
-                }
+			protected KeyValue custom(ServerRequestObservationContext context) {
+				HttpServletRequest request = context.getCarrier();
+				String uri = null;
+				if (servlet.isPresent() && !configuration.isUriTagDynamic()) {
+					HttpConsumer consumer = servlet.get().getServletResolveConsumerStrategy().resolve(request, servlet.get().getConsumers());
+					if (consumer != null) {
+						uri = consumer.getPath();
+					}
+				}
 
-                // the request may not be for camel servlet, so we need to capture uri from request
-                if (uri == null || uri.isEmpty()) {
-                    // dynamic uri with the actual value from the http request
-                    uri = request.getServletPath();
-                    if (uri == null || uri.isEmpty()) {
-                        uri = request.getPathInfo();
-                    } else {
-                        String p = request.getPathInfo();
-                        if (p != null) {
-                            uri = uri + p;
-                        }
-                    }
-                }
-                if (uri == null) {
-                    uri = "";
-                }
-                return Tags.concat(
-                        super.getTags(request, response, handler, exception),
-                        Tags.of(Tag.of("uri", uri))
-                );
-            }
-        };
-    }
+				// the request may not be for camel servlet, so we need to capture uri from request
+				if (uri == null || uri.isEmpty()) {
+					// dynamic uri with the actual value from the http request
+					uri = request.getServletPath();
+					if (uri == null || uri.isEmpty()) {
+						uri = request.getPathInfo();
+					} else {
+						String p = request.getPathInfo();
+						if (p != null) {
+							uri = uri + p;
+						}
+					}
+				}
+				if (uri == null) {
+					uri = "";
+				}
+
+				return KeyValue.of("uri", context.getCarrier().getMethod());
+			}
+//            @Override
+//            public Iterable<Tag> getTags(HttpServletRequest request, HttpServletResponse response,
+//                                         Object handler, Throwable exception) {
+//
+//                String uri = null;
+//                if (servlet.isPresent() && !configuration.isUriTagDynamic()) {
+//                    HttpConsumer consumer = servlet.get().getServletResolveConsumerStrategy().resolve(request, servlet.get().getConsumers());
+//                    if (consumer != null) {
+//                        uri = consumer.getPath();
+//                    }
+//                }
+//
+//                // the request may not be for camel servlet, so we need to capture uri from request
+//                if (uri == null || uri.isEmpty()) {
+//                    // dynamic uri with the actual value from the http request
+//                    uri = request.getServletPath();
+//                    if (uri == null || uri.isEmpty()) {
+//                        uri = request.getPathInfo();
+//                    } else {
+//                        String p = request.getPathInfo();
+//                        if (p != null) {
+//                            uri = uri + p;
+//                        }
+//                    }
+//                }
+//                if (uri == null) {
+//                    uri = "";
+//                }
+//                return Tags.concat(
+//                        super.getTags(request, response, handler, exception),
+//                        Tags.of(Tag.of("uri", uri))
+//                );
+//            }
+		};
+	}
 }
diff --git a/pom.xml b/pom.xml
index 9c478b9d83d..35579d53c7d 100644
--- a/pom.xml
+++ b/pom.xml
@@ -108,7 +108,7 @@
         <compiler.fork>false</compiler.fork>
 
         <!-- Spring-Boot target version -->
-        <spring-boot-version>3.1.5</spring-boot-version>
+        <spring-boot-version>3.2.0</spring-boot-version>
 
         <!-- Camel target version -->
         <camel-version>4.3.0-SNAPSHOT</camel-version>
diff --git a/tests/camel-itest-spring-boot/pom.xml b/tests/camel-itest-spring-boot/pom.xml
index ec92c54c729..3d273510027 100644
--- a/tests/camel-itest-spring-boot/pom.xml
+++ b/tests/camel-itest-spring-boot/pom.xml
@@ -182,6 +182,13 @@
             <scope>test</scope>
         </dependency>
 
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-actuator</artifactId>
+            <version>${spring-boot-version}</version>
+            <scope>test</scope>
+        </dependency>
+
     </dependencies>
 
     <build>
diff --git a/tests/camel-itest-spring-boot/src/test/java/org/apache/camel/itest/springboot/arquillian/ArquillianSyncBootJarLauncher.java b/tests/camel-itest-spring-boot/src/test/java/org/apache/camel/itest/springboot/arquillian/ArquillianSyncBootJarLauncher.java
index ce3f7447511..96f292337e9 100644
--- a/tests/camel-itest-spring-boot/src/test/java/org/apache/camel/itest/springboot/arquillian/ArquillianSyncBootJarLauncher.java
+++ b/tests/camel-itest-spring-boot/src/test/java/org/apache/camel/itest/springboot/arquillian/ArquillianSyncBootJarLauncher.java
@@ -18,18 +18,17 @@ package org.apache.camel.itest.springboot.arquillian;
 
 import java.io.File;
 import java.lang.management.ManagementFactory;
+import java.lang.reflect.Method;
 import java.net.MalformedURLException;
 import java.net.URL;
 import java.net.URLClassLoader;
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.List;
+import java.util.*;
 import java.util.stream.Collectors;
 import java.util.stream.Stream;
 
-import org.springframework.boot.loader.JarLauncher;
-import org.springframework.boot.loader.LaunchedURLClassLoader;
-import org.springframework.boot.loader.MainMethodRunner;
+import org.springframework.boot.loader.launch.JarLauncher;
+import org.springframework.boot.loader.launch.LaunchedClassLoader;
+
 /**
  * A Spring-boot jar launcher that uses the current thread instead of creating a new thread for spring-boot.
  */
@@ -37,7 +36,8 @@ public class ArquillianSyncBootJarLauncher extends JarLauncher {
 
     private ClassLoader classLoader;
 
-    public ArquillianSyncBootJarLauncher() {
+    public ArquillianSyncBootJarLauncher() throws Exception {
+        super();
     }
 
     public void run(String[] args) throws Exception {
@@ -45,32 +45,29 @@ public class ArquillianSyncBootJarLauncher extends JarLauncher {
     }
 
     @Override
-    protected void launch(String[] args, String mainClass, ClassLoader classLoader) throws Exception {
+    protected void launch(ClassLoader classLoader, String mainClassName, String[] args) throws Exception {
         this.classLoader = classLoader;
 
-        MainMethodRunner runner = createMainMethodRunner(mainClass, args, classLoader);
-
         Thread.currentThread().setContextClassLoader(classLoader);
-        runner.run();
+        Class<?> mainClass = Class.forName(mainClassName, false, classLoader);
+        Method mainMethod = mainClass.getDeclaredMethod("main", String[].class);
+        mainMethod.setAccessible(true);
+        mainMethod.invoke(null, new Object[] { args });
     }
 
     @Override
-    protected ClassLoader createClassLoader(URL[] urls) throws Exception {
+    protected ClassLoader createClassLoader(Collection<URL> urls) throws Exception {
         // The spring classloader should not be built on top of the current classloader, it should just share the test classes if available
         List<URL> parentUrls = Arrays.asList(urlsFromClassLoader(this.getClassLoader()));
         List<URL> additionalURLs = parentUrls.stream().filter(u -> u.toString().startsWith("file") && !u.toString().endsWith(".jar")).collect(Collectors.toList());
 
-        ArrayList<URL> newURLs = new ArrayList(Arrays.asList(urls));
+        ArrayList<URL> newURLs = new ArrayList<>();
+        newURLs.addAll(urls);
         newURLs.addAll(additionalURLs);
 
-        ClassLoader appClassLoader = null;
-        // Until https://github.com/spring-projects/spring-boot/issues/12832 is resolved
-        if (getJavaMajorVersion() >= 9) {
-            // use the Platform classloader to resolve classes on the module path
-            appClassLoader = getClass().getClassLoader().getParent();
-        }
-        LaunchedURLClassLoader launchedURLClassLoader = new LaunchedURLClassLoader(newURLs.toArray(new URL[0]), appClassLoader);
-        return launchedURLClassLoader;
+        ClassLoader appClassLoader = getClass().getClassLoader().getParent();
+
+        return new LaunchedClassLoader(isExploded(), getArchive(), newURLs.toArray(new URL[0]), appClassLoader);
     }
 
     /**
@@ -109,4 +106,4 @@ public class ArquillianSyncBootJarLauncher extends JarLauncher {
                     "URL could not be created from '" + classPathEntry + "'", ex);
         }
     }
-}
\ No newline at end of file
+}
diff --git a/tests/camel-itest-spring-boot/src/test/resources/application-pom-sb3.xml b/tests/camel-itest-spring-boot/src/test/resources/application-pom-sb3.xml
index d10e98b54ac..fac447f4969 100644
--- a/tests/camel-itest-spring-boot/src/test/resources/application-pom-sb3.xml
+++ b/tests/camel-itest-spring-boot/src/test/resources/application-pom-sb3.xml
@@ -57,6 +57,10 @@
             <groupId>org.apache.camel.springboot</groupId>
             <artifactId>camel-spring-boot-starter</artifactId>
         </dependency>
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-actuator</artifactId>
+        </dependency>
 
         <dependency>
             <groupId>org.apache.camel.springboot</groupId>