|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.apache.pulsar.functions.utils; |
| 20 | + |
| 21 | +import static org.apache.commons.lang.StringUtils.isEmpty; |
| 22 | +import com.google.gson.Gson; |
| 23 | +import com.google.gson.reflect.TypeToken; |
| 24 | +import java.lang.reflect.Constructor; |
| 25 | +import java.lang.reflect.InvocationTargetException; |
| 26 | +import java.lang.reflect.Type; |
| 27 | +import java.util.Map; |
| 28 | +import org.apache.pulsar.client.api.MessagePayloadProcessor; |
| 29 | +import org.apache.pulsar.common.functions.MessagePayloadProcessorConfig; |
| 30 | +import org.apache.pulsar.common.util.ClassLoaderUtils; |
| 31 | +import org.apache.pulsar.functions.proto.Function; |
| 32 | + |
| 33 | +public class MessagePayloadProcessorUtils { |
| 34 | + public static MessagePayloadProcessor getMessagePayloadProcessorInstance(String className, |
| 35 | + Map<String, Object> configs, |
| 36 | + ClassLoader classLoader) { |
| 37 | + Class<?> payloadProcessorClass; |
| 38 | + try { |
| 39 | + payloadProcessorClass = ClassLoaderUtils.loadClass(className, classLoader); |
| 40 | + } catch (ClassNotFoundException e) { |
| 41 | + throw new RuntimeException( |
| 42 | + String.format("Failed to load message payload processor class %sx", className)); |
| 43 | + } |
| 44 | + |
| 45 | + try { |
| 46 | + if (configs == null || configs.isEmpty()) { |
| 47 | + Constructor<?> ctor = payloadProcessorClass.getConstructor(); |
| 48 | + return (MessagePayloadProcessor) ctor.newInstance(); |
| 49 | + } else { |
| 50 | + Constructor<?> ctor = payloadProcessorClass.getConstructor(Map.class); |
| 51 | + return (MessagePayloadProcessor) ctor.newInstance(configs); |
| 52 | + } |
| 53 | + } catch (NoSuchMethodException e) { |
| 54 | + if (configs == null || configs.isEmpty()) { |
| 55 | + throw new RuntimeException("Message payload processor class does not have default constructor", e); |
| 56 | + } else { |
| 57 | + throw new RuntimeException("Message payload processor class does not have constructor accepts map", e); |
| 58 | + } |
| 59 | + } catch (IllegalAccessException | InstantiationException | InvocationTargetException e) { |
| 60 | + throw new RuntimeException("Failed to create instance for message payload processor class", e); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + public static MessagePayloadProcessorConfig convertFromSpec(Function.MessagePayloadProcessorSpec spec) { |
| 65 | + if (spec == null || isEmpty(spec.getClassName())) { |
| 66 | + return null; |
| 67 | + } |
| 68 | + |
| 69 | + MessagePayloadProcessorConfig.MessagePayloadProcessorConfigBuilder bldr = |
| 70 | + MessagePayloadProcessorConfig.builder(); |
| 71 | + |
| 72 | + Type type = new TypeToken<Map<String, Object>>() { |
| 73 | + }.getType(); |
| 74 | + Map<String, Object> configs = new Gson().fromJson(spec.getConfigs(), type); |
| 75 | + |
| 76 | + bldr.className(spec.getClassName()).config(configs); |
| 77 | + |
| 78 | + return bldr.build(); |
| 79 | + } |
| 80 | + |
| 81 | + public static Function.MessagePayloadProcessorSpec convert(MessagePayloadProcessorConfig config) { |
| 82 | + Function.MessagePayloadProcessorSpec.Builder bldr = Function.MessagePayloadProcessorSpec.newBuilder() |
| 83 | + .setClassName(config.getClassName()); |
| 84 | + |
| 85 | + if (config.getConfig() != null) { |
| 86 | + Type type = new TypeToken<Map<String, Object>>() { |
| 87 | + }.getType(); |
| 88 | + String readerConfigString = new Gson().toJson(config.getConfig(), type); |
| 89 | + bldr.setConfigs(readerConfigString); |
| 90 | + } |
| 91 | + |
| 92 | + return bldr.build(); |
| 93 | + } |
| 94 | +} |
0 commit comments