JBoss.orgCommunity Documentation

Chapter 6. Content Repositories for Java (JCR)

6.1. Obtaining JCR repositories
6.2. Creating JCR sessions
6.3. Summary

The Content Repository for Java technology API provides a standard Java API for working with content repositories. Abbreviated "JCR", this API was developed as part of the Java Community Process under JSR-170 (JCR 1.0) and is being revised under JSR-283. JBoss DNA provides a JCR 1.0 implementation that allows you to work with the contents of a repository using the JCR API. For information about how to use the JCR API, please see the JSR-170 specification.

The JCR API doesn't define how your application first obtains a reference to a Repository implementation. With JBoss DNA, you simply creating a JcrRepository object and supply an ExecutionContextFactory and a RepositoryConnectionFactory (such as a RepositoryLibrary or RepositoryService). Since JcrRepository implements the JCR Repository interface, from this point forward you can just use the standard JCR API.

Creating sessions is done using a Repository one of its login(...) methods, where the name of the workspace corresponds to the name of the RepositorySource:

JcrRepository jcrRepository = new JcrRepository(contextFactory, sources);
Session session = jcrRepository.login(sourceName);

Now, this code doesn't do any authentication; it essentially trusts the caller has the appropriate privileges. Normally, your application will need to authenticate the user, so let's look at how that's done.

As we mentioned in the security section, JBoss DNA uses JAAS for authentication and authorization. So how does this work with the JCR API?

The JCR API defines a Credentials marker interface, an instance of which can be passed to the Session.login(...) method. Rather than provide a concrete implementation of this interface, JBoss DNA allows you to pass any implementation of Credentials that also has one of the following methods:

This way, your application can obtain the JAAS LoginContext or AccessControlContext however it wants, and then merely passes that into DNA through the JCR Credentials. No interfaces or classes specific to JBoss DNA are required.

The following code shows how this is done, using an anonymous inner class for the Credentials implementation.

CallbackHandler callbackHandler = // as needed by your app, according to JAAS
final LoginContext loginContext = new LoginContext("MyAppContextName",callbackHandler);

// Now pass to JBoss DNA to create a JCR Session ...
Credentials credentials = new Credentials() {
public LoginContext getLoginContext() { return loginContext; }
JcrRepository jcrRepository = new JcrRepository(contextFactory, sources);
Session session = jcrRepository.login(credentials, sourceName);

In this chapter, we covered how to use JCR with JBoss DNA. Now that you know how JBoss DNA repositories work, and how to use JCR to work with DNA repositories, we'll move on in the next chapter to describing in detail how the sequencing of file content works.