NissimShiman commented on code in PR #7781:
URL: https://github.com/apache/nifi/pull/7781#discussion_r1344571240


##########
nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/IdentifyMimeType.java:
##########
@@ -136,8 +151,11 @@ public class IdentifyMimeType extends AbstractProcessor {
     private List<PropertyDescriptor> properties;
 
     private final TikaConfig config;
-    private Detector detector;
-    private MimeTypes mimeTypes;
+    private volatile Detector defaultDetector;
+    private volatile MimeTypes defaultMimeTypes;
+
+    private volatile Detector customDetector;
+    private volatile MimeTypes customMimeTypes;

Review Comment:
   volatile removed.  Since we are using only one MimeTypes, using original 
variable names



##########
nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/IdentifyMimeType.java:
##########
@@ -161,31 +180,42 @@ protected void init(final ProcessorInitializationContext 
context) {
     public void setup(final ProcessContext context) {
         String configBody = context.getProperty(MIME_CONFIG_BODY).getValue();
         String configFile = 
context.getProperty(MIME_CONFIG_FILE).evaluateAttributeExpressions().getValue();
+        String configStrategy = 
context.getProperty(CONFIG_STRATEGY).getValue();
 
-        if (configBody == null && configFile == null){
-            this.detector = config.getDetector();
-            this.mimeTypes = config.getMimeRepository();
-        } else if (configBody != null) {
-            try {
-                this.detector = MimeTypesFactory.create(new 
ByteArrayInputStream(configBody.getBytes()));
-                this.mimeTypes = (MimeTypes)this.detector;
-            } catch (Exception e) {
-                context.yield();
-                throw new ProcessException("Failed to load config body", e);
-            }
-
+        if (configBody == null && configFile == null) {
+            setDefaultMimeTypes();
+        } else if (configStrategy.equals(ADD.getValue())) {
+            setDefaultMimeTypes();
+            setCustomMimeTypes(configBody, configFile, context);
         } else {
-            try (final FileInputStream fis = new FileInputStream(configFile);
-                 final InputStream bis = new BufferedInputStream(fis)) {
-                this.detector = MimeTypesFactory.create(bis);
-                this.mimeTypes = (MimeTypes)this.detector;
-            } catch (Exception e) {
-                context.yield();
-                throw new ProcessException("Failed to load config file", e);
-            }
+            setCustomMimeTypes(configBody, configFile, context);
         }
     }
 
+    private void setDefaultMimeTypes() {
+        this.defaultDetector = config.getDetector();
+        this.defaultMimeTypes = config.getMimeRepository();
+    }
+
+    private void setCustomMimeTypes(String configBody, String configFile, 
ProcessContext context) {
+        try (final InputStream customInputStream = configBody != null ? new 
ByteArrayInputStream(configBody.getBytes()) : new FileInputStream(configFile)) {
+            this.customDetector = MimeTypesFactory.create(customInputStream);
+            this.customMimeTypes = (MimeTypes) this.customDetector;
+        } catch (Exception e) {
+            context.yield();
+            String configSource = configBody != null ? "body" : "file";
+            throw new ProcessException("Failed to load config " + 
configSource, e);
+        }
+    }
+
+    @OnStopped
+    public void onStopped() {
+        defaultDetector = null;
+        defaultMimeTypes = null;
+
+        customDetector = null;
+        customMimeTypes = null;
+    }

Review Comment:
   Method removed



##########
nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/IdentifyMimeType.java:
##########
@@ -161,31 +180,42 @@ protected void init(final ProcessorInitializationContext 
context) {
     public void setup(final ProcessContext context) {
         String configBody = context.getProperty(MIME_CONFIG_BODY).getValue();
         String configFile = 
context.getProperty(MIME_CONFIG_FILE).evaluateAttributeExpressions().getValue();
+        String configStrategy = 
context.getProperty(CONFIG_STRATEGY).getValue();
 
-        if (configBody == null && configFile == null){
-            this.detector = config.getDetector();
-            this.mimeTypes = config.getMimeRepository();
-        } else if (configBody != null) {
-            try {
-                this.detector = MimeTypesFactory.create(new 
ByteArrayInputStream(configBody.getBytes()));
-                this.mimeTypes = (MimeTypes)this.detector;
-            } catch (Exception e) {
-                context.yield();
-                throw new ProcessException("Failed to load config body", e);
-            }
-
+        if (configBody == null && configFile == null) {
+            setDefaultMimeTypes();
+        } else if (configStrategy.equals(ADD.getValue())) {
+            setDefaultMimeTypes();
+            setCustomMimeTypes(configBody, configFile, context);
         } else {
-            try (final FileInputStream fis = new FileInputStream(configFile);
-                 final InputStream bis = new BufferedInputStream(fis)) {
-                this.detector = MimeTypesFactory.create(bis);
-                this.mimeTypes = (MimeTypes)this.detector;
-            } catch (Exception e) {
-                context.yield();
-                throw new ProcessException("Failed to load config file", e);
-            }
+            setCustomMimeTypes(configBody, configFile, context);
         }
     }
 
+    private void setDefaultMimeTypes() {
+        this.defaultDetector = config.getDetector();
+        this.defaultMimeTypes = config.getMimeRepository();
+    }
+
+    private void setCustomMimeTypes(String configBody, String configFile, 
ProcessContext context) {
+        try (final InputStream customInputStream = configBody != null ? new 
ByteArrayInputStream(configBody.getBytes()) : new FileInputStream(configFile)) {
+            this.customDetector = MimeTypesFactory.create(customInputStream);
+            this.customMimeTypes = (MimeTypes) this.customDetector;
+        } catch (Exception e) {
+            context.yield();
+            String configSource = configBody != null ? "body" : "file";
+            throw new ProcessException("Failed to load config " + 
configSource, e);
+        }
+    }

Review Comment:
   Done



-- 
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