mark-bathori commented on code in PR #8250:
URL: https://github.com/apache/nifi/pull/8250#discussion_r1501869967


##########
nifi-nar-bundles/nifi-protobuf-bundle/nifi-protobuf-services/src/main/java/org/apache/nifi/services/protobuf/ProtobufReader.java:
##########
@@ -0,0 +1,136 @@
+/*
+ * 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.nifi.services.protobuf;
+
+import com.squareup.wire.schema.CoreLoaderKt;
+import com.squareup.wire.schema.Location;
+import com.squareup.wire.schema.Schema;
+import com.squareup.wire.schema.SchemaLoader;
+import org.apache.nifi.annotation.documentation.CapabilityDescription;
+import org.apache.nifi.annotation.documentation.Tags;
+import org.apache.nifi.annotation.lifecycle.OnEnabled;
+import org.apache.nifi.components.AllowableValue;
+import org.apache.nifi.components.PropertyDescriptor;
+import org.apache.nifi.context.PropertyContext;
+import org.apache.nifi.controller.ConfigurationContext;
+import org.apache.nifi.expression.ExpressionLanguageScope;
+import org.apache.nifi.logging.ComponentLog;
+import org.apache.nifi.processor.util.StandardValidators;
+import org.apache.nifi.schema.access.SchemaAccessStrategy;
+import org.apache.nifi.schema.access.SchemaAccessUtils;
+import org.apache.nifi.schema.access.SchemaNotFoundException;
+import org.apache.nifi.schemaregistry.services.SchemaRegistry;
+import org.apache.nifi.serialization.RecordReader;
+import org.apache.nifi.serialization.RecordReaderFactory;
+import org.apache.nifi.serialization.SchemaRegistryService;
+import org.apache.nifi.services.protobuf.schema.ProtoSchemaStrategy;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.nio.file.FileSystems;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+
+@Tags({"protobuf", "record", "reader", "parser"})
+@CapabilityDescription("Parses a Protocol Buffers message from binary format.")
+public class ProtobufReader extends SchemaRegistryService implements 
RecordReaderFactory {
+
+    private static final String ANY_PROTO = "google/protobuf/any.proto";
+    private static final String DURATION_PROTO = 
"google/protobuf/duration.proto";
+    private static final String EMPTY_PROTO = "google/protobuf/empty.proto";
+    private static final String STRUCT_PROTO = "google/protobuf/struct.proto";
+    private static final String TIMESTAMP_PROTO = 
"google/protobuf/timestamp.proto";
+    private static final String WRAPPERS_PROTO = 
"google/protobuf/wrappers.proto";
+
+    private static final AllowableValue GENERATE_FROM_PROTO_FILE = new 
AllowableValue("generate-from-proto-file",
+            "Generate from Proto file", "The record schema is generated from 
the provided proto file");
+
+    private String message;
+    private Schema protoSchema;
+
+    public static final PropertyDescriptor PROTOBUF_DIRECTORY = new 
PropertyDescriptor.Builder()
+            .name("Proto Directory")
+            .displayName("Proto Directory")
+            .description("Directory containing Protocol Buffers message 
definition (.proto) file(s).")
+            .required(true)
+            
.addValidator(StandardValidators.createDirectoryExistsValidator(true, false))
+            .expressionLanguageSupported(ExpressionLanguageScope.ENVIRONMENT)
+            .build();
+
+    public static final PropertyDescriptor MESSAGE_TYPE = new 
PropertyDescriptor.Builder()
+            .name("Message Type")
+            .displayName("Message Type")
+            .description("Fully qualified name of the Protocol Buffers message 
type including its package (eg. mypackage.MyMessage). " +
+                    "The .proto files configured in '" + 
PROTOBUF_DIRECTORY.getDisplayName() + "' must contain the definition of this 
message type.")
+            .required(true)
+            .addValidator(StandardValidators.NON_EMPTY_VALIDATOR)
+            .expressionLanguageSupported(ExpressionLanguageScope.ENVIRONMENT)
+            .build();
+
+    @Override
+    protected List<PropertyDescriptor> getSupportedPropertyDescriptors() {
+        final List<PropertyDescriptor> properties = new 
ArrayList<>(super.getSupportedPropertyDescriptors());
+        properties.add(PROTOBUF_DIRECTORY);
+        properties.add(MESSAGE_TYPE);
+        return properties;
+    }
+
+    @OnEnabled
+    public void onEnabled(final ConfigurationContext context) {
+        final String protoDirectory = 
context.getProperty(PROTOBUF_DIRECTORY).evaluateAttributeExpressions().getValue();
+        message = 
context.getProperty(MESSAGE_TYPE).evaluateAttributeExpressions().getValue();
+
+        final SchemaLoader schemaLoader = new 
SchemaLoader(FileSystems.getDefault());
+        schemaLoader.initRoots(Arrays.asList(Location.get(protoDirectory),
+                Location.get(CoreLoaderKt.WIRE_RUNTIME_JAR, ANY_PROTO),
+                Location.get(CoreLoaderKt.WIRE_RUNTIME_JAR, DURATION_PROTO),
+                Location.get(CoreLoaderKt.WIRE_RUNTIME_JAR, EMPTY_PROTO),
+                Location.get(CoreLoaderKt.WIRE_RUNTIME_JAR, STRUCT_PROTO),
+                Location.get(CoreLoaderKt.WIRE_RUNTIME_JAR, TIMESTAMP_PROTO),
+                Location.get(CoreLoaderKt.WIRE_RUNTIME_JAR, WRAPPERS_PROTO)), 
Collections.emptyList());
+        protoSchema = schemaLoader.loadSchema();
+    }
+
+    @Override
+    protected SchemaAccessStrategy getSchemaAccessStrategy(final String 
allowableValue, final SchemaRegistry schemaRegistry, final PropertyContext 
context) {

Review Comment:
   I've tested this by creating a Controller Service with name starting with 
letter 'Z' and the @OnEnabled methods were called in proper order. It seems 
like they are being called in class inheritance order.



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to