001/*
002 * Licensed to DuraSpace under one or more contributor license agreements.
003 * See the NOTICE file distributed with this work for additional information
004 * regarding copyright ownership.
005 *
006 * DuraSpace licenses this file to you under the Apache License,
007 * Version 2.0 (the "License"); you may not use this file except in
008 * compliance with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.fcrepo.kernel.modeshape;
019
020import javax.jcr.Credentials;
021import javax.jcr.Repository;
022import javax.jcr.RepositoryException;
023
024import org.fcrepo.kernel.api.FedoraRepository;
025import org.fcrepo.kernel.api.FedoraSession;
026import org.fcrepo.kernel.api.exception.RepositoryRuntimeException;
027
028/**
029 * The basic abstraction for a Fedora repository
030 * @author acoburn
031 */
032public class FedoraRepositoryImpl implements FedoraRepository {
033
034    private final Repository repository;
035
036    /**
037     * Create a FedoraRepositoryImpl with a JCR-based Repository
038     *
039     * @param repository the JCR repository
040     */
041    public FedoraRepositoryImpl(final Repository repository) {
042        this.repository = repository;
043    }
044
045    @Override
046    public FedoraSession login() {
047        try {
048            return new FedoraSessionImpl(repository.login());
049        } catch (final RepositoryException ex) {
050            throw new RepositoryRuntimeException(ex);
051        }
052    }
053
054    @Override
055    public FedoraSession login(final Object credentials) {
056        if (credentials instanceof Credentials) {
057            try {
058                return new FedoraSessionImpl(repository.login((Credentials) credentials));
059            } catch (final RepositoryException ex) {
060                throw new RepositoryRuntimeException(ex);
061            }
062        }
063        throw new ClassCastException("login credentials are not an instance of " +
064                Credentials.class.getCanonicalName());
065    }
066
067    /**
068     * Retrieve the internal JCR Repository object
069     *
070     * @return the JCR Repository
071     */
072    public Repository getJcrRepository() {
073        return repository;
074    }
075
076    /**
077     * Retrieve the internal JCR Repository from a FedoraRepository object
078     *
079     * @param repository the FedoraRepository
080     * @return the JCR Repository
081     */
082    public static Repository getJcrRepository(final FedoraRepository repository) {
083        if (repository instanceof FedoraRepositoryImpl) {
084            return ((FedoraRepositoryImpl)repository).getJcrRepository();
085        }
086        throw new ClassCastException("FedoraRepository is not a " + FedoraRepositoryImpl.class.getCanonicalName());
087    }
088}