Skip to content

Commit

Permalink
Register parent service locator in servlet context, closes JERSEY-2704
Browse files Browse the repository at this point in the history
  • Loading branch information
sarxos committed Dec 2, 2014
1 parent 2a4d4cc commit bcf38a2
Show file tree
Hide file tree
Showing 13 changed files with 635 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,4 @@ nb-configuration.xml

# Maven plugins noise
dependency-reduced-pom.xml
pom.xml.versionsBackup
Original file line number Diff line number Diff line change
Expand Up @@ -588,4 +588,13 @@ public void reload(ResourceConfig configuration) {
public ApplicationHandler getApplicationHandler() {
return webComponent.appHandler;
}

/**
* Get {@link WebComponent} used by this servlet container.
*
* @return The web component.
*/
public WebComponent getWebComponent() {
return webComponent;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
*/
package org.glassfish.jersey.servlet;

import org.glassfish.hk2.api.ServiceLocator;
import org.glassfish.jersey.internal.util.PropertiesClass;

/**
Expand Down Expand Up @@ -147,6 +148,19 @@ public final class ServletProperties {
*/
public static final String PROVIDER_WEB_APP = "jersey.config.servlet.provider.webapp";

/**
* Identifies the object that will be used as a parent {@link ServiceLocator} in the Jersey
* {@link WebComponent}.
* <p></p>
* This property gives a possibility to use HK2 services that are registered and/or created
* outside of the Jersey server context.
* <p></p>
* By default this property is not set.
* <p></p>
* The name of the configuration property is <tt>{@value}</tt>.
*/
public static final String SERVICE_LOCATOR = "jersey.config.servlet.context.serviceLocator";

private ServletProperties() {
// prevents instantiation
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,9 @@ public void dispose(final WebConfig instance) {
* resource configuration.
*/
public WebComponent(final WebConfig webConfig, ResourceConfig resourceConfig) throws ServletException {

this.webConfig = webConfig;

if (resourceConfig == null) {
resourceConfig = createResourceConfig(webConfig);
}
Expand All @@ -308,7 +310,9 @@ public WebComponent(final WebConfig webConfig, ResourceConfig resourceConfig) th
final AbstractBinder webComponentBinder = new WebComponentBinder(resourceConfig.getProperties());
resourceConfig.register(webComponentBinder);

this.appHandler = new ApplicationHandler(resourceConfig, webComponentBinder);
ServiceLocator locator = (ServiceLocator) webConfig.getServletContext().getAttribute(ServletProperties.SERVICE_LOCATOR);

this.appHandler = new ApplicationHandler(resourceConfig, webComponentBinder, locator);

this.asyncExtensionDelegate = getAsyncExtensionDelegate();
this.forwardOn404 = webConfig.getConfigType().equals(WebConfig.ConfigType.FilterConfig)
Expand Down Expand Up @@ -562,4 +566,13 @@ private void filterFormParameters(final HttpServletRequest servletRequest, final
}
}
}

/**
* Get {@link ApplicationHandler} used by this web component.
*
* @return The application handler
*/
public ApplicationHandler getAppHandler() {
return appHandler;
}
}
90 changes: 90 additions & 0 deletions tests/integration/jersey-2704/pom.xml
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>
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) {
}
}
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);
}
}
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();
}
}
Loading

0 comments on commit bcf38a2

Please sign in to comment.