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.ecm.platform.importer.mqueues.automation;
020
021import org.apache.commons.logging.Log;
022import org.apache.commons.logging.LogFactory;
023import org.nuxeo.ecm.automation.OperationContext;
024import org.nuxeo.ecm.automation.core.Constants;
025import org.nuxeo.ecm.automation.core.annotations.Context;
026import org.nuxeo.ecm.automation.core.annotations.Operation;
027import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
028import org.nuxeo.ecm.automation.core.annotations.Param;
029import org.nuxeo.ecm.core.api.NuxeoPrincipal;
030import org.nuxeo.ecm.platform.importer.mqueues.chronicle.ChronicleConfig;
031import org.nuxeo.ecm.platform.importer.mqueues.kafka.KafkaConfigService;
032import org.nuxeo.ecm.platform.importer.mqueues.mqueues.MQManager;
033import org.nuxeo.ecm.platform.importer.mqueues.mqueues.chronicle.ChronicleMQManager;
034import org.nuxeo.ecm.platform.importer.mqueues.mqueues.kafka.KafkaMQManager;
035import org.nuxeo.ecm.platform.importer.mqueues.pattern.message.BlobMessage;
036import org.nuxeo.ecm.platform.importer.mqueues.pattern.producer.ProducerPool;
037import org.nuxeo.ecm.platform.importer.mqueues.pattern.producer.RandomStringBlobMessageProducerFactory;
038import org.nuxeo.runtime.api.Framework;
039
040/**
041 * @since 9.1
042 */
043@Operation(id = RandomBlobProducers.ID, category = Constants.CAT_SERVICES, label = "Produces random blobs", since = "9.1",
044        description = "Produces random blobs in a mqueues.")
045public class RandomBlobProducers {
046    private static final Log log = LogFactory.getLog(RandomBlobProducers.class);
047    public static final String ID = "MQImporter.runRandomBlobProducers";
048    public static final String DEFAULT_MQ_NAME = "mq-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 = "mqName", required = false)
066    protected String mqName;
067
068    @Param(name = "mqSize", required = false)
069    protected Integer mqSize;
070
071    @Param(name = "kafkaConfig", required = false)
072    protected String kafkaConfig;
073
074
075    @OperationMethod
076    public void run() {
077        checkAccess(ctx);
078        try (MQManager<BlobMessage> manager = getManager()) {
079            manager.createIfNotExists(getMQName(), getMQSize());
080            try (ProducerPool<BlobMessage> producers = new ProducerPool<>(getMQName(), manager,
081                    new RandomStringBlobMessageProducerFactory(nbBlobs, lang, avgBlobSizeKB), nbThreads.shortValue())) {
082                producers.start().get();
083            }
084        } catch (Exception e) {
085            log.error(e.getMessage(), e);
086        }
087    }
088
089    protected String getMQName() {
090        if (mqName != null) {
091            return mqName;
092        }
093        return DEFAULT_MQ_NAME;
094    }
095
096    protected int getMQSize() {
097        if (mqSize != null && mqSize > 0) {
098            return mqSize;
099        }
100        return nbThreads;
101    }
102
103    protected MQManager<BlobMessage> getManager() {
104        if (kafkaConfig == null || kafkaConfig.isEmpty()) {
105            return new ChronicleMQManager<>(ChronicleConfig.getBasePath("import"),
106                    ChronicleConfig.getRetentionDuration());
107        }
108        KafkaConfigService service = Framework.getService(KafkaConfigService.class);
109        return new KafkaMQManager<>(service.getZkServers(kafkaConfig),
110                service.getTopicPrefix(kafkaConfig),
111                service.getProducerProperties(kafkaConfig),
112                service.getConsumerProperties(kafkaConfig));
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
122
123}