001/* 002 * (C) Copyright 2015 Nuxeo SAS (http://nuxeo.com/) and contributors. 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the GNU Lesser General Public License 006 * (LGPL) version 2.1 which accompanies this distribution, and is available at 007 * http://www.gnu.org/licenses/lgpl.html 008 * 009 * This library is distributed in the hope that it will be useful, 010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 012 * Lesser General Public License for more details. 013 * 014 * Contributors: 015 * Nuxeo - initial API and implementation 016 */ 017 018package org.nuxeo.ecm.core.transientstore; 019 020import java.io.File; 021import java.io.IOException; 022import java.io.Serializable; 023import java.util.ArrayList; 024import java.util.HashMap; 025import java.util.List; 026import java.util.Map; 027import java.util.UUID; 028 029import org.nuxeo.ecm.core.api.Blob; 030import org.nuxeo.ecm.core.api.NuxeoException; 031import org.nuxeo.ecm.core.api.impl.blob.FileBlob; 032import org.nuxeo.ecm.core.transientstore.api.StorageEntry; 033 034/** 035 * Base class for {@link StorageEntry} implementation 036 * 037 * @author <a href="mailto:tdelprat@nuxeo.com">Tiry</a> 038 * @since 7.2 039 */ 040public abstract class AbstractStorageEntry implements StorageEntry { 041 042 private static final long serialVersionUID = 1L; 043 044 protected final String id; 045 046 protected boolean hasBlobs = false; 047 048 protected Map<String, Serializable> params; 049 050 protected transient List<Blob> blobs; 051 052 protected List<Map<String, String>> cachedBlobs; 053 054 protected long lastStorageSize; 055 056 protected boolean completed; 057 058 protected AbstractStorageEntry(String id) { 059 this.id = id; 060 } 061 062 @Override 063 public String getId() { 064 return id; 065 } 066 067 @Override 068 public void setBlobs(List<Blob> blobs) { 069 this.blobs = blobs; 070 hasBlobs = blobs != null; 071 } 072 073 @Override 074 public List<Blob> getBlobs() { 075 return blobs; 076 } 077 078 @Override 079 public void put(String key, Serializable value) { 080 if (params == null) { 081 params = new HashMap<>(); 082 } 083 params.put(key, value); 084 } 085 086 @Override 087 public void putAll(Map<String, Serializable> p) { 088 if (params == null) { 089 params = new HashMap<>(); 090 } 091 params.putAll(p); 092 093 } 094 095 @Override 096 public Serializable get(String key) { 097 if (params != null) { 098 return params.get(key); 099 } 100 return null; 101 } 102 103 @Override 104 public Map<String, Serializable> getParameters() { 105 return params; 106 } 107 108 @Override 109 public void persist(File directory) { 110 lastStorageSize = getSize(); 111 if (hasBlobs) { 112 cachedBlobs = new ArrayList<>(); 113 for (Blob blob : blobs) { 114 Map<String, String> cached = new HashMap<>(); 115 File cachedFile = new File(directory, UUID.randomUUID().toString()); 116 try { 117 if (blob instanceof FileBlob && ((FileBlob) blob).isTemporary()) { 118 ((FileBlob) blob).moveTo(cachedFile); 119 } else { 120 blob.transferTo(cachedFile); 121 } 122 } catch (IOException e) { 123 throw new NuxeoException(e); 124 } 125 cached.put("file", cachedFile.getAbsolutePath()); 126 cached.put("filename", blob.getFilename()); 127 cached.put("encoding", blob.getEncoding()); 128 cached.put("mimetype", blob.getMimeType()); 129 cached.put("digest", blob.getDigest()); 130 cachedBlobs.add(cached); 131 } 132 blobs = null; 133 } 134 } 135 136 @Override 137 public void load(File directory) { 138 if (!hasBlobs || blobs != null) { 139 return; 140 } 141 blobs = new ArrayList<>(); 142 for (Map<String, String> info : cachedBlobs) { 143 File cachedFile = new File(info.get("file")); 144 Blob blob = new FileBlob(cachedFile); 145 blob.setEncoding(info.get("encoding")); 146 blob.setMimeType(info.get("mimetype")); 147 blob.setFilename(info.get("filename")); 148 blob.setDigest(info.get("digest")); 149 blobs.add(blob); 150 } 151 cachedBlobs = null; 152 } 153 154 @Override 155 public long getSize() { 156 int size = 0; 157 if (blobs != null) { 158 for (Blob blob : blobs) { 159 size += blob.getLength(); 160 } 161 } 162 return size; 163 } 164 165 @Override 166 public long getLastStorageSize() { 167 return lastStorageSize; 168 } 169 170 public void setLastStorageSize(long lastStorageSize) { 171 this.lastStorageSize = lastStorageSize; 172 } 173 174 @Override 175 public boolean isCompleted() { 176 return completed; 177 } 178 179 @Override 180 public void setCompleted(boolean completed) { 181 this.completed = completed; 182 } 183 184}