You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tomee.apache.org by rm...@apache.org on 2014/10/22 09:28:05 UTC
git commit: TOMEE-1424 basic comparator logic for jaxrs providers
Repository: tomee
Updated Branches:
refs/heads/tomee-1.7.x 991213375 -> cc331bb80
TOMEE-1424 basic comparator logic for jaxrs providers
Project: http://git-wip-us.apache.org/repos/asf/tomee/repo
Commit: http://git-wip-us.apache.org/repos/asf/tomee/commit/cc331bb8
Tree: http://git-wip-us.apache.org/repos/asf/tomee/tree/cc331bb8
Diff: http://git-wip-us.apache.org/repos/asf/tomee/diff/cc331bb8
Branch: refs/heads/tomee-1.7.x
Commit: cc331bb80856e060692fa316710597a9db0f46d3
Parents: 9912133
Author: Romain Manni-Bucau <rm...@apache.org>
Authored: Wed Oct 22 09:27:07 2014 +0200
Committer: Romain Manni-Bucau <rm...@apache.org>
Committed: Wed Oct 22 09:27:07 2014 +0200
----------------------------------------------------------------------
.../server/cxf/rs/CxfRsHttpListener.java | 128 ++++++++++++++++++-
1 file changed, 122 insertions(+), 6 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/tomee/blob/cc331bb8/server/openejb-cxf-rs/src/main/java/org/apache/openejb/server/cxf/rs/CxfRsHttpListener.java
----------------------------------------------------------------------
diff --git a/server/openejb-cxf-rs/src/main/java/org/apache/openejb/server/cxf/rs/CxfRsHttpListener.java b/server/openejb-cxf-rs/src/main/java/org/apache/openejb/server/cxf/rs/CxfRsHttpListener.java
index 08cefe4..dce2c01 100644
--- a/server/openejb-cxf-rs/src/main/java/org/apache/openejb/server/cxf/rs/CxfRsHttpListener.java
+++ b/server/openejb-cxf-rs/src/main/java/org/apache/openejb/server/cxf/rs/CxfRsHttpListener.java
@@ -78,6 +78,7 @@ import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.core.Application;
import javax.xml.bind.Marshaller;
+import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.lang.annotation.Annotation;
@@ -88,6 +89,7 @@ import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
+import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashSet;
@@ -98,6 +100,8 @@ import java.util.concurrent.CopyOnWriteArrayList;
import java.util.logging.Level;
import java.util.regex.Pattern;
+import static org.apache.openejb.loader.JarLocation.jarLocation;
+
public class CxfRsHttpListener implements RsHttpListener {
private static final Logger LOGGER = Logger.getInstance(LogCategory.OPENEJB_RS, CxfRsHttpListener.class);
@@ -355,8 +359,8 @@ public class CxfRsHttpListener implements RsHttpListener {
}
}
- private Collection<Object> providers(final Collection<ServiceInfo> services, final Collection<Object> additionalProviders, final WebBeansContext ctx) {
- final Collection<Object> instances = new ArrayList<Object>();
+ private List<Object> providers(final Collection<ServiceInfo> services, final Collection<Object> additionalProviders, final WebBeansContext ctx) {
+ final List<Object> instances = new ArrayList<Object>();
final BeanManagerImpl bm = ctx == null ? null : ctx.getBeanManagerImpl();
for (final Object o : additionalProviders) {
if (o instanceof Class<?>) {
@@ -687,7 +691,7 @@ public class CxfRsHttpListener implements RsHttpListener {
{
final String provider = serviceConfiguration.getProperties().getProperty(PROVIDERS_KEY);
- if (provider != null) {
+ if (provider != null) { // already ordered and high priority since they were configured manually
providersConfig = new HashSet<String>();
for (final String p : Arrays.asList(provider.split(","))) {
providersConfig.add(p.trim());
@@ -695,7 +699,7 @@ public class CxfRsHttpListener implements RsHttpListener {
}
{
- if (GLOBAL_PROVIDERS != null) {
+ if (GLOBAL_PROVIDERS != null) { // same idea, Note: this doesn't affect much cases which are not embedded so don't spend time on it
if (providersConfig == null) {
providersConfig = new HashSet<String>();
}
@@ -712,13 +716,13 @@ public class CxfRsHttpListener implements RsHttpListener {
if (providersConfig != null) {
providers = ServiceInfos.resolve(services, providersConfig.toArray(new String[providersConfig.size()]), ProviderFactory.INSTANCE);
if (providers != null && additionalProviders != null && !additionalProviders.isEmpty()) {
- providers.addAll(providers(services, additionalProviders, ctx));
+ providers.addAll(sortProviders(serviceConfiguration, ctx, additionalProviders));
}
}
if (providers == null) {
providers = new ArrayList<Object>(4);
if (additionalProviders != null && !additionalProviders.isEmpty()) {
- providers.addAll(providers(services, additionalProviders, ctx));
+ providers.addAll(sortProviders(serviceConfiguration, ctx, additionalProviders));
} else {
providers.addAll(defaultProviders());
}
@@ -735,6 +739,51 @@ public class CxfRsHttpListener implements RsHttpListener {
factory.setProviders(providers);
}
+ private List<Object> sortProviders(final ServiceConfiguration serviceConfiguration, final WebBeansContext ctx,
+ final Collection<Object> additionalProviders) {
+ final Collection<ServiceInfo> services = serviceConfiguration.getAvailableServices();
+ final List<Object> loadedProviders = providers(services, additionalProviders, ctx);
+ if ("true".equalsIgnoreCase(SystemInstance.get().getProperty("openejb.cxf.rs.skip-provider-sorting", "false"))) {
+ return loadedProviders;
+ }
+
+ final String comparatorClass = serviceConfiguration.getProperties().getProperty(CXF_JAXRS_PREFIX + "provider-comparator");
+
+ Comparator<Object> comparator = null;
+ if (comparatorClass == null) {
+ comparator = DefaultProviderComparator.INSTANCE;
+ } else {
+ final BeanManagerImpl bm = ctx.getBeanManagerImpl();
+ if (bm != null && bm.isInUse()) {
+ try {
+ final Class<?> clazz = Thread.currentThread().getContextClassLoader().loadClass(comparatorClass);
+ final Set<Bean<?>> beans = bm.getBeans(clazz);
+ if (beans != null && !beans.isEmpty()) {
+ final Bean<?> bean = bm.resolve(beans);
+ final CreationalContextImpl<?> creationalContext = bm.createCreationalContext(bean);
+ comparator = Comparator.class.cast(bm.getReference(bean, clazz, creationalContext));
+ toRelease.add(creationalContext);
+ }
+ } catch (final Throwable th) {
+ LOGGER.debug("Can't use CDI to load comparator " + comparatorClass);
+ }
+ }
+
+ if (comparator == null) {
+ comparator = Comparator.class.cast(ServiceInfos.resolve(services, comparatorClass));
+ }
+ if (comparator == null) {
+ try {
+ comparator = Comparator.class.cast(Thread.currentThread().getContextClassLoader().loadClass(comparatorClass).newInstance());
+ } catch (final Exception e) {
+ throw new IllegalArgumentException(e);
+ }
+ }
+ }
+ Collections.sort(loadedProviders, comparator);
+ return loadedProviders;
+ }
+
private static List<Object> defaultProviders() {
final JAXBElementProvider jaxb = new JAXBElementProvider();
final Map<String, Object> jaxbProperties = new HashMap<String, Object>();
@@ -747,6 +796,73 @@ public class CxfRsHttpListener implements RsHttpListener {
return providers;
}
+ // we use Object cause an app with a custom comparator can desire to compare instances
+ private static final class DefaultProviderComparator implements Comparator<Object> {
+ private static final DefaultProviderComparator INSTANCE = new DefaultProviderComparator();
+ private static final ClassLoader SYSTEM_LOADER = ClassLoader.getSystemClassLoader();
+ private static final ClassLoader OPENEJB_LOADER = DefaultProviderComparator.class.getClassLoader();
+
+ @Override
+ public int compare(final Object o1, final Object o2) {
+ if (o1 == o2 || (o1 != null && o1.equals(o2))) {
+ return 0;
+ }
+ if (o1 == null) {
+ return -1;
+ }
+ if (o2 == null) {
+ return 1;
+ }
+
+ final Class<?> c1 = o1.getClass();
+ final Class<?> c2 = o2.getClass();
+
+ final ClassLoader classLoader1 = c1.getClassLoader();
+ final ClassLoader classLoader2 = c2.getClassLoader();
+
+ final boolean loadersNotNull = classLoader1 != null && classLoader2 != null;
+
+ if (classLoader1 != classLoader2
+ && loadersNotNull
+ && !classLoader1.equals(classLoader2) && !classLoader2.equals(classLoader1)) {
+ if (isParent(classLoader1, classLoader2)) {
+ return 1;
+ }
+ if (isParent(classLoader2, classLoader1)) {
+ return -1;
+ }
+ } else {
+ final File l1 = jarLocation(c1);
+ final File l2 = jarLocation(c2);
+ if (l1 == null) {
+ return 1;
+ }
+ if (l2 == null) {
+ return -1;
+ }
+
+ try { // WEB-INF/classes will be before WEB-INF/lib automatically
+ return l1.getCanonicalPath().compareTo(l2.getCanonicalPath());
+ } catch (final IOException e) {
+ // no-op: sort by class name
+ }
+ }
+
+ return c1.getName().compareTo(c2.getName());
+ }
+
+ private static boolean isParent(final ClassLoader l1, ClassLoader l2) {
+ ClassLoader current = l2;
+ while (current != null && current != SYSTEM_LOADER) {
+ if (current.equals(l1) || l1.equals(current)) {
+ return true;
+ }
+ current = current.getParent();
+ }
+ return false;
+ }
+ }
+
private static class ProviderFactory implements ServiceInfos.Factory {
private static final ServiceInfos.Factory INSTANCE = new ProviderFactory();