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:10 UTC
[1/2] git commit: TOMEE-1424 basic comparator logic for jaxrs
providers
Repository: tomee
Updated Branches:
refs/heads/develop 7e820eef5 -> 8968a1ce5
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/d173b0ab
Tree: http://git-wip-us.apache.org/repos/asf/tomee/tree/d173b0ab
Diff: http://git-wip-us.apache.org/repos/asf/tomee/diff/d173b0ab
Branch: refs/heads/develop
Commit: d173b0ab65549fa435b777864bbc7ffa1a20b10e
Parents: 7e820ee
Author: Romain Manni-Bucau <rm...@apache.org>
Authored: Wed Oct 22 09:27:32 2014 +0200
Committer: Romain Manni-Bucau <rm...@apache.org>
Committed: Wed Oct 22 09:27:32 2014 +0200
----------------------------------------------------------------------
.../server/cxf/rs/CxfRsHttpListener.java | 126 ++++++++++++++++++-
1 file changed, 122 insertions(+), 4 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/tomee/blob/d173b0ab/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 19630df..146490e 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
@@ -84,6 +84,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;
@@ -94,6 +95,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;
@@ -104,6 +106,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);
@@ -345,8 +349,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<>();
+ private List<Object> providers(final Collection<ServiceInfo> services, final Collection<Object> additionalProviders, final WebBeansContext ctx) {
+ final List<Object> instances = new ArrayList<>();
final BeanManagerImpl bm = ctx == null ? null : ctx.getBeanManagerImpl();
for (final Object o : additionalProviders) {
if (o instanceof Class<?>) {
@@ -715,13 +719,13 @@ public class CxfRsHttpListener implements RsHttpListener {
if (providersConfig != null) {
providers = ServiceInfos.resolve(services, providersConfig.toArray(new String[providersConfig.size()]), OpenEJBProviderFactory.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<>(4);
if (additionalProviders != null && !additionalProviders.isEmpty()) {
- providers.addAll(providers(services, additionalProviders, ctx));
+ providers.addAll(sortProviders(serviceConfiguration, ctx, additionalProviders));
} else {
providers.addAll(defaultProviders());
}
@@ -738,6 +742,53 @@ 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 comparatorKey = CXF_JAXRS_PREFIX + "provider-comparator";
+ final String comparatorClass = serviceConfiguration.getProperties()
+ .getProperty(comparatorKey, SystemInstance.get().getProperty(comparatorKey));
+
+ 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<>();
@@ -750,6 +801,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 OpenEJBProviderFactory implements ServiceInfos.Factory {
private static final ServiceInfos.Factory INSTANCE = new OpenEJBProviderFactory();
[2/2] git commit: TOMEE-1424 basic comparator logic for jaxrs
providers - test
Posted by rm...@apache.org.
TOMEE-1424 basic comparator logic for jaxrs providers - test
Project: http://git-wip-us.apache.org/repos/asf/tomee/repo
Commit: http://git-wip-us.apache.org/repos/asf/tomee/commit/8968a1ce
Tree: http://git-wip-us.apache.org/repos/asf/tomee/tree/8968a1ce
Diff: http://git-wip-us.apache.org/repos/asf/tomee/diff/8968a1ce
Branch: refs/heads/develop
Commit: 8968a1ce5617121b07c8991f2a55e44e3c0da691
Parents: d173b0a
Author: Romain Manni-Bucau <rm...@apache.org>
Authored: Wed Oct 22 09:27:41 2014 +0200
Committer: Romain Manni-Bucau <rm...@apache.org>
Committed: Wed Oct 22 09:27:41 2014 +0200
----------------------------------------------------------------------
.../openejb/server/cxf/rs/SortProviderTest.java | 149 +++++++++++++++++++
1 file changed, 149 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/tomee/blob/8968a1ce/server/openejb-cxf-rs/src/test/java/org/apache/openejb/server/cxf/rs/SortProviderTest.java
----------------------------------------------------------------------
diff --git a/server/openejb-cxf-rs/src/test/java/org/apache/openejb/server/cxf/rs/SortProviderTest.java b/server/openejb-cxf-rs/src/test/java/org/apache/openejb/server/cxf/rs/SortProviderTest.java
new file mode 100644
index 0000000..74e5f38
--- /dev/null
+++ b/server/openejb-cxf-rs/src/test/java/org/apache/openejb/server/cxf/rs/SortProviderTest.java
@@ -0,0 +1,149 @@
+/*
+ * 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.openejb.server.cxf.rs;
+
+import org.apache.openejb.OpenEjbContainer;
+import org.apache.openejb.jee.WebApp;
+import org.apache.openejb.junit.ApplicationComposerRule;
+import org.apache.openejb.loader.IO;
+import org.apache.openejb.testing.Classes;
+import org.apache.openejb.testing.Configuration;
+import org.apache.openejb.testing.Module;
+import org.apache.openejb.testng.PropertiesBuilder;
+import org.apache.openejb.util.NetworkUtil;
+import org.junit.Rule;
+import org.junit.Test;
+
+import javax.ws.rs.GET;
+import javax.ws.rs.Path;
+import javax.ws.rs.Produces;
+import javax.ws.rs.core.Context;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.MultivaluedMap;
+import javax.ws.rs.ext.MessageBodyWriter;
+import javax.ws.rs.ext.Provider;
+import javax.ws.rs.ext.Providers;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.lang.annotation.Annotation;
+import java.lang.reflect.Type;
+import java.net.URL;
+import java.util.Comparator;
+import java.util.Properties;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+public class SortProviderTest {
+ @Rule
+ public final ApplicationComposerRule container = new ApplicationComposerRule(this);
+
+ private final int port = NetworkUtil.getNextAvailablePort();
+
+ @Module
+ @Classes(innerClassesAsBean = true)
+ public WebApp web() {
+ return new WebApp();
+ }
+
+ @Configuration
+ public Properties props() {
+ return new PropertiesBuilder()
+ .p("httpejbd.port", Integer.toString(port))
+ .p("cxf.jaxrs.provider-comparator", MyComp.class.getName())
+ .p(OpenEjbContainer.OPENEJB_EMBEDDED_REMOTABLE, "true")
+ .build();
+ }
+
+ @Test
+ public void run() throws IOException {
+ assertTrue(MyComp.saw);
+ assertEquals("it works!", IO.slurp(new URL("http://localhost:" + port + "/openejb/test")));
+ }
+
+ public static class MyComp implements Comparator<Object> {
+ private static boolean saw;
+
+ @Override
+ public int compare(final Object o1, final Object o2) {
+ saw = true;
+ return o1.getClass().getName().compareTo(o2.getClass().getName());
+ }
+ }
+
+ @Path("/test")
+ public static class Endpoint {
+ @Context
+ private Providers providers;
+
+ @GET
+ @Produces("test/test")
+ public String asserts() {
+ return "fail";
+ }
+ }
+
+ @Provider
+ @Produces("test/test")
+ public static class TestProviderA<T> implements MessageBodyWriter<T> {
+ private String reverse(String str) {
+ if (str == null) {
+ return "";
+ }
+
+ StringBuilder s = new StringBuilder(str.length());
+ for (int i = str.length() - 1; i >= 0; i--) {
+ s.append(str.charAt(i));
+ }
+ return s.toString();
+ }
+
+ @Override
+ public long getSize(T t, Class<?> rawType, Type genericType, Annotation[] annotations, MediaType mediaType) {
+ return -1;
+ }
+
+ @Override
+ public boolean isWriteable(Class<?> rawType, Type genericType, Annotation[] annotations, MediaType mediaType) {
+ return true;
+ }
+
+ @Override
+ public void writeTo(T t, Class<?> rawType, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException {
+ entityStream.write(reverse((String) t).getBytes());
+ }
+ }
+
+ @Provider
+ @Produces("test/test")
+ public static class TestProvider11<T> implements MessageBodyWriter<T> {
+ @Override
+ public long getSize(T t, Class<?> rawType, Type genericType, Annotation[] annotations, MediaType mediaType) {
+ return -1;
+ }
+
+ @Override
+ public boolean isWriteable(Class<?> rawType, Type genericType, Annotation[] annotations, MediaType mediaType) {
+ return true;
+ }
+
+ @Override
+ public void writeTo(T t, Class<?> rawType, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException {
+ entityStream.write("it works!".getBytes());
+ }
+ }
+}