Repository: cxf Updated Branches: refs/heads/3.1.x-fixes fbe8db21d -> 1d2f49e37
Fixed handling resource classes in CDI context by introducing dedicated resource provider implementation Project: http://git-wip-us.apache.org/repos/asf/cxf/repo Commit: http://git-wip-us.apache.org/repos/asf/cxf/commit/1d2f49e3 Tree: http://git-wip-us.apache.org/repos/asf/cxf/tree/1d2f49e3 Diff: http://git-wip-us.apache.org/repos/asf/cxf/diff/1d2f49e3 Branch: refs/heads/3.1.x-fixes Commit: 1d2f49e37fa341aaa56960235cc784def4b4a70a Parents: fbe8db2 Author: reta <[email protected]> Authored: Thu Nov 10 21:07:02 2016 -0500 Committer: reta <[email protected]> Committed: Thu Nov 10 21:07:02 2016 -0500 ---------------------------------------------------------------------- .../org/apache/cxf/cdi/CdiResourceProvider.java | 51 ++++++++++++++++++++ .../cxf/cdi/JAXRSCdiResourceExtension.java | 25 ++++------ 2 files changed, 61 insertions(+), 15 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cxf/blob/1d2f49e3/integration/cdi/src/main/java/org/apache/cxf/cdi/CdiResourceProvider.java ---------------------------------------------------------------------- diff --git a/integration/cdi/src/main/java/org/apache/cxf/cdi/CdiResourceProvider.java b/integration/cdi/src/main/java/org/apache/cxf/cdi/CdiResourceProvider.java new file mode 100644 index 0000000..997cf40 --- /dev/null +++ b/integration/cdi/src/main/java/org/apache/cxf/cdi/CdiResourceProvider.java @@ -0,0 +1,51 @@ +/** + * 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.cxf.cdi; + +import org.apache.cxf.jaxrs.lifecycle.ResourceProvider; +import org.apache.cxf.message.Message; + +public class CdiResourceProvider implements ResourceProvider { + private final Object instance; + private final Class<?> resourceClass; + + CdiResourceProvider(final Class<?> resourceClass, final Object instance) { + this.resourceClass = resourceClass; + this.instance = instance; + } + + @Override + public Object getInstance(Message m) { + return instance; + } + + @Override + public void releaseInstance(Message m, Object o) { + } + + @Override + public Class<?> getResourceClass() { + return resourceClass; + } + + @Override + public boolean isSingleton() { + return true; + } +} http://git-wip-us.apache.org/repos/asf/cxf/blob/1d2f49e3/integration/cdi/src/main/java/org/apache/cxf/cdi/JAXRSCdiResourceExtension.java ---------------------------------------------------------------------- diff --git a/integration/cdi/src/main/java/org/apache/cxf/cdi/JAXRSCdiResourceExtension.java b/integration/cdi/src/main/java/org/apache/cxf/cdi/JAXRSCdiResourceExtension.java index 968424e..9fb59d8 100644 --- a/integration/cdi/src/main/java/org/apache/cxf/cdi/JAXRSCdiResourceExtension.java +++ b/integration/cdi/src/main/java/org/apache/cxf/cdi/JAXRSCdiResourceExtension.java @@ -168,10 +168,15 @@ public class JAXRSCdiResourceExtension implements Extension { private JAXRSServerFactoryBean createFactoryInstance(final Application application, final BeanManager beanManager) { final JAXRSServerFactoryBean instance = ResourceUtils.createApplication(application, false, false, bus); - final Map< Class< ? >, List< Object > > classified = classifySingletons(application, beanManager); - instance.setServiceBeans(classified.get(Path.class)); + final Map< Class< ? >, List< Object > > classified = classes2singletons(application, beanManager); + instance.setProviders(classified.get(Provider.class)); - instance.setFeatures(CastUtils.cast(classified.get(Feature.class), Feature.class)); + instance.getFeatures().addAll(CastUtils.cast(classified.get(Feature.class), Feature.class)); + + for (final Object resource: classified.get(Path.class)) { + instance.setResourceProvider(resource.getClass(), + new CdiResourceProvider(resource.getClass(), resource)); + } return instance; } @@ -183,9 +188,8 @@ public class JAXRSCdiResourceExtension implements Extension { * @param application the application instance * @return classified singletons by instance types */ - private Map< Class< ? >, List< Object > > classifySingletons(final Application application, + private Map< Class< ? >, List< Object > > classes2singletons(final Application application, final BeanManager beanManager) { - final Set<Object> singletons = application.getSingletons(); final Map< Class< ? >, List< Object > > classified = new HashMap<>(); @@ -193,16 +197,6 @@ public class JAXRSCdiResourceExtension implements Extension { classified.put(Provider.class, new ArrayList<>()); classified.put(Path.class, new ArrayList<>()); - for (final Object singleton: singletons) { - if (singleton instanceof Feature) { - classified.get(Feature.class).add(singleton); - } else if (singleton.getClass().isAnnotationPresent(Provider.class)) { - classified.get(Provider.class).add(singleton); - } else if (singleton.getClass().isAnnotationPresent(Path.class)) { - classified.get(Path.class).add(singleton); - } - } - // now loop through the classes Set<Class<?>> classes = application.getClasses(); if (!classes.isEmpty()) { @@ -210,6 +204,7 @@ public class JAXRSCdiResourceExtension implements Extension { classified.get(Provider.class).addAll(loadProviders(beanManager, classes)); classified.get(Feature.class).addAll(loadFeatures(beanManager, classes)); } + return classified; }
