forked from javaee/jersey
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Register parent service locator in servlet context, closes JERSEY-2704
- Loading branch information
Showing
13 changed files
with
635 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,3 +28,4 @@ nb-configuration.xml | |
|
||
# Maven plugins noise | ||
dependency-reduced-pom.xml | ||
pom.xml.versionsBackup |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!-- | ||
DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. | ||
Copyright (c) 2011-2014 Oracle and/or its affiliates. All rights reserved. | ||
The contents of this file are subject to the terms of either the GNU | ||
General Public License Version 2 only ("GPL") or the Common Development | ||
and Distribution License("CDDL") (collectively, the "License"). You | ||
may not use this file except in compliance with the License. You can | ||
obtain a copy of the License at | ||
http://glassfish.java.net/public/CDDL+GPL_1_1.html | ||
or packager/legal/LICENSE.txt. See the License for the specific | ||
language governing permissions and limitations under the License. | ||
When distributing the software, include this License Header Notice in each | ||
file and include the License file at packager/legal/LICENSE.txt. | ||
GPL Classpath Exception: | ||
Oracle designates this particular file as subject to the "Classpath" | ||
exception as provided by Oracle in the GPL Version 2 section of the License | ||
file that accompanied this code. | ||
Modifications: | ||
If applicable, add the following below the License Header, with the fields | ||
enclosed by brackets [] replaced by your own identifying information: | ||
"Portions Copyright [year] [name of copyright owner]" | ||
Contributor(s): | ||
If you wish your version of this file to be governed by only the CDDL or | ||
only the GPL Version 2, indicate your decision by adding "[Contributor] | ||
elects to include this software in this distribution under the [CDDL or GPL | ||
Version 2] license." If you don't indicate a single choice of license, a | ||
recipient has the option to distribute your version of this file under | ||
either the CDDL, the GPL Version 2 or to extend the choice of license to | ||
its licensees as provided above. However, if you add GPL Version 2 code | ||
and therefore, elected the GPL Version 2 license, then the option applies | ||
only if the new code is made subject to such option by the copyright | ||
holder. | ||
--> | ||
<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.14-SNAPSHOT</version> | ||
</parent> | ||
|
||
<artifactId>jersey-2704</artifactId> | ||
<packaging>war</packaging> | ||
<name>jersey-tests-integration-jersey-2704</name> | ||
|
||
<description> | ||
This test is to verify if ServiceLocator can be configured within the server context | ||
in such a way that Jersey can use it as a parent ServiceLocator in the WebComponent. | ||
More details can be found in JERSEY-2704. | ||
</description> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.glassfish.jersey.containers</groupId> | ||
<artifactId>jersey-container-servlet-core</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.glassfish.jersey.test-framework.providers</groupId> | ||
<artifactId>jersey-test-framework-provider-external</artifactId> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
</plugin> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-failsafe-plugin</artifactId> | ||
</plugin> | ||
<plugin> | ||
<groupId>org.mortbay.jetty</groupId> | ||
<artifactId>jetty-maven-plugin</artifactId> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
</project> |
70 changes: 70 additions & 0 deletions
70
.../src/main/java/org/glassfish/jersey/tests/integration/jersey2704/ServiceLocatorSetup.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* | ||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. | ||
* | ||
* Copyright (c) 2012-2014 Oracle and/or its affiliates. All rights reserved. | ||
* | ||
* The contents of this file are subject to the terms of either the GNU | ||
* General Public License Version 2 only ("GPL") or the Common Development | ||
* and Distribution License("CDDL") (collectively, the "License"). You | ||
* may not use this file except in compliance with the License. You can | ||
* obtain a copy of the License at | ||
* http://glassfish.java.net/public/CDDL+GPL_1_1.html | ||
* or packager/legal/LICENSE.txt. See the License for the specific | ||
* language governing permissions and limitations under the License. | ||
* | ||
* When distributing the software, include this License Header Notice in each | ||
* file and include the License file at packager/legal/LICENSE.txt. | ||
* | ||
* GPL Classpath Exception: | ||
* Oracle designates this particular file as subject to the "Classpath" | ||
* exception as provided by Oracle in the GPL Version 2 section of the License | ||
* file that accompanied this code. | ||
* | ||
* Modifications: | ||
* If applicable, add the following below the License Header, with the fields | ||
* enclosed by brackets [] replaced by your own identifying information: | ||
* "Portions Copyright [year] [name of copyright owner]" | ||
* | ||
* Contributor(s): | ||
* If you wish your version of this file to be governed by only the CDDL or | ||
* only the GPL Version 2, indicate your decision by adding "[Contributor] | ||
* elects to include this software in this distribution under the [CDDL or GPL | ||
* Version 2] license." If you don't indicate a single choice of license, a | ||
* recipient has the option to distribute your version of this file under | ||
* either the CDDL, the GPL Version 2 or to extend the choice of license to | ||
* its licensees as provided above. However, if you add GPL Version 2 code | ||
* and therefore, elected the GPL Version 2 license, then the option applies | ||
* only if the new code is made subject to such option by the copyright | ||
* holder. | ||
*/ | ||
package org.glassfish.jersey.tests.integration.jersey2704; | ||
|
||
import javax.servlet.ServletContextEvent; | ||
import javax.servlet.ServletContextListener; | ||
|
||
import org.glassfish.hk2.api.ServiceLocator; | ||
import org.glassfish.hk2.utilities.ServiceLocatorUtilities; | ||
import org.glassfish.jersey.servlet.ServletProperties; | ||
import org.glassfish.jersey.tests.integration.jersey2704.services.HappyService; | ||
|
||
|
||
/** | ||
* This class is to listen for {@link ServletContextEvent} generated whenever context | ||
* is initialized and set {@link ServletProperties#SERVICE_LOCATOR} attribute to point | ||
* {@link ServiceLocator} pre-populated with {@link HappyService} instance. | ||
* | ||
* @author Bartosz Firyn (sarxos) | ||
*/ | ||
public class ServiceLocatorSetup implements ServletContextListener { | ||
|
||
@Override | ||
public void contextInitialized(ServletContextEvent event) { | ||
ServiceLocator locator = ServiceLocatorUtilities.createAndPopulateServiceLocator(); | ||
ServiceLocatorUtilities.addOneConstant(locator, new HappyService()); | ||
event.getServletContext().setAttribute(ServletProperties.SERVICE_LOCATOR, locator); | ||
} | ||
|
||
@Override | ||
public void contextDestroyed(ServletContextEvent event) { | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
...2704/src/main/java/org/glassfish/jersey/tests/integration/jersey2704/TestApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. | ||
* | ||
* Copyright (c) 2012-2014 Oracle and/or its affiliates. All rights reserved. | ||
* | ||
* The contents of this file are subject to the terms of either the GNU | ||
* General Public License Version 2 only ("GPL") or the Common Development | ||
* and Distribution License("CDDL") (collectively, the "License"). You | ||
* may not use this file except in compliance with the License. You can | ||
* obtain a copy of the License at | ||
* http://glassfish.java.net/public/CDDL+GPL_1_1.html | ||
* or packager/legal/LICENSE.txt. See the License for the specific | ||
* language governing permissions and limitations under the License. | ||
* | ||
* When distributing the software, include this License Header Notice in each | ||
* file and include the License file at packager/legal/LICENSE.txt. | ||
* | ||
* GPL Classpath Exception: | ||
* Oracle designates this particular file as subject to the "Classpath" | ||
* exception as provided by Oracle in the GPL Version 2 section of the License | ||
* file that accompanied this code. | ||
* | ||
* Modifications: | ||
* If applicable, add the following below the License Header, with the fields | ||
* enclosed by brackets [] replaced by your own identifying information: | ||
* "Portions Copyright [year] [name of copyright owner]" | ||
* | ||
* Contributor(s): | ||
* If you wish your version of this file to be governed by only the CDDL or | ||
* only the GPL Version 2, indicate your decision by adding "[Contributor] | ||
* elects to include this software in this distribution under the [CDDL or GPL | ||
* Version 2] license." If you don't indicate a single choice of license, a | ||
* recipient has the option to distribute your version of this file under | ||
* either the CDDL, the GPL Version 2 or to extend the choice of license to | ||
* its licensees as provided above. However, if you add GPL Version 2 code | ||
* and therefore, elected the GPL Version 2 license, then the option applies | ||
* only if the new code is made subject to such option by the copyright | ||
* holder. | ||
*/ | ||
package org.glassfish.jersey.tests.integration.jersey2704; | ||
|
||
import org.glassfish.jersey.server.ResourceConfig; | ||
|
||
|
||
/** | ||
* Jersey application. | ||
* | ||
* @author Bartosz Firyn (bartoszfiryn at gmail.com) | ||
*/ | ||
public class TestApplication extends ResourceConfig { | ||
|
||
public TestApplication() { | ||
register(TestResource.class); | ||
} | ||
} |
89 changes: 89 additions & 0 deletions
89
...ey-2704/src/main/java/org/glassfish/jersey/tests/integration/jersey2704/TestResource.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/* | ||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. | ||
* | ||
* Copyright (c) 2012-2014 Oracle and/or its affiliates. All rights reserved. | ||
* | ||
* The contents of this file are subject to the terms of either the GNU | ||
* General Public License Version 2 only ("GPL") or the Common Development | ||
* and Distribution License("CDDL") (collectively, the "License"). You | ||
* may not use this file except in compliance with the License. You can | ||
* obtain a copy of the License at | ||
* http://glassfish.java.net/public/CDDL+GPL_1_1.html | ||
* or packager/legal/LICENSE.txt. See the License for the specific | ||
* language governing permissions and limitations under the License. | ||
* | ||
* When distributing the software, include this License Header Notice in each | ||
* file and include the License file at packager/legal/LICENSE.txt. | ||
* | ||
* GPL Classpath Exception: | ||
* Oracle designates this particular file as subject to the "Classpath" | ||
* exception as provided by Oracle in the GPL Version 2 section of the License | ||
* file that accompanied this code. | ||
* | ||
* Modifications: | ||
* If applicable, add the following below the License Header, with the fields | ||
* enclosed by brackets [] replaced by your own identifying information: | ||
* "Portions Copyright [year] [name of copyright owner]" | ||
* | ||
* Contributor(s): | ||
* If you wish your version of this file to be governed by only the CDDL or | ||
* only the GPL Version 2, indicate your decision by adding "[Contributor] | ||
* elects to include this software in this distribution under the [CDDL or GPL | ||
* Version 2] license." If you don't indicate a single choice of license, a | ||
* recipient has the option to distribute your version of this file under | ||
* either the CDDL, the GPL Version 2 or to extend the choice of license to | ||
* its licensees as provided above. However, if you add GPL Version 2 code | ||
* and therefore, elected the GPL Version 2 license, then the option applies | ||
* only if the new code is made subject to such option by the copyright | ||
* holder. | ||
*/ | ||
package org.glassfish.jersey.tests.integration.jersey2704; | ||
|
||
import javax.inject.Inject; | ||
import javax.ws.rs.GET; | ||
import javax.ws.rs.Path; | ||
import javax.ws.rs.PathParam; | ||
import javax.ws.rs.Produces; | ||
import javax.ws.rs.core.Response; | ||
|
||
import org.glassfish.hk2.api.ServiceLocator; | ||
|
||
|
||
/** | ||
* This resource is used to test if specific service class instance is available in the | ||
* {@link ServiceLocator} that comes from Jersey context. | ||
* | ||
* @author Bartosz Firyn (bartoszfiryn at gmail.com) | ||
*/ | ||
@Path("test") | ||
public class TestResource { | ||
|
||
ServiceLocator locator; | ||
|
||
/** | ||
* Inject {@link ServiceLocator} from Jersey context. | ||
* | ||
* @param locator the {@link ServiceLocator} | ||
*/ | ||
@Inject | ||
public TestResource(ServiceLocator locator) { | ||
this.locator = locator; | ||
} | ||
|
||
/** | ||
* This method will test given class by checking if it is available in {@link ServiceLocator} | ||
* that has been injected from the Jersey context. | ||
* | ||
* @param clazz the service class name to check | ||
* @return {@link Response} with status code 200 if service is available, 600 otherwise | ||
* @throws Exception in case when there are any error (e.g. class not exist) | ||
*/ | ||
@GET | ||
@Path("{clazz}") | ||
@Produces("text/plain") | ||
public Response test(@PathParam("clazz") String clazz) throws Exception { | ||
return Response | ||
.status(locator.getService(Class.forName(clazz)) != null ? 200 : 600) | ||
.build(); | ||
} | ||
} |
Oops, something went wrong.