001/**
002 * Copyright 2015 DuraSpace, Inc.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.fcrepo.kernel.modeshape.services;
017
018import static com.codahale.metrics.MetricRegistry.name;
019import static org.fcrepo.kernel.modeshape.services.ServiceHelpers.getRepositoryCount;
020import static org.slf4j.LoggerFactory.getLogger;
021
022import org.fcrepo.kernel.api.exception.RepositoryRuntimeException;
023import org.fcrepo.metrics.RegistryService;
024
025import java.io.File;
026
027import javax.inject.Inject;
028import javax.jcr.Repository;
029import javax.jcr.RepositoryException;
030import javax.jcr.Session;
031
032import org.fcrepo.kernel.api.services.RepositoryService;
033import org.modeshape.jcr.api.Problems;
034import org.modeshape.jcr.api.RepositoryManager;
035import org.slf4j.Logger;
036import org.springframework.stereotype.Component;
037
038import com.codahale.metrics.Timer;
039
040/**
041 * Service for repository-wide management and querying
042 *
043 * @author Chris Beer
044 * @since Mar 11, 2013
045 */
046@Component
047public class RepositoryServiceImpl extends AbstractService implements RepositoryService {
048
049    @Inject
050    private Repository repo;
051
052    private static final Logger LOGGER = getLogger(RepositoryServiceImpl.class);
053
054    private final Timer objectSizeCalculationTimer = RegistryService.getInstance().getMetrics().timer(
055            name(RepositoryService.class, "objectSizeCalculation"));
056
057    /**
058     * Calculate the total size of all the binary properties in the repository
059     *
060     * @return size in bytes
061     */
062    @Override
063    public Long getRepositorySize() {
064        try {
065
066            LOGGER.debug("Calculating repository size from index");
067
068            try (final Timer.Context context = objectSizeCalculationTimer.time()) {
069                // Differentiating between the local getRepositorySize and
070                // ServiceHelpers
071                return ServiceHelpers.getRepositorySize(repo);
072
073            }
074        } catch (final RepositoryException e) {
075            throw new RepositoryRuntimeException(e);
076        }
077    }
078
079    /*
080     * (non-Javadoc)
081     * @see
082     * org.fcrepo.kernel.api.services.RepositoryService#getRepositoryObjectCount()
083     */
084    @Override
085    public Long getRepositoryObjectCount() {
086        try {
087            return getRepositoryCount(repo);
088        } catch (final RepositoryException e) {
089            throw new RepositoryRuntimeException(e);
090        }
091    }
092
093    /*
094     * (non-Javadoc)
095     * @see
096     * org.fcrepo.kernel.api.services.RepositoryService#backupRepository(javax.jcr
097     * .Session, java.io.File)
098     */
099    @Override
100    public Problems backupRepository(final Session session,
101                                     final File backupDirectory) {
102        try {
103            final RepositoryManager repoMgr = ((org.modeshape.jcr.api.Session) session)
104                    .getWorkspace()
105                    .getRepositoryManager();
106
107            return repoMgr.backupRepository(backupDirectory);
108        } catch (final RepositoryException e) {
109            throw new RepositoryRuntimeException(e);
110        }
111    }
112
113    /*
114     * (non-Javadoc)
115     * @see
116     * org.fcrepo.kernel.api.services.RepositoryService#restoreRepository(javax.
117     * jcr.Session, java.io.File)
118     */
119    @Override
120    public Problems restoreRepository(final Session session,
121                                      final File backupDirectory) {
122        try {
123            final RepositoryManager repoMgr = ((org.modeshape.jcr.api.Session) session)
124                    .getWorkspace()
125                    .getRepositoryManager();
126
127            return repoMgr.restoreRepository(backupDirectory);
128        } catch (final RepositoryException e) {
129            throw new RepositoryRuntimeException(e);
130        }
131    }
132
133}