Skip to content

Commit 53da848

Browse files
committed
Support for Spring 5
Fixes: gh-3736
1 parent 61e6399 commit 53da848

File tree

48 files changed

+3388
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+3388
-0
lines changed

ext/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
<module>rx</module>
5454
<module>servlet-portability</module>
5555
<module>spring4</module>
56+
<module>spring5</module>
5657
<module>wadl-doclet</module>
5758
</modules>
5859

ext/spring5/pom.xml

Lines changed: 224 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,224 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
4+
DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
5+
6+
Copyright (c) 2012-2017 Oracle and/or its affiliates. All rights reserved.
7+
8+
The contents of this file are subject to the terms of either the GNU
9+
General Public License Version 2 only ("GPL") or the Common Development
10+
and Distribution License("CDDL") (collectively, the "License"). You
11+
may not use this file except in compliance with the License. You can
12+
obtain a copy of the License at
13+
https://oss.oracle.com/licenses/CDDL+GPL-1.1
14+
or LICENSE.txt. See the License for the specific
15+
language governing permissions and limitations under the License.
16+
17+
When distributing the software, include this License Header Notice in each
18+
file and include the License file at LICENSE.txt.
19+
20+
GPL Classpath Exception:
21+
Oracle designates this particular file as subject to the "Classpath"
22+
exception as provided by Oracle in the GPL Version 2 section of the License
23+
file that accompanied this code.
24+
25+
Modifications:
26+
If applicable, add the following below the License Header, with the fields
27+
enclosed by brackets [] replaced by your own identifying information:
28+
"Portions Copyright [year] [name of copyright owner]"
29+
30+
Contributor(s):
31+
If you wish your version of this file to be governed by only the CDDL or
32+
only the GPL Version 2, indicate your decision by adding "[Contributor]
33+
elects to include this software in this distribution under the [CDDL or GPL
34+
Version 2] license." If you don't indicate a single choice of license, a
35+
recipient has the option to distribute your version of this file under
36+
either the CDDL, the GPL Version 2 or to extend the choice of license to
37+
its licensees as provided above. However, if you add GPL Version 2 code
38+
and therefore, elected the GPL Version 2 license, then the option applies
39+
only if the new code is made subject to such option by the copyright
40+
holder.
41+
42+
-->
43+
44+
<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">
45+
46+
<modelVersion>4.0.0</modelVersion>
47+
48+
<parent>
49+
<groupId>org.glassfish.jersey.ext</groupId>
50+
<artifactId>project</artifactId>
51+
<version>2.29-SNAPSHOT</version>
52+
</parent>
53+
54+
<artifactId>jersey-spring5</artifactId>
55+
<name>jersey-spring5</name>
56+
57+
<packaging>jar</packaging>
58+
59+
<description>
60+
Jersey extension module providing support for Spring 5 integration.
61+
</description>
62+
63+
<dependencies>
64+
<dependency>
65+
<groupId>org.glassfish.jersey.core</groupId>
66+
<artifactId>jersey-server</artifactId>
67+
<version>${project.version}</version>
68+
</dependency>
69+
70+
<dependency>
71+
<groupId>org.glassfish.jersey.inject</groupId>
72+
<artifactId>jersey-hk2</artifactId>
73+
<version>${project.version}</version>
74+
</dependency>
75+
76+
<dependency>
77+
<groupId>org.glassfish.jersey.containers</groupId>
78+
<artifactId>jersey-container-servlet-core</artifactId>
79+
<version>${project.version}</version>
80+
</dependency>
81+
82+
<dependency>
83+
<groupId>org.glassfish.jersey.test-framework.providers</groupId>
84+
<artifactId>jersey-test-framework-provider-grizzly2</artifactId>
85+
<version>${project.version}</version>
86+
<scope>test</scope>
87+
</dependency>
88+
89+
<dependency>
90+
<groupId>commons-logging</groupId>
91+
<artifactId>commons-logging</artifactId>
92+
<version>1.2</version>
93+
<scope>test</scope>
94+
</dependency>
95+
96+
<dependency>
97+
<groupId>org.glassfish.hk2</groupId>
98+
<artifactId>hk2</artifactId>
99+
<version>${hk2.version}</version>
100+
</dependency>
101+
102+
<dependency>
103+
<groupId>org.glassfish.hk2</groupId>
104+
<artifactId>spring-bridge</artifactId>
105+
<version>${hk2.version}</version>
106+
<exclusions>
107+
<exclusion> <!-- already pulled in by jersey-server -->
108+
<groupId>javax.inject</groupId>
109+
<artifactId>javax.inject</artifactId>
110+
</exclusion>
111+
<exclusion>
112+
<groupId>org.glassfish.hk2</groupId>
113+
<artifactId>hk2-api</artifactId>
114+
</exclusion>
115+
</exclusions>
116+
</dependency>
117+
118+
<dependency>
119+
<groupId>org.springframework</groupId>
120+
<artifactId>spring-beans</artifactId>
121+
<version>${spring5.version}</version>
122+
</dependency>
123+
124+
<dependency>
125+
<groupId>org.springframework</groupId>
126+
<artifactId>spring-core</artifactId>
127+
<version>${spring5.version}</version>
128+
<exclusions>
129+
<exclusion>
130+
<groupId>commons-logging</groupId>
131+
<artifactId>commons-logging</artifactId>
132+
</exclusion>
133+
</exclusions>
134+
</dependency>
135+
136+
<dependency>
137+
<groupId>org.springframework</groupId>
138+
<artifactId>spring-web</artifactId>
139+
<version>${spring5.version}</version>
140+
</dependency>
141+
142+
<dependency>
143+
<groupId>org.springframework</groupId>
144+
<artifactId>spring-context</artifactId>
145+
<version>${spring5.version}</version>
146+
</dependency>
147+
148+
<dependency>
149+
<groupId>org.springframework</groupId>
150+
<artifactId>spring-aop</artifactId>
151+
<version>${spring5.version}</version>
152+
</dependency>
153+
154+
<dependency>
155+
<groupId>javax.servlet</groupId>
156+
<artifactId>javax.servlet-api</artifactId>
157+
<version>3.0.1</version>
158+
<scope>provided</scope>
159+
</dependency>
160+
161+
<dependency>
162+
<groupId>org.glassfish.jersey.test-framework</groupId>
163+
<artifactId>jersey-test-framework-core</artifactId>
164+
<version>${project.version}</version>
165+
<scope>test</scope>
166+
</dependency>
167+
168+
<dependency>
169+
<groupId>org.aspectj</groupId>
170+
<artifactId>aspectjrt</artifactId>
171+
<version>1.6.11</version>
172+
<scope>test</scope>
173+
</dependency>
174+
<dependency>
175+
<groupId>org.aspectj</groupId>
176+
<artifactId>aspectjweaver</artifactId>
177+
<version>1.6.11</version>
178+
<scope>test</scope>
179+
</dependency>
180+
181+
</dependencies>
182+
183+
<properties>
184+
<spring5.version>5.0.0.RELEASE</spring5.version>
185+
</properties>
186+
187+
<build>
188+
<plugins>
189+
<plugin>
190+
<groupId>com.sun.istack</groupId>
191+
<artifactId>maven-istack-commons-plugin</artifactId>
192+
<inherited>true</inherited>
193+
</plugin>
194+
<plugin>
195+
<groupId>org.codehaus.mojo</groupId>
196+
<artifactId>build-helper-maven-plugin</artifactId>
197+
<inherited>true</inherited>
198+
</plugin>
199+
</plugins>
200+
</build>
201+
202+
<profiles>
203+
<profile>
204+
<id>delayed-strategy-skip-test</id>
205+
<activation>
206+
<property>
207+
<name>org.glassfish.jersey.injection.manager.strategy</name>
208+
<value>delayed</value>
209+
</property>
210+
</activation>
211+
<build>
212+
<plugins>
213+
<plugin>
214+
<groupId>org.apache.maven.plugins</groupId>
215+
<artifactId>maven-surefire-plugin</artifactId>
216+
<configuration>
217+
<skipTests>true</skipTests>
218+
</configuration>
219+
</plugin>
220+
</plugins>
221+
</build>
222+
</profile>
223+
</profiles>
224+
</project>
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
/*
2+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3+
*
4+
* Copyright (c) 2013-2017 Oracle and/or its affiliates. All rights reserved.
5+
*
6+
* The contents of this file are subject to the terms of either the GNU
7+
* General Public License Version 2 only ("GPL") or the Common Development
8+
* and Distribution License("CDDL") (collectively, the "License"). You
9+
* may not use this file except in compliance with the License. You can
10+
* obtain a copy of the License at
11+
* https://oss.oracle.com/licenses/CDDL+GPL-1.1
12+
* or LICENSE.txt. See the License for the specific
13+
* language governing permissions and limitations under the License.
14+
*
15+
* When distributing the software, include this License Header Notice in each
16+
* file and include the License file at LICENSE.txt.
17+
*
18+
* GPL Classpath Exception:
19+
* Oracle designates this particular file as subject to the "Classpath"
20+
* exception as provided by Oracle in the GPL Version 2 section of the License
21+
* file that accompanied this code.
22+
*
23+
* Modifications:
24+
* If applicable, add the following below the License Header, with the fields
25+
* enclosed by brackets [] replaced by your own identifying information:
26+
* "Portions Copyright [year] [name of copyright owner]"
27+
*
28+
* Contributor(s):
29+
* If you wish your version of this file to be governed by only the CDDL or
30+
* only the GPL Version 2, indicate your decision by adding "[Contributor]
31+
* elects to include this software in this distribution under the [CDDL or GPL
32+
* Version 2] license." If you don't indicate a single choice of license, a
33+
* recipient has the option to distribute your version of this file under
34+
* either the CDDL, the GPL Version 2 or to extend the choice of license to
35+
* its licensees as provided above. However, if you add GPL Version 2 code
36+
* and therefore, elected the GPL Version 2 license, then the option applies
37+
* only if the new code is made subject to such option by the copyright
38+
* holder.
39+
*/
40+
41+
package org.glassfish.jersey.server.spring;
42+
43+
import java.lang.reflect.AnnotatedElement;
44+
import java.lang.reflect.Constructor;
45+
import java.lang.reflect.Field;
46+
import java.lang.reflect.Method;
47+
import java.util.HashSet;
48+
import java.util.Set;
49+
import java.util.logging.Logger;
50+
51+
import javax.inject.Singleton;
52+
53+
import org.glassfish.jersey.internal.inject.Injectee;
54+
import org.glassfish.jersey.internal.inject.InjectionResolver;
55+
56+
import org.springframework.beans.BeansException;
57+
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
58+
import org.springframework.beans.factory.annotation.Autowired;
59+
import org.springframework.beans.factory.annotation.Qualifier;
60+
import org.springframework.beans.factory.config.DependencyDescriptor;
61+
import org.springframework.context.ApplicationContext;
62+
import org.springframework.core.MethodParameter;
63+
64+
/**
65+
* HK2 injection resolver for Spring framework {@link Autowired} annotation injection.
66+
*
67+
* @author Marko Asplund (marko.asplund at yahoo.com)
68+
* @author Vetle Leinonen-Roeim (vetle at roeim.net)
69+
*/
70+
@Singleton
71+
public class AutowiredInjectResolver implements InjectionResolver<Autowired> {
72+
73+
private static final Logger LOGGER = Logger.getLogger(AutowiredInjectResolver.class.getName());
74+
75+
private volatile ApplicationContext ctx;
76+
77+
/**
78+
* Create a new instance.
79+
*
80+
* @param ctx Spring application context.
81+
*/
82+
public AutowiredInjectResolver(ApplicationContext ctx) {
83+
this.ctx = ctx;
84+
}
85+
86+
@Override
87+
public Object resolve(Injectee injectee) {
88+
AnnotatedElement parent = injectee.getParent();
89+
String beanName = null;
90+
if (parent != null) {
91+
Qualifier an = parent.getAnnotation(Qualifier.class);
92+
if (an != null) {
93+
beanName = an.value();
94+
}
95+
}
96+
boolean required = parent != null ? parent.getAnnotation(Autowired.class).required() : false;
97+
return getBeanFromSpringContext(beanName, injectee, required);
98+
}
99+
100+
private Object getBeanFromSpringContext(String beanName, Injectee injectee, final boolean required) {
101+
try {
102+
DependencyDescriptor dependencyDescriptor = createSpringDependencyDescriptor(injectee);
103+
Set<String> autowiredBeanNames = new HashSet<>(1);
104+
autowiredBeanNames.add(beanName);
105+
return ctx.getAutowireCapableBeanFactory().resolveDependency(dependencyDescriptor, null,
106+
autowiredBeanNames, null);
107+
} catch (BeansException e) {
108+
if (required) {
109+
LOGGER.warning(e.getMessage());
110+
throw e;
111+
}
112+
return null;
113+
}
114+
}
115+
116+
private DependencyDescriptor createSpringDependencyDescriptor(final Injectee injectee) {
117+
AnnotatedElement annotatedElement = injectee.getParent();
118+
119+
if (annotatedElement.getClass().isAssignableFrom(Field.class)) {
120+
return new DependencyDescriptor((Field) annotatedElement, !injectee.isOptional());
121+
} else if (annotatedElement.getClass().isAssignableFrom(Method.class)) {
122+
return new DependencyDescriptor(
123+
new MethodParameter((Method) annotatedElement, injectee.getPosition()), !injectee.isOptional());
124+
} else {
125+
return new DependencyDescriptor(
126+
new MethodParameter((Constructor) annotatedElement, injectee.getPosition()), !injectee.isOptional());
127+
}
128+
}
129+
130+
@Override
131+
public boolean isConstructorParameterIndicator() {
132+
return false;
133+
}
134+
135+
@Override
136+
public boolean isMethodParameterIndicator() {
137+
return false;
138+
}
139+
140+
@Override
141+
public Class<Autowired> getAnnotation() {
142+
return Autowired.class;
143+
}
144+
}

0 commit comments

Comments
 (0)