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.message.BlobMessage;
031import org.nuxeo.ecm.platform.importer.mqueues.mqueues.CQMQueues;
032import org.nuxeo.ecm.platform.importer.mqueues.mqueues.MQueues;
033import org.nuxeo.ecm.platform.importer.mqueues.producer.ProducerPool;
034import org.nuxeo.ecm.platform.importer.mqueues.producer.RandomStringBlobMessageProducerFactory;
035import org.nuxeo.runtime.api.Framework;
036
037import java.io.File;
038import java.nio.file.Paths;
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_BLOB_QUEUE_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 = "queuePath", required = false)
066    protected String queuePath;
067
068    @OperationMethod
069    public void run() {
070        checkAccess(ctx);
071        queuePath = getQueuePath();
072        try (MQueues<BlobMessage> mQueues = new CQMQueues<>(new File(queuePath), nbThreads)) {
073            ProducerPool<BlobMessage> producers = new ProducerPool<>(mQueues,
074                    new RandomStringBlobMessageProducerFactory(nbBlobs, lang, avgBlobSizeKB), nbThreads);
075            producers.start().get();
076        } catch (Exception e) {
077            log.error(e.getMessage(), e);
078        }
079    }
080
081    public String getQueuePath() {
082        if (queuePath != null && !queuePath.isEmpty()) {
083            return queuePath;
084        }
085        return getDefaultBlobQueuePath();
086    }
087
088    public static String getDefaultBlobQueuePath() {
089        return getDefaultQueuePath(DEFAULT_BLOB_QUEUE_NAME);
090    }
091
092    public static String getDefaultQueuePath(String name) {
093        return Paths.get(Framework.getRuntime().getHome().toString(), "tmp", name).toString();
094    }
095
096    public static void checkAccess(OperationContext context) {
097        NuxeoPrincipal principal = (NuxeoPrincipal) context.getPrincipal();
098        if (principal == null || !principal.isAdministrator()) {
099            throw new RuntimeException("Unauthorized access: " + principal);
100        }
101    }
102
103
104}