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.rdf.impl;
017
018import static com.google.common.collect.ImmutableSet.builder;
019import static com.hp.hpl.jena.graph.Triple.create;
020import static com.hp.hpl.jena.rdf.model.ResourceFactory.createTypedLiteral;
021import static org.fcrepo.kernel.api.RdfLexicon.HAS_FIXITY_CHECK_COUNT;
022import static org.fcrepo.kernel.api.RdfLexicon.HAS_FIXITY_ERROR_COUNT;
023import static org.fcrepo.kernel.api.RdfLexicon.HAS_FIXITY_REPAIRED_COUNT;
024import static org.fcrepo.kernel.modeshape.FedoraJcrConstants.ROOT;
025import static org.slf4j.LoggerFactory.getLogger;
026
027import com.hp.hpl.jena.rdf.model.Resource;
028import org.fcrepo.kernel.api.identifiers.IdentifierConverter;
029import org.fcrepo.kernel.api.models.FedoraResource;
030import org.fcrepo.metrics.RegistryService;
031
032import java.util.Map;
033import org.slf4j.Logger;
034
035import com.codahale.metrics.Counter;
036import com.google.common.collect.ImmutableSet;
037import com.hp.hpl.jena.graph.Triple;
038
039/**
040 * Assemble {@link Triple}s derived from the root of a repository.
041 *
042 * @author ajs6f
043 * @since Oct 18, 2013
044 */
045public class RootRdfContext extends NodeRdfContext {
046
047    private static final String PREFIX = "org.fcrepo.services.";
048    private static final String FIXITY_REPAIRED_COUNTER = "LowLevelStorageService.fixity-repaired-counter";
049    private static final String FIXITY_ERROR_COUNTER = "LowLevelStorageService.fixity-error-counter";
050    private static final String FIXITY_CHECK_COUNTER = "LowLevelStorageService.fixity-check-counter";
051    private static final Logger LOGGER = getLogger(RootRdfContext.class);
052    static final RegistryService registryService = RegistryService.getInstance();
053
054    /**
055     * Ordinary constructor.
056     *
057     * @param resource the resource
058     * @param idTranslator the id translator
059     */
060    public RootRdfContext(final FedoraResource resource,
061                          final IdentifierConverter<Resource, FedoraResource> idTranslator) {
062        super(resource, idTranslator);
063
064        if (resource().hasType(ROOT)) {
065            concatRepositoryTriples();
066        }
067    }
068
069    private void concatRepositoryTriples() {
070        LOGGER.trace("Creating RDF triples for repository description");
071
072        final ImmutableSet.Builder<Triple> b = builder();
073
074        /*
075            FIXME: removing due to performance problems, esp. w/ many files on federated filesystem
076            see: https://www.pivotaltracker.com/story/show/78647248
077
078            final Repository repository = session().getRepository();
079
080            b.add(create(subject(), HAS_OBJECT_COUNT.asNode(), createLiteral(String
081                    .valueOf(getRepositoryCount(repository)))));
082            b.add(create(subject(), HAS_OBJECT_SIZE.asNode(), createLiteral(String
083                    .valueOf(getRepositorySize(repository)))));
084        */
085
086        // retrieve the metrics from the service
087        final Map<String, Counter> counters = registryService.getMetrics().getCounters();
088        // and add the repository metrics to the RDF model
089        if (counters.containsKey(FIXITY_CHECK_COUNTER)) {
090            b.add(create(subject(), HAS_FIXITY_CHECK_COUNT.asNode(),
091                    createTypedLiteral(counters.get(PREFIX + FIXITY_CHECK_COUNTER).getCount()).asNode()));
092        }
093
094        if (counters.containsKey(FIXITY_ERROR_COUNTER)) {
095            b.add(create(subject(), HAS_FIXITY_ERROR_COUNT.asNode(),
096                    createTypedLiteral(counters.get(PREFIX + FIXITY_ERROR_COUNTER).getCount()).asNode()));
097        }
098
099        if (counters.containsKey(FIXITY_REPAIRED_COUNTER)) {
100            b.add(create(subject(), HAS_FIXITY_REPAIRED_COUNT.asNode(),
101                    createTypedLiteral(counters.get(PREFIX + FIXITY_REPAIRED_COUNTER).getCount()).asNode()));
102        }
103
104        // offer all these accumulated triples
105        concat(b.build());
106    }
107}