Skip to content

Instantly share code, notes, and snippets.

@alexkli
Last active June 25, 2024 16:30

Revisions

  1. alexkli revised this gist Apr 28, 2017. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion readme.md
    Original file line number Diff line number Diff line change
    @@ -12,10 +12,12 @@ This is a Groovy script that allows to render hidden Oak content structures with

    ### Example output

    Note: `(...)` after a node name indicates the depth limit stopped here.

    ```
    /var
    - jcr:primaryType = sling:Folder
    - jcr:mixinTypes = [rep:AccessControllable, ]
    - jcr:mixinTypes = [rep:AccessControllable]
    - jcr:createdBy = admin
    - jcr:created = 2013-07-08T13:25:36.725-04:00
    + discovery
  2. alexkli revised this gist Apr 28, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion readme.md
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    This is a Groovy script that allows to render hidden Oak content structures within the Sling script console. This is useful for inspecting the oak:index structures for examples. Hidden nodes in Oak start with ":".
    This is a Groovy script that allows to render hidden Oak content structures within the Sling script console on a running Oak/Sling system. This is useful for inspecting the oak:index structures for examples. Hidden nodes in Oak start with ":".

    ### Instructions

  3. alexkli revised this gist Apr 28, 2017. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions readme.md
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,5 @@
    This is a Groovy script that allows to render hidden Oak content structures within the Sling script console. This is useful for inspecting the oak:index structures for examples. Hidden nodes in Oak start with ":".

    ### Instructions

    1. Install [Sling Script Console bundle](http://central.maven.org/maven2/org/apache/sling/org.apache.sling.scripting.console/1.0.0/org.apache.sling.scripting.console-1.0.0.jar)
  4. alexkli revised this gist Apr 28, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion readme.md
    Original file line number Diff line number Diff line change
    @@ -4,7 +4,7 @@
    2. Install [Groovy Bundle](http://repo1.maven.org/maven2/org/codehaus/groovy/groovy-all/1.8.6/groovy-all-1.8.6.jar)
    3. Go to <http://localhost:4502/system/console/scriptconsole>
    4. Ensure "Groovy" is selected as language
    5. Copy & paste [oak-browse-tree.groovy](#file-oak-browse-tree-groovy)
    5. Copy & paste [oak-browse-tree.groovy](#file-oak-browse-tree-groovy) from this gist
    6. Adapt "path" and "depth" to what part of the repository you want to look at
    7. Hit "Execute"

  5. alexkli revised this gist Apr 28, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion readme.md
    Original file line number Diff line number Diff line change
    @@ -4,7 +4,7 @@
    2. Install [Groovy Bundle](http://repo1.maven.org/maven2/org/codehaus/groovy/groovy-all/1.8.6/groovy-all-1.8.6.jar)
    3. Go to <http://localhost:4502/system/console/scriptconsole>
    4. Ensure "Groovy" is selected as language
    5. Copy & paste [oak-browse-tree.groovy](#file-oak-browse-tree.groovy)
    5. Copy & paste [oak-browse-tree.groovy](#file-oak-browse-tree-groovy)
    6. Adapt "path" and "depth" to what part of the repository you want to look at
    7. Hit "Execute"

  6. alexkli revised this gist Apr 28, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion readme.md
    Original file line number Diff line number Diff line change
    @@ -4,7 +4,7 @@
    2. Install [Groovy Bundle](http://repo1.maven.org/maven2/org/codehaus/groovy/groovy-all/1.8.6/groovy-all-1.8.6.jar)
    3. Go to <http://localhost:4502/system/console/scriptconsole>
    4. Ensure "Groovy" is selected as language
    5. Copy & paste [oak-browse-tree.groovy](#oak-browse-tree.groovy)
    5. Copy & paste [oak-browse-tree.groovy](#file-oak-browse-tree.groovy)
    6. Adapt "path" and "depth" to what part of the repository you want to look at
    7. Hit "Execute"

  7. alexkli revised this gist Apr 28, 2017. 1 changed file with 4 additions and 1 deletion.
    5 changes: 4 additions & 1 deletion oak-browse-tree.groovy
    Original file line number Diff line number Diff line change
    @@ -42,7 +42,10 @@ static def renderNode(node, p, depth) {
    if (prop.isArray()) {
    p.print("- " + prop.name + " = [");
    for (def i = 0; i < prop.count(); i++) {
    p.print(prop.getValue(Type.STRING, i) + ", ");
    p.print(prop.getValue(Type.STRING, i));
    if (i < prop.count() - 1) {
    p.print(", ");
    }
    }
    p.println("]")
    } else {
  8. alexkli revised this gist Apr 28, 2017. 1 changed file with 33 additions and 1 deletion.
    34 changes: 33 additions & 1 deletion readme.md
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,39 @@
    ### Instructions

    1. Install [Sling Script Console bundle](http://central.maven.org/maven2/org/apache/sling/org.apache.sling.scripting.console/1.0.0/org.apache.sling.scripting.console-1.0.0.jar)
    2. Install [Groovy Bundle](http://repo1.maven.org/maven2/org/codehaus/groovy/groovy-all/1.8.6/groovy-all-1.8.6.jar)
    3. Go to <http://localhost:4502/system/console/scriptconsole>
    4. Ensure "Groovy" is selected as language
    5. Copy & paste [oak-browse-tree.groovy](#oak-browse-tree.groovy)
    6. Adapt "path" and "depth" to what part of the repository you want to look at
    7. Hit "Execute"
    7. Hit "Execute"

    ### Example output

    ```
    /var
    - jcr:primaryType = sling:Folder
    - jcr:mixinTypes = [rep:AccessControllable, ]
    - jcr:createdBy = admin
    - jcr:created = 2013-07-08T13:25:36.725-04:00
    + discovery
    - jcr:primaryType = sling:Folder
    - jcr:createdBy = admin
    - jcr:created = 2015-01-31T04:02:42.262Z
    + oak
    - jcr:primaryType = sling:Folder
    - jcr:createdBy = admin
    - jcr:created = 2016-12-16T13:28:38.239Z
    - sling:resourceType = sling:Folder
    + idMap (...)
    + clusterInstances (...)
    + syncTokens (...)
    + impl
    - jcr:primaryType = sling:Folder
    - jcr:createdBy = admin
    - jcr:created = 2015-01-31T04:02:42.263Z
    + establishedView (...)
    + ongoingVotings (...)
    + previousView (...)
    + clusterInstances (...)
    ```
  9. alexkli revised this gist Apr 28, 2017. 1 changed file with 7 additions and 0 deletions.
    7 changes: 7 additions & 0 deletions readme.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,7 @@
    1. Install [Sling Script Console bundle](http://central.maven.org/maven2/org/apache/sling/org.apache.sling.scripting.console/1.0.0/org.apache.sling.scripting.console-1.0.0.jar)
    2. Install [Groovy Bundle](http://repo1.maven.org/maven2/org/codehaus/groovy/groovy-all/1.8.6/groovy-all-1.8.6.jar)
    3. Go to <http://localhost:4502/system/console/scriptconsole>
    4. Ensure "Groovy" is selected as language
    5. Copy & paste [oak-browse-tree.groovy](#oak-browse-tree.groovy)
    6. Adapt "path" and "depth" to what part of the repository you want to look at
    7. Hit "Execute"
  10. alexkli revised this gist Apr 28, 2017. 1 changed file with 3 additions and 4 deletions.
    7 changes: 3 additions & 4 deletions oak-browse-tree.groovy
    Original file line number Diff line number Diff line change
    @@ -15,21 +15,20 @@ import org.apache.jackrabbit.oak.spi.state.NodeStateUtils
    showTree(sling, out, path, depth);

    static def showTree(sling, out, path, depth) {
    def p = new IndentPrinter(out)
    def repo = sling.getService(SlingRepository.class)
    def session = null
    try {
    session = repo.loginAdministrative(null);
    def cs = getOakContentSession(session);

    def p = new IndentPrinter(out)

    def rootNode = cs.latestRoot.getTree("/").nodeState;
    def node = NodeStateUtils.getNode(rootNode, path);
    if (node.exists()) {
    out.println(path);
    p.println(path);
    renderNode(node, p, depth);
    } else {
    out.println("Path not found: " + path)
    p.println("Path not found: " + path)
    }
    } finally {
    session?.logout()
  11. alexkli revised this gist Apr 28, 2017. 1 changed file with 3 additions and 6 deletions.
    9 changes: 3 additions & 6 deletions oak-browse-tree.groovy
    Original file line number Diff line number Diff line change
    @@ -9,19 +9,16 @@ def depth = 2;
    // --------------------------------------------------------------

    import org.apache.sling.jcr.api.SlingRepository
    import javax.jcr.Session
    import org.apache.jackrabbit.oak.api.Type
    import org.apache.jackrabbit.oak.spi.state.NodeStateUtils

    showTree(sling, out, path, depth);

    static def showTree(sling, out, path, depth) {

    SlingRepository repo = sling.getService(SlingRepository.class)
    Session session = null
    def repo = sling.getService(SlingRepository.class)
    def session = null
    try {
    session = repo.loginAdministrative(null);

    def cs = getOakContentSession(session);

    def p = new IndentPrinter(out)
    @@ -66,7 +63,7 @@ static def renderNode(node, p, depth) {
    }

    // get oak ContentSession via reflection
    static def getOakContentSession(Session session) {
    static def getOakContentSession(session) {
    def obj1 = java.lang.reflect.Proxy.getInvocationHandler(session).delegatee;
    def field = obj1.class.superclass.getDeclaredField("sd");
    field.setAccessible(true);
  12. alexkli revised this gist Apr 28, 2017. No changes.
  13. alexkli revised this gist Apr 28, 2017. No changes.
  14. alexkli revised this gist Apr 28, 2017. 1 changed file with 66 additions and 32 deletions.
    98 changes: 66 additions & 32 deletions oak-browse-tree.groovy
    Original file line number Diff line number Diff line change
    @@ -1,43 +1,77 @@
    // --------------------------------------------------------------
    // change these input values

    // path: what oak path to show
    def path = "/oak:index/nodetype/:index";
    // depth: how deep the tree structure should be rendered
    def depth = 2;

    // --------------------------------------------------------------

    import org.apache.sling.jcr.api.SlingRepository
    import javax.jcr.Session
    import javax.jcr.Node
    import javax.jcr.Property
    import org.apache.jackrabbit.oak.api.Type
    import org.apache.jackrabbit.oak.spi.state.NodeStateUtils

    showTree(sling, out, path, depth);

    static def showTree(sling, out, path, depth) {

    SlingRepository repo = sling.getService(SlingRepository.class)
    Session session = null
    InputStream is = null
    try {
    session = repo.loginAdministrative(null);
    SlingRepository repo = sling.getService(SlingRepository.class)
    Session session = null
    try {
    session = repo.loginAdministrative(null);

    // get oak ContentSession via reflection
    def cs = getOakContentSession(session);

    def p = new IndentPrinter(out)

    def rootNode = cs.latestRoot.getTree("/").nodeState;
    def node = NodeStateUtils.getNode(rootNode, path);
    if (node.exists()) {
    out.println(path);
    renderNode(node, p, depth);
    } else {
    out.println("Path not found: " + path)
    }
    } finally {
    session?.logout()
    }
    }

    static def renderNode(node, p, depth) {
    p.incrementIndent();
    node.properties.each { prop ->
    p.printIndent()
    if (prop.isArray()) {
    p.print("- " + prop.name + " = [");
    for (def i = 0; i < prop.count(); i++) {
    p.print(prop.getValue(Type.STRING, i) + ", ");
    }
    p.println("]")
    } else {
    p.println("- " + prop.name + " = " + prop.getValue(Type.STRING))
    }
    }
    node.childNodeEntries.each { child ->
    p.printIndent()
    if (depth == 0) {
    p.println("+ " + child.name + " (...)")
    } else {
    p.println("+ " + child.name)
    renderNode(child.nodeState, p, depth - 1)
    }
    }
    p.decrementIndent();
    }

    // get oak ContentSession via reflection
    static def getOakContentSession(Session session) {
    def obj1 = java.lang.reflect.Proxy.getInvocationHandler(session).delegatee;
    def field = obj1.class.superclass.getDeclaredField("sd");
    field.setAccessible(true);
    def obj2 = field.get(obj1);
    field = obj2.class.superclass.getDeclaredField("contentSession");
    field.setAccessible(true);
    def cs = field.get(obj2);

    out.println(cs.dump());

    def root = cs.latestRoot;

    // go wild here
    // Note: hidden nodes (prefixed with ":") do not appear in getChildren()
    // instead you have to know their path
    def t = root.getTree("/oak:index/nodetype/:index");
    out.println(t.exists());
    t.properties.each { kid ->
    out.println(kid);
    }
    out.println(t.getChildrenCount(1000));
    t.children.each { kid ->
    out.println(kid);
    }


    } finally {
    is?.close()
    session?.logout()
    }
    return field.get(obj2);
    }
  15. alexkli revised this gist Apr 27, 2017. 1 changed file with 4 additions and 1 deletion.
    5 changes: 4 additions & 1 deletion oak-browse-tree.groovy
    Original file line number Diff line number Diff line change
    @@ -11,7 +11,7 @@ try {
    session = repo.loginAdministrative(null);

    // get oak ContentSession via reflection
    def obj1 = java.lang.reflect.Proxy.getInvocationHandler(session).delegatee;
    def obj1 = java.lang.reflect.Proxy.getInvocationHandler(session).delegatee;
    def field = obj1.class.superclass.getDeclaredField("sd");
    field.setAccessible(true);
    def obj2 = field.get(obj1);
    @@ -23,6 +23,9 @@ try {

    def root = cs.latestRoot;

    // go wild here
    // Note: hidden nodes (prefixed with ":") do not appear in getChildren()
    // instead you have to know their path
    def t = root.getTree("/oak:index/nodetype/:index");
    out.println(t.exists());
    t.properties.each { kid ->
  16. alexkli created this gist Apr 27, 2017.
    40 changes: 40 additions & 0 deletions oak-browse-tree.groovy
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,40 @@
    import org.apache.sling.jcr.api.SlingRepository
    import javax.jcr.Session
    import javax.jcr.Node
    import javax.jcr.Property


    SlingRepository repo = sling.getService(SlingRepository.class)
    Session session = null
    InputStream is = null
    try {
    session = repo.loginAdministrative(null);

    // get oak ContentSession via reflection
    def obj1 = java.lang.reflect.Proxy.getInvocationHandler(session).delegatee;
    def field = obj1.class.superclass.getDeclaredField("sd");
    field.setAccessible(true);
    def obj2 = field.get(obj1);
    field = obj2.class.superclass.getDeclaredField("contentSession");
    field.setAccessible(true);
    def cs = field.get(obj2);

    out.println(cs.dump());

    def root = cs.latestRoot;

    def t = root.getTree("/oak:index/nodetype/:index");
    out.println(t.exists());
    t.properties.each { kid ->
    out.println(kid);
    }
    out.println(t.getChildrenCount(1000));
    t.children.each { kid ->
    out.println(kid);
    }


    } finally {
    is?.close()
    session?.logout()
    }