001/* 002 * (C) Copyright 2018 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 * pierre 018 */ 019package org.nuxeo.ecm.core.bulk.action.computation; 020 021import java.io.IOException; 022import java.nio.file.Files; 023import java.nio.file.Path; 024import java.util.Collections; 025import java.util.List; 026 027import org.apache.logging.log4j.LogManager; 028import org.apache.logging.log4j.Logger; 029import org.nuxeo.ecm.core.api.Blob; 030import org.nuxeo.ecm.core.transientstore.api.TransientStore; 031import org.nuxeo.ecm.core.transientstore.api.TransientStoreService; 032import org.nuxeo.lib.stream.computation.AbstractComputation; 033import org.nuxeo.lib.stream.computation.ComputationContext; 034import org.nuxeo.runtime.api.Framework; 035 036/** 037 * @since 10.3 038 */ 039public abstract class AbstractTransientBlobComputation extends AbstractComputation { 040 041 private static final Logger log = LogManager.getLogger(AbstractTransientBlobComputation.class); 042 043 private Path temp; 044 045 @Override 046 public void init(ComputationContext context) { 047 super.init(context); 048 try { 049 temp = Files.createTempDirectory(metadata().name()); 050 } catch (IOException e) { 051 throw new IllegalStateException("Cannot create temp directory for " + this); 052 } 053 } 054 055 public AbstractTransientBlobComputation(String name) { 056 super(name, 1, 1); 057 } 058 059 public AbstractTransientBlobComputation(String name, int nbOutputStreams) { 060 super(name, 1, nbOutputStreams); 061 } 062 063 064 protected String getTransientStoreKey(String commandId) { 065 return metadata.name() + commandId; 066 } 067 068 public Blob getBlob(String key, String storeName) { 069 TransientStore store = Framework.getService(TransientStoreService.class).getStore(storeName); 070 List<Blob> blobs = store.getBlobs(key); 071 Blob blob = blobs == null || blobs.isEmpty() ? null : blobs.get(0); 072 if (blob == null) { 073 log.error("[{}] Could not retrieve blob for key {}", metadata.name(), key); 074 } 075 return blob; 076 } 077 078 protected void storeBlob(Blob blob, String commandId, String storeName) { 079 TransientStore store = Framework.getService(TransientStoreService.class).getStore(storeName); 080 store.putBlobs(getTransientStoreKey(commandId), Collections.singletonList(blob)); 081 store.setCompleted(getTransientStoreKey(commandId), true); 082 } 083 084 protected Path createTemp(String commandId) { 085 return temp.resolve(commandId + ".csv"); 086 } 087}