public SparkLambdaContainerHandler()

in aws-serverless-java-container-spark/src/main/java/com/amazonaws/serverless/proxy/spark/SparkLambdaContainerHandler.java [151:194]


    public SparkLambdaContainerHandler(Class<RequestType> requestTypeClass,
                                       Class<ResponseType> responseTypeClass,
                                       RequestReader<RequestType, HttpServletRequest> requestReader,
                                       ResponseWriter<AwsHttpServletResponse, ResponseType> responseWriter,
                                       SecurityContextWriter<RequestType> securityContextWriter,
                                       ExceptionHandler<ResponseType> exceptionHandler,
                                       LambdaEmbeddedServerFactory embeddedServerFactory)
            throws ContainerInitializationException {
        super(requestTypeClass, responseTypeClass, requestReader, responseWriter, securityContextWriter, exceptionHandler);
        Timer.start("SPARK_CONTAINER_HANDLER_CONSTRUCTOR");

        EmbeddedServers.add(LAMBDA_EMBEDDED_SERVER_CODE, embeddedServerFactory);
        this.lambdaServerFactory = embeddedServerFactory;

        // TODO: This is pretty bad but we are not given access to the embeddedServerIdentifier property of the
        // Service object
        try {
            AccessController.doPrivileged((PrivilegedExceptionAction<Object>) () -> {
                log.debug("Changing visibility of getInstance method and embeddedServerIdentifier properties");
                Method serviceInstanceMethod = Spark.class.getDeclaredMethod("getInstance");
                serviceInstanceMethod.setAccessible(true);
                Service sparkService = (Service) serviceInstanceMethod.invoke(null);
                Field serverIdentifierField = Service.class.getDeclaredField("embeddedServerIdentifier");
                serverIdentifierField.setAccessible(true);
                serverIdentifierField.set(sparkService, LAMBDA_EMBEDDED_SERVER_CODE);
                return null;
            });
        } catch (PrivilegedActionException e) {
            if (e.getException() instanceof NoSuchFieldException) {
                log.error("Could not fine embeddedServerIdentifier field in Service class", e.getException());
            } else if (e.getException() instanceof NoSuchMethodException) {
                log.error("Could not find getInstance method in Spark class", e.getException());
            } else if (e.getException() instanceof IllegalAccessException) {
                log.error("Could not access getInstance method in Spark class", e.getException());
            } else if (e.getException() instanceof InvocationTargetException) {
                log.error("Could not invoke getInstance method in Spark class", e.getException());
            } else {
                log.error("Unknown exception while modifying Spark class", e.getException());
            }
            Timer.stop("SPARK_CONTAINER_HANDLER_CONSTRUCTOR");
            throw new ContainerInitializationException("Could not initialize Spark server", e.getException());
        }
        Timer.stop("SPARK_CONTAINER_HANDLER_CONSTRUCTOR");
    }