You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ro...@apache.org on 2015/02/18 16:36:18 UTC

svn commit: r1660658 - in /sling/trunk/tooling/ide: eclipse-core/src/org/apache/sling/ide/eclipse/core/internal/ eclipse-test/src/org/apache/sling/ide/test/impl/helpers/ impl-vlt-test/src/test/java/org/apache/sling/ide/impl/vlt/ impl-vlt/src/org/apache...

Author: rombert
Date: Wed Feb 18 15:36:18 2015
New Revision: 1660658

URL: http://svn.apache.org/r1660658
Log:
SLING-4069 - Intermediate resources that do not match the filter are not
created when publishing

Implement prerequisite node creation

Modified:
    sling/trunk/tooling/ide/eclipse-core/src/org/apache/sling/ide/eclipse/core/internal/ResourceAndInfo.java
    sling/trunk/tooling/ide/eclipse-core/src/org/apache/sling/ide/eclipse/core/internal/ResourceChangeCommandFactory.java
    sling/trunk/tooling/ide/eclipse-test/src/org/apache/sling/ide/test/impl/helpers/FailOnModificationEventsRule.java
    sling/trunk/tooling/ide/impl-vlt-test/src/test/java/org/apache/sling/ide/impl/vlt/AddOrUpdateNodeCommandTest.java
    sling/trunk/tooling/ide/impl-vlt/src/org/apache/sling/ide/impl/vlt/AddOrUpdateNodeCommand.java

Modified: sling/trunk/tooling/ide/eclipse-core/src/org/apache/sling/ide/eclipse/core/internal/ResourceAndInfo.java
URL: http://svn.apache.org/viewvc/sling/trunk/tooling/ide/eclipse-core/src/org/apache/sling/ide/eclipse/core/internal/ResourceAndInfo.java?rev=1660658&r1=1660657&r2=1660658&view=diff
==============================================================================
--- sling/trunk/tooling/ide/eclipse-core/src/org/apache/sling/ide/eclipse/core/internal/ResourceAndInfo.java (original)
+++ sling/trunk/tooling/ide/eclipse-core/src/org/apache/sling/ide/eclipse/core/internal/ResourceAndInfo.java Wed Feb 18 15:36:18 2015
@@ -27,10 +27,16 @@ import org.apache.sling.ide.transport.Re
 public class ResourceAndInfo {
     private final ResourceProxy resource;
     private final FileInfo info;
+    private final boolean onlyWhenMissing;
 
     public ResourceAndInfo(ResourceProxy resource, FileInfo info) {
+        this(resource, info, false);
+    }
+
+    public ResourceAndInfo(ResourceProxy resource, FileInfo info, boolean onlyIfMissing) {
         this.resource = resource;
         this.info = info;
+        this.onlyWhenMissing = onlyIfMissing;
     }
 
     public ResourceProxy getResource() {
@@ -40,4 +46,8 @@ public class ResourceAndInfo {
     public FileInfo getInfo() {
         return info;
     }
+
+    public boolean isOnlyWhenMissing() {
+        return onlyWhenMissing;
+    }
 }
\ No newline at end of file

Modified: sling/trunk/tooling/ide/eclipse-core/src/org/apache/sling/ide/eclipse/core/internal/ResourceChangeCommandFactory.java
URL: http://svn.apache.org/viewvc/sling/trunk/tooling/ide/eclipse-core/src/org/apache/sling/ide/eclipse/core/internal/ResourceChangeCommandFactory.java?rev=1660658&r1=1660657&r2=1660658&view=diff
==============================================================================
--- sling/trunk/tooling/ide/eclipse-core/src/org/apache/sling/ide/eclipse/core/internal/ResourceChangeCommandFactory.java (original)
+++ sling/trunk/tooling/ide/eclipse-core/src/org/apache/sling/ide/eclipse/core/internal/ResourceChangeCommandFactory.java Wed Feb 18 15:36:18 2015
@@ -92,6 +92,11 @@ public class ResourceChangeCommandFactor
             return null;
         }
 
+        if (rai.isOnlyWhenMissing()) {
+            return repository.newAddOrUpdateNodeCommand(rai.getInfo(), rai.getResource(),
+                    Repository.CommandExecutionFlag.CREATE_ONLY_WHEN_MISSING);
+        }
+
         return repository.newAddOrUpdateNodeCommand(rai.getInfo(), rai.getResource());
     }
 
@@ -193,11 +198,21 @@ public class ResourceChangeCommandFactor
 
         FilterResult filterResult = getFilterResult(resource, resourceProxy, filter);
 
-        if (filterResult != FilterResult.ALLOW) {
-            return null;
-        }
+        switch (filterResult) {
 
-        return new ResourceAndInfo(resourceProxy, info);
+            case ALLOW:
+                return new ResourceAndInfo(resourceProxy, info);
+            case PREREQUISITE:
+                // never try to 'create' the root node, we assume it exists
+                if (!resourceProxy.getPath().equals("/")) {
+                    // we don't explicitly set the primary type, which will allow the the repository to choose the best
+                    // suited one ( typically nt:unstructured )
+                    return new ResourceAndInfo(new ResourceProxy(resourceProxy.getPath()), null, true);
+                }
+            case DENY: // falls through
+            default:
+                return null;
+        }
     }
 
     private FileInfo createFileInfo(IResource resource) throws CoreException {
@@ -555,7 +570,7 @@ public class ResourceChangeCommandFactor
         try {
             ResourceAndInfo rai = buildResourceAndInfo(res, repository);
 
-            if (rai == null) {
+            if (rai == null || rai.isOnlyWhenMissing()) {
                 return null;
             }
 

Modified: sling/trunk/tooling/ide/eclipse-test/src/org/apache/sling/ide/test/impl/helpers/FailOnModificationEventsRule.java
URL: http://svn.apache.org/viewvc/sling/trunk/tooling/ide/eclipse-test/src/org/apache/sling/ide/test/impl/helpers/FailOnModificationEventsRule.java?rev=1660658&r1=1660657&r2=1660658&view=diff
==============================================================================
--- sling/trunk/tooling/ide/eclipse-test/src/org/apache/sling/ide/test/impl/helpers/FailOnModificationEventsRule.java (original)
+++ sling/trunk/tooling/ide/eclipse-test/src/org/apache/sling/ide/test/impl/helpers/FailOnModificationEventsRule.java Wed Feb 18 15:36:18 2015
@@ -25,6 +25,7 @@ import java.util.concurrent.CopyOnWriteA
 
 import org.apache.sling.ide.eclipse.core.internal.Activator;
 import org.apache.sling.ide.transport.CommandExecutionProperties;
+import org.apache.sling.ide.transport.Repository.CommandExecutionFlag;
 import org.junit.rules.TestRule;
 import org.junit.runner.Description;
 import org.junit.runners.model.Statement;
@@ -141,7 +142,13 @@ public class FailOnModificationEventsRul
 
         if ("AddOrUpdateNodeCommand".equals(type) || "ReorderChildNodesCommand".equals(type)
                 || "DeleteNodeCommand".equals(type)) {
-            unexpectedEvents.add(event);
+            String flags = (String) event.getProperty(CommandExecutionProperties.ACTION_FLAGS);
+
+            // it's OK to create prerequisites if needed
+            if (flags == null || !CommandExecutionFlag.CREATE_ONLY_WHEN_MISSING.toString().equals(flags)) {
+                unexpectedEvents.add(event);
+            }
+
         }
     }
 

Modified: sling/trunk/tooling/ide/impl-vlt-test/src/test/java/org/apache/sling/ide/impl/vlt/AddOrUpdateNodeCommandTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/tooling/ide/impl-vlt-test/src/test/java/org/apache/sling/ide/impl/vlt/AddOrUpdateNodeCommandTest.java?rev=1660658&r1=1660657&r2=1660658&view=diff
==============================================================================
--- sling/trunk/tooling/ide/impl-vlt-test/src/test/java/org/apache/sling/ide/impl/vlt/AddOrUpdateNodeCommandTest.java (original)
+++ sling/trunk/tooling/ide/impl-vlt-test/src/test/java/org/apache/sling/ide/impl/vlt/AddOrUpdateNodeCommandTest.java Wed Feb 18 15:36:18 2015
@@ -16,6 +16,7 @@
  */
 package org.apache.sling.ide.impl.vlt;
 
+import static org.apache.sling.ide.transport.Repository.CommandExecutionFlag.CREATE_ONLY_WHEN_MISSING;
 import static org.hamcrest.CoreMatchers.equalTo;
 import static org.junit.Assert.assertThat;
 
@@ -242,4 +243,90 @@ public class AddOrUpdateNodeCommandTest
         resource.addProperty("jcr:primaryType", primaryType);
         return resource;
     }
+
+    @Test
+    public void createIfRequiredFlagSkipsExistingResources() throws Exception {
+
+        File out = new File(new File("target"), "jackrabbit");
+        TransientRepository repo = new TransientRepository(new File(out, "repository.xml"), new File(out, "repository"));
+        SimpleCredentials credentials = new SimpleCredentials("admin", "admin".toCharArray());
+        Session session = repo.login(credentials);
+
+        try {
+            Node content = session.getRootNode().addNode("content", "nt:folder");
+
+            session.save();
+
+            ResourceProxy resource = newResource("/content", "nt:unstructured");
+
+            AddOrUpdateNodeCommand cmd = new AddOrUpdateNodeCommand(repo, credentials, null, resource, logger,
+                    CREATE_ONLY_WHEN_MISSING);
+            cmd.execute().get();
+
+            session.refresh(false);
+
+            content = session.getRootNode().getNode("content");
+            assertThat(content.getPrimaryNodeType().getName(), equalTo("nt:folder"));
+
+        } finally {
+            session.removeItem("/content");
+            session.save();
+            session.logout();
+        }
+    }
+
+    @Test
+    public void createIfRequiredFlagCreatesNeededResources() throws Exception {
+
+        File out = new File(new File("target"), "jackrabbit");
+        TransientRepository repo = new TransientRepository(new File(out, "repository.xml"), new File(out, "repository"));
+        SimpleCredentials credentials = new SimpleCredentials("admin", "admin".toCharArray());
+        Session session = repo.login(credentials);
+
+        try {
+            ResourceProxy resource = newResource("/content", "nt:unstructured");
+
+            AddOrUpdateNodeCommand cmd = new AddOrUpdateNodeCommand(repo, credentials, null, resource, logger,
+                    CREATE_ONLY_WHEN_MISSING);
+            cmd.execute().get();
+
+            session.refresh(false);
+
+            Node content = session.getRootNode().getNode("content");
+            assertThat(content.getPrimaryNodeType().getName(), equalTo("nt:unstructured"));
+
+        } finally {
+            session.removeItem("/content");
+            session.save();
+            session.logout();
+        }
+    }
+
+    @Test
+    public void createIfRequiredFlagCreatesNeededResourcesEvenWhenPrimaryTypeIsMissing() throws Exception {
+
+        File out = new File(new File("target"), "jackrabbit");
+        TransientRepository repo = new TransientRepository(new File(out, "repository.xml"), new File(out, "repository"));
+        SimpleCredentials credentials = new SimpleCredentials("admin", "admin".toCharArray());
+        Session session = repo.login(credentials);
+
+        try {
+            ResourceProxy resource = new ResourceProxy("/content");
+
+            AddOrUpdateNodeCommand cmd = new AddOrUpdateNodeCommand(repo, credentials, null, resource, logger,
+                    CREATE_ONLY_WHEN_MISSING);
+            cmd.execute().get();
+
+            session.refresh(false);
+
+            Node content = session.getRootNode().getNode("content");
+            assertThat(content.getPrimaryNodeType().getName(), equalTo("nt:unstructured"));
+
+        } finally {
+            if (session.itemExists("/content"))
+                session.removeItem("/content");
+            session.save();
+            session.logout();
+        }
+    }
 }

Modified: sling/trunk/tooling/ide/impl-vlt/src/org/apache/sling/ide/impl/vlt/AddOrUpdateNodeCommand.java
URL: http://svn.apache.org/viewvc/sling/trunk/tooling/ide/impl-vlt/src/org/apache/sling/ide/impl/vlt/AddOrUpdateNodeCommand.java?rev=1660658&r1=1660657&r2=1660658&view=diff
==============================================================================
--- sling/trunk/tooling/ide/impl-vlt/src/org/apache/sling/ide/impl/vlt/AddOrUpdateNodeCommand.java (original)
+++ sling/trunk/tooling/ide/impl-vlt/src/org/apache/sling/ide/impl/vlt/AddOrUpdateNodeCommand.java Wed Feb 18 15:36:18 2015
@@ -97,6 +97,10 @@ public class AddOrUpdateNodeCommand exte
             getLogger().trace("Created node at {0} with primaryType {1}", path, node.getPrimaryNodeType().getName());
         }
 
+        if (nodeExists && getFlags().contains(CommandExecutionFlag.CREATE_ONLY_WHEN_MISSING)) {
+            return;
+        }
+
         updateNode(node, resource);
         processDeletedNodes(node, resource);
 
@@ -155,13 +159,13 @@ public class AddOrUpdateNodeCommand exte
         }
 
         String primaryType = (String) resource.getProperties().get(JCR_PRIMARYTYPE);
-
+        Node parent = session.getNode(parentLocation);
+        String childName = PathUtil.getName(resource.getPath());
         if (primaryType == null) {
-            throw new IllegalArgumentException("Missing " + JCR_PRIMARYTYPE + " for ResourceProxy at path "
-                    + resource.getPath());
+            return parent.addNode(childName, primaryType);
+        } else {
+            return parent.addNode(childName);
         }
-
-        return session.getNode(parentLocation).addNode(PathUtil.getName(resource.getPath()), primaryType);
     }
 
     private void updateNode(Node node, ResourceProxy resource) throws RepositoryException, IOException {
@@ -200,7 +204,7 @@ public class AddOrUpdateNodeCommand exte
         }
         
         String primaryType = (String) resource.getProperties().get(JcrConstants.JCR_PRIMARYTYPE);
-        if (!node.getPrimaryNodeType().getName().equals(primaryType) && node.getDepth() != 0) {
+        if (primaryType != null && !node.getPrimaryNodeType().getName().equals(primaryType) && node.getDepth() != 0) {
             node.setPrimaryType(primaryType);
             session.save();
             getLogger().trace("Set new primary type {0} for node at {1}", primaryType, node.getPath());