001/*
002 * (C) Copyright 2017 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 *     bdelbosc
018 */
019package org.nuxeo.ecm.platform.importer.mqueues.pattern.producer;
020
021import org.nuxeo.ecm.platform.importer.mqueues.pattern.message.BlobMessage;
022import org.nuxeo.ecm.platform.importer.random.HunspellDictionaryHolder;
023import org.nuxeo.ecm.platform.importer.random.RandomTextGenerator;
024
025import java.util.concurrent.ThreadLocalRandom;
026
027/**
028 * Build random StringBlob message.
029 *
030 * @since 9.1
031 */
032public class RandomStringBlobMessageProducer extends AbstractProducer<BlobMessage> {
033    private static final String DEFAULT_MIME_TYPE = "plain/text";
034    private final long nbBlobs;
035    private final int averageSizeKB;
036    private final ThreadLocalRandom rand;
037    private long count = 0;
038    private static RandomTextGenerator gen;
039    private final String mimetype;
040
041    public RandomStringBlobMessageProducer(int producerId, long nbBlobs, String lang, int averageSizeKB) {
042        super(producerId);
043        this.nbBlobs = nbBlobs;
044        this.averageSizeKB = averageSizeKB;
045        this.mimetype = DEFAULT_MIME_TYPE;
046        synchronized (RandomDocumentMessageProducer.class) {
047            if (gen == null) {
048                gen = new RandomTextGenerator(new HunspellDictionaryHolder(lang));
049                gen.prefilCache();
050            }
051        }
052        rand = ThreadLocalRandom.current();
053    }
054
055    @Override
056    public int getPartition(BlobMessage message, int partitions) {
057        return ((int) count) % partitions;
058    }
059
060    @Override
061    public boolean hasNext() {
062        return count < nbBlobs;
063    }
064
065    @Override
066    public BlobMessage next() {
067        String filename = generateFilename();
068        String content = generateContent();
069        BlobMessage ret = new BlobMessage.StringMessageBuilder(content)
070                .setFilename(filename).setMimetype(mimetype).build();
071        count++;
072        return ret;
073    }
074
075    private String generateFilename() {
076        return gen.getRandomTitle(rand.nextInt(4) + 1).trim().replaceAll("\\W+", "-").toLowerCase() + ".txt";
077    }
078
079    private String generateContent() {
080        return gen.getRandomText(averageSizeKB);
081    }
082
083
084}