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