/* * Copyright (c) 2006-2011 Nuxeo SA (http://nuxeo.com/) and others. * * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * Florent Guillaume */ package org.eclipse.ecr.core.storage.sql.coremodel; import java.io.IOException; import java.io.InputStream; import java.io.ObjectStreamException; import java.io.Serializable; import org.eclipse.ecr.core.api.Blob; import org.eclipse.ecr.core.api.impl.blob.DefaultStreamBlob; import org.eclipse.ecr.core.api.impl.blob.StreamingBlob; import org.eclipse.ecr.core.storage.sql.Binary; import org.eclipse.ecr.runtime.api.Framework; import org.eclipse.ecr.runtime.services.streaming.StreamSource; /** * A {@link Blob} wrapping a {@link Binary} value. * * @author Florent Guillaume * @author Bogdan Stefanescu */ public class SQLBlob extends DefaultStreamBlob implements Serializable { private static final long serialVersionUID = 1L; /** * By default the SQLBlob is remotable through Nuxeo streaming service. * You can disable this by defining the following runtime (or system) property: * <code>org.eclipse.ecr.core.storage.sql.blob_streaming = false</code> * This way the blob will use the default serialization (file serialization) that is optimized for * servers that are using a shared file system (and not nuxeo streaming) */ public static final boolean IS_STREAMING_ENABLED = Boolean.parseBoolean( Framework.getProperty("org.eclipse.ecr.core.storage.sql.blob_streaming", "true")); protected final Binary binary; public SQLBlob(Binary binary, String filename, String mimeType, String encoding, String digest) { this.binary = binary; setFilename(filename); setMimeType(mimeType); setEncoding(encoding); setDigest(digest); } @Override public long getLength() { return binary.getLength(); } @Override public InputStream getStream() throws IOException { return binary.getStream(); } @Override public boolean isPersistent() { return true; } @Override public Blob persist() { return this; } public Binary getBinary() { return binary; } /** * Replace this object with a {@link StreamingBlob} when serialized. * The StreamingBlob object can be sent to remote machines through nuxeo streaming mechanism. * If IS_STREAMING_ENABLED is false then no replace takes place. * * @return a streaming blob that points to the same content as this one * @throws ObjectStreamException */ public Object writeReplace() throws ObjectStreamException { if (IS_STREAMING_ENABLED) { StreamSource src = binary.getStreamSource(); return new StreamingBlob(src, getMimeType(), getEncoding(), getFilename(), getDigest()); } else { return this; } } }