001/*
002 * (C) Copyright 2009 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 *     Thierry Delprat
018 */
019package org.nuxeo.ecm.platform.importer.executor.jaxrs;
020
021import javax.ws.rs.GET;
022import javax.ws.rs.Path;
023import javax.ws.rs.Produces;
024import javax.ws.rs.QueryParam;
025
026import groovy.util.IFileNameFinder;
027import org.apache.commons.logging.Log;
028import org.apache.commons.logging.LogFactory;
029import org.nuxeo.ecm.platform.importer.base.GenericMultiThreadedImporter;
030import org.nuxeo.ecm.platform.importer.base.ImporterRunner;
031import org.nuxeo.ecm.platform.importer.base.ImporterRunnerConfiguration;
032import org.nuxeo.ecm.platform.importer.filter.EventServiceConfiguratorFilter;
033import org.nuxeo.ecm.platform.importer.filter.ImporterFilter;
034import org.nuxeo.ecm.platform.importer.service.DefaultImporterService;
035import org.nuxeo.ecm.platform.importer.source.RandomTextSourceNode;
036import org.nuxeo.ecm.platform.importer.source.SourceNode;
037import org.nuxeo.runtime.api.Framework;
038
039@Path("randomImporter")
040public class RandomImporterExecutor extends AbstractJaxRSImporterExecutor {
041
042    private static final Log log = LogFactory.getLog(RandomImporterExecutor.class);
043
044    @Override
045    protected Log getJavaLogger() {
046        return log;
047    }
048
049    @GET
050    @Path("run")
051    @Produces("text/plain; charset=UTF-8")
052    public String run(@QueryParam("targetPath") String targetPath,
053            @QueryParam("skipRootContainerCreation") Boolean skipRootContainerCreation,
054            @QueryParam("batchSize") Integer batchSize, @QueryParam("nbThreads") Integer nbThreads,
055            @QueryParam("interactive") Boolean interactive, @QueryParam("nbNodes") Integer nbNodes,
056            @QueryParam("fileSizeKB") Integer fileSizeKB, @QueryParam("onlyText") Boolean onlyText,
057            @QueryParam("blockSyncPostCommitProcessing") Boolean blockSyncPostCommitProcessing,
058            @QueryParam("blockAsyncProcessing") Boolean blockAsyncProcessing, @QueryParam("bulkMode") Boolean bulkMode,
059            @QueryParam("transactionTimeout") Integer transactionTimeout) {
060
061        if (onlyText == null) {
062            onlyText = true;
063        }
064
065        if (bulkMode == null) {
066            bulkMode = true;
067        }
068        getLogger().info("Init Random text generator");
069        SourceNode source = RandomTextSourceNode.init(nbNodes, fileSizeKB, onlyText);
070        getLogger().info("Random text generator initialized");
071
072        ImporterRunnerConfiguration configuration = new ImporterRunnerConfiguration.Builder(source, targetPath,
073                getLogger()).skipRootContainerCreation(skipRootContainerCreation).batchSize(batchSize).nbThreads(
074                nbThreads).build();
075        GenericMultiThreadedImporter runner = new GenericMultiThreadedImporter(configuration);
076
077        ImporterFilter filter = new EventServiceConfiguratorFilter(blockSyncPostCommitProcessing, blockAsyncProcessing,
078                !onlyText, bulkMode);
079        runner.addFilter(filter);
080        if (transactionTimeout != null) {
081            Framework.getService(DefaultImporterService.class).setTransactionTimeout(transactionTimeout);
082        }
083        String res = run(runner, interactive);
084        return res;
085    }
086
087    @Override
088    public String run(ImporterRunner runner, Boolean interactive) {
089        return doRun(runner, interactive);
090    }
091
092}