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.impl.utils.impl;
017
018import org.fcrepo.kernel.impl.utils.BinaryCacheEntry;
019import org.fcrepo.kernel.utils.CacheEntry;
020import org.fcrepo.kernel.impl.utils.ProjectedCacheEntry;
021import org.modeshape.jcr.GetBinaryStore;
022import org.modeshape.jcr.value.BinaryValue;
023import org.modeshape.jcr.value.binary.BinaryStore;
024import org.modeshape.jcr.value.binary.CompositeBinaryStore;
025import org.modeshape.jcr.value.binary.ExternalBinaryValue;
026import org.modeshape.jcr.value.binary.FileSystemBinaryStore;
027import org.modeshape.jcr.value.binary.InMemoryBinaryValue;
028import org.modeshape.jcr.value.binary.infinispan.InfinispanBinaryStore;
029
030import javax.jcr.Binary;
031import javax.jcr.Property;
032import javax.jcr.Repository;
033import javax.jcr.RepositoryException;
034
035/**
036 * @author cabeer
037 */
038public final class CacheEntryFactory {
039    private static GetBinaryStore getBinaryStore = new GetBinaryStore();
040
041    /**
042     * No public constructor on utility class
043     */
044    private CacheEntryFactory() {
045    }
046
047    /**
048     * Load a store-specific CacheEntry model
049     * @param repository the repository
050     * @param property the property
051     * @return CacheEntry model for the property in the given repository
052     * @throws RepositoryException if repository exception occurred
053     */
054    public static CacheEntry forProperty(final Repository repository, final Property property)
055        throws RepositoryException {
056        final Binary binary = property.getBinary();
057        final BinaryStore store = binaryStore(repository);
058
059        if (binary instanceof ExternalBinaryValue) {
060            return new ProjectedCacheEntry(property);
061        } else if (binary instanceof InMemoryBinaryValue) {
062            return new BinaryCacheEntry(property);
063        } else {
064            return forProperty(store, property);
065        }
066    }
067
068    /**
069     * Get a store-specific Cache Entry
070     * @param store the store
071     * @param property the property
072     * @return store specific cache entry
073     * @throws RepositoryException if repository exception occurred
074     */
075    public static CacheEntry forProperty(final BinaryStore store, final Property property)
076        throws RepositoryException {
077        final BinaryValue binary = (BinaryValue)property.getBinary();
078        if (store instanceof InfinispanBinaryStore) {
079            return new InfinispanCacheStoreEntry((InfinispanBinaryStore)store, property);
080        } else if (store instanceof FileSystemBinaryStore) {
081            return new FileSystemBinaryStoreEntry((FileSystemBinaryStore)store, property);
082        } else if (store instanceof CompositeBinaryStore) {
083            final CompositeBinaryStore compositeBinaryStore = (CompositeBinaryStore) store;
084            final BinaryStore binaryStoreContainingKey
085                = compositeBinaryStore.findBinaryStoreContainingKey(binary.getKey());
086            return forProperty(binaryStoreContainingKey, property);
087        } else {
088            return new LocalBinaryStoreEntry(store, property);
089        }
090    }
091
092    private static BinaryStore binaryStore(final Repository repo) {
093        final BinaryStore store = getBinaryStore.apply(repo);
094        assert store != null;
095        return store;
096    }
097
098}