JBoss.orgCommunity Documentation

Repositories and workspaces

A content repository consists of one or more workspaces. Each workspace contains a tree of items.

Access the repository's content from a service

1. Get the session object.



import javax.jcr.Session;
import org.exoplatform.services.jcr.RepositoryService;
import org.exoplatform.services.jcr.core.ManageableRepository;
import org.exoplatform.services.jcr.ext.common.SessionProvider;
import org.exoplatform.services.wcm.utils.WCMCoreUtils;
// For example
RepositoryService repositoryService = WCMCoreUtils.getService(RepositoryService.class);
ManageableRepository manageableRepository = repositoryService.getRepository(repository);
SessionProvider sessionProvider = WCMCoreUtils.getSessionProvider();
Session session = sessionProvider.getSession(workspace, manageableRepository);

i. Obtain the javax.jcr.Repository object via one of the following ways:

The first way

Call the getRepository() method of RepositoryService.



RepositoryService repositoryService = (RepositoryService) container.getComponentInstanceOfType(RepositoryService.class);
Repository repository = repositoryService.getRepository("repositoryName");

The second way

Call the getCurrentRepository() method, especially when you plan to use a single repository which covers more than 90% of usecases.



// set current repository at initial time
RepositoryService repositoryService = (RepositoryService) container.getComponentInstanceOfType(RepositoryService.class);
repositoryService.setCurrentRepositoryName("repositoryName");
....
// retrieve and use this  repository
Repository repository = repositoryService.getCurrentRepository();

The third way

Using JNDI as specified in JSR-170.



Context ctx = new InitialContext();
Repository repository =(Repository) ctx.lookup("repositoryName");

ii. Log in the server to get a Session object by either of two ways:

The first way

Create a Credential object, for example:



Credentials credentials = new SimpleCredentials("exo", "exo".toCharArray());
Session jcrSession = repository.login(credentials, "production");

The second way

Log in the server without using a Credential object.



Session jcrSession = repository.login("production");

This way is only applied when you run an implementation of eXo Platform. The eXo Platform implementation will directly leverage the organization and security services that rely on LDAP or DB storage and JAAS login modules. Single-Sign-On products can now also be used as eXo Platform v.2 which supports them.

Note

There are some JCR Session common considerations as follows:

Because javax.jcr.Session is not a safe object of thread, it is recommended that you should not share it between threads.

Do not use the System session from the user-related code because a system session has unlimited rights. Call ManageableRepository.getSystemSession() from the process-related code only.

Call Session.logout() explicitly to release resources assigned to the session.

When designing your application, you should take care of the Session policy inside your application. Two strategies are possible: Stateless (Session per business request) and Stateful (Session per User) or some mixings.

2. Retrieve your node content of the session object.



String path = "/"; // put your node path here
Node node = (Node) session.getItem(path);