You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by GitBox <gi...@apache.org> on 2021/09/18 13:11:22 UTC

[GitHub] [pulsar] BewareMyPower commented on a change in pull request #12088: [PIP 96] Add payload converter for Pulsar client

BewareMyPower commented on a change in pull request #12088:
URL: https://github.com/apache/pulsar/pull/12088#discussion_r711588876



##########
File path: pulsar-client/src/main/java/org/apache/pulsar/client/converter/PayloadConverterProxy.java
##########
@@ -0,0 +1,85 @@
+/**
+ * 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.converter;
+
+import io.netty.buffer.ByteBuf;
+import java.util.Iterator;
+import java.util.Set;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.pulsar.common.api.proto.BrokerEntryMetadata;
+import org.apache.pulsar.common.api.proto.MessageMetadata;
+import org.apache.pulsar.common.api.raw.PayloadConverter;
+import org.reflections.Reflections;
+import org.reflections.scanners.SubTypesScanner;
+
+/**
+ * The proxy class for {@link PayloadConverter}.
+ */
+@Slf4j
+public class PayloadConverterProxy {
+
+    private final PayloadConverter payloadConverter;
+
+    public PayloadConverterProxy(final boolean enabled, final String packageName) {
+        this.payloadConverter = createConverter(enabled, packageName);
+    }
+
+    public ByteBuf convert(final BrokerEntryMetadata brokerEntryMetadata,
+                           final MessageMetadata metadata,
+                           final ByteBuf payload) {
+        return payloadConverter.convert(brokerEntryMetadata, metadata, payload);
+    }
+
+    private static PayloadConverter createConverter(final boolean enabled, final String packageName) {
+        if (!enabled) {
+            return (brokerEntryMetadata, metadata, payload) -> payload;
+        }
+
+        final Reflections reflections = new Reflections(packageName, new SubTypesScanner(false));
+        final Set<Class<? extends PayloadConverter>> converters = reflections.getSubTypesOf(PayloadConverter.class);
+        final Iterator<Class<? extends PayloadConverter>> iterator = converters.iterator();
+
+        PayloadConverter payloadConverter = null;
+        while (iterator.hasNext()) {
+            try {
+                final Class<? extends PayloadConverter> clazz = iterator.next();
+                if (!clazz.getPackage().getName().equals(packageName)) {
+                    // Ignore the sub packages
+                    continue;
+                }
+                payloadConverter = clazz.newInstance();

Review comment:
       I used `clazz.getConstructor().getInstance()` instead, because it only allows public constructor. PTAL again.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org