创建新的工作区 [英] Create new workspace

查看:110
本文介绍了创建新的工作区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Modeshape文档的7.1.6节说:您的应用程序现在可以使用标准JCR 2.0 API创建和删除工作区."

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

JCR 2.0文档说要使用Workspace.createWorkspace(String name)

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?

谢谢

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

推荐答案

javax.jcr.Repository接口允许您获取存储库的描述符并登录以建立到存储库中工作空间的会话.但是所有其他操作都需要身份验证和授权,这意味着它们可以使用javax.jcr.Session或通过其他特定于会话的接口(例如javax.jcr.Workspace)执行.

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

下面显示的所有示例均在标准JCR API上使用.

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

请注意,此调用不提供任何凭据,并且会导致使用默认工作空间的匿名"会话.匿名会话可能没有太多权限,因此您可能需要使用login方法的其他重载形式之一,该形式允许您提供凭据和/或工作空间名称的各种组合. (使用ModeShape配置,可以指定默认工作空间的名称,控制是否允许匿名会话,并指定匿名会话允许的角色.)如果指定工作空间名称,但该工作空间不存在,则该方法将抛出javax.jcr.NoSuchWorkspaceException异常(这是javax.jcr.RepositoryException的子类).

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

要获取工作区列表,请获取会话的Workspace对象并调用getAccessibleWorkspaceNames()方法:

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.

要创建新的 工作区,只需使用Workspace对象:

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

请注意,mix:referenceable节点在原始和新工作空间中将具有相同的标识符.这是JCR工作空间的重要特征,并且经常是使用单独工作空间(而不是单个工作空间的单独区域)的重要原因.有关更多详细信息,请参见JSR-283规范.

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

这篇关于创建新的工作区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆