BasicShotokuFunctions

Version 15

    Basic functions of shotoku

     

    Please note that to use any annotations mentioned here, you must enable aop loadtime weaving in your AS configuration, and use application server mode. Annotations in embedded mode won't be supported until 1.0 final. Of course, shotoku is fully functional without them, too. They just make life easier.

     

    Obtaining a ContentManager using dependency injection

    You can annotate a field of class ContentManager with @Inject (from package org.jboss.shotoku.aop), and it will be automatically initialized with an instance of ContentManager. The annotation has two optional parameters:

    • id - id of a repository to use. Defaults to the default repository.

    • prefix - prefix of the content manager to inject. Defaults to an empty string.

     

    Example code:

    @Inject(prefix="wiki/images")
    private ContentManager cm;
    

     

    Obtaining a ContentManager from inside your code

    Just call ContentManager.getContentManager(), ContentManager.getContentManager(prefix) for a prefixed content manager or ContentManager.getContentManager(id, prefix). This prefix will cause your content manager to be prefix-relative, for example, you may wish to easily access resources from a given directory.

     

    Getting nodes and directories

    The basic node and directory accessing functions are:

    • contentManager.getNode(path)

    • contentManager.getDirectory(path)

    • directory.getNode(name)

    • directory.getDirectory(name)

     

    All operations on nodes/ directories are quite intuitive, so you shouldn't have any trouble after reading the

    javadocs.

     

    @NodeInject, @DirectoryInject annotations

    If you know at the time of writing your progam the exact path to a resource that you'll want to read, you can have them easily injected. Just write:

     

    @NodeInject("path/to/node")
    Node myNode;
    
    @NodeInject("path/to/node")
    String myNodeContent;
    
    @DirectoryInject("path/to/directory")
    Directory myDirectory;
    

     

    A note about exceptions

    Some exceptions are runtime exceptions, so you won't be forced to catch them. However, you can see them all in the javadocs.

     

    Simple read-write-save example.

     

    Let's suppose you have configured Shotoku to use the SVN implementation as the default content manager. To use it,

    we have to obtain an instance of ContentManager. If we only want to access files and directories in a certain

    directory, we can get a prefixed content manager to save us the trouble of having to add it to every path:

    ConentManager cm = ContentManager.getContentManager("prefix");
    

     

    Now we can, for example, create a new node:

    Node n1 = cm.getRootDirectory().newNode("subdirectory/newnode");
    

    or get an existing one, together with its content and properties:

    Node n2 = cm.getRootDirectory().getNode("existingdirectory/existingnode");
    System.out.println(n2.getContent());
    System.out.println(n2.getProperty("some prop"));
    

    Finally, we can change the content and properties and save everything (the changes will be commited to the SVN repository only after saving!):

    n1.setContent("new content");
    n2.setContent("replaced content");
    n2.setProperty("prop1", "val1");
    cm.save(n1, n2); // or, in a less effective way (2 commits): n1.save(); n2.save();