Allow overriding the service delegate (#5662)

* Added support for injecting service implementations via web.xml

* Updated samples
This commit is contained in:
Mads Hvelplund
2017-06-09 10:09:27 +02:00
committed by wing328
parent dabf01c3fa
commit 8f7e2a1563
5 changed files with 115 additions and 5 deletions

View File

@@ -18,6 +18,7 @@ import java.io.InputStream;
import org.glassfish.jersey.media.multipart.FormDataContentDisposition;
import org.glassfish.jersey.media.multipart.FormDataParam;
import javax.servlet.ServletConfig;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.SecurityContext;
@@ -33,7 +34,28 @@ import javax.validation.constraints.*;
{{>generatedAnnotation}}
{{#operations}}
public class {{classname}} {
private final {{classname}}Service delegate = {{classname}}ServiceFactory.get{{classname}}();
private final {{classname}}Service delegate;
public {{classname}}(@Context ServletConfig servletContext) {
{{classname}}Service delegate = null;
if (servletContext != null) {
String implClass = servletContext.getInitParameter("{{classname}}.implementation");
if (implClass != null && !"".equals(implClass.trim())) {
try {
delegate = ({{classname}}Service) Class.forName(implClass).newInstance();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
if (delegate == null) {
delegate = {{classname}}ServiceFactory.get{{classname}}();
}
this.delegate = delegate;
}
{{#operation}}
@{{httpMethod}}

View File

@@ -20,6 +20,7 @@ import java.io.InputStream;
import org.glassfish.jersey.media.multipart.FormDataContentDisposition;
import org.glassfish.jersey.media.multipart.FormDataParam;
import javax.servlet.ServletConfig;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.SecurityContext;
@@ -32,7 +33,28 @@ import javax.validation.constraints.*;
@io.swagger.annotations.Api(description = "the fake API")
public class FakeApi {
private final FakeApiService delegate = FakeApiServiceFactory.getFakeApi();
private final FakeApiService delegate;
public FakeApi(@Context ServletConfig servletContext) {
FakeApiService delegate = null;
if (servletContext != null) {
String implClass = servletContext.getInitParameter("FakeApi.implementation");
if (implClass != null && !"".equals(implClass.trim())) {
try {
delegate = (FakeApiService) Class.forName(implClass).newInstance();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
if (delegate == null) {
delegate = FakeApiServiceFactory.getFakeApi();
}
this.delegate = delegate;
}
@POST
@Path("/outer/boolean")

View File

@@ -19,6 +19,7 @@ import java.io.InputStream;
import org.glassfish.jersey.media.multipart.FormDataContentDisposition;
import org.glassfish.jersey.media.multipart.FormDataParam;
import javax.servlet.ServletConfig;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.SecurityContext;
@@ -31,7 +32,28 @@ import javax.validation.constraints.*;
@io.swagger.annotations.Api(description = "the pet API")
public class PetApi {
private final PetApiService delegate = PetApiServiceFactory.getPetApi();
private final PetApiService delegate;
public PetApi(@Context ServletConfig servletContext) {
PetApiService delegate = null;
if (servletContext != null) {
String implClass = servletContext.getInitParameter("PetApi.implementation");
if (implClass != null && !"".equals(implClass.trim())) {
try {
delegate = (PetApiService) Class.forName(implClass).newInstance();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
if (delegate == null) {
delegate = PetApiServiceFactory.getPetApi();
}
this.delegate = delegate;
}
@POST

View File

@@ -18,6 +18,7 @@ import java.io.InputStream;
import org.glassfish.jersey.media.multipart.FormDataContentDisposition;
import org.glassfish.jersey.media.multipart.FormDataParam;
import javax.servlet.ServletConfig;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.SecurityContext;
@@ -30,7 +31,28 @@ import javax.validation.constraints.*;
@io.swagger.annotations.Api(description = "the store API")
public class StoreApi {
private final StoreApiService delegate = StoreApiServiceFactory.getStoreApi();
private final StoreApiService delegate;
public StoreApi(@Context ServletConfig servletContext) {
StoreApiService delegate = null;
if (servletContext != null) {
String implClass = servletContext.getInitParameter("StoreApi.implementation");
if (implClass != null && !"".equals(implClass.trim())) {
try {
delegate = (StoreApiService) Class.forName(implClass).newInstance();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
if (delegate == null) {
delegate = StoreApiServiceFactory.getStoreApi();
}
this.delegate = delegate;
}
@DELETE
@Path("/order/{order_id}")

View File

@@ -18,6 +18,7 @@ import java.io.InputStream;
import org.glassfish.jersey.media.multipart.FormDataContentDisposition;
import org.glassfish.jersey.media.multipart.FormDataParam;
import javax.servlet.ServletConfig;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.SecurityContext;
@@ -30,7 +31,28 @@ import javax.validation.constraints.*;
@io.swagger.annotations.Api(description = "the user API")
public class UserApi {
private final UserApiService delegate = UserApiServiceFactory.getUserApi();
private final UserApiService delegate;
public UserApi(@Context ServletConfig servletContext) {
UserApiService delegate = null;
if (servletContext != null) {
String implClass = servletContext.getInitParameter("UserApi.implementation");
if (implClass != null && !"".equals(implClass.trim())) {
try {
delegate = (UserApiService) Class.forName(implClass).newInstance();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
if (delegate == null) {
delegate = UserApiServiceFactory.getUserApi();
}
this.delegate = delegate;
}
@POST