sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bdelacre...@apache.org
Subject svn commit: r1751251 - in /sling/trunk: bundles/extensions/repoinit/it/src/test/java/org/apache/sling/repoinit/it/ bundles/extensions/repoinit/jcr/src/main/java/org/apache/sling/repoinit/jcr/ bundles/extensions/repoinit/jcr/src/main/java/org/apache/sli...
Date Mon, 04 Jul 2016 11:38:33 GMT
Author: bdelacretaz
Date: Mon Jul  4 11:38:33 2016
New Revision: 1751251

URL: http://svn.apache.org/viewvc?rev=1751251&view=rev
Log:
SLING-5819 - jcr.repoinit bundle should only export what's strictly needed

Added:
    sling/trunk/bundles/extensions/repoinit/jcr/src/main/java/org/apache/sling/repoinit/jcr/JcrRepoInitOpsProcessor.java
    sling/trunk/bundles/extensions/repoinit/jcr/src/main/java/org/apache/sling/repoinit/jcr/impl/JcrRepoInitOperationVisitor.java
    sling/trunk/bundles/extensions/repoinit/jcr/src/main/java/org/apache/sling/repoinit/jcr/impl/JcrRepoInitOpsProcessorImpl.java
    sling/trunk/bundles/extensions/repoinit/jcr/src/main/java/org/apache/sling/repoinit/jcr/impl/RepositoryInitializer.java
      - copied, changed from r1751220, sling/trunk/bundles/extensions/repoinit/jcr/src/main/java/org/apache/sling/repoinit/jcr/RepositoryInitializer.java
    sling/trunk/bundles/extensions/repoinit/jcr/src/test/java/org/apache/sling/repoinit/jcr/impl/
    sling/trunk/bundles/extensions/repoinit/jcr/src/test/java/org/apache/sling/repoinit/jcr/impl/TestUtil.java
Removed:
    sling/trunk/bundles/extensions/repoinit/jcr/src/main/java/org/apache/sling/repoinit/jcr/JcrRepoInitOpVisitor.java
    sling/trunk/bundles/extensions/repoinit/jcr/src/main/java/org/apache/sling/repoinit/jcr/RepositoryInitializer.java
    sling/trunk/bundles/extensions/repoinit/jcr/src/test/java/org/apache/sling/repoinit/jcr/TestUtil.java
Modified:
    sling/trunk/bundles/extensions/repoinit/it/src/test/java/org/apache/sling/repoinit/it/RepoInitIT.java
    sling/trunk/bundles/extensions/repoinit/jcr/src/main/java/org/apache/sling/repoinit/jcr/package-info.java
    sling/trunk/bundles/extensions/repoinit/jcr/src/test/java/org/apache/sling/repoinit/jcr/CreatePathsTest.java
    sling/trunk/bundles/extensions/repoinit/jcr/src/test/java/org/apache/sling/repoinit/jcr/CreateServiceUsersTest.java
    sling/trunk/bundles/extensions/repoinit/jcr/src/test/java/org/apache/sling/repoinit/jcr/GeneralAclTest.java
    sling/trunk/bundles/extensions/repoinit/jcr/src/test/java/org/apache/sling/repoinit/jcr/NonExistentPathTest.java
    sling/trunk/launchpad/test-services/pom.xml
    sling/trunk/launchpad/test-services/src/main/java/org/apache/sling/launchpad/testservices/repository/SystemUsersInitializer.java

Modified: sling/trunk/bundles/extensions/repoinit/it/src/test/java/org/apache/sling/repoinit/it/RepoInitIT.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/repoinit/it/src/test/java/org/apache/sling/repoinit/it/RepoInitIT.java?rev=1751251&r1=1751250&r2=1751251&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/repoinit/it/src/test/java/org/apache/sling/repoinit/it/RepoInitIT.java
(original)
+++ sling/trunk/bundles/extensions/repoinit/it/src/test/java/org/apache/sling/repoinit/it/RepoInitIT.java
Mon Jul  4 11:38:33 2016
@@ -27,10 +27,8 @@ import javax.jcr.Session;
 
 import org.apache.sling.jcr.api.SlingRepository;
 import org.apache.sling.junit.rules.TeleporterRule;
-import org.apache.sling.repoinit.jcr.JcrRepoInitOpVisitor;
+import org.apache.sling.repoinit.jcr.JcrRepoInitOpsProcessor;
 import org.apache.sling.repoinit.parser.RepoInitParser;
-import org.apache.sling.repoinit.parser.operations.Operation;
-import org.apache.sling.repoinit.parser.operations.OperationVisitor;
 import org.junit.After;
 import org.junit.Before;
 import org.junit.Rule;
@@ -60,10 +58,8 @@ public class RepoInitIT {
         assertNotNull("Expecting " + REPO_INIT_FILE, is);
         try {
             final RepoInitParser  parser = teleporter.getService(RepoInitParser.class);
-            final OperationVisitor v = new JcrRepoInitOpVisitor(session);
-            for(Operation op : parser.parse(new InputStreamReader(is, "UTF-8"))) {
-                op.accept(v);
-            }
+            final JcrRepoInitOpsProcessor processor = teleporter.getService(JcrRepoInitOpsProcessor.class);
+            processor.apply(session, parser.parse(new InputStreamReader(is, "UTF-8")));
             session.save();
         } finally {
             is.close();

Added: sling/trunk/bundles/extensions/repoinit/jcr/src/main/java/org/apache/sling/repoinit/jcr/JcrRepoInitOpsProcessor.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/repoinit/jcr/src/main/java/org/apache/sling/repoinit/jcr/JcrRepoInitOpsProcessor.java?rev=1751251&view=auto
==============================================================================
--- sling/trunk/bundles/extensions/repoinit/jcr/src/main/java/org/apache/sling/repoinit/jcr/JcrRepoInitOpsProcessor.java
(added)
+++ sling/trunk/bundles/extensions/repoinit/jcr/src/main/java/org/apache/sling/repoinit/jcr/JcrRepoInitOpsProcessor.java
Mon Jul  4 11:38:33 2016
@@ -0,0 +1,28 @@
+/*
+ * 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.sling.repoinit.jcr;
+
+import java.util.List;
+
+import javax.jcr.Session;
+
+import org.apache.sling.repoinit.parser.operations.Operation;
+
+/** Apply Operations produced by the repoinit parser to a JCR Repository */
+public interface JcrRepoInitOpsProcessor {
+    void apply(Session session, List<Operation> ops);
+}

Added: sling/trunk/bundles/extensions/repoinit/jcr/src/main/java/org/apache/sling/repoinit/jcr/impl/JcrRepoInitOperationVisitor.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/repoinit/jcr/src/main/java/org/apache/sling/repoinit/jcr/impl/JcrRepoInitOperationVisitor.java?rev=1751251&view=auto
==============================================================================
--- sling/trunk/bundles/extensions/repoinit/jcr/src/main/java/org/apache/sling/repoinit/jcr/impl/JcrRepoInitOperationVisitor.java
(added)
+++ sling/trunk/bundles/extensions/repoinit/jcr/src/main/java/org/apache/sling/repoinit/jcr/impl/JcrRepoInitOperationVisitor.java
Mon Jul  4 11:38:33 2016
@@ -0,0 +1,142 @@
+/*
+ * 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.sling.repoinit.jcr.impl;
+
+import static org.apache.sling.repoinit.parser.operations.AclLine.PROP_PATHS;
+import static org.apache.sling.repoinit.parser.operations.AclLine.PROP_PRINCIPALS;
+import static org.apache.sling.repoinit.parser.operations.AclLine.PROP_PRIVILEGES;
+
+import java.util.List;
+
+import javax.jcr.Node;
+import javax.jcr.Session;
+
+import org.apache.sling.repoinit.parser.operations.AclLine;
+import org.apache.sling.repoinit.parser.operations.CreatePath;
+import org.apache.sling.repoinit.parser.operations.CreateServiceUser;
+import org.apache.sling.repoinit.parser.operations.DeleteServiceUser;
+import org.apache.sling.repoinit.parser.operations.OperationVisitor;
+import org.apache.sling.repoinit.parser.operations.PathSegmentDefinition;
+import org.apache.sling.repoinit.parser.operations.SetAclPaths;
+import org.apache.sling.repoinit.parser.operations.SetAclPrincipals;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/** JCR visitor for the Operations produced by the repoinit parser */
+class JcrRepoInitOperationVisitor implements OperationVisitor {
+
+    private final Logger log = LoggerFactory.getLogger(getClass());
+    
+    private Session session;
+    
+    /** Create a visitor using the supplied JCR Session.
+     * @param s must have sufficient rights to create users
+     *      and set ACLs.
+     */
+    public JcrRepoInitOperationVisitor(Session s) {
+        session = s;
+    }
+    
+    private void report(Exception e, String message) {
+        throw new RuntimeException(message, e);
+    }
+    
+    @Override
+    public void visitCreateServiceUser(CreateServiceUser s) {
+        final String id = s.getUsername();
+        try {
+            if(!ServiceUserUtil.serviceUserExists(session, id)) {
+                log.info("Creating service user {}", id);
+                ServiceUserUtil.createServiceUser(session, id);
+            } else {
+                log.info("Service user {} already exists, no changes made", id);
+            }
+        } catch(Exception e) {
+            report(e, "Unable to create service user [" + id + "]:" + e);
+        }
+    }
+
+    @Override
+    public void visitDeleteServiceUser(DeleteServiceUser s) {
+        final String id = s.getUsername();
+        log.info("Deleting service user {}", id);
+        try {
+            ServiceUserUtil.deleteServiceUser(session, id);
+        } catch(Exception e) {
+            report(e, "Unable to delete service user [" + id + "]:" + e);
+        }
+    }
+
+    private List<String> require(AclLine line, String propertyName) {
+        final List<String> result = line.getProperty(propertyName);
+        if(result == null) {
+            throw new IllegalStateException("Missing property " + propertyName + " on " +
line);
+        }
+        return result;
+    }
+    
+    private void setAcl(AclLine line, Session s, List<String> principals, List<String>
paths, List<String> privileges, boolean isAllow) {
+        try {
+            AclUtil.setAcl(s, principals, paths, privileges, isAllow);
+        } catch(Exception e) {
+            throw new RuntimeException("Failed to set ACL (" + e.toString() + ") " + line,
e);
+        }
+    }
+    
+    @Override
+    public void visitSetAclPrincipal(SetAclPrincipals s) {
+        final List<String> principals = s.getPrincipals();
+        for(AclLine line : s.getLines()) {
+            final boolean isAllow = line.getAction().equals(AclLine.Action.ALLOW);
+            setAcl(line, session, principals, require(line, PROP_PATHS), require(line, PROP_PRIVILEGES),
isAllow);
+        }
+     }
+
+    @Override
+    public void visitSetAclPaths(SetAclPaths s) {
+        final List<String> paths = s.getPaths();
+        for(AclLine line : s.getLines()) {
+            final boolean isAllow = line.getAction().equals(AclLine.Action.ALLOW);
+            setAcl(line, session, require(line, PROP_PRINCIPALS), paths, require(line, PROP_PRIVILEGES),
isAllow); 
+        }
+    }
+
+    @Override
+    public void visitCreatePath(CreatePath cp) {
+        String parentPath = "";
+            for(PathSegmentDefinition psd : cp.getDefinitions()) {
+                final String fullPath = parentPath + "/" + psd.getSegment();
+                try {
+                    if(session.itemExists(fullPath)) {
+                        log.info("Path already exists, nothing to do (and not checking its
primary type for now): {}", fullPath); 
+                    } else {
+                        final Node n = parentPath.equals("") ? session.getRootNode() : session.getNode(parentPath);
+                        log.info("Creating node {} with primary type {}", fullPath, psd.getPrimaryType());
+                        n.addNode(psd.getSegment(), psd.getPrimaryType());
+                    }
+                } catch(Exception e) {
+                    throw new RuntimeException("CreatePath execution failed at " + psd +
": " + e, e);
+                }
+                parentPath += "/" + psd.getSegment();
+            }
+        try {
+            session.save();
+        } catch(Exception e) {
+            throw new RuntimeException("Session.save failed: "+ e, e);
+        }
+    }
+}

Added: sling/trunk/bundles/extensions/repoinit/jcr/src/main/java/org/apache/sling/repoinit/jcr/impl/JcrRepoInitOpsProcessorImpl.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/repoinit/jcr/src/main/java/org/apache/sling/repoinit/jcr/impl/JcrRepoInitOpsProcessorImpl.java?rev=1751251&view=auto
==============================================================================
--- sling/trunk/bundles/extensions/repoinit/jcr/src/main/java/org/apache/sling/repoinit/jcr/impl/JcrRepoInitOpsProcessorImpl.java
(added)
+++ sling/trunk/bundles/extensions/repoinit/jcr/src/main/java/org/apache/sling/repoinit/jcr/impl/JcrRepoInitOpsProcessorImpl.java
Mon Jul  4 11:38:33 2016
@@ -0,0 +1,38 @@
+/*
+ * 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.sling.repoinit.jcr.impl;
+
+import java.util.List;
+
+import javax.jcr.Session;
+
+import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.Service;
+import org.apache.sling.repoinit.jcr.JcrRepoInitOpsProcessor;
+import org.apache.sling.repoinit.parser.operations.Operation;
+
+/** Apply Operations produced by the repoinit parser to a JCR Repository */
+@Component
+@Service(JcrRepoInitOpsProcessor.class)
+public class JcrRepoInitOpsProcessorImpl implements JcrRepoInitOpsProcessor {
+    public void apply(Session session, List<Operation> ops) {
+        final JcrRepoInitOperationVisitor v = new JcrRepoInitOperationVisitor(session);
+        for(Operation op : ops) {
+            op.accept(v);
+        }
+    }
+}

Copied: sling/trunk/bundles/extensions/repoinit/jcr/src/main/java/org/apache/sling/repoinit/jcr/impl/RepositoryInitializer.java
(from r1751220, sling/trunk/bundles/extensions/repoinit/jcr/src/main/java/org/apache/sling/repoinit/jcr/RepositoryInitializer.java)
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/repoinit/jcr/src/main/java/org/apache/sling/repoinit/jcr/impl/RepositoryInitializer.java?p2=sling/trunk/bundles/extensions/repoinit/jcr/src/main/java/org/apache/sling/repoinit/jcr/impl/RepositoryInitializer.java&p1=sling/trunk/bundles/extensions/repoinit/jcr/src/main/java/org/apache/sling/repoinit/jcr/RepositoryInitializer.java&r1=1751220&r2=1751251&rev=1751251&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/repoinit/jcr/src/main/java/org/apache/sling/repoinit/jcr/RepositoryInitializer.java
(original)
+++ sling/trunk/bundles/extensions/repoinit/jcr/src/main/java/org/apache/sling/repoinit/jcr/impl/RepositoryInitializer.java
Mon Jul  4 11:38:33 2016
@@ -14,7 +14,7 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-package org.apache.sling.repoinit.jcr;
+package org.apache.sling.repoinit.jcr.impl;
 
 import java.io.IOException;
 import java.io.InputStream;
@@ -22,6 +22,7 @@ import java.io.StringReader;
 import java.io.StringWriter;
 import java.net.URL;
 import java.net.URLConnection;
+import java.util.List;
 import java.util.Map;
 
 import javax.jcr.Session;
@@ -40,6 +41,7 @@ import org.apache.sling.provisioning.mod
 import org.apache.sling.provisioning.model.Model;
 import org.apache.sling.provisioning.model.Section;
 import org.apache.sling.provisioning.model.io.ModelReader;
+import org.apache.sling.repoinit.jcr.JcrRepoInitOpsProcessor;
 import org.apache.sling.repoinit.parser.RepoInitParser;
 import org.apache.sling.repoinit.parser.operations.Operation;
 import org.osgi.framework.Constants;
@@ -87,6 +89,9 @@ public class RepositoryInitializer imple
     @Reference
     private RepoInitParser parser;
     
+    @Reference
+    private JcrRepoInitOpsProcessor processor;
+    
     @Activate
     public void activate(Map<String, Object> config) {
         textURL = PropertiesUtil.toString(config.get(PROP_TEXT_URL), DEFAULT_TEXT_URL);
@@ -96,19 +101,14 @@ public class RepositoryInitializer imple
     @Override
     public void processRepository(SlingRepository repo) throws Exception {
         final String repoinit = getRepoInitText();
-        int count=0;
         
         // loginAdministrative is ok here, definitely an admin operation
         final Session s = repo.loginAdministrative(null);
-        final JcrRepoInitOpVisitor v = new JcrRepoInitOpVisitor(s);
         try {
-            for(Operation op : parser.parse(new StringReader(repoinit))) {
-                count++;
-                log.info("Executing {}", op);
-                op.accept(v);
-            }
+            final List<Operation> ops = parser.parse(new StringReader(repoinit));
+            log.info("Executing {} repoinit operations", ops.size());
+            processor.apply(s, ops);
             s.save();
-            log.info("{} repoinit operations executed", count);
         } finally {
             s.logout();
         }
@@ -164,4 +164,4 @@ public class RepositoryInitializer imple
         }
         return result;
     }
-}
\ No newline at end of file
+}

Modified: sling/trunk/bundles/extensions/repoinit/jcr/src/main/java/org/apache/sling/repoinit/jcr/package-info.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/repoinit/jcr/src/main/java/org/apache/sling/repoinit/jcr/package-info.java?rev=1751251&r1=1751250&r2=1751251&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/repoinit/jcr/src/main/java/org/apache/sling/repoinit/jcr/package-info.java
(original)
+++ sling/trunk/bundles/extensions/repoinit/jcr/src/main/java/org/apache/sling/repoinit/jcr/package-info.java
Mon Jul  4 11:38:33 2016
@@ -15,7 +15,7 @@
  * limitations under the License.
  ******************************************************************************/
 
-@Version("1.0.0")
+@Version("1.1.0")
 package org.apache.sling.repoinit.jcr;
 
 import aQute.bnd.annotation.Version;

Modified: sling/trunk/bundles/extensions/repoinit/jcr/src/test/java/org/apache/sling/repoinit/jcr/CreatePathsTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/repoinit/jcr/src/test/java/org/apache/sling/repoinit/jcr/CreatePathsTest.java?rev=1751251&r1=1751250&r2=1751251&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/repoinit/jcr/src/test/java/org/apache/sling/repoinit/jcr/CreatePathsTest.java
(original)
+++ sling/trunk/bundles/extensions/repoinit/jcr/src/test/java/org/apache/sling/repoinit/jcr/CreatePathsTest.java
Mon Jul  4 11:38:33 2016
@@ -21,6 +21,7 @@ import java.io.IOException;
 import javax.jcr.RepositoryException;
 
 import org.apache.sling.commons.testing.jcr.RepositoryUtil;
+import org.apache.sling.repoinit.jcr.impl.TestUtil;
 import org.apache.sling.testing.mock.sling.ResourceResolverType;
 import org.apache.sling.testing.mock.sling.junit.SlingContext;
 import org.junit.Before;

Modified: sling/trunk/bundles/extensions/repoinit/jcr/src/test/java/org/apache/sling/repoinit/jcr/CreateServiceUsersTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/repoinit/jcr/src/test/java/org/apache/sling/repoinit/jcr/CreateServiceUsersTest.java?rev=1751251&r1=1751250&r2=1751251&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/repoinit/jcr/src/test/java/org/apache/sling/repoinit/jcr/CreateServiceUsersTest.java
(original)
+++ sling/trunk/bundles/extensions/repoinit/jcr/src/test/java/org/apache/sling/repoinit/jcr/CreateServiceUsersTest.java
Mon Jul  4 11:38:33 2016
@@ -20,6 +20,7 @@ import java.util.Random;
 
 import javax.jcr.RepositoryException;
 
+import org.apache.sling.repoinit.jcr.impl.TestUtil;
 import org.apache.sling.testing.mock.sling.ResourceResolverType;
 import org.apache.sling.testing.mock.sling.junit.SlingContext;
 import org.junit.Before;

Modified: sling/trunk/bundles/extensions/repoinit/jcr/src/test/java/org/apache/sling/repoinit/jcr/GeneralAclTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/repoinit/jcr/src/test/java/org/apache/sling/repoinit/jcr/GeneralAclTest.java?rev=1751251&r1=1751250&r2=1751251&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/repoinit/jcr/src/test/java/org/apache/sling/repoinit/jcr/GeneralAclTest.java
(original)
+++ sling/trunk/bundles/extensions/repoinit/jcr/src/test/java/org/apache/sling/repoinit/jcr/GeneralAclTest.java
Mon Jul  4 11:38:33 2016
@@ -26,6 +26,7 @@ import javax.jcr.PathNotFoundException;
 import javax.jcr.RepositoryException;
 import javax.jcr.Session;
 
+import org.apache.sling.repoinit.jcr.impl.TestUtil;
 import org.apache.sling.repoinit.parser.RepoInitParsingException;
 import org.apache.sling.testing.mock.sling.ResourceResolverType;
 import org.apache.sling.testing.mock.sling.junit.SlingContext;

Modified: sling/trunk/bundles/extensions/repoinit/jcr/src/test/java/org/apache/sling/repoinit/jcr/NonExistentPathTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/repoinit/jcr/src/test/java/org/apache/sling/repoinit/jcr/NonExistentPathTest.java?rev=1751251&r1=1751250&r2=1751251&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/repoinit/jcr/src/test/java/org/apache/sling/repoinit/jcr/NonExistentPathTest.java
(original)
+++ sling/trunk/bundles/extensions/repoinit/jcr/src/test/java/org/apache/sling/repoinit/jcr/NonExistentPathTest.java
Mon Jul  4 11:38:33 2016
@@ -23,6 +23,7 @@ import javax.jcr.PathNotFoundException;
 import javax.jcr.RepositoryException;
 import javax.jcr.Session;
 
+import org.apache.sling.repoinit.jcr.impl.TestUtil;
 import org.apache.sling.repoinit.parser.RepoInitParsingException;
 import org.apache.sling.testing.mock.sling.ResourceResolverType;
 import org.apache.sling.testing.mock.sling.junit.SlingContext;

Added: sling/trunk/bundles/extensions/repoinit/jcr/src/test/java/org/apache/sling/repoinit/jcr/impl/TestUtil.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/repoinit/jcr/src/test/java/org/apache/sling/repoinit/jcr/impl/TestUtil.java?rev=1751251&view=auto
==============================================================================
--- sling/trunk/bundles/extensions/repoinit/jcr/src/test/java/org/apache/sling/repoinit/jcr/impl/TestUtil.java
(added)
+++ sling/trunk/bundles/extensions/repoinit/jcr/src/test/java/org/apache/sling/repoinit/jcr/impl/TestUtil.java
Mon Jul  4 11:38:33 2016
@@ -0,0 +1,114 @@
+/*
+ * 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.sling.repoinit.jcr.impl;
+
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.fail;
+
+import java.io.Reader;
+import java.io.StringReader;
+import java.util.List;
+import java.util.UUID;
+
+import javax.jcr.Node;
+import javax.jcr.RepositoryException;
+import javax.jcr.Session;
+import javax.jcr.SimpleCredentials;
+
+import org.apache.commons.io.IOUtils;
+import org.apache.jackrabbit.api.security.user.Authorizable;
+import org.apache.jackrabbit.api.security.user.User;
+import org.apache.sling.repoinit.jcr.impl.JcrRepoInitOperationVisitor;
+import org.apache.sling.repoinit.jcr.impl.ServiceUserUtil;
+import org.apache.sling.repoinit.parser.RepoInitParsingException;
+import org.apache.sling.repoinit.parser.impl.RepoInitParserService;
+import org.apache.sling.repoinit.parser.operations.Operation;
+import org.apache.sling.testing.mock.sling.junit.SlingContext;
+
+/** Test utilities */
+public class TestUtil {
+    
+    public static final String JCR_PRIMARY_TYPE = "jcr:primaryType";
+    public final Session adminSession;
+    public final String id;
+    public final String username;
+    
+    public TestUtil(SlingContext ctx) {
+        adminSession = ctx.resourceResolver().adaptTo(Session.class);
+        id = UUID.randomUUID().toString();
+        username = "user_" + id;
+    }
+    
+    public List<Operation> parse(String input) throws RepoInitParsingException {
+        final Reader r = new StringReader(input);
+        try {
+            return new RepoInitParserService().parse(r);
+        } finally {
+            IOUtils.closeQuietly(r);
+        }
+    }
+
+    public void assertServiceUser(String info, String id, boolean expectToExist) throws RepositoryException
{
+        final Authorizable a = ServiceUserUtil.getUserManager(adminSession).getAuthorizable(id);
+        if(!expectToExist) {
+            assertNull(info + ", expecting Principal to be absent:" + id, a);
+        } else {
+            assertNotNull(info + ", expecting Principal to exist:" + id, a);
+            final User u = (User)a;
+            assertNotNull(info + ", expecting Principal to be a System user:" + id, u.isSystemUser());
+        }
+    }
+    
+    public void assertNodeExists(String path) throws RepositoryException {
+        assertNodeExists(path, null);
+    }
+    
+    public void assertNodeExists(String path, String primaryType) throws RepositoryException
{
+        if(!adminSession.nodeExists(path)) {
+            fail("Node does not exist:" + path);
+        }
+        if(primaryType != null) {
+            final Node n = adminSession.getNode(path);
+            if(!n.hasProperty(JCR_PRIMARY_TYPE)) {
+                fail("No " + JCR_PRIMARY_TYPE + " property at " + path);
+            }
+            final String actual = n.getProperty(JCR_PRIMARY_TYPE).getString();
+            if(!primaryType.equals(actual)) {
+                fail("Primary type mismatch for " + path + ", expected " + primaryType +
" but got " + actual);
+            }
+        }
+    }
+    
+    public void parseAndExecute(String input) throws RepositoryException, RepoInitParsingException
{
+        final JcrRepoInitOperationVisitor v = new JcrRepoInitOperationVisitor(adminSession);
+        for(Operation o : parse(input)) {
+            o.accept(v);
+        }
+        adminSession.save();
+    }
+    
+    public void cleanupUser() throws RepositoryException, RepoInitParsingException {
+        parseAndExecute("delete service user " + username);
+        assertServiceUser("in cleanupUser()", username, false);
+    }
+    
+    public Session loginService(String serviceUsername) throws RepositoryException {
+        final SimpleCredentials cred = new SimpleCredentials(serviceUsername, new char[0]);
+        return adminSession.impersonate(cred);
+    }
+}

Modified: sling/trunk/launchpad/test-services/pom.xml
URL: http://svn.apache.org/viewvc/sling/trunk/launchpad/test-services/pom.xml?rev=1751251&r1=1751250&r2=1751251&view=diff
==============================================================================
--- sling/trunk/launchpad/test-services/pom.xml (original)
+++ sling/trunk/launchpad/test-services/pom.xml Mon Jul  4 11:38:33 2016
@@ -197,8 +197,8 @@
         </dependency>
         <dependency>
             <groupId>org.apache.sling</groupId>
-            <artifactId>org.apache.sling.repoinit.oak-jcr</artifactId>
-            <version>1.0.1-SNAPSHOT</version>
+            <artifactId>org.apache.sling.jcr.repoinit</artifactId>
+            <version>0.9.9-SNAPSHOT</version>
             <scope>provided</scope>
         </dependency>
         <dependency>

Modified: sling/trunk/launchpad/test-services/src/main/java/org/apache/sling/launchpad/testservices/repository/SystemUsersInitializer.java
URL: http://svn.apache.org/viewvc/sling/trunk/launchpad/test-services/src/main/java/org/apache/sling/launchpad/testservices/repository/SystemUsersInitializer.java?rev=1751251&r1=1751250&r2=1751251&view=diff
==============================================================================
--- sling/trunk/launchpad/test-services/src/main/java/org/apache/sling/launchpad/testservices/repository/SystemUsersInitializer.java
(original)
+++ sling/trunk/launchpad/test-services/src/main/java/org/apache/sling/launchpad/testservices/repository/SystemUsersInitializer.java
Mon Jul  4 11:38:33 2016
@@ -20,6 +20,7 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.io.Reader;
+import java.util.List;
 
 import javax.jcr.Session;
 
@@ -28,7 +29,7 @@ import org.apache.felix.scr.annotations.
 import org.apache.felix.scr.annotations.Service;
 import org.apache.sling.jcr.api.SlingRepository;
 import org.apache.sling.jcr.api.SlingRepositoryInitializer;
-import org.apache.sling.repoinit.jcr.JcrRepoInitOpVisitor;
+import org.apache.sling.repoinit.jcr.JcrRepoInitOpsProcessor;
 import org.apache.sling.repoinit.parser.RepoInitParser;
 import org.apache.sling.repoinit.parser.operations.Operation;
 import org.slf4j.Logger;
@@ -50,6 +51,9 @@ public class SystemUsersInitializer impl
     @Reference
     private RepoInitParser parser;
     
+    @Reference
+    private JcrRepoInitOpsProcessor processor;
+    
     @Override
     public void processRepository(SlingRepository repo) throws Exception {
         final Session s = repo.loginAdministrative(null);
@@ -59,14 +63,10 @@ public class SystemUsersInitializer impl
                 throw new IOException("Class Resource not found:" + REPOINIT_FILE);
             }
             final Reader r = new InputStreamReader(is, "UTF-8");
-            JcrRepoInitOpVisitor v = new JcrRepoInitOpVisitor(s);
-            int count = 0;
-            for(Operation op : parser.parse(r)) {
-                op.accept(v);
-                count++;
-            }
+            final List<Operation> ops = parser.parse(r); 
+            log.info("Executing {} repoinit Operations", ops.size());
+            processor.apply(s, ops);
             s.save();
-            log.info("{} repoinit Operations executed", count);
         } finally {
             s.logout();
             is.close();



Mime
View raw message