com.amazonaws.services.lambda.runtime.api.client.PojoSerializerLoader Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of aws-lambda-java-runtime-interface-client Show documentation
Show all versions of aws-lambda-java-runtime-interface-client Show documentation
The AWS Lambda Java Runtime Interface Client implements the Lambda programming model for Java
/* Copyright 2023 Amazon.com, Inc. or its affiliates. All Rights Reserved. */
package com.amazonaws.services.lambda.runtime.api.client;
import com.amazonaws.services.lambda.runtime.serialization.PojoSerializer;
import com.amazonaws.services.lambda.runtime.CustomPojoSerializer;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.Type;
import java.util.Iterator;
import java.util.ServiceConfigurationError;
import java.util.ServiceLoader;
public class PojoSerializerLoader {
// The serializer obtained from the provider will always be the same so we can cache it as a filed.
private static CustomPojoSerializer customPojoSerializer;
// If Input and Output type are different, the runtime will try to search for a serializer twice due to
// the getSerializerCached method. Save the initialization state in order to search for the provider only once.
private static boolean initialized = false;
private static CustomPojoSerializer loadSerializer()
throws ServiceConfigurationError, TooManyServiceProvidersFoundException {
if (customPojoSerializer != null) {
return customPojoSerializer;
}
ServiceLoader loader = ServiceLoader.load(CustomPojoSerializer.class, AWSLambda.customerClassLoader);
Iterator serializers = loader.iterator();
if (!serializers.hasNext()) {
initialized = true;
return null;
}
customPojoSerializer = serializers.next();
if (serializers.hasNext()) {
throw new TooManyServiceProvidersFoundException(
"Too many serializers provided inside the META-INF/services folder, only one is allowed"
);
}
initialized = true;
return customPojoSerializer;
}
public static PojoSerializer