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.utils.impl;
019
020import static org.fcrepo.kernel.api.RdfLexicon.PROXY_FOR;
021import static org.fcrepo.kernel.api.RdfLexicon.REDIRECTS_TO;
022import static org.slf4j.LoggerFactory.getLogger;
023
024import org.fcrepo.kernel.api.utils.CacheEntry;
025import org.fcrepo.kernel.modeshape.utils.BinaryCacheEntry;
026import org.fcrepo.kernel.modeshape.utils.ExternalResourceCacheEntry;
027import org.fcrepo.kernel.modeshape.utils.ProjectedCacheEntry;
028import org.modeshape.jcr.value.binary.ExternalBinaryValue;
029import org.slf4j.Logger;
030
031import javax.jcr.Binary;
032import javax.jcr.Property;
033import javax.jcr.RepositoryException;
034
035/**
036 * @author lsitu
037 * @author cabeer
038 */
039public final class CacheEntryFactory {
040
041    private static final Logger LOGGER = getLogger(CacheEntryFactory.class);
042
043    /**
044     * No public constructor on utility class
045     */
046    private CacheEntryFactory() {
047    }
048
049    /**
050     * Load a store-specific CacheEntry model
051     * @param property the property
052     * @return CacheEntry model for the property in the given repository
053     * @throws RepositoryException if repository exception occurred
054     */
055    public static CacheEntry forProperty(final Property property) throws RepositoryException {
056        // if it's an external binary, catch that here and treat it differently.
057        if (property.getName().endsWith(PROXY_FOR.getLocalName()) ||
058                property.getName().endsWith(REDIRECTS_TO.getLocalName())) {
059            LOGGER.debug("Creating ExternalResourceCacheEntry for property: {} {}", property.getName(),
060                    property.getValue().toString());
061            return new ExternalResourceCacheEntry(property);
062        }
063
064        final Binary binary = property.getBinary();
065
066        if (binary instanceof ExternalBinaryValue) {
067           return new ProjectedCacheEntry(property);
068        }
069        return new BinaryCacheEntry(property);
070    }
071}