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 static java.util.Objects.hash;
019
020import java.io.InputStream;
021import java.net.URI;
022import java.net.URISyntaxException;
023
024import javax.jcr.Property;
025import javax.jcr.RepositoryException;
026
027import org.fcrepo.kernel.impl.services.functions.GetBinaryKey;
028import org.fcrepo.kernel.impl.utils.BasicCacheEntry;
029import org.modeshape.jcr.value.BinaryKey;
030import org.modeshape.jcr.value.binary.BinaryStore;
031
032/**
033 * A LowLevelCacheEntry within a local binary store
034 *
035 * @author awoods
036 */
037public class LocalBinaryStoreEntry extends BasicCacheEntry {
038
039    private GetBinaryKey getBinaryKey = new GetBinaryKey();
040    private final BinaryStore store;
041    private final Property property;
042
043    /**
044     * @param store a Modeshape BinaryStore
045     * @param property the property we're interested in
046     */
047    public LocalBinaryStoreEntry(final BinaryStore store, final Property property) {
048        this.property = property;
049        this.store = store;
050    }
051
052    BinaryStore store() {
053        return store;
054    }
055
056    /**
057     * Get a raw input stream from the underlying store
058     * @return the content for this entry
059     */
060    @Override
061    public InputStream getInputStream() throws RepositoryException {
062        return store.getInputStream(binaryKey());
063    }
064
065    /**
066     * Generate a human-readable identifier for the location of this entry
067     *
068     * @return human-readable identifier for the location of this entry
069     */
070    @Override
071    public String getExternalIdentifier() {
072        try {
073            return new URI("info", store.toString(), null) + "/" + binaryKey();
074        } catch (URISyntaxException e) {
075            return binaryKey().toString();
076        }
077    }
078
079    /**
080     * Two LowLevelCacheEntries are the same if they have the same key,
081     * come from the same BinaryStore,
082     * and have the same underlying store configuration
083     * @param other the other object
084     * @return true if the given binary store entries have the same key
085     */
086    @Override
087    public boolean equals(final Object other) {
088        if (other instanceof LocalBinaryStoreEntry) {
089            final LocalBinaryStoreEntry that = (LocalBinaryStoreEntry) other;
090
091            return property().equals(that.property()) &&
092                   ((store == null && that.store == null) ||
093                    (store != null && store.equals(that.store)));
094        }
095        return false;
096    }
097
098    @Override
099    public int hashCode() {
100        return hash(store, property);
101    }
102
103    protected Property property() {
104        return property;
105    }
106
107    protected BinaryKey binaryKey() {
108        return getBinaryKey.apply(property);
109    }
110
111}