JBoss.orgCommunity Documentation

Chapter 6. Content Repositories for Java (JCR)

6.1. Obtaining JCR repositories
6.2. Creating JCR sessions
6.3. JCR Support
6.4. 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 partial 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 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.


For more information about the and RepositoryConnectionFactory classes, see the chapter on setting up a JBoss DNA environment and setting up the RepositoryService.

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);

JBoss DNA currently supports most of the Level 1 and Level 2 feature set defined by the JSR-170 specification. Queries, which are part of Level 1, are not implemented, nor are all of the update methods that make up Level 2. So while the current implementation provides many of the features that may be needed by an application, we really hope that this release will allow you to give us some feedback on what we have so far.

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.