001/* 002 * (C) Copyright 2018 Nuxeo (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 * Benoit Delbosc 018 */ 019package org.nuxeo.importer.stream.automation; 020 021import static org.apache.commons.lang3.StringUtils.isEmpty; 022import static org.nuxeo.importer.stream.StreamImporters.DEFAULT_LOG_BLOB_NAME; 023import static org.nuxeo.importer.stream.StreamImporters.DEFAULT_LOG_CONFIG; 024 025import java.io.File; 026import java.util.concurrent.ExecutionException; 027 028import org.apache.commons.logging.Log; 029import org.apache.commons.logging.LogFactory; 030import org.nuxeo.ecm.automation.OperationContext; 031import org.nuxeo.ecm.automation.OperationException; 032import org.nuxeo.ecm.automation.core.Constants; 033import org.nuxeo.ecm.automation.core.annotations.Context; 034import org.nuxeo.ecm.automation.core.annotations.Operation; 035import org.nuxeo.ecm.automation.core.annotations.OperationMethod; 036import org.nuxeo.ecm.automation.core.annotations.Param; 037import org.nuxeo.ecm.core.api.NuxeoPrincipal; 038import org.nuxeo.importer.stream.StreamImporters; 039import org.nuxeo.importer.stream.message.BlobMessage; 040import org.nuxeo.importer.stream.producer.FileBlobMessageProducerFactory; 041import org.nuxeo.lib.stream.codec.Codec; 042import org.nuxeo.lib.stream.log.LogManager; 043import org.nuxeo.lib.stream.log.Name; 044import org.nuxeo.lib.stream.pattern.producer.ProducerPool; 045import org.nuxeo.runtime.api.Framework; 046import org.nuxeo.runtime.stream.StreamService; 047 048/** 049 * Create blob messages using a list of files. 050 * 051 * @since 10.2 052 */ 053@Operation(id = FileBlobProducers.ID, category = Constants.CAT_SERVICES, label = "Produces blobs from a list of files", since = "10.2", description = "Produces blobs from a list of files.") 054public class FileBlobProducers { 055 private static final Log log = LogFactory.getLog(FileBlobProducers.class); 056 057 public static final String ID = "StreamImporter.runFileBlobProducers"; 058 059 @Context 060 protected OperationContext ctx; 061 062 @Param(name = "listFile") 063 protected String listFile; 064 065 @Param(name = "nbBlobs", required = false) 066 protected Integer nbBlobs; 067 068 @Param(name = "nbThreads", required = false) 069 protected Integer nbThreads = 1; 070 071 @Param(name = "logName", required = false) 072 protected String logName = DEFAULT_LOG_BLOB_NAME; 073 074 @Param(name = "logSize", required = false) 075 protected Integer logSize; 076 077 @Param(name = "basePath", required = false) 078 protected String basePath; 079 080 @OperationMethod 081 public void run() throws OperationException { 082 checkAccess(ctx); 083 LogManager manager = Framework.getService(StreamService.class).getLogManager(); 084 manager.createIfNotExists(Name.ofUrn(logName), getLogSize()); 085 Codec<BlobMessage> codec = StreamImporters.getBlobCodec(); 086 try (ProducerPool<BlobMessage> producers = new ProducerPool<>(logName, manager, codec, 087 new FileBlobMessageProducerFactory(getListFile(), getBasePath(), getNbBlobs()), 088 nbThreads.shortValue())) { 089 producers.start().get(); 090 } catch (InterruptedException e) { 091 Thread.currentThread().interrupt(); 092 log.warn("Operation interrupted"); 093 throw new RuntimeException(e); 094 } catch (ExecutionException e) { 095 log.error("Operation fails", e); 096 throw new OperationException(e); 097 } 098 } 099 100 protected long getNbBlobs() { 101 if (nbBlobs == null) { 102 // this means all the files listed in the files 103 return 0; 104 } 105 return nbBlobs.longValue(); 106 } 107 108 protected int getLogSize() { 109 if (logSize != null && logSize > 0) { 110 return logSize; 111 } 112 return nbThreads; 113 } 114 115 protected String getBasePath() { 116 if (isEmpty(basePath)) { 117 return null; 118 } 119 if (!new File(basePath).exists()) { 120 throw new IllegalArgumentException("Can not access basePath: " + basePath); 121 } 122 return basePath; 123 } 124 125 protected File getListFile() { 126 File ret = new File(listFile); 127 if (!ret.exists() || !ret.canRead()) { 128 throw new IllegalArgumentException("Can not access or read listFile: " + listFile); 129 } 130 return ret; 131 } 132 133 protected static void checkAccess(OperationContext context) { 134 NuxeoPrincipal principal = context.getPrincipal(); 135 if (principal == null || !principal.isAdministrator()) { 136 throw new RuntimeException("Unauthorized access: " + principal); 137 } 138 } 139}