Crash + eXo Platform – sizeOf JCR content command

This guest post was adapted from an original article written in French by Stefan Letzner.

Crash is a tool for connecting to a JVM and using all the libraries it loads to run Groovy scripts, browse the JCR, etc…

This tool can operate in different ways:

  • As a stantalone
  • In web mode, deployed as a web app
  • Directly injected by Spring

In our case, we’re going to use a web version deployed in a Tomcat to get connected to the eXo Platform JCR. Two versions of the web mode exist: one is configured to connect to eXo Platform while the other is more generic.

Getting started with Crash

At first we will see how to connect to Crash once it has been deployed in the eXo Platform Tomcat.

By default, Crash listens to port 2000 for ssh and port 5000 for telnet. These are both available for connecting. In our case, we will connect via ssh, with the root user (password: gtn):

ssh -p 2000 -l root localhost

where “localhost” is the name of the server hosting the Tomcat.

We then access the Crash command prompt:

   ______
 .~      ~. |`````````,       .'.                   ..'''' |         |
|           |'''|'''''      .''```.              .''       |_________|
|           |    `.       .'       `.         ..'          |         |
 `.______.' |      `.   .'           `. ....''             |         | 1.2.8

Then, we need to connect to the repository:

repo use container=portal

And connect to the workspace (here it is: “collaboration”):

ws login -u root -p gtn collaboration

From here, we can navigate the JCR and do things with nodes such as creating, deleting and moving them.

Creating a new Crash command

A new command is created via a Groovy script.

In the WAR, in WEB-INF there is a directory named crash/commands. It has two directories for hosting Groovy scripts. The first contains general commands (system) and the second contains JCR commands.

Then we create a Groovy script named “sizeof.groovy” and move it into the JCR directory.

New scripts and changes to existing scripts are hot deployed.

This new command includes the options:

-t (type): filters on “jcr:primaryType”
-l (limit): limits the number of results
-f (outPutFile): the path and name of the file of results

and a mandatory parameter, “jcr:path”, which is the path from where we want to launch the research.

Here is the script code:

package crash.commands.jcr
 
import javax.jcr.query.Query
 
import org.crsh.text.ui.UIBuilder
import org.crsh.cli.Usage
import org.crsh.cli.Command
import org.crsh.cli.Man
import org.crsh.cli.Argument
import org.crsh.cli.Option
import org.crsh.cli.Required
 
@Usage("sizeOf JCR nodes command")
class sizeof extends org.crsh.jcr.command.JCRCommand {
 
@Usage("size of a single content")
@Command
public Object list(
@Option(names=["t","type"]) @Usage("jcr:primaryType") String type,
@Option(names=["l","limit"]) @Usage("the result limit") @Man("The number of nodes displayed, by default this value is equals to 5") Integer limit,
@Option(names=["f","outPutFile"]) @Usage("Path with name of the output file") String outPutFile,
@Argument @Usage("JCR path") String path) {
 
    // Default limit set to 5
    limit = limit ?: 5;
 
    assertConnected();
 
    def queryMgr = session.workspace.queryManager;
 
    // JCR Query to retrieve all the subnodes of the given path
    def statement = "select * from " + (type != null ? type : "nt:base") + " where jcr:path like '" + path + "/%'";
 
    // Exceution of the query
    def select = queryMgr.createQuery(statement, Query.SQL);
    def result = select.execute()
    def nodes = result.nodes
    def total = nodes.size
 
    // output result
    def stream = new StringBuilder()
 
    def builder = new UIBuilder();
    builder.node("The query matched " + total + " nodes") {
    def index = 0;
    def contentMap = [:]
 
    while (nodes.hasNext()) {
      def n = nodes.next()
      def nodeSize = 0
 
      // calculate the node size
      if (n.hasProperty("jcr:content/jcr:data")) {
        nodeSize = n.getProperty("jcr:content/jcr:data").getLength() / 1024
      }
 
      contentMap.put(n.path,nodeSize)
 
      index++
      if (limit != null && index >= limit) {
        break;
      }
    }
 
    // Sort the new map from the biggest to the smallest
    contentMap = contentMap.sort{a,b -> b.value <=> a.value}
    def chaine
    def file
 
    for (item in contentMap){
      chaine = item.key + " : " + item.value + " Ko"
      stream.append(chaine + "\r\n")
      label(chaine)
    }
 }
 
 // Store in the file
 if (outPutFile != null) {
   System.out.println("Output file : " + outPutFile)
 
   file = new File(outPutFile)
   file.write(stream.toString())
 }
 
 return builder;
 }
}

Once the script has been deployed, we can use the “help” command to ensure the script is properly available in Crash:

% help
Try one of these commands with the -h or --help switch:
 
NAME DESCRIPTION
cd : changes the current node
commit : saves changes
cp : copy a node to another
dashboard
env : display the term env
filter : A filter for a stream of map
help : provides basic help
java : various java language commands
jdbc : JDBC connection
jmx : Java Management Extensions
jndi : Java Naming and Directory Interface
jpa : Java persistance API
jvm : JVM informations
log : logging commands
ls : list the content of a node
man : format and display the on-line manual pages
mixin : mixin commands
mv : move a node
node : node commands
pwd : print the current node path
repo : repository interaction commands
rm : remove one or several node or a property
rollback : ollback changes
selec : execute a JCR sql query
shell : shell related command
sizeof : sizeOf JCR nodes command
sleep : sleep for some time
sort : Sort a map
system : vm system properties commands
thread : JVM thread commands
version : versioning commands
ws : workspace commands
xpath : execute a JCR xpath query

Now, we can easily execute the following command:

sizeof list -t nt:file -l 10 "/sites content/live/Contenus/MonRepertoireDeContenus"

In return, we’ll get a table listing the content paths as well as their size, from the largest to the smallest.

% sizeof content -t nt:file -l 10 "/sites content/live/Contenus/Footer
The query matched 8 nodes
+-/sites content/live/Contenus/Footer/Footer simple/footer/default.html : 2.556640625 Ko
+-/sites content/live/Contenus/Footer/Footer simple/footer-authentification/default.html : 2.3564453125 Ko
+-/sites content/live/Contenus/Footer/Footer simple/footer/js/default.js : 0 Ko
+-/sites content/live/Contenus/Footer/Footer simple/footer-authentification/js/default.js : 0 Ko
+-/sites content/live/Contenus/Footer/Footer simple/footer/css/default.css : 0 Ko
+-/sites content/live/Contenus/Footer/Footer simple/footer/medias/images/illustration : 0 Ko
+-/sites content/live/Contenus/Footer/Footer simple/footer-authentification/css/default.css : 0 Ko
+-/sites content/live/Contenus/Footer/Footer simple/footer-authentification/medias/images/illustration : 0 Ko

Crash is very well documented. You can find all the information you need on http://www.crashub.org

Join the eXo tribe by registering for the community and access tutorials, support, and downloads!

About Stefan Letzner

Stefan moved from web design to development and thus worked on many projects involving very different technologies: C#/WPF, Java/J2EE, JQuery, Groovy, Android…

A developer at Capgemini since 2008, Stefan has worked on different projects, and he is very interested in open source environments. Recently, Stefan has been hosting a blog which regroups code snippets, patterns and tips that are used on projects and that might need to be found quickly. It makes sure the knowledge is not lost.

Related Posts

Comments

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>