Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
org.apache.pulsar.client.internal.DefaultImplementation Maven / Gradle / Ivy
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.pulsar.client.internal;
import static org.apache.pulsar.client.internal.ReflectionUtils.catchExceptions;
import static org.apache.pulsar.client.internal.ReflectionUtils.getConstructor;
import static org.apache.pulsar.client.internal.ReflectionUtils.getStaticMethod;
import static org.apache.pulsar.client.internal.ReflectionUtils.newClassInstance;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.sql.Time;
import java.sql.Timestamp;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.Date;
import java.util.Map;
import java.util.function.Supplier;
import lombok.experimental.UtilityClass;
import org.apache.pulsar.client.api.Authentication;
import org.apache.pulsar.client.api.BatcherBuilder;
import org.apache.pulsar.client.api.ClientBuilder;
import org.apache.pulsar.client.api.MessageId;
import org.apache.pulsar.client.api.PulsarClientException.UnsupportedAuthenticationException;
import org.apache.pulsar.client.api.Schema;
import org.apache.pulsar.client.api.schema.GenericRecord;
import org.apache.pulsar.client.api.schema.GenericSchema;
import org.apache.pulsar.client.api.schema.RecordSchemaBuilder;
import org.apache.pulsar.client.api.schema.SchemaDefinition;
import org.apache.pulsar.client.api.schema.SchemaDefinitionBuilder;
import org.apache.pulsar.common.schema.KeyValue;
import org.apache.pulsar.common.schema.KeyValueEncodingType;
import org.apache.pulsar.common.schema.SchemaInfo;
import org.apache.pulsar.common.schema.SchemaInfoWithVersion;
import org.apache.pulsar.common.schema.SchemaType;
/**
* Helper class for class instantiations and it also contains methods to work with schemas.
*/
@SuppressWarnings("unchecked")
@UtilityClass
public class DefaultImplementation {
private static final Class CLIENT_BUILDER_IMPL = newClassInstance(
"org.apache.pulsar.client.impl.ClientBuilderImpl");
private static final Constructor MESSAGE_ID_IMPL_long_long_int = getConstructor(
"org.apache.pulsar.client.impl.MessageIdImpl",
Long.TYPE, Long.TYPE, Integer.TYPE);
private static final Method MESSAGE_ID_IMPL_fromByteArray = getStaticMethod(
"org.apache.pulsar.client.impl.MessageIdImpl", "fromByteArray",
byte[].class);
private static final Method MESSAGE_ID_IMPL_fromByteArrayWithTopic = getStaticMethod(
"org.apache.pulsar.client.impl.MessageIdImpl",
"fromByteArrayWithTopic", byte[].class, String.class);
private static final Constructor AUTHENTICATION_TOKEN_String = getConstructor(
"org.apache.pulsar.client.impl.auth.AuthenticationToken", String.class);
private static final Constructor AUTHENTICATION_TOKEN_Supplier = getConstructor(
"org.apache.pulsar.client.impl.auth.AuthenticationToken", Supplier.class);
private static final Constructor AUTHENTICATION_TLS_String_String = getConstructor(
"org.apache.pulsar.client.impl.auth.AuthenticationTls", String.class, String.class);
private static final Constructor SCHEMA_DEFINITION_BUILDER_CONSTRUCTOR = getConstructor(
"org.apache.pulsar.client.impl.schema.SchemaDefinitionBuilderImpl");
public static SchemaDefinitionBuilder newSchemaDefinitionBuilder() {
return catchExceptions(() -> (SchemaDefinitionBuilder) SCHEMA_DEFINITION_BUILDER_CONSTRUCTOR.newInstance());
}
public static ClientBuilder newClientBuilder() {
return catchExceptions(() -> CLIENT_BUILDER_IMPL.newInstance());
}
public static MessageId newMessageId(long ledgerId, long entryId, int partitionIndex) {
return catchExceptions(() -> MESSAGE_ID_IMPL_long_long_int.newInstance(ledgerId, entryId, partitionIndex));
}
public static MessageId newMessageIdFromByteArray(byte[] data) {
return catchExceptions(() -> (MessageId) MESSAGE_ID_IMPL_fromByteArray.invoke(null, data));
}
public static MessageId newMessageIdFromByteArrayWithTopic(byte[] data, String topicName) {
return catchExceptions(() -> (MessageId) MESSAGE_ID_IMPL_fromByteArrayWithTopic.invoke(null, data, topicName));
}
public static Authentication newAuthenticationToken(String token) {
return catchExceptions(() -> AUTHENTICATION_TOKEN_String.newInstance(token));
}
public static Authentication newAuthenticationToken(Supplier supplier) {
return catchExceptions(() -> AUTHENTICATION_TOKEN_Supplier.newInstance(supplier));
}
public static Authentication newAuthenticationTLS(String certFilePath, String keyFilePath) {
return catchExceptions(
() -> AUTHENTICATION_TLS_String_String.newInstance(certFilePath, keyFilePath));
}
public static Authentication createAuthentication(String authPluginClassName, String authParamsString)
throws UnsupportedAuthenticationException {
return catchExceptions(
() -> (Authentication) getStaticMethod("org.apache.pulsar.client.impl.AuthenticationUtil", "create",
String.class, String.class)
.invoke(null, authPluginClassName, authParamsString));
}
public static Authentication createAuthentication(String authPluginClassName, Map authParams)
throws UnsupportedAuthenticationException {
return catchExceptions(
() -> (Authentication) getStaticMethod("org.apache.pulsar.client.impl.AuthenticationUtil", "create",
String.class, Map.class)
.invoke(null, authPluginClassName, authParams));
}
public static Schema newBytesSchema() {
return catchExceptions(
() -> (Schema) newClassInstance("org.apache.pulsar.client.impl.schema.BytesSchema")
.newInstance());
}
public static Schema newStringSchema() {
return catchExceptions(
() -> (Schema) newClassInstance("org.apache.pulsar.client.impl.schema.StringSchema")
.newInstance());
}
public static Schema newStringSchema(Charset charset) {
return catchExceptions(
() -> (Schema) getConstructor(
"org.apache.pulsar.client.impl.schema.StringSchema", Charset.class)
.newInstance(charset));
}
public static Schema newByteSchema() {
return catchExceptions(
() -> (Schema) newClassInstance("org.apache.pulsar.client.impl.schema.ByteSchema")
.newInstance());
}
public static Schema newShortSchema() {
return catchExceptions(
() -> (Schema) newClassInstance("org.apache.pulsar.client.impl.schema.ShortSchema")
.newInstance());
}
public static Schema newIntSchema() {
return catchExceptions(
() -> (Schema) newClassInstance("org.apache.pulsar.client.impl.schema.IntSchema")
.newInstance());
}
public static Schema newLongSchema() {
return catchExceptions(
() -> (Schema) newClassInstance("org.apache.pulsar.client.impl.schema.LongSchema")
.newInstance());
}
public static Schema newBooleanSchema() {
return catchExceptions(
() -> (Schema) newClassInstance("org.apache.pulsar.client.impl.schema.BooleanSchema")
.newInstance());
}
public static Schema newByteBufferSchema() {
return catchExceptions(
() -> (Schema) newClassInstance("org.apache.pulsar.client.impl.schema.ByteBufferSchema")
.newInstance());
}
public static Schema newFloatSchema() {
return catchExceptions(
() -> (Schema) newClassInstance("org.apache.pulsar.client.impl.schema.FloatSchema")
.newInstance());
}
public static Schema newDoubleSchema() {
return catchExceptions(
() -> (Schema) newClassInstance("org.apache.pulsar.client.impl.schema.DoubleSchema")
.newInstance());
}
public static Schema newDateSchema() {
return catchExceptions(
() -> (Schema) getStaticMethod(
"org.apache.pulsar.client.impl.schema.DateSchema", "of", null)
.invoke(null, null));
}
public static Schema newTimeSchema() {
return catchExceptions(
() -> (Schema) getStaticMethod(
"org.apache.pulsar.client.impl.schema.TimeSchema", "of", null)
.invoke(null, null));
}
public static Schema newTimestampSchema() {
return catchExceptions(
() -> (Schema) getStaticMethod(
"org.apache.pulsar.client.impl.schema.TimestampSchema", "of", null)
.invoke(null, null));
}
public static Schema newInstantSchema() {
return catchExceptions(
() -> (Schema) getStaticMethod(
"org.apache.pulsar.client.impl.schema.InstantSchema", "of", null)
.invoke(null, null));
}
public static Schema newLocalDateSchema() {
return catchExceptions(
() -> (Schema) getStaticMethod(
"org.apache.pulsar.client.impl.schema.LocalDateSchema", "of", null)
.invoke(null, null));
}
public static Schema newLocalTimeSchema() {
return catchExceptions(
() -> (Schema) getStaticMethod(
"org.apache.pulsar.client.impl.schema.LocalTimeSchema", "of", null)
.invoke(null, null));
}
public static Schema newLocalDateTimeSchema() {
return catchExceptions(
() -> (Schema) getStaticMethod(
"org.apache.pulsar.client.impl.schema.LocalDateTimeSchema", "of", null)
.invoke(null, null));
}
public static Schema newAvroSchema(SchemaDefinition schemaDefinition) {
return catchExceptions(
() -> (Schema) getStaticMethod(
"org.apache.pulsar.client.impl.schema.AvroSchema", "of", SchemaDefinition.class)
.invoke(null, schemaDefinition));
}
public static Schema newProtobufSchema(
SchemaDefinition schemaDefinition) {
return catchExceptions(
() -> (Schema) getStaticMethod(
"org.apache.pulsar.client.impl.schema.ProtobufSchema", "of", SchemaDefinition.class)
.invoke(null, schemaDefinition));
}
public static Schema newProtobufNativeSchema(
SchemaDefinition schemaDefinition) {
return catchExceptions(
() -> (Schema) getStaticMethod(
"org.apache.pulsar.client.impl.schema.ProtobufNativeSchema", "of", SchemaDefinition.class)
.invoke(null, schemaDefinition));
}
public static Schema newJSONSchema(SchemaDefinition schemaDefinition) {
return catchExceptions(
() -> (Schema) getStaticMethod(
"org.apache.pulsar.client.impl.schema.JSONSchema", "of", SchemaDefinition.class)
.invoke(null, schemaDefinition));
}
public static Schema newAutoConsumeSchema() {
return catchExceptions(
() -> (Schema) newClassInstance(
"org.apache.pulsar.client.impl.schema.AutoConsumeSchema")
.newInstance());
}
public static Schema newAutoProduceSchema() {
return catchExceptions(
() -> (Schema) newClassInstance(
"org.apache.pulsar.client.impl.schema.AutoProduceBytesSchema")
.newInstance());
}
public static Schema newAutoProduceSchema(Schema schema) {
return catchExceptions(
() -> (Schema) getConstructor(
"org.apache.pulsar.client.impl.schema.AutoProduceBytesSchema", Schema.class)
.newInstance(schema));
}
public static Schema> newKeyValueBytesSchema() {
return catchExceptions(
() -> (Schema>) getStaticMethod(
"org.apache.pulsar.client.impl.schema.KeyValueSchemaImpl",
"kvBytes").invoke(null));
}
public static Schema> newKeyValueSchema(Schema keySchema, Schema valueSchema) {
return catchExceptions(
() -> (Schema>) getStaticMethod(
"org.apache.pulsar.client.impl.schema.KeyValueSchemaImpl",
"of", Schema.class, Schema.class).invoke(null, keySchema, valueSchema));
}
public static Schema> newKeyValueSchema(Schema keySchema, Schema valueSchema,
KeyValueEncodingType keyValueEncodingType) {
return catchExceptions(
() -> (Schema>) getStaticMethod("org.apache.pulsar.client.impl.schema.KeyValueSchemaImpl",
"of", Schema.class, Schema.class, KeyValueEncodingType.class)
.invoke(null, keySchema, valueSchema, keyValueEncodingType));
}
public static Schema> newKeyValueSchema(Class key, Class value, SchemaType type) {
return catchExceptions(
() -> (Schema>) getStaticMethod("org.apache.pulsar.client.impl.schema.KeyValueSchemaImpl",
"of", Class.class, Class.class, SchemaType.class).invoke(null, key, value, type));
}
public static Schema getSchema(SchemaInfo schemaInfo) {
return catchExceptions(
() -> (Schema) getStaticMethod("org.apache.pulsar.client.impl.schema.AutoConsumeSchema",
"getSchema", SchemaInfo.class).invoke(null, schemaInfo));
}
public static GenericSchema getGenericSchema(SchemaInfo schemaInfo) {
switch (schemaInfo.getType()) {
case PROTOBUF_NATIVE:
return (GenericSchema) ReflectionUtils.catchExceptions(() -> {
return (GenericSchema) ReflectionUtils.getStaticMethod(
"org.apache.pulsar.client.impl.schema.generic.GenericProtobufNativeSchema",
"of", new Class[]{SchemaInfo.class}).invoke((Object) null, schemaInfo);
});
default:
return (GenericSchema) ReflectionUtils.catchExceptions(() -> {
return (GenericSchema) ReflectionUtils.getStaticMethod(
"org.apache.pulsar.client.impl.schema.generic.GenericSchemaImpl",
"of", new Class[]{SchemaInfo.class}).invoke((Object) null, schemaInfo);
});
}
}
public static RecordSchemaBuilder newRecordSchemaBuilder(String name) {
return catchExceptions(
() -> (RecordSchemaBuilder) getConstructor(
"org.apache.pulsar.client.impl.schema.RecordSchemaBuilderImpl",
String.class).newInstance(name));
}
/**
* Decode the kv encoding type from the schema info.
*
* @param schemaInfo the schema info
* @return the kv encoding type
*/
public static KeyValueEncodingType decodeKeyValueEncodingType(SchemaInfo schemaInfo) {
return catchExceptions(
() -> (KeyValueEncodingType) getStaticMethod("org.apache.pulsar.client.impl.schema.KeyValueSchemaInfo",
"decodeKeyValueEncodingType", SchemaInfo.class
).invoke(null, schemaInfo));
}
/**
* Encode key & value into schema into a KeyValue schema.
*
* @param keySchema the key schema
* @param valueSchema the value schema
* @param keyValueEncodingType the encoding type to encode and decode key value pair
* @return the final schema info
*/
public static SchemaInfo encodeKeyValueSchemaInfo(Schema keySchema,
Schema valueSchema,
KeyValueEncodingType keyValueEncodingType) {
return encodeKeyValueSchemaInfo("KeyValue", keySchema, valueSchema, keyValueEncodingType);
}
/**
* Encode key & value into schema into a KeyValue schema.
*
* @param schemaName the final schema name
* @param keySchema the key schema
* @param valueSchema the value schema
* @param keyValueEncodingType the encoding type to encode and decode key value pair
* @return the final schema info
*/
public static SchemaInfo encodeKeyValueSchemaInfo(String schemaName,
Schema keySchema,
Schema valueSchema,
KeyValueEncodingType keyValueEncodingType) {
return catchExceptions(
() -> (SchemaInfo) getStaticMethod("org.apache.pulsar.client.impl.schema.KeyValueSchemaInfo",
"encodeKeyValueSchemaInfo", String.class, Schema.class, Schema.class, KeyValueEncodingType.class
).invoke(null, schemaName, keySchema, valueSchema, keyValueEncodingType));
}
/**
* Decode the key/value schema info to get key schema info and value schema info.
*
* @param schemaInfo key/value schema info.
* @return the pair of key schema info and value schema info
*/
public static KeyValue decodeKeyValueSchemaInfo(SchemaInfo schemaInfo) {
return catchExceptions(
() -> (KeyValue) getStaticMethod(
"org.apache.pulsar.client.impl.schema.KeyValueSchemaInfo",
"decodeKeyValueSchemaInfo", SchemaInfo.class
).invoke(null, schemaInfo));
}
/**
* Jsonify the schema info.
*
* @param schemaInfo the schema info
* @return the jsonified schema info
*/
public static String jsonifySchemaInfo(SchemaInfo schemaInfo) {
return catchExceptions(
() -> (String) getStaticMethod("org.apache.pulsar.client.impl.schema.SchemaUtils",
"jsonifySchemaInfo", SchemaInfo.class
).invoke(null, schemaInfo));
}
/**
* Jsonify the schema info with version.
*
* @param schemaInfoWithVersion the schema info with version
* @return the jsonified schema info with version
*/
public static String jsonifySchemaInfoWithVersion(SchemaInfoWithVersion schemaInfoWithVersion) {
return catchExceptions(
() -> (String) getStaticMethod("org.apache.pulsar.client.impl.schema.SchemaUtils",
"jsonifySchemaInfoWithVersion", SchemaInfoWithVersion.class
).invoke(null, schemaInfoWithVersion));
}
/**
* Jsonify the key/value schema info.
*
* @param kvSchemaInfo the key/value schema info
* @return the jsonified schema info
*/
public static String jsonifyKeyValueSchemaInfo(KeyValue kvSchemaInfo) {
return catchExceptions(
() -> (String) getStaticMethod("org.apache.pulsar.client.impl.schema.SchemaUtils",
"jsonifyKeyValueSchemaInfo", KeyValue.class
).invoke(null, kvSchemaInfo));
}
/**
* Convert the key/value schema data.
*
* @param kvSchemaInfo the key/value schema info
* @return the convert key/value schema data string
*/
public static String convertKeyValueSchemaInfoDataToString(KeyValue kvSchemaInfo) {
return catchExceptions(
() -> (String) getStaticMethod("org.apache.pulsar.client.impl.schema.SchemaUtils",
"convertKeyValueSchemaInfoDataToString", KeyValue.class
).invoke(null, kvSchemaInfo));
}
/**
* Convert the key/value schema info data json bytes to key/value schema info data bytes.
*
* @param keyValueSchemaInfoDataJsonBytes the key/value schema info data json bytes
* @return the key/value schema info data bytes
*/
public static byte[] convertKeyValueDataStringToSchemaInfoSchema(byte[] keyValueSchemaInfoDataJsonBytes) {
return catchExceptions(
() -> (byte[]) getStaticMethod("org.apache.pulsar.client.impl.schema.SchemaUtils",
"convertKeyValueDataStringToSchemaInfoSchema", byte[].class
).invoke(null, keyValueSchemaInfoDataJsonBytes));
}
public static BatcherBuilder newDefaultBatcherBuilder() {
return catchExceptions(
() -> (BatcherBuilder) getConstructor("org.apache.pulsar.client.impl.DefaultBatcherBuilder")
.newInstance());
}
public static BatcherBuilder newKeyBasedBatcherBuilder() {
return catchExceptions(
() -> (BatcherBuilder) getConstructor("org.apache.pulsar.client.impl.KeyBasedBatcherBuilder")
.newInstance());
}
/**
* Retrieves ByteBuffer data into byte[].
*
* @param byteBuffer
* @return
*/
public static byte[] getBytes(ByteBuffer byteBuffer) {
if (byteBuffer == null) {
return null;
}
if (byteBuffer.hasArray() && byteBuffer.arrayOffset() == 0
&& byteBuffer.array().length == byteBuffer.remaining()) {
return byteBuffer.array();
}
// Direct buffer is not backed by array and it needs to be read from direct memory
byte[] array = new byte[byteBuffer.remaining()];
byteBuffer.get(array);
return array;
}
}