bejancsaba commented on code in PR #6075:
URL: https://github.com/apache/nifi/pull/6075#discussion_r891077359


##########
minifi/minifi-bootstrap/src/main/java/org/apache/nifi/minifi/bootstrap/service/MiNiFiConfigurationChangeListener.java:
##########
@@ -0,0 +1,215 @@
+/*
+ * 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.minifi.bootstrap.service;
+
+import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;
+import static org.apache.nifi.minifi.bootstrap.RunMiNiFi.CONF_DIR_KEY;
+import static 
org.apache.nifi.minifi.bootstrap.RunMiNiFi.MINIFI_CONFIG_FILE_KEY;
+import static 
org.apache.nifi.minifi.bootstrap.configuration.ingestors.PullHttpChangeIngestor.OVERRIDE_SECURITY;
+import static 
org.apache.nifi.minifi.bootstrap.configuration.ingestors.PullHttpChangeIngestor.PULL_HTTP_BASE_KEY;
+import static 
org.apache.nifi.minifi.bootstrap.util.ConfigTransformer.generateConfigFiles;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.io.InputStream;
+import java.nio.ByteBuffer;
+import java.nio.file.Files;
+import java.util.Properties;
+import java.util.concurrent.locks.ReentrantLock;
+import org.apache.commons.io.IOUtils;
+import org.apache.nifi.minifi.bootstrap.RunMiNiFi;
+import 
org.apache.nifi.minifi.bootstrap.configuration.ConfigurationChangeException;
+import 
org.apache.nifi.minifi.bootstrap.configuration.ConfigurationChangeListener;
+import org.apache.nifi.minifi.bootstrap.util.ByteBufferInputStream;
+import org.apache.nifi.minifi.bootstrap.util.ConfigTransformer;
+import org.apache.nifi.minifi.commons.schema.ConfigSchema;
+import org.apache.nifi.minifi.commons.schema.common.ConvertableSchema;
+import org.apache.nifi.minifi.commons.schema.serialization.SchemaLoader;
+import org.slf4j.Logger;
+
+public class MiNiFiConfigurationChangeListener implements 
ConfigurationChangeListener {
+
+    private final RunMiNiFi runner;
+    private final Logger logger;
+    private final BootstrapFileProvider bootstrapFileProvider;
+
+    private static final ReentrantLock handlingLock = new ReentrantLock();
+
+    public MiNiFiConfigurationChangeListener(RunMiNiFi runner, Logger logger, 
BootstrapFileProvider bootstrapFileProvider) {
+        this.runner = runner;
+        this.logger = logger;
+        this.bootstrapFileProvider = bootstrapFileProvider;
+    }
+
+    @Override
+    public void handleChange(InputStream configInputStream) throws 
ConfigurationChangeException {
+        logger.info("Received notification of a change");
+
+        if (!handlingLock.tryLock()) {
+            throw new ConfigurationChangeException("Instance is already 
handling another change");
+        }
+        // Store the incoming stream as a byte array to be shared among 
components that need it
+        try(ByteArrayOutputStream bufferedConfigOs = new 
ByteArrayOutputStream()) {
+
+            Properties bootstrapProperties = 
bootstrapFileProvider.getBootstrapProperties();
+            File configFile = new 
File(bootstrapProperties.getProperty(MINIFI_CONFIG_FILE_KEY));
+
+            byte[] copyArray = new byte[1024];
+            int available;
+            while ((available = configInputStream.read(copyArray)) > 0) {
+                bufferedConfigOs.write(copyArray, 0, available);
+            }
+
+            File swapConfigFile = bootstrapFileProvider.getSwapFile();
+            logger.info("Persisting old configuration to {}", 
swapConfigFile.getAbsolutePath());
+
+            try (FileInputStream configFileInputStream = new 
FileInputStream(configFile)) {
+                Files.copy(configFileInputStream, swapConfigFile.toPath(), 
REPLACE_EXISTING);
+            }
+
+            
persistBackNonFlowSectionsFromOriginalSchema(bufferedConfigOs.toByteArray(), 
bootstrapProperties, configFile);
+
+            // Create an input stream to feed to the config transformer
+            try (ByteArrayInputStream newConfigBais = new 
ByteArrayInputStream(IOUtils.toByteArray(new FileInputStream(configFile)))) {
+                newConfigBais.mark(-1);

Review Comment:
   You are right I went with FileInputStream (there were a few rewrites and 
code movement around here so the added pair of eyes is welcome)



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