Question

Section 7.1.6 of the Modeshape docs say 'Your application can now create and remove workspaces using the standard JCR 2.0 API.'

The JCR 2.0 doc says to use Workspace.createWorkspace(String name)

How do I make this part of my repository obtained using the code at the bottom of this post?

Also, how to I get a list of the workspaces already in the repository?

Thanks

for (RepositoryFactory factory : ServiceLoader.load(RepositoryFactory.class)) {

    if (factory instanceof org.modeshape.jcr.api.RepositoryFactory) {
        org.modeshape.jcr.api.RepositoryFactory modeshapeRepositoryFactory = (org.modeshape.jcr.api.RepositoryFactory) factory;

        final Repositories repositories = modeshapeRepositoryFactory.getRepositories(JCR_CONFIG_FILE_URL);

        if (repositories != null) {

            Set<String> repositoryNames = repositories.getRepositoryNames();
            if (repositoryNames != null) {
                for (String repoName : repositoryNames) {
                    log.info(repoName);
                }
            }
        }
        else {
            System.out.println("repositories reference was null");
        }
    }

    try {

        repository = factory.getRepository(parameters);
        if (repository != null) {
            printRepoDetails(repository, parameters, factory);
            repositoryFactory = factory; // Keep reference to allow clean shutdown.  Not part of JCR 2.0
            break;
        }
    }
    catch (RepositoryException e) {
        log.error("Error getting repository: \n" + e.toString());
        e.printStackTrace();
    }
}
Was it helpful?

Solution

The javax.jcr.Repository interface allows you to get the descriptors of the repository and to log in to establish a session to a workspace in the repository. But all other operations require authentication and authorization, which means they can be performed with a javax.jcr.Session or via the other session-specific interfaces (such as javax.jcr.Workspace).

All examples shown below use on the standard JCR API.

To obtain a Session, simply log into the Repository:

javax.jcr.Repository repository = ...
javax.jcr.Session session = repository.login();

Note that this call doesn't supply any credentials and results in an "anonymous" session that uses the default workspace. An anonymous session may not have privilege to do much, so you may need to use one of the other overloaded forms of the login method that allow you to supply various combinations of credentials and/or workspace names. (The ModeShape configuration allows you to dictate the name of the default workspace, to control whether anonymous sessions are allowed, and to specify the roles allowed by anonymous sessions.) If you specify a workspace name and that workspace doesn't exist, the method will throw a javax.jcr.NoSuchWorkspaceException exception (that is a subclass of the javax.jcr.RepositoryException).

To get the list of workspaces, get the session's Workspace object and call the getAccessibleWorkspaceNames() method:

javax.jcr.Workspace workspace = session.getWorkspace();
String[] workspaceNames = workspace.getAccessibleWorkspaceNames();

You can then do something with the workspace names, such as check whether the workspace you need already exists.

To create a new empty workspace, simply use the Workspace object:

String newWorkspaceName = ...
workspace.createWorkspace(newWorkspaceName);

Alternatively, you can create a new workspace that is a copy of an existing workspace.

String newWorkspaceName = ...
String originalWorkspaceName = ...
workspace.createWorkspace(newWorkspaceName,originalWorkspaceName);

Note that the mix:referenceable nodes will have the same identifiers in both the original and new workspaces. This is an important characteristic of JCR workspaces and often a big reason for using separate workspaces (rather than separate areas of a single workspace). See the JSR-283 specification for more detail.

And finally, you can destroy existing workspaces, too:

String existingWorkspaceName
workspace.deleteWorkspace(existingWorkspaceName);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top