001/*
002 * (C) Copyright 2011-2014 Nuxeo SA (http://nuxeo.com/) and others.
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 *
016 * Contributors:
017 *     Florent Guillaume
018 */
019package org.nuxeo.ecm.core.blob.binary;
020
021import java.io.File;
022import java.io.FileInputStream;
023import java.io.IOException;
024import java.io.InputStream;
025
026import org.nuxeo.ecm.core.blob.BlobManager;
027import org.nuxeo.ecm.core.blob.BlobProvider;
028import org.nuxeo.runtime.api.Framework;
029
030/**
031 * Lazy Binary that fetches its remote stream on first access.
032 */
033public class LazyBinary extends Binary {
034
035    private static final long serialVersionUID = 1L;
036
037    protected boolean hasLength;
038
039    // transient to be Serializable
040    protected transient CachingBinaryManager cbm;
041
042    public LazyBinary(String digest, String repoName, CachingBinaryManager cbm) {
043        super(digest, repoName);
044        this.cbm = cbm;
045    }
046
047    public LazyBinary(String digest, long length, String repoName, CachingBinaryManager cbm) {
048        this(digest, repoName, cbm);
049        this.length = length;
050        hasLength = true;
051    }
052
053    // because the class is Serializable, re-acquire the CachingBinaryManager
054    protected CachingBinaryManager getCachingBinaryManager() {
055        if (cbm == null) {
056            if (blobProviderId == null) {
057                throw new UnsupportedOperationException("Cannot find binary manager, no blob provider id");
058            }
059            BlobManager bm = Framework.getService(BlobManager.class);
060            BlobProvider bp = bm.getBlobProvider(blobProviderId);
061            cbm = (CachingBinaryManager) bp.getBinaryManager();
062        }
063        return cbm;
064    }
065
066    @Override
067    public InputStream getStream() throws IOException {
068        File file = getFile();
069        return file == null ? null : new FileInputStream(file);
070    }
071
072    @Override
073    public File getFile() {
074        File file;
075        try {
076            file = getCachingBinaryManager().getFile(digest);
077        } catch (IOException e) {
078            throw new RuntimeException(e);
079        }
080        if (file != null) {
081            length = file.length();
082            hasLength = true;
083        }
084        return file;
085    }
086
087    @Override
088    public long getLength() {
089        if (!hasLength) {
090            Long len;
091            try {
092                len = getCachingBinaryManager().getLength(digest);
093            } catch (IOException e) {
094                throw new RuntimeException(e);
095            }
096            length = len == null ? 0 : len.longValue();
097            hasLength = true;
098        }
099        return length;
100    }
101
102}