Skip to content

Commit

Permalink
Performance improvement fixes for profile set and reload. Completing …
Browse files Browse the repository at this point in the history
…first two items in #142.
  • Loading branch information
sapessi committed Apr 6, 2018
1 parent 5d82f78 commit 0a81cde
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
*/
package com.amazonaws.serverless.proxy.spring;

import com.amazonaws.serverless.exceptions.ContainerInitializationException;
import com.amazonaws.serverless.proxy.internal.testutils.Timer;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
Expand Down Expand Up @@ -66,7 +67,7 @@ public class LambdaSpringApplicationInitializer extends HttpServlet implements W
private volatile boolean refreshContext = true;
@SuppressFBWarnings("SE_TRANSIENT_FIELD_NOT_RESTORED")
private transient volatile List<ServletContextListener> contextListeners;
private volatile ArrayList<String> springProfiles;
private volatile List<String> springProfiles;

// Dynamically instantiated properties
private volatile DispatcherServlet dispatcherServlet;
Expand Down Expand Up @@ -121,19 +122,19 @@ public DispatcherServlet getDispatcherServlet() {
return dispatcherServlet;
}

public List<String> getSpringProfiles() {
return Collections.unmodifiableList(springProfiles);
}
public void setSpringProfiles(ServletContext ctx, String... springProfiles)
throws ContainerInitializationException {
this.springProfiles = Arrays.asList(springProfiles);

public void setSpringProfiles(List<String> springProfiles) {
this.springProfiles = new ArrayList<>(springProfiles);
applicationContext.stop();
applicationContext.registerShutdownHook();
applicationContext.close();
applicationContext.getEnvironment().setActiveProfiles(springProfiles.toArray(new String[0]));
//applicationContext.start();
applicationContext.refresh();
contextListeners.clear();
try {
onStartup(ctx);
} catch (ServletException e) {
throw new ContainerInitializationException("Could not reload Spring context", e);
}

//dispatcherServlet.refresh();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,28 @@ public static SpringBootLambdaContainerHandler<AwsProxyRequest, AwsProxyResponse
return newHandler;
}

/**
* Creates a default SpringLambdaContainerHandler initialized with the `AwsProxyRequest` and `AwsProxyResponse` objects and the given Spring profiles
* @param springBootInitializer {@code SpringBootServletInitializer} class
* @param profiles A list of Spring profiles to activate
* @return An initialized instance of the `SpringLambdaContainerHandler`
* @throws ContainerInitializationException If an error occurs while initializing the Spring framework
*/
public static SpringBootLambdaContainerHandler<AwsProxyRequest, AwsProxyResponse> getAwsProxyHandler(Class<? extends WebApplicationInitializer> springBootInitializer, String... profiles)
throws ContainerInitializationException {
SpringBootLambdaContainerHandler<AwsProxyRequest, AwsProxyResponse> newHandler = new SpringBootLambdaContainerHandler<>(
AwsProxyRequest.class,
AwsProxyResponse.class,
new AwsProxyHttpServletRequestReader(),
new AwsProxyHttpServletResponseWriter(),
new AwsProxySecurityContextWriter(),
new AwsProxyExceptionHandler(),
springBootInitializer);
newHandler.activateSpringProfiles(profiles);
newHandler.initialize();
return newHandler;
}

/**
* Creates a new container handler with the given reader and writer objects
*
Expand All @@ -100,6 +122,7 @@ public SpringBootLambdaContainerHandler(Class<RequestType> requestTypeClass,
super(requestTypeClass, responseTypeClass, requestReader, responseWriter, securityContextWriter, exceptionHandler);
Timer.start("SPRINGBOOT_CONTAINER_HANDLER_CONSTRUCTOR");
setServletContext(new SpringBootAwsServletContext());
initialized = false;
this.springBootInitializer = springBootInitializer;
Timer.stop("SPRINGBOOT_CONTAINER_HANDLER_CONSTRUCTOR");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,8 @@ public void activateSpringProfiles(String... profiles) throws ContainerInitializ
if (initializer == null) {
throw new ContainerInitializationException(LambdaSpringApplicationInitializer.ERROR_NO_CONTEXT, null);
}

initializer.setSpringProfiles(Arrays.asList(profiles));
setServletContext(new AwsServletContext(this));
initializer.setSpringProfiles(getServletContext(), profiles);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ public class SpringAwsProxyTest {
@Autowired
private SpringLambdaContainerHandler<AwsProxyRequest, AwsProxyResponse> handler;

/*@Before
@Before
public void clearServletContextCache() {
AwsServletContext.clearServletContextCache();
}*/
}

@Test
public void controllerAdvice_invalidPath_returnAdvice() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,19 +62,6 @@ public void profile_defaultProfile() throws Exception {
public void profile_overrideProfile() throws Exception {
AwsProxyRequest request = new AwsProxyRequestBuilder("/profile/spring-properties", "GET")
.build();
/*
This change works
AnnotationConfigWebApplicationContext applicationContext = new AnnotationConfigWebApplicationContext();
applicationContext.register(EchoSpringAppConfig.class);
SpringLambdaContainerHandler<AwsProxyRequest, AwsProxyResponse> handler = new SpringLambdaContainerHandler<>(AwsProxyRequest.class,
AwsProxyResponse.class,
new AwsProxyHttpServletRequestReader(),
new AwsProxyHttpServletResponseWriter(),
new AwsProxySecurityContextWriter(),
new AwsProxyExceptionHandler(), applicationContext);
handler.activateSpringProfiles("override");
handler.initialize();*/
SpringLambdaContainerHandler<AwsProxyRequest, AwsProxyResponse> handler = SpringLambdaContainerHandler.getAwsProxyHandler(EchoSpringAppConfig.class);
handler.activateSpringProfiles("override");
AwsProxyResponse output = handler.proxy(request, lambdaContext);
Expand Down

0 comments on commit 0a81cde

Please sign in to comment.