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