001/** 002 * Copyright 2014 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.impl.services; 017 018import static com.codahale.metrics.MetricRegistry.name; 019import static com.google.common.base.Throwables.propagate; 020import static org.fcrepo.kernel.impl.services.ServiceHelpers.getRepositoryCount; 021import static org.slf4j.LoggerFactory.getLogger; 022 023import org.fcrepo.kernel.exception.RepositoryRuntimeException; 024import org.fcrepo.metrics.RegistryService; 025 026import java.io.File; 027 028import javax.inject.Inject; 029import javax.jcr.Repository; 030import javax.jcr.RepositoryException; 031import javax.jcr.Session; 032 033import org.fcrepo.kernel.services.RepositoryService; 034import org.modeshape.jcr.api.Problems; 035import org.modeshape.jcr.api.RepositoryManager; 036import org.slf4j.Logger; 037import org.springframework.stereotype.Component; 038 039import com.codahale.metrics.Timer; 040 041/** 042 * Service for repository-wide management and querying 043 * 044 * @author Chris Beer 045 * @since Mar 11, 2013 046 */ 047@Component 048public class RepositoryServiceImpl extends AbstractService implements RepositoryService { 049 050 @Inject 051 private Repository repo; 052 053 private static final Logger LOGGER = getLogger(RepositoryServiceImpl.class); 054 055 private final Timer objectSizeCalculationTimer = RegistryService.getInstance().getMetrics().timer( 056 name(RepositoryService.class, "objectSizeCalculation")); 057 058 /** 059 * Calculate the total size of all the binary properties in the repository 060 * 061 * @return size in bytes 062 */ 063 @Override 064 public Long getRepositorySize() { 065 try { 066 067 LOGGER.debug("Calculating repository size from index"); 068 069 try (final Timer.Context context = objectSizeCalculationTimer.time()) { 070 // Differentiating between the local getRepositorySize and 071 // ServiceHelpers 072 return ServiceHelpers.getRepositorySize(repo); 073 074 } 075 } catch (final RepositoryException e) { 076 throw propagate(e); 077 } 078 } 079 080 /* 081 * (non-Javadoc) 082 * @see 083 * org.fcrepo.kernel.services.RepositoryService#getRepositoryObjectCount() 084 */ 085 @Override 086 public Long getRepositoryObjectCount() { 087 try { 088 return getRepositoryCount(repo); 089 } catch (final RepositoryException e) { 090 throw propagate(e); 091 } 092 } 093 094 /* 095 * (non-Javadoc) 096 * @see 097 * org.fcrepo.kernel.services.RepositoryService#backupRepository(javax.jcr 098 * .Session, java.io.File) 099 */ 100 @Override 101 public Problems backupRepository(final Session session, 102 final File backupDirectory) { 103 try { 104 final RepositoryManager repoMgr = ((org.modeshape.jcr.api.Session) session) 105 .getWorkspace() 106 .getRepositoryManager(); 107 108 return repoMgr.backupRepository(backupDirectory); 109 } catch (final RepositoryException e) { 110 throw new RepositoryRuntimeException(e); 111 } 112 } 113 114 /* 115 * (non-Javadoc) 116 * @see 117 * org.fcrepo.kernel.services.RepositoryService#restoreRepository(javax. 118 * jcr.Session, java.io.File) 119 */ 120 @Override 121 public Problems restoreRepository(final Session session, 122 final File backupDirectory) { 123 try { 124 final RepositoryManager repoMgr = ((org.modeshape.jcr.api.Session) session) 125 .getWorkspace() 126 .getRepositoryManager(); 127 128 return repoMgr.restoreRepository(backupDirectory); 129 } catch (final RepositoryException e) { 130 throw new RepositoryRuntimeException(e); 131 } 132 } 133 134}