Skip to content

Commit

Permalink
CXF-7346: CDI Extension ignores producer methods and fields
Browse files Browse the repository at this point in the history
  • Loading branch information
reta committed May 8, 2017
1 parent c2e63fa commit 4b96a22
Show file tree
Hide file tree
Showing 27 changed files with 1,193 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/
package org.apache.cxf.cdi;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
Expand All @@ -37,6 +38,8 @@
import javax.enterprise.inject.spi.Extension;
import javax.enterprise.inject.spi.InjectionTarget;
import javax.enterprise.inject.spi.ProcessBean;
import javax.enterprise.inject.spi.ProcessProducerField;
import javax.enterprise.inject.spi.ProcessProducerMethod;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.Path;
import javax.ws.rs.core.Application;
Expand Down Expand Up @@ -116,6 +119,16 @@ public <T> void collect(@Observes final ProcessBean< T > event) {
hasBus = true;
}
}

public <T, X> void collect(@Observes final ProcessProducerField< T, X > event) {
final Type baseType = event.getAnnotatedProducerField().getBaseType();
processProducer(event, baseType);
}

public <T, X> void collect(@Observes final ProcessProducerMethod< T, X > event) {
final Type baseType = event.getAnnotatedProducerMethod().getBaseType();
processProducer(event, baseType);
}

public void load(@Observes final AfterDeploymentValidation event, final BeanManager beanManager) {
// no need of creational context, it only works for app scoped instances anyway
Expand Down Expand Up @@ -301,7 +314,7 @@ private List< Object > loadBeans(final BeanManager beanManager, Collection<Class
instances.add(
beanManager.getReference(
bean,
bean.getBeanClass(),
Object.class,
createCreationalContext(beanManager, bean)
)
);
Expand All @@ -324,7 +337,7 @@ private List< Feature > loadFeatures(final BeanManager beanManager, Collection<C
features.add(
(Feature) beanManager.getReference(
bean,
bean.getBeanClass(),
Feature.class,
createCreationalContext(beanManager, bean)
)
);
Expand Down Expand Up @@ -367,4 +380,23 @@ private<T> CreationalContext< T > createCreationalContext(final BeanManager bean
}
return creationalContext;
}

/**
* Extracts relevant beans from producers.
* @param event process bean event
* @param baseType base type of the producer
*/
private <T> void processProducer(final ProcessBean<T> event, final Type baseType) {
if (baseType instanceof Class<?>) {
final Class<?> clazz = (Class<?>)baseType;
if (clazz.isAnnotationPresent(Path.class)) {
serviceBeans.add(event.getBean());
} else if (clazz.isAnnotationPresent(Provider.class)) {
providerBeans.add(event.getBean());
} else if (clazz.isAnnotationPresent(ApplicationPath.class)) {
applicationBeans.add(event.getBean());
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
package org.apache.cxf.systests.cdi.base;

import javax.validation.constraints.NotNull;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Book {
@NotNull private String name;
private String id;
Expand Down
51 changes: 51 additions & 0 deletions systests/cdi/cdi-owb/cdi-producers-owb/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ 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.
-->

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>org.apache.cxf.systests</groupId>
<artifactId>cxf-systests-cdi-owb</artifactId>
<version>3.2.0-SNAPSHOT</version>
<relativePath>../</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>cxf-systests-cdi-owb-producers</artifactId>
<name>Apache CXF CDI Integration System Tests - OWB with producers</name>
<description>Apache CXF CDI Integration System Tests - OpenWebBeans with producers</description>
<url>http://cxf.apache.org</url>

<dependencies>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-rs-extension-providers</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.apache.abdera</groupId>
<artifactId>abdera-core</artifactId>
</dependency>
<dependency>
<groupId>org.apache.abdera</groupId>
<artifactId>abdera-parser</artifactId>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/**
* 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.systest.jaxrs.cdi;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;

import org.apache.abdera.model.Feed;
import org.apache.abdera.parser.stax.FOMEntry;
import org.apache.abdera.parser.stax.FOMFeed;
import org.apache.cxf.systests.cdi.base.Book;
import org.apache.cxf.systests.cdi.base.BookStoreService;

@Path("/bookstore/")
public class BookStoreFeed {
private final BookStoreService service;

public BookStoreFeed(BookStoreService service) {
this.service = service;
}

@GET
@Path("/books/feed")
@NotNull @Valid
@Produces("application/atom+xml")
public Feed getBooks() {
final FOMFeed feed = new FOMFeed();

for (final Book book: service.all()) {
final FOMEntry entry = new FOMEntry();
entry.addLink("/bookstore/books/" + book.getId());
feed.addEntry(entry);
}

return feed;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/**
* 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.systest.jaxrs.cdi;

import javax.enterprise.inject.Produces;
import javax.inject.Inject;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
import javax.ws.rs.core.Feature;

import org.apache.cxf.jaxrs.validation.JAXRSBeanValidationFeature;
import org.apache.cxf.jaxrs.validation.ValidationExceptionMapper;
import org.apache.cxf.systests.cdi.base.BookStoreService;

@ApplicationPath("/")
public class BookStoreProducerApplication extends Application {
@Produces protected BookStoreValidatingFeed bookStoreValidatingFeed = new BookStoreValidatingFeed();
@Inject private BookStoreService service;

@Produces
public ValidationExceptionMapper validationExceptionMapper() {
return new ValidationExceptionMapper();
}

@Produces
public Feature sampleFeature() {
return new SampleFeature();
}

@Produces
public BookStoreFeed bookStoreFeed() {
return new BookStoreFeed(service);
}

@Produces
public org.apache.cxf.feature.Feature validationFeature() {
return new JAXRSBeanValidationFeature();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
* 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.systest.jaxrs.cdi;

import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import javax.ws.rs.FormParam;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;

@Path("/bookstore/")
public class BookStoreValidatingFeed {
@POST
@Path("/books/feed")
@Produces("application/atom+xml")
public Response addBook(@Context final UriInfo uriInfo,
@NotNull @Size(min = 1, max = 50) @FormParam("id") String id,
@NotNull @FormParam("name") String name) {
return Response.created(uriInfo.getRequestUriBuilder().path(id).build()).build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* 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.systest.jaxrs.cdi;

import javax.ws.rs.core.Feature;
import javax.ws.rs.core.FeatureContext;

import org.apache.cxf.jaxrs.provider.atom.AtomFeedProvider;

public class SampleFeature implements Feature {
@Override
public boolean configure(FeatureContext context) {
context.register(AtomFeedProvider.class);
return false;
}
}
Loading

0 comments on commit 4b96a22

Please sign in to comment.