You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by cz...@apache.org on 2018/02/02 13:33:34 UTC

[sling-org-apache-sling-installer-core] branch master updated: SLING-7466 : EntityResourceList cleaning up resource instead of updating

This is an automated email from the ASF dual-hosted git repository.

cziegeler pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-installer-core.git


The following commit(s) were added to refs/heads/master by this push:
     new 0897a1d  SLING-7466 : EntityResourceList cleaning up resource instead of updating
0897a1d is described below

commit 0897a1d5297bfbfb14fce02e17d184177e245fe7
Author: Carsten Ziegeler <cz...@adobe.com>
AuthorDate: Fri Feb 2 14:33:29 2018 +0100

    SLING-7466 : EntityResourceList cleaning up resource instead of updating
---
 .../installer/core/impl/EntityResourceList.java    | 37 +++++++++++-----------
 .../core/impl/RegisteredResourceImpl.java          | 30 ++++++++++++++----
 2 files changed, 42 insertions(+), 25 deletions(-)

diff --git a/src/main/java/org/apache/sling/installer/core/impl/EntityResourceList.java b/src/main/java/org/apache/sling/installer/core/impl/EntityResourceList.java
index e143af6..ed11429 100644
--- a/src/main/java/org/apache/sling/installer/core/impl/EntityResourceList.java
+++ b/src/main/java/org/apache/sling/installer/core/impl/EntityResourceList.java
@@ -56,7 +56,7 @@ public class EntityResourceList implements Serializable, TaskResourceGroup {
     private static final Logger LOGGER = LoggerFactory.getLogger(EntityResourceList.class);
 
     /** The list of registered resources for this entity. */
-    private final List<RegisteredResourceImpl> resources = new ArrayList<RegisteredResourceImpl>();
+    private final List<RegisteredResourceImpl> resources = new ArrayList<>();
 
     /** Alias for this id. */
     private String alias;
@@ -353,7 +353,7 @@ public class EntityResourceList implements Serializable, TaskResourceGroup {
     public Collection<RegisteredResourceImpl> getResources() {
         final List<RegisteredResourceImpl> list;
         synchronized ( lock ) {
-            list = new ArrayList<RegisteredResourceImpl>(this.resources);
+            list = new ArrayList<>(this.resources);
         }
         Collections.sort(list);
         return list;
@@ -372,21 +372,22 @@ public class EntityResourceList implements Serializable, TaskResourceGroup {
                 final TaskResource rr = taskIter.next();
                 if ( rr.getURL().equals(r.getURL()) ) {
                     if ( RegisteredResourceImpl.isSameResource((RegisteredResourceImpl)rr, r) ) {
+                        // this check is questionable. The digest could be the same and
+                        // the resource URI might have changed nevertheless
                         if ( !rr.getDigest().equals(r.getDigest()) ) {
-                            // same resource but different digest, we need to to update the file
-                            LOGGER.debug("Updating resource with due to different digest: {}", r);
-                            try {
-								InternalResource intRes = InternalResource.create(r.getScheme(), 
-										new InstallableResource(r.getEntityId(), 
-												r.getInputStream(), 
-												r.getDictionary(),
-												r.getDigest(), 
-												r.getType(), 
-												r.getPriority()));
-								((RegisteredResourceImpl)rr).update(intRes);
-                            } catch (IOException e) {
-								LOGGER.error("Failed to update resource with different digest: {}", r);
-							}
+                            // same resource but different digest
+                            LOGGER.debug("Updating resource due to different digests: {} vs {}", r, rr);
+
+                            // we need to check the resource URI if provided (aka getDataURI())
+                            // if different that one needs to be updated
+                            // this update only handles the following cases:
+                            // 1. existing resource has a resource URI and new resource has a resource URI
+                            // 2. existing resource has a file and new resource has a resource URI
+                            // It doesn't necessarily handle:
+                            // 1. existing resource has a resource URI and new resource has a file
+                            // 2. existing resource has a file and new resource has a file
+                            ((RegisteredResourceImpl)rr).updateResourceUri(r.getDataURI());
+
                             LOGGER.debug("Cleanup duplicate resource: {}", r);
                             this.cleanup(r);
                         }
@@ -455,7 +456,7 @@ public class EntityResourceList implements Serializable, TaskResourceGroup {
         synchronized ( lock ) {
             Collections.sort(this.resources);
             boolean startNewCycle = false;
-            final List<TaskResource> toDelete = new ArrayList<TaskResource>();
+            final List<TaskResource> toDelete = new ArrayList<>();
             boolean first = true;
             for(final TaskResource r : resources) {
                 if ( r.getState() == ResourceState.UNINSTALLED || (!first && r.getState() == ResourceState.UNINSTALL) ) {
@@ -468,7 +469,7 @@ public class EntityResourceList implements Serializable, TaskResourceGroup {
                 // Avoid resources.remove(r) as the resource might have
                 // changed since it was added, which causes it to compare()
                 // differently and trip the TreeSet.remove() search.
-                final Set<RegisteredResourceImpl> copy = new HashSet<RegisteredResourceImpl>(resources);
+                final Set<RegisteredResourceImpl> copy = new HashSet<>(resources);
                 for(final RegisteredResource r : toDelete) {
                     copy.remove(r);
                     this.cleanup(r);
diff --git a/src/main/java/org/apache/sling/installer/core/impl/RegisteredResourceImpl.java b/src/main/java/org/apache/sling/installer/core/impl/RegisteredResourceImpl.java
index e1cf8e7..59d980d 100644
--- a/src/main/java/org/apache/sling/installer/core/impl/RegisteredResourceImpl.java
+++ b/src/main/java/org/apache/sling/installer/core/impl/RegisteredResourceImpl.java
@@ -69,7 +69,7 @@ public class RegisteredResourceImpl
 	private final Dictionary<String, Object> dictionary;
 
 	/** Additional attributes. */
-	private final Map<String, Object> attributes = new HashMap<String, Object>();
+	private final Map<String, Object> attributes = new HashMap<>();
 
 	private String dataUri;
 
@@ -250,12 +250,16 @@ public class RegisteredResourceImpl
 	    return this.dataFile;
 	}
 
-	/**
+    public String getDataURI() {
+        return this.dataUri;
+    }
+
+    /**
 	 * Remove the data file
 	 */
 	private void removeDataFile() {
         if ( this.dataFile != null && this.dataFile.exists() ) {
-            dataFile.delete();
+            this.dataFile.delete();
         }
         this.dataUri = null;
 	}
@@ -528,7 +532,7 @@ public class RegisteredResourceImpl
     @Override
     public void setTemporaryAttribute(final String key, final Object value) {
         if ( this.temporaryAttributes == null ) {
-            this.temporaryAttributes = new HashMap<String, Object>();
+            this.temporaryAttributes = new HashMap<>();
         }
         if ( value == null ) {
             this.temporaryAttributes.remove(key);
@@ -598,9 +602,7 @@ public class RegisteredResourceImpl
                 if ( this.dictionary != null ) {
                     this.dictionary.remove(InstallableResource.RESOURCE_URI_HINT);
                 }
-                if ( this.dataFile != null ) {
-                    this.removeDataFile();
-                }
+                this.removeDataFile();
                 this.dataFile = rsrc.getPrivateCopyOfFile();
                 FileDataStore.SHARED.updateDigestCache(this.url, this.dataFile, this.digest);
             }
@@ -608,6 +610,20 @@ public class RegisteredResourceImpl
     }
 
     /**
+     * Update the resource uri - if provided.
+     */
+    public void updateResourceUri(final String updatedResourceUri) {
+        if ( updatedResourceUri != null ) {
+            FileDataStore.SHARED.removeFromDigestCache(this.url, this.digest);
+            this.removeDataFile();
+            this.dataUri = updatedResourceUri;
+            if ( this.dictionary != null ) {
+                this.dictionary.put(InstallableResource.RESOURCE_URI_HINT, updatedResourceUri);
+            }
+        }
+    }
+
+    /**
      * Create a new resource with updated information
      */
     public TaskResource clone(TransformationResult transformationResult)

-- 
To stop receiving notification emails like this one, please contact
cziegeler@apache.org.