All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.jackrabbit.test.RepositoryHelper Maven / Gradle / Ivy

There is a newer version: 2.23.1-beta
Show newest version
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.jackrabbit.test;

import java.security.Principal;
import java.util.HashMap;
import java.util.Map;

import javax.jcr.Credentials;
import javax.jcr.Repository;
import javax.jcr.RepositoryException;
import javax.jcr.Session;

/**
 * Utility class to get access to {@link javax.jcr.Session} instances.
 */
public class RepositoryHelper {

    /**
     * Repository stub reference.
     */
    private RepositoryStub repoStub;

    /**
     * Overlay configuration.
     */
    private Map configuration = new HashMap();

    /**
     * Creates a repository helper with configuration from
     * repositoryStubImpl.properties file.
     */
    public RepositoryHelper() {
    }

    /**
     * Creates a repository helper with additional configuration parameters.
     *
     * @param config configuration which overlays the values from the property
     *   file.
     */
    public RepositoryHelper(Map config) {
        configuration.putAll(config);
    }

    /**
     * Returns the repository instance to test.
     * @return the repository instance to test.
     * @throws RepositoryException if the repository could not be obtained.
     */
    public Repository getRepository() throws RepositoryException {
        try {
            if (repoStub == null) {
                repoStub = RepositoryStub.getInstance(configuration);
            }
            return repoStub.getRepository();
        } catch (RepositoryStubException e) {
            throw new RepositoryException("Failed to get Repository instance.", e);
        }
    }

    /**
     * Returns a superuser Session of the default workspace. The
     * returned Session has read and write access to the whole
     * workspace.
     * @return a superuser Session.
     * @throws RepositoryException if login to the repository failed.
     */
    public Session getSuperuserSession() throws RepositoryException {
        return getSuperuserSession(null);
    }

    /**
     * Returns a superuser Session of the workspace with name
     * workspaceName. The returned Session has read
     * and write access to the whole workspace.
     * @return a superuser Session.
     * @throws RepositoryException if login to the repository failed.
     */
    public Session getSuperuserSession(String workspaceName) throws RepositoryException {
        return getRepository().login(repoStub.getSuperuserCredentials(), workspaceName);
    }

    /**
     * Returns a Session of the default workspace with read and
     * write access to the workspace.
     * @return a Session with read and write access.
     * @throws RepositoryException if login to the repository failed.
     */
    public Session getReadWriteSession() throws RepositoryException {
        return getReadWriteSession(null);
    }

    /**
     * Returns a Session of the workspace with name
     * workspaceName with read and write access to the workspace.
     * @return a Session with read and write access.
     * @throws RepositoryException if login to the repository failed.
     */
    public Session getReadWriteSession(String workspaceName) throws RepositoryException {
        return getRepository().login(repoStub.getReadWriteCredentials(), workspaceName);
    }

    /**
     * Returns a Session of the default workspace with read only
     * access to the workspace.
     * @return a Session with read only.
     * @throws RepositoryException if login to the repository failed.
     */
    public Session getReadOnlySession() throws RepositoryException {
        return getReadOnlySession(null);
    }

    /**
     * Returns a Session of the workspace with name
     * workspaceName with read only access to the workspace.
     * @return a Session with read only access.
     * @throws RepositoryException if login to the repository failed.
     */
    public Session getReadOnlySession(String workspaceName) throws RepositoryException {
        return getRepository().login(repoStub.getReadOnlyCredentials(), workspaceName);
    }

    /**
     * Returns the value of the configuration property with specified
     * name. If the property does not exist null is
     * returned.
     * 

* Configuration properties are defined in the file: * repositoryStubImpl.properties. * * @param name the name of the property to retrieve. * @return the value of the property or null if non existent. * @throws RepositoryException if the configuration file cannot be found. */ public String getProperty(String name) throws RepositoryException { // force assignment of repoStub getRepository(); return repoStub.getProperty(name); } /** * Returns the read-only credentials created from the username * and password specified in the configuration properties. * Configuration properties are defined in the file: * repositoryStubImpl.properties. * * @return read-only Credentials */ public Credentials getReadOnlyCredentials() { return repoStub.getReadOnlyCredentials(); } /** * Returns the read-write credentials created from the username * and password specified in the configuration properties. * Configuration properties are defined in the file: * repositoryStubImpl.properties. * * @return read-write Credentials */ public Credentials getReadWriteCredentials() { return repoStub.getReadWriteCredentials(); } /** * Returns the superuser credentials created from the username * and password specified in the configuration properties. * Configuration properties are defined in the file: * repositoryStubImpl.properties. * * @return superuser Credentials */ public Credentials getSuperuserCredentials() { return repoStub.getSuperuserCredentials(); } /** * Returns a {@link Principal} identifying a known user. * @param session */ public Principal getKnownPrincipal(Session session) throws RepositoryException { return repoStub.getKnownPrincipal(session); } /** * Returns a {@link Principal} identifiying an unknown user. * @param session */ public Principal getUnknownPrincipal(Session session) throws NotExecutableException, RepositoryException { return repoStub.getUnknownPrincipal(session); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy