NIFI-4864 - Additional improvements to additonal resource loading

This closes #2470.

Signed-off-by: Bryan Bende <bbe...@apache.org>


Project: http://git-wip-us.apache.org/repos/asf/nifi/repo
Commit: http://git-wip-us.apache.org/repos/asf/nifi/commit/9b098f32
Tree: http://git-wip-us.apache.org/repos/asf/nifi/tree/9b098f32
Diff: http://git-wip-us.apache.org/repos/asf/nifi/diff/9b098f32

Branch: refs/heads/master
Commit: 9b098f3235c05e1f47cbb109cffacbaf2baee398
Parents: fad152f
Author: Bryan Bende <bbe...@apache.org>
Authored: Mon Mar 12 15:51:02 2018 -0400
Committer: Bryan Bende <bbe...@apache.org>
Committed: Tue Mar 13 13:23:28 2018 -0400

----------------------------------------------------------------------
 .../util/file/classloader/ClassLoaderUtils.java | 12 +++----
 .../controller/AbstractConfiguredComponent.java | 37 +++++++-------------
 .../nifi/controller/ConfiguredComponent.java    |  6 ----
 .../apache/nifi/controller/FlowController.java  |  2 +-
 4 files changed, 19 insertions(+), 38 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/nifi/blob/9b098f32/nifi-commons/nifi-utils/src/main/java/org/apache/nifi/util/file/classloader/ClassLoaderUtils.java
----------------------------------------------------------------------
diff --git 
a/nifi-commons/nifi-utils/src/main/java/org/apache/nifi/util/file/classloader/ClassLoaderUtils.java
 
b/nifi-commons/nifi-utils/src/main/java/org/apache/nifi/util/file/classloader/ClassLoaderUtils.java
index ca114ce..fbf76bc 100644
--- 
a/nifi-commons/nifi-utils/src/main/java/org/apache/nifi/util/file/classloader/ClassLoaderUtils.java
+++ 
b/nifi-commons/nifi-utils/src/main/java/org/apache/nifi/util/file/classloader/ClassLoaderUtils.java
@@ -139,26 +139,24 @@ public class ClassLoaderUtils {
     }
 
     public static String generateAdditionalUrlsFingerprint(Set<URL> urls) {
-        MessageDigest md;
         List<String> listOfUrls = 
urls.stream().map(Object::toString).collect(Collectors.toList());
-        byte[] bytesOfAdditionalUrls, bytesOfDigest;
         StringBuffer urlBuffer = new StringBuffer();
 
         //Sorting so that the order is maintained for generating the 
fingerprint
         Collections.sort(listOfUrls);
         try {
-            md = MessageDigest.getInstance("MD5");
+            MessageDigest md = MessageDigest.getInstance("MD5");
             listOfUrls.forEach(url -> {
                 
urlBuffer.append(url).append("-").append(getLastModified(url)).append(";");
             });
-            bytesOfAdditionalUrls = urlBuffer.toString().getBytes("UTF-8");
-            bytesOfDigest = md.digest(bytesOfAdditionalUrls);
+            byte[] bytesOfAdditionalUrls = 
urlBuffer.toString().getBytes("UTF-8");
+            byte[] bytesOfDigest = md.digest(bytesOfAdditionalUrls);
 
             return DatatypeConverter.printHexBinary(bytesOfDigest);
         } catch (NoSuchAlgorithmException | UnsupportedEncodingException e) {
             LOGGER.error("Unable to generate fingerprint for the provided 
additional resources {}", new Object[]{urls, e});
+            return null;
         }
-        return null;
     }
 
     private static long getLastModified(String url) {
@@ -166,7 +164,7 @@ public class ClassLoaderUtils {
         try {
             file = new File(new URI(url));
         } catch (URISyntaxException e) {
-            e.printStackTrace();
+            LOGGER.error("Error getting last modified date for " + url);
         }
         return file != null ? file.lastModified() : 0;
     }

http://git-wip-us.apache.org/repos/asf/nifi/blob/9b098f32/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-core-api/src/main/java/org/apache/nifi/controller/AbstractConfiguredComponent.java
----------------------------------------------------------------------
diff --git 
a/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-core-api/src/main/java/org/apache/nifi/controller/AbstractConfiguredComponent.java
 
b/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-core-api/src/main/java/org/apache/nifi/controller/AbstractConfiguredComponent.java
index 34d0471..e51dd82 100644
--- 
a/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-core-api/src/main/java/org/apache/nifi/controller/AbstractConfiguredComponent.java
+++ 
b/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-core-api/src/main/java/org/apache/nifi/controller/AbstractConfiguredComponent.java
@@ -179,6 +179,8 @@ public abstract class AbstractConfiguredComponent 
implements ConfigurableCompone
 
                 // if at least one property with 
dynamicallyModifiesClasspath(true) was set, then reload the component with the 
new urls
                 if (classpathChanged) {
+                    logger.info("Updating classpath for " + this.componentType 
+ " with the ID " + this.getIdentifier());
+
                     final Set<URL> additionalUrls = 
getAdditionalClasspathResources(getComponent().getPropertyDescriptors());
                     try {
                         reload(additionalUrls);
@@ -303,24 +305,19 @@ public abstract class AbstractConfiguredComponent 
implements ConfigurableCompone
      * fingerprint value. If the fingerprint values don't match, the function 
calls the
      * component's reload() to load the newly found resources.
      */
-    public void reloadAdditionalResourcesIfNecessary(){
-        String oldFingerprint, newFingerprint;
-
+    @Override
+    public synchronized void reloadAdditionalResourcesIfNecessary() {
         final List<PropertyDescriptor> descriptors = new 
ArrayList<>(this.getProperties().keySet());
         final Set<URL> additionalUrls = 
this.getAdditionalClasspathResources(descriptors);
 
-        newFingerprint = 
ClassLoaderUtils.generateAdditionalUrlsFingerprint(additionalUrls);
-
-        if(this.hasAdditionalResourcesFingerprint()){
-            oldFingerprint = this.getAdditionalResourcesFingerprint();
-            if(!oldFingerprint.equals(newFingerprint)) {
-                this.setAdditionalResourcesFingerprint(newFingerprint);
-                try {
-                    logger.info("Adding new resources found to classpath for 
the component"+ this.componentType +" with the ID "+this.getIdentifier());
-                    reload(additionalUrls);
-                } catch (Exception e) {
-                    logger.error("Error reloading component with id " + id + 
": " + e.getMessage(), e);
-                }
+        final String newFingerprint = 
ClassLoaderUtils.generateAdditionalUrlsFingerprint(additionalUrls);
+        if(!StringUtils.equals(additionalResourcesFingerprint, 
newFingerprint)) {
+            setAdditionalResourcesFingerprint(newFingerprint);
+            try {
+                logger.info("Updating classpath for " + this.componentType + " 
with the ID " + this.getIdentifier());
+                reload(additionalUrls);
+            } catch (Exception e) {
+                logger.error("Error reloading component with id " + id + ": " 
+ e.getMessage(), e);
             }
         }
     }
@@ -597,15 +594,7 @@ public abstract class AbstractConfiguredComponent 
implements ConfigurableCompone
         }
     }
 
-    public String getAdditionalResourcesFingerprint() {
-        return additionalResourcesFingerprint;
-    }
-
-    public boolean hasAdditionalResourcesFingerprint() {
-        return !StringUtils.isEmpty(additionalResourcesFingerprint);
-    }
-
-    public void setAdditionalResourcesFingerprint(String 
additionalResourcesFingerprint) {
+    protected void setAdditionalResourcesFingerprint(String 
additionalResourcesFingerprint) {
         this.additionalResourcesFingerprint = additionalResourcesFingerprint;
     }
 

http://git-wip-us.apache.org/repos/asf/nifi/blob/9b098f32/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-core-api/src/main/java/org/apache/nifi/controller/ConfiguredComponent.java
----------------------------------------------------------------------
diff --git 
a/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-core-api/src/main/java/org/apache/nifi/controller/ConfiguredComponent.java
 
b/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-core-api/src/main/java/org/apache/nifi/controller/ConfiguredComponent.java
index 099ce6b..2271e81 100644
--- 
a/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-core-api/src/main/java/org/apache/nifi/controller/ConfiguredComponent.java
+++ 
b/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-core-api/src/main/java/org/apache/nifi/controller/ConfiguredComponent.java
@@ -81,12 +81,6 @@ public interface ConfiguredComponent extends 
ComponentAuthorizable {
 
     void verifyCanUpdateBundle(BundleCoordinate bundleCoordinate) throws 
IllegalStateException;
 
-    boolean hasAdditionalResourcesFingerprint();
-
-    String getAdditionalResourcesFingerprint();
-
-    void setAdditionalResourcesFingerprint(String newFingerprint);
-
     void reloadAdditionalResourcesIfNecessary();
 
     /**

http://git-wip-us.apache.org/repos/asf/nifi/blob/9b098f32/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-core/src/main/java/org/apache/nifi/controller/FlowController.java
----------------------------------------------------------------------
diff --git 
a/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-core/src/main/java/org/apache/nifi/controller/FlowController.java
 
b/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-core/src/main/java/org/apache/nifi/controller/FlowController.java
index 348f061..fbef291 100644
--- 
a/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-core/src/main/java/org/apache/nifi/controller/FlowController.java
+++ 
b/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-framework-core/src/main/java/org/apache/nifi/controller/FlowController.java
@@ -3447,8 +3447,8 @@ public class FlowController implements EventAccess, 
ControllerServiceProvider, R
         if (isTerminated()) {
             throw new IllegalStateException("Cannot start reporting task " + 
reportingTaskNode.getIdentifier() + " because the controller is terminated");
         }
-        reportingTaskNode.reloadAdditionalResourcesIfNecessary();
         reportingTaskNode.verifyCanStart();
+        reportingTaskNode.reloadAdditionalResourcesIfNecessary();
         processScheduler.schedule(reportingTaskNode);
     }
 

Reply via email to