Skip to content
This repository was archived by the owner on May 28, 2018. It is now read-only.

Commit f8f7661

Browse files
author
Adam Lindenthal
committed
Merge pull request #128 from sarxos/JERSEY-2704
Register parent service locator in servlet context, closes JERSEY-2704
2 parents 99ab6ef + bcf38a2 commit f8f7661

File tree

13 files changed

+635
-1
lines changed

13 files changed

+635
-1
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,4 @@ nb-configuration.xml
2828

2929
# Maven plugins noise
3030
dependency-reduced-pom.xml
31+
pom.xml.versionsBackup

containers/jersey-servlet-core/src/main/java/org/glassfish/jersey/servlet/ServletContainer.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -588,4 +588,13 @@ public void reload(ResourceConfig configuration) {
588588
public ApplicationHandler getApplicationHandler() {
589589
return webComponent.appHandler;
590590
}
591+
592+
/**
593+
* Get {@link WebComponent} used by this servlet container.
594+
*
595+
* @return The web component.
596+
*/
597+
public WebComponent getWebComponent() {
598+
return webComponent;
599+
}
591600
}

containers/jersey-servlet-core/src/main/java/org/glassfish/jersey/servlet/ServletProperties.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
*/
4040
package org.glassfish.jersey.servlet;
4141

42+
import org.glassfish.hk2.api.ServiceLocator;
4243
import org.glassfish.jersey.internal.util.PropertiesClass;
4344

4445
/**
@@ -147,6 +148,19 @@ public final class ServletProperties {
147148
*/
148149
public static final String PROVIDER_WEB_APP = "jersey.config.servlet.provider.webapp";
149150

151+
/**
152+
* Identifies the object that will be used as a parent {@link ServiceLocator} in the Jersey
153+
* {@link WebComponent}.
154+
* <p></p>
155+
* This property gives a possibility to use HK2 services that are registered and/or created
156+
* outside of the Jersey server context.
157+
* <p></p>
158+
* By default this property is not set.
159+
* <p></p>
160+
* The name of the configuration property is <tt>{@value}</tt>.
161+
*/
162+
public static final String SERVICE_LOCATOR = "jersey.config.servlet.context.serviceLocator";
163+
150164
private ServletProperties() {
151165
// prevents instantiation
152166
}

containers/jersey-servlet-core/src/main/java/org/glassfish/jersey/servlet/WebComponent.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,9 @@ public void dispose(final WebConfig instance) {
297297
* resource configuration.
298298
*/
299299
public WebComponent(final WebConfig webConfig, ResourceConfig resourceConfig) throws ServletException {
300+
300301
this.webConfig = webConfig;
302+
301303
if (resourceConfig == null) {
302304
resourceConfig = createResourceConfig(webConfig);
303305
}
@@ -308,7 +310,9 @@ public WebComponent(final WebConfig webConfig, ResourceConfig resourceConfig) th
308310
final AbstractBinder webComponentBinder = new WebComponentBinder(resourceConfig.getProperties());
309311
resourceConfig.register(webComponentBinder);
310312

311-
this.appHandler = new ApplicationHandler(resourceConfig, webComponentBinder);
313+
ServiceLocator locator = (ServiceLocator) webConfig.getServletContext().getAttribute(ServletProperties.SERVICE_LOCATOR);
314+
315+
this.appHandler = new ApplicationHandler(resourceConfig, webComponentBinder, locator);
312316

313317
this.asyncExtensionDelegate = getAsyncExtensionDelegate();
314318
this.forwardOn404 = webConfig.getConfigType().equals(WebConfig.ConfigType.FilterConfig)
@@ -562,4 +566,13 @@ private void filterFormParameters(final HttpServletRequest servletRequest, final
562566
}
563567
}
564568
}
569+
570+
/**
571+
* Get {@link ApplicationHandler} used by this web component.
572+
*
573+
* @return The application handler
574+
*/
575+
public ApplicationHandler getAppHandler() {
576+
return appHandler;
577+
}
565578
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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) 2011-2014 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+
http://glassfish.java.net/public/CDDL+GPL_1_1.html
14+
or packager/legal/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 packager/legal/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+
<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">
44+
<modelVersion>4.0.0</modelVersion>
45+
46+
<parent>
47+
<groupId>org.glassfish.jersey.tests.integration</groupId>
48+
<artifactId>project</artifactId>
49+
<version>2.14-SNAPSHOT</version>
50+
</parent>
51+
52+
<artifactId>jersey-2704</artifactId>
53+
<packaging>war</packaging>
54+
<name>jersey-tests-integration-jersey-2704</name>
55+
56+
<description>
57+
This test is to verify if ServiceLocator can be configured within the server context
58+
in such a way that Jersey can use it as a parent ServiceLocator in the WebComponent.
59+
More details can be found in JERSEY-2704.
60+
</description>
61+
62+
<dependencies>
63+
<dependency>
64+
<groupId>org.glassfish.jersey.containers</groupId>
65+
<artifactId>jersey-container-servlet-core</artifactId>
66+
</dependency>
67+
<dependency>
68+
<groupId>org.glassfish.jersey.test-framework.providers</groupId>
69+
<artifactId>jersey-test-framework-provider-external</artifactId>
70+
</dependency>
71+
</dependencies>
72+
73+
<build>
74+
<plugins>
75+
<plugin>
76+
<groupId>org.apache.maven.plugins</groupId>
77+
<artifactId>maven-compiler-plugin</artifactId>
78+
</plugin>
79+
<plugin>
80+
<groupId>org.apache.maven.plugins</groupId>
81+
<artifactId>maven-failsafe-plugin</artifactId>
82+
</plugin>
83+
<plugin>
84+
<groupId>org.mortbay.jetty</groupId>
85+
<artifactId>jetty-maven-plugin</artifactId>
86+
</plugin>
87+
</plugins>
88+
</build>
89+
90+
</project>
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3+
*
4+
* Copyright (c) 2012-2014 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+
* http://glassfish.java.net/public/CDDL+GPL_1_1.html
12+
* or packager/legal/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 packager/legal/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+
package org.glassfish.jersey.tests.integration.jersey2704;
41+
42+
import javax.servlet.ServletContextEvent;
43+
import javax.servlet.ServletContextListener;
44+
45+
import org.glassfish.hk2.api.ServiceLocator;
46+
import org.glassfish.hk2.utilities.ServiceLocatorUtilities;
47+
import org.glassfish.jersey.servlet.ServletProperties;
48+
import org.glassfish.jersey.tests.integration.jersey2704.services.HappyService;
49+
50+
51+
/**
52+
* This class is to listen for {@link ServletContextEvent} generated whenever context
53+
* is initialized and set {@link ServletProperties#SERVICE_LOCATOR} attribute to point
54+
* {@link ServiceLocator} pre-populated with {@link HappyService} instance.
55+
*
56+
* @author Bartosz Firyn (sarxos)
57+
*/
58+
public class ServiceLocatorSetup implements ServletContextListener {
59+
60+
@Override
61+
public void contextInitialized(ServletContextEvent event) {
62+
ServiceLocator locator = ServiceLocatorUtilities.createAndPopulateServiceLocator();
63+
ServiceLocatorUtilities.addOneConstant(locator, new HappyService());
64+
event.getServletContext().setAttribute(ServletProperties.SERVICE_LOCATOR, locator);
65+
}
66+
67+
@Override
68+
public void contextDestroyed(ServletContextEvent event) {
69+
}
70+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3+
*
4+
* Copyright (c) 2012-2014 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+
* http://glassfish.java.net/public/CDDL+GPL_1_1.html
12+
* or packager/legal/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 packager/legal/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+
package org.glassfish.jersey.tests.integration.jersey2704;
41+
42+
import org.glassfish.jersey.server.ResourceConfig;
43+
44+
45+
/**
46+
* Jersey application.
47+
*
48+
* @author Bartosz Firyn (bartoszfiryn at gmail.com)
49+
*/
50+
public class TestApplication extends ResourceConfig {
51+
52+
public TestApplication() {
53+
register(TestResource.class);
54+
}
55+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3+
*
4+
* Copyright (c) 2012-2014 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+
* http://glassfish.java.net/public/CDDL+GPL_1_1.html
12+
* or packager/legal/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 packager/legal/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+
package org.glassfish.jersey.tests.integration.jersey2704;
41+
42+
import javax.inject.Inject;
43+
import javax.ws.rs.GET;
44+
import javax.ws.rs.Path;
45+
import javax.ws.rs.PathParam;
46+
import javax.ws.rs.Produces;
47+
import javax.ws.rs.core.Response;
48+
49+
import org.glassfish.hk2.api.ServiceLocator;
50+
51+
52+
/**
53+
* This resource is used to test if specific service class instance is available in the
54+
* {@link ServiceLocator} that comes from Jersey context.
55+
*
56+
* @author Bartosz Firyn (bartoszfiryn at gmail.com)
57+
*/
58+
@Path("test")
59+
public class TestResource {
60+
61+
ServiceLocator locator;
62+
63+
/**
64+
* Inject {@link ServiceLocator} from Jersey context.
65+
*
66+
* @param locator the {@link ServiceLocator}
67+
*/
68+
@Inject
69+
public TestResource(ServiceLocator locator) {
70+
this.locator = locator;
71+
}
72+
73+
/**
74+
* This method will test given class by checking if it is available in {@link ServiceLocator}
75+
* that has been injected from the Jersey context.
76+
*
77+
* @param clazz the service class name to check
78+
* @return {@link Response} with status code 200 if service is available, 600 otherwise
79+
* @throws Exception in case when there are any error (e.g. class not exist)
80+
*/
81+
@GET
82+
@Path("{clazz}")
83+
@Produces("text/plain")
84+
public Response test(@PathParam("clazz") String clazz) throws Exception {
85+
return Response
86+
.status(locator.getService(Class.forName(clazz)) != null ? 200 : 600)
87+
.build();
88+
}
89+
}

0 commit comments

Comments
 (0)