001/* 002 * (C) Copyright 2015 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 * Benoit Delbosc 018 */ 019package org.nuxeo.importer.stream.automation; 020 021import static org.nuxeo.importer.stream.automation.BlobConsumers.DEFAULT_LOG_CONFIG; 022 023import org.apache.commons.logging.Log; 024import org.apache.commons.logging.LogFactory; 025import org.nuxeo.ecm.automation.OperationContext; 026import org.nuxeo.ecm.automation.core.Constants; 027import org.nuxeo.ecm.automation.core.annotations.Context; 028import org.nuxeo.ecm.automation.core.annotations.Operation; 029import org.nuxeo.ecm.automation.core.annotations.OperationMethod; 030import org.nuxeo.ecm.automation.core.annotations.Param; 031import org.nuxeo.ecm.core.api.NuxeoPrincipal; 032import org.nuxeo.importer.stream.message.BlobMessage; 033import org.nuxeo.importer.stream.producer.RandomStringBlobMessageProducerFactory; 034import org.nuxeo.lib.stream.log.LogManager; 035import org.nuxeo.lib.stream.pattern.producer.ProducerPool; 036import org.nuxeo.runtime.api.Framework; 037import org.nuxeo.runtime.stream.StreamService; 038 039/** 040 * @since 9.1 041 */ 042@Operation(id = RandomBlobProducers.ID, category = Constants.CAT_SERVICES, label = "Produces random blobs", since = "9.1", description = "Produces random blobs in a Log.") 043public class RandomBlobProducers { 044 private static final Log log = LogFactory.getLog(RandomBlobProducers.class); 045 046 public static final String ID = "StreamImporter.runRandomBlobProducers"; 047 048 public static final String DEFAULT_BLOB_LOG_NAME = "import-blob"; 049 050 @Context 051 protected OperationContext ctx; 052 053 @Param(name = "nbBlobs") 054 protected Integer nbBlobs; 055 056 @Param(name = "nbThreads", required = false) 057 protected Integer nbThreads = 8; 058 059 @Param(name = "avgBlobSizeKB", required = false) 060 protected Integer avgBlobSizeKB = 1; 061 062 @Param(name = "lang", required = false) 063 protected String lang = "en_US"; 064 065 @Param(name = "logName", required = false) 066 protected String logName; 067 068 @Param(name = "logSize", required = false) 069 protected Integer logSize; 070 071 @Param(name = "logConfig", required = false) 072 protected String logConfig; 073 074 @Param(name = "blobMarker", required = false) 075 protected String blobMarker; 076 077 @OperationMethod 078 public void run() { 079 checkAccess(ctx); 080 StreamService service = Framework.getService(StreamService.class); 081 LogManager manager = service.getLogManager(getLogConfig()); 082 try { 083 manager.createIfNotExists(getLogName(), getLogSize()); 084 try (ProducerPool<BlobMessage> producers = new ProducerPool<>(getLogName(), manager, 085 new RandomStringBlobMessageProducerFactory(nbBlobs, lang, avgBlobSizeKB, blobMarker), 086 nbThreads.shortValue())) { 087 producers.start().get(); 088 } 089 } catch (Exception e) { 090 log.error(e.getMessage(), e); 091 } 092 } 093 094 protected String getLogConfig() { 095 if (logConfig != null) { 096 return logConfig; 097 } 098 return DEFAULT_LOG_CONFIG; 099 } 100 101 protected String getLogName() { 102 if (logName != null) { 103 return logName; 104 } 105 return DEFAULT_BLOB_LOG_NAME; 106 } 107 108 protected int getLogSize() { 109 if (logSize != null && logSize > 0) { 110 return logSize; 111 } 112 return nbThreads; 113 } 114 115 protected static void checkAccess(OperationContext context) { 116 NuxeoPrincipal principal = (NuxeoPrincipal) context.getPrincipal(); 117 if (principal == null || !principal.isAdministrator()) { 118 throw new RuntimeException("Unauthorized access: " + principal); 119 } 120 } 121}