You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cxf.apache.org by re...@apache.org on 2022/08/14 16:22:57 UTC

[cxf] branch main updated: CXF-8732: [regression] activation DataSource requires to run JAXRS

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

reta pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/cxf.git


The following commit(s) were added to refs/heads/main by this push:
     new c05666fc41 CXF-8732: [regression] activation DataSource requires to run JAXRS
c05666fc41 is described below

commit c05666fc41161450258a48fbba6298f56d857a63
Author: Andriy Redko <dr...@gmail.com>
AuthorDate: Sun Aug 14 12:22:33 2022 -0400

    CXF-8732: [regression] activation DataSource requires to run JAXRS
---
 .../org/apache/cxf/jaxrs/utils/JAXRSUtils.java     | 39 ++++++++++++++++++++--
 .../apache/cxf/jaxrs/impl/UriBuilderImplTest.java  |  1 -
 2 files changed, 37 insertions(+), 3 deletions(-)

diff --git a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/JAXRSUtils.java b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/JAXRSUtils.java
index dfaad96439..f6abf4d695 100644
--- a/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/JAXRSUtils.java
+++ b/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/utils/JAXRSUtils.java
@@ -39,6 +39,7 @@ import java.util.LinkedHashMap;
 import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
+import java.util.Optional;
 import java.util.ResourceBundle;
 import java.util.Set;
 import java.util.SortedMap;
@@ -51,7 +52,6 @@ import java.util.stream.Collectors;
 import javax.xml.namespace.QName;
 import javax.xml.transform.Source;
 
-import jakarta.activation.DataSource;
 import jakarta.ws.rs.ClientErrorException;
 import jakarta.ws.rs.Consumes;
 import jakarta.ws.rs.HttpMethod;
@@ -85,6 +85,7 @@ import jakarta.ws.rs.ext.ReaderInterceptor;
 import jakarta.ws.rs.ext.ReaderInterceptorContext;
 import jakarta.ws.rs.ext.WriterInterceptor;
 import jakarta.ws.rs.ext.WriterInterceptorContext;
+import org.apache.cxf.common.classloader.ClassLoaderUtils;
 import org.apache.cxf.common.i18n.BundleUtils;
 import org.apache.cxf.common.logging.LogUtils;
 import org.apache.cxf.common.util.PackageUtils;
@@ -202,6 +203,40 @@ public final class JAXRSUtils {
             "java.util.concurrent.CompletableFuture",
             "java.util.concurrent.CompletionStage"
         ));
+    private static final LazyLoadedClass DATA_SOURCE_CLASS = new LazyLoadedClass("javax.activation.DataSource");
+
+    // Class to lazily call the ClassLoaderUtil.loadClass, but do it once
+    // and cache the result.  Then use the class to create instances as needed.
+    // This avoids calling loadClass every time as calling loadClass is super expensive, 
+    // particularly if the class cannot be found and particularly in OSGi where the 
+    // search is very complex. This would record that the class is not found and prevent 
+    // future searches.
+    private static class LazyLoadedClass {
+        private final String className;
+        private volatile boolean initialized;
+        private Class<?> cls;
+
+        LazyLoadedClass(String cn) {
+            className = cn;
+        }
+
+        synchronized Optional<Class<?>> load() {
+            if (!initialized) {
+                try {
+                    cls = ClassLoaderUtils.loadClass(className, ProviderFactory.class);
+                } catch (final Throwable ex) {
+                    LOG.fine(className + " not available, skipping");
+                }
+                initialized = true;
+            }
+
+            return Optional.ofNullable(cls);
+        }
+
+        boolean isAssignableFrom(Class<?> cls) {
+            return load().map(c -> c.isAssignableFrom(cls)).orElse(false);
+        }
+    }
 
     private JAXRSUtils() {
     }
@@ -242,7 +277,7 @@ public final class JAXRSUtils {
         return STREAMING_OUT_TYPES.contains(type) 
             || Closeable.class.isAssignableFrom(type)
             || Source.class.isAssignableFrom(type)
-            || DataSource.class.isAssignableFrom(type);
+            || DATA_SOURCE_CLASS.isAssignableFrom(type);
     }
 
     public static List<PathSegment> getPathSegments(String thePath, boolean decode) {
diff --git a/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/UriBuilderImplTest.java b/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/UriBuilderImplTest.java
index 7a2962f7af..63dfe41ff6 100644
--- a/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/UriBuilderImplTest.java
+++ b/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/impl/UriBuilderImplTest.java
@@ -33,7 +33,6 @@ import jakarta.ws.rs.core.MultivaluedMap;
 import jakarta.ws.rs.core.Response;
 import jakarta.ws.rs.core.UriBuilder;
 import jakarta.ws.rs.core.UriBuilderException;
-
 import org.apache.cxf.jaxrs.resources.Book;
 import org.apache.cxf.jaxrs.resources.BookStore;
 import org.apache.cxf.jaxrs.resources.UriBuilderWrongAnnotations;