0 Replies Latest reply: Mar 18, 2012 1:47 PM by djg2002 RSS

Beginners Guide

djg2002 Newbie

Given a repository name is there a way to get the source(s) in java?  In the below example I couldn't get anything from:

 

RepositorySource source = engine.getRepositorySource("beginners_repo");

        source.getName();

 

If you use the SessionFactory approach recommended by the docs you don't get to see the JcrEngine so is there a way to get sources from a Session?

 

 

 

 

I am new to Modeshape and JCR in general so bootstrapping my first instance was proving difficult.

 

I spent some time this weekend:

 

  • Creating an example to configure a bare-bones repository and workspace
    (XML attached)

  • Write a folders/files into it (I just used the config file itself as an InputStream)
    (Java below)

  • Query the repository and display the results.
    (Java formatter class to make results more readable attached)

The documentation doesn't (yet) provide end-to-end examples, I guess because there are so many use cases, but because there is little example code for using ModeShape in general I figured this would help people get over the initial setup hump and start using a JCR.

 

If anyone has suggestions on how to improve on this, or if I've provided a dumb way to use ModeShape I'd very much like to hear about it.

 

So lets dive straight in - see attached the XML configuration that does pretty much the bear minimum to give JCR access to your file system.  Access control is non-existent all your files will go in as created by 'anonymous'.

 

Next the java to demonstrate working with the beginners_repo and beginners_workspace as configured.  I made pretty much every reference final to highlight the fact that very little is changing, we're just navigating the API to achieve the desired result.

 

import JCRPrintQueryResultsImpl; // The utility class attached


import org.modeshape.jcr.JcrConfiguration;

import org.modeshape.jcr.JcrEngine;

import org.modeshape.jcr.JcrRepository;

import org.xml.sax.SAXException;

import javax.jcr.Binary;

import javax.jcr.Node;

import javax.jcr.RepositoryException;

import javax.jcr.Session;

import javax.jcr.query.Query;

import javax.jcr.query.QueryManager;

import javax.jcr.query.QueryResult;

import java.io.File;

import java.io.FileInputStream;

import java.io.IOException;

 

 

public class TestJCR {

 

    public static void main(String[] args) throws IOException, SAXException, RepositoryException {

        TestJCR j = new TestJCR();

        j.doExample();

    }

 

    public void doExample() throws IOException, SAXException, RepositoryException {

        final String JCR_CONFIG_FILE_URL = "/modeshape_beginners_config.xml";

        final String REPO_NAME = "beginners_repo";

        final String WORKSPACE_NAME = "beginners_workspace";

 

        // Assuming config file is at root of project's classpath

        final File configFile = new File(this.getClass().getResource(JCR_CONFIG_FILE_URL).getPath());

 

        // Configure the JCR engine

        final JcrConfiguration jcrConfig = new JcrConfiguration();       // org.modeshape.jcr.JcrConfiguration

        final JcrEngine engine = jcrConfig.loadFrom(configFile).build(); // org.modeshape.jcr.JcrEngine;

        engine.start();

 

        final JcrRepository repo = engine.getRepository(REPO_NAME);      // org.modeshape.jcr.JcrRepository

        final Session session = repo.login(WORKSPACE_NAME);              // javax.jcr.Session

 

 

        // Create a new directory and add write a file into it.

 

        // Lets just use the config file as an {@link InputStream}

        final FileInputStream inputStream = new FileInputStream(configFile);

        final String filename = configFile.getAbsoluteFile().getName();

 

        // JCR 2.0 deprecates writing content using an InputStream

        // Modeshape provides a ValueFactory to convert to Binary

        final Binary binaryFileData = session.getValueFactory().createBinary(inputStream);

 

        final Node rootNode = session.getRootNode();

 

        final Node testFolder = rootNode.addNode("test_folder", "nt:folder");

 

        final Node fileNode = testFolder.addNode(filename, "nt:file");

 

        final Node fileContent = fileNode.addNode("jcr:content", "nt:resource");

 

        fileContent.setProperty("jcr:data", binaryFileData);

 

        session.save();

 

 

        // Query the JCR...

        final String jql = "SELECT * FROM [nt:file] where [jcr:createdBy] = '<anonymous>'";

 

        final QueryManager queryManager = session.getWorkspace().getQueryManager();  // javax.jcr.QueryManager

        final Query query = queryManager.createQuery(jql, Query.JCR_SQL2);           // javax.jcr.Query

        final QueryResult queryResult = query.execute();                             // javax.jcr.QueryResult

 

        // ...and print results

        // Utility class attached to this post

        System.out.println(JCRPrintQueryResultsImpl.getFormattedOutput(jql, queryResult));

 

        // Clean shutdown

        session.logout();

        engine.shutdown();

    }

 

}

 

Output:

 

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

SELECT * FROM [nt:file] where [jcr:createdBy] = '<anonymous>'

 

> 1 rows

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

jcr:primaryType   jcr:created                jcr:createdBy   jcr:path                                      jcr:name                         jcr:score            mode:localName                   mode:depth  

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

nt:file           2012-03-18T16:17:16.307Z   <anonymous>     /test_folder/modeshape_beginners_config.xml   modeshape_beginners_config.xml   2.1271748542785645   modeshape_beginners_config.xml   2           

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

 

 

 

Next time I'll post about getting access control (once I've figured it out) by providing ServletCredentials from Spring rather than the JAAS examples in the docs.  It's straightforward if you already have a users DAO and/or an HttpServletRequest to get credentials from