Skip to content

Commit

Permalink
jersey-3992 CDI Bean created (but fails) when interface has @path ann…
Browse files Browse the repository at this point in the history
…otation

Signed-off-by: Gaurav Gupta <gaurav.gupta.jc@gmail.com>
  • Loading branch information
jGauravGupta authored and jansupol committed Feb 14, 2019
1 parent 6f425d9 commit 310db2c
Show file tree
Hide file tree
Showing 10 changed files with 386 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* Copyright (c) 2013, 2018 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2018 Payara Foundation and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand Down Expand Up @@ -292,6 +293,11 @@ public boolean bind(final Class<?> clazz, final Set<Class<?>> providerContracts)

final boolean isJaxRsResource = jaxRsResourceCache.apply(clazz);

if (isJaxRsResource && !Resource.isAcceptable(clazz)) {
LOGGER.warning(LocalizationMessages.CDI_NON_INSTANTIABLE_COMPONENT(clazz));
return false;
}

final Class<? extends Annotation> beanScopeAnnotation = CdiUtil.getBeanScope(clazz, beanManager);
final boolean isRequestScoped = beanScopeAnnotation == RequestScoped.class
|| (beanScopeAnnotation == Dependent.class && isJaxRsResource);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#
# Copyright (c) 2013, 2018 Oracle and/or its affiliates. All rights reserved.
# Copyright (c) 2018 Payara Foundation and/or its affiliates. All rights reserved.
#
# This program and the accompanying materials are made available under the
# terms of the Eclipse Public License v. 2.0, which is available at
Expand All @@ -24,3 +25,4 @@ cdi.multiple.locators.into.simple.app=Trying to register multiple service locato
cdi.provider.initialized=Jersey CDI component provider initialized.
cdi.request.scoped.components.recognized=The following CDI types were recognized as request scoped components in Jersey: {0}.
cdi.type.vetoed=The following types have been vetoed by Jersey as per {0} configuration: {1}
cdi.non.instantiable.component=Component of class {0} cannot be instantiated and will be ignored.
93 changes: 93 additions & 0 deletions tests/integration/jersey-3992/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (c) 2013, 2018 Oracle and/or its affiliates. All rights reserved.
Copyright (c) 2018 Payara Foundation and/or its affiliates. All rights reserved.
This program and the accompanying materials are made available under the
terms of the Eclipse Public License v. 2.0, which is available at
http://www.eclipse.org/legal/epl-2.0.
This Source Code may also be made available under the following Secondary
Licenses when the conditions for such availability set forth in the
Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
version 2 with the GNU Classpath Exception, which is available at
https://www.gnu.org/software/classpath/license.html.
SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
-->

<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">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.glassfish.jersey.tests.integration</groupId>
<artifactId>project</artifactId>
<version>2.28-SNAPSHOT</version>
</parent>

<artifactId>jersey-3992</artifactId>
<packaging>war</packaging>
<name>jersey-3992</name>

<description>Jersey CDI Inheritance test web application</description>

<dependencies>
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.enterprise</groupId>
<artifactId>cdi-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.test-framework.providers</groupId>
<artifactId>jersey-test-framework-provider-bundle</artifactId>
<type>pom</type>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.ext.cdi</groupId>
<artifactId>jersey-weld2-se</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<finalName>${project.artifactId}</finalName>
</build>

<profiles>
<profile>
<id>run-external-tests</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<systemPropertyVariables>
<jersey.config.test.container.factory>${external.container.factory}</jersey.config.test.container.factory>
<jersey.config.test.container.port>${external.container.port}</jersey.config.test.container.port>
</systemPropertyVariables>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<!-- External test container configuration is done via properties to allow overriding via command line. -->
<external.container.factory>org.glassfish.jersey.test.external.ExternalTestContainerFactory</external.container.factory>
<external.container.port>8080</external.container.port>
<maven.test.skip>false</maven.test.skip>
</properties>
</profile>
</profiles>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* Copyright (c) 2010, 2017 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2018 Payara Foundation and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/

package org.glassfish.jersey.tests.cdi.resources;

import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.logging.Logger;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.inject.spi.BeanManager;

import javax.inject.Inject;

import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;

/**
* JAX-RS application to configure resources.
*
* @author Jonathan Benoit (jonathan.benoit at oracle.com)
*/
@ApplicationPath("main")
@ApplicationScoped
public class MainApplication extends Application {

static AtomicInteger postConstructCounter = new AtomicInteger();

@Inject BeanManager bm;

private static final Logger LOGGER = Logger.getLogger(MainApplication.class.getName());

@Override
public Set<Class<?>> getClasses() {
final Set<Class<?>> classes = new HashSet<Class<?>>();
classes.add(UserResource.class);
classes.add(UserResourceImpl.class);
return classes;
}

// JERSEY-2531: make sure this type gets managed by CDI
@PostConstruct
public void postConstruct() {
LOGGER.info(String.format("%s: POST CONSTRUCT.", this.getClass().getName()));
postConstructCounter.incrementAndGet();
if (bm == null) {
throw new IllegalStateException("BeanManager should have been injected into a CDI managed bean.");
}
if (postConstructCounter.intValue() > 1) {
throw new IllegalStateException("postConstruct should have been invoked only once on app scoped bean.");
}
}

@PreDestroy
public void preDestroy() {
LOGGER.info(String.format("%s: PRE DESTROY.", this.getClass().getName()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright (c) 2018 Payara Foundation and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/

package org.glassfish.jersey.tests.cdi.resources;

import javax.ws.rs.GET;
import javax.ws.rs.Path;

/**
*
* @author Gaurav Gupta (gaurav.gupta@payara.fish)
*/
public interface UserResource {

/**
* Gets the user count
*
* @return user count.
*/
@GET
@Path("/count")
public int getCount();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright (c) 2018 Payara Foundation and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/

package org.glassfish.jersey.tests.cdi.resources;

import javax.ws.rs.GET;
import javax.ws.rs.Path;

/**
*
* @author Gaurav Gupta (gaurav.gupta@payara.fish)
*/
@Path("user")
public class UserResourceImpl implements UserResource {

/**
* Gets the user count
*
* @return user count.
*/
@GET
@Path("/count")
@Override
public int getCount() {
return 0;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (c) 2018 Payara Foundation and/or its affiliates. All rights reserved.
This program and the accompanying materials are made available under the
terms of the Eclipse Public License v. 2.0, which is available at
http://www.eclipse.org/legal/epl-2.0.
This Source Code may also be made available under the following Secondary
Licenses when the conditions for such availability set forth in the
Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
version 2 with the GNU Classpath Exception, which is available at
https://www.gnu.org/software/classpath/license.html.
SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
-->

<beans
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
bean-discovery-mode="annotated">
</beans>
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Copyright (c) 2013, 2017 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2018 Payara Foundation and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/

package org.glassfish.jersey.tests.cdi.resources;

import java.net.URI;

import javax.ws.rs.core.Application;
import javax.ws.rs.core.UriBuilder;
import org.glassfish.jersey.test.JerseyTest;
import org.jboss.weld.environment.se.Weld;

/**
* Test for CDI web application resources.
* Run with:
* <pre>
* mvn clean package
* $AS_HOME/bin/asadmin deploy target/cdi-test-webapp
* mvn -DskipTests=false test</pre>
*
* @author Jakub Podlesak (jakub.podlesak at oracle.com)
*/
public class CdiTest extends JerseyTest {

Weld weld;

@Override
public void setUp() throws Exception {
weld = new Weld();
weld.initialize();
super.setUp();
}

@Override
public void tearDown() throws Exception {
weld.shutdown();
super.tearDown();
}

@Override
protected Application configure() {
return new MainApplication();
}

@Override
protected URI getBaseUri() {
return UriBuilder.fromUri(super.getBaseUri()).path("cdi-test-webapp/main").build();
}
}

Loading

0 comments on commit 310db2c

Please sign in to comment.