<commit_before>/*
 * ################################################################
 *
 * ProActive Parallel Suite(TM): The Java(TM) library for
 *    Parallel, Distributed, Multi-Core Computing for
 *    Enterprise Grids & Clouds
 *
 * Copyright (C) 1997-2011 INRIA/University of
 *                 Nice-Sophia Antipolis/ActiveEon
 * Contact: <EMAIL> or <EMAIL>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Affero General Public License
 * as published by the Free Software Foundation; version 3 of
 * the License.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
 * USA
 *
 * If needed, contact us to obtain a release under GPL Version 2 or 3
 * or a different license than the AGPL.
 *
 *  Initial developer(s):               The ProActive Team
 *                        http://proactive.inria.fr/team_members.htm
 *  Contributor(s):
 *
 * ################################################################
 * $$PROACTIVE_INITIAL_DEV$$
 */
package org.ow2.proactive.scheduler.job;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.log4j.Logger;
import org.objectweb.proactive.core.util.log.ProActiveLogger;
import org.ow2.proactive.authentication.crypto.Credentials;
import org.ow2.proactive.scheduler.common.exception.InternalException;
import org.ow2.proactive.scheduler.common.exception.JobCreationException;
import org.ow2.proactive.scheduler.common.job.Job;
import org.ow2.proactive.scheduler.common.job.TaskFlowJob;
import org.ow2.proactive.scheduler.common.job.factories.FlowChecker;
import org.ow2.proactive.scheduler.common.job.factories.FlowError;
import org.ow2.proactive.scheduler.common.task.CommonAttribute;
import org.ow2.proactive.scheduler.common.task.JavaTask;
import org.ow2.proactive.scheduler.common.task.NativeTask;
import org.ow2.proactive.scheduler.common.task.Task;
import org.ow2.proactive.scheduler.common.task.flow.FlowActionType;
import org.ow2.proactive.scheduler.task.ForkedJavaExecutableContainer;
import org.ow2.proactive.scheduler.task.JavaExecutableContainer;
import org.ow2.proactive.scheduler.task.NativeExecutableContainer;
import org.ow2.proactive.scheduler.task.TaskIdImpl;
import org.ow2.proactive.scheduler.task.internal.InternalForkedJavaTask;
import org.ow2.proactive.scheduler.task.internal.InternalJavaTask;
import org.ow2.proactive.scheduler.task.internal.InternalNativeTask;
import org.ow2.proactive.scheduler.task.internal.InternalTask;
import org.ow2.proactive.scheduler.util.SchedulerDevLoggers;


/**
 * This is the factory to build Internal job with a job (user).
 * For the moment it performs a simple copy from userJob to InternalJob
 * and recreate the dependences if needed.
 *
 * @author The ProActive Team
 * @since ProActive Scheduling 0.9
 */
public class InternalJobFactory {

    public static final Logger logger_dev = ProActiveLogger.getLogger(SchedulerDevLoggers.FACTORY);

    /**
     * Create a new internal job with the given job (user).
     *
     * @param job the user job that will be used to create the internal job.
     * @return the created internal job.
     * @throws JobCreationException an exception if the factory cannot create the given job.
     */
    public static InternalJob createJob(Job job, Credentials cred) throws JobCreationException {
        InternalJob iJob = null;

        logger_dev.info("Create job '" + job.getName() + "' - " + job.getClass().getName());

        switch (job.getType()) {
            case PARAMETER_SWEEPING:
                logger_dev.error("The type of the given job is not yet implemented !");
                throw new JobCreationException("The type of the given job is not yet implemented !");
            case TASKSFLOW:
                iJob = createJob((TaskFlowJob) job);
                break;
            default:
                logger_dev.error("The type of the given job is unknown !");
                throw new JobCreationException("The type of the given job is unknown !");
        }

        try {
            //set the job common properties
            iJob.setCredentials(cred);
            setJobCommonProperties(job, iJob);
            return iJob;
        } catch (Exception e) {
            logger_dev.error("", e);
            throw new InternalException("Error while creating the internalJob !", e);
        }
    }

    /**
     * Create an internalTaskFlow job with the given task flow job (user)
     *
     * @param job the user job that will be used to create the internal job.
     * @return the created internal job.
     * @throws JobCreationException an exception if the factory cannot create the given job.
     */
    private static InternalJob createJob(TaskFlowJob userJob) throws JobCreationException {
        if (userJob.getTasks().size() == 0) {
            logger_dev.info("Job '" + userJob.getName() + "' must contain tasks !");
            throw new JobCreationException("This job must contains tasks !");
        }

        // validate taskflow
        List<FlowChecker.Block> blocks = new ArrayList<FlowChecker.Block>();
        FlowError err = FlowChecker.validate(userJob, blocks);
        if (err != null) {
            String e = "";

            e += "Invalid taskflow: " + err.getMessage() + "; context: " + err.getTask();
            logger_dev.error(e);
            throw new JobCreationException(e, err);
        }<commit_msg>SCHEDULING-1368 : Check maximum number of tasks per job in the InternalJobFactory<commit_after>/*
 * ################################################################
 *
 * ProActive Parallel Suite(TM): The Java(TM) library for
 *    Parallel, Distributed, Multi-Core Computing for
 *    Enterprise Grids & Clouds
 *
 * Copyright (C) 1997-2011 INRIA/University of
 *                 Nice-Sophia Antipolis/ActiveEon
 * Contact: <EMAIL> or <EMAIL>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Affero General Public License
 * as published by the Free Software Foundation; version 3 of
 * the License.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
 * USA
 *
 * If needed, contact us to obtain a release under GPL Version 2 or 3
 * or a different license than the AGPL.
 *
 *  Initial developer(s):               The ProActive Team
 *                        http://proactive.inria.fr/team_members.htm
 *  Contributor(s):
 *
 * ################################################################
 * $$PROACTIVE_INITIAL_DEV$$
 */
package org.ow2.proactive.scheduler.job;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.log4j.Logger;
import org.objectweb.proactive.core.util.log.ProActiveLogger;
import org.ow2.proactive.authentication.crypto.Credentials;
import org.ow2.proactive.scheduler.common.exception.InternalException;
import org.ow2.proactive.scheduler.common.exception.JobCreationException;
import org.ow2.proactive.scheduler.common.job.Job;
import org.ow2.proactive.scheduler.common.job.TaskFlowJob;
import org.ow2.proactive.scheduler.common.job.factories.FlowChecker;
import org.ow2.proactive.scheduler.common.job.factories.FlowError;
import org.ow2.proactive.scheduler.common.task.CommonAttribute;
import org.ow2.proactive.scheduler.common.task.JavaTask;
import org.ow2.proactive.scheduler.common.task.NativeTask;
import org.ow2.proactive.scheduler.common.task.Task;
import org.ow2.proactive.scheduler.common.task.flow.FlowActionType;
import org.ow2.proactive.scheduler.core.properties.PASchedulerProperties;
import org.ow2.proactive.scheduler.task.ForkedJavaExecutableContainer;
import org.ow2.proactive.scheduler.task.JavaExecutableContainer;
import org.ow2.proactive.scheduler.task.NativeExecutableContainer;
import org.ow2.proactive.scheduler.task.TaskIdImpl;
import org.ow2.proactive.scheduler.task.internal.InternalForkedJavaTask;
import org.ow2.proactive.scheduler.task.internal.InternalJavaTask;
import org.ow2.proactive.scheduler.task.internal.InternalNativeTask;
import org.ow2.proactive.scheduler.task.internal.InternalTask;
import org.ow2.proactive.scheduler.util.SchedulerDevLoggers;


/**
 * This is the factory to build Internal job with a job (user).
 * For the moment it performs a simple copy from userJob to InternalJob
 * and recreate the dependences if needed.
 *
 * @author The ProActive Team
 * @since ProActive Scheduling 0.9
 */
public class InternalJobFactory {

    public static final Logger logger_dev = ProActiveLogger.getLogger(SchedulerDevLoggers.FACTORY);

    /**
     * Create a new internal job with the given job (user).
     *
     * @param job the user job that will be used to create the internal job.
     * @return the created internal job.
     * @throws JobCreationException an exception if the factory cannot create the given job.
     */
    public static InternalJob createJob(Job job, Credentials cred) throws JobCreationException {
        InternalJob iJob = null;

        logger_dev.info("Create job '" + job.getName() + "' - " + job.getClass().getName());

        switch (job.getType()) {
            case PARAMETER_SWEEPING:
                logger_dev.error("The type of the given job is not yet implemented !");
                throw new JobCreationException("The type of the given job is not yet implemented !");
            case TASKSFLOW:
                iJob = createJob((TaskFlowJob) job);
                break;
            default:
                logger_dev.error("The type of the given job is unknown !");
                throw new JobCreationException("The type of the given job is unknown !");
        }

        try {
            //set the job common properties
            iJob.setCredentials(cred);
            setJobCommonProperties(job, iJob);
            return iJob;
        } catch (Exception e) {
            logger_dev.error("", e);
            throw new InternalException("Error while creating the internalJob !", e);
        }
    }

    /**
     * Create an internalTaskFlow job with the given task flow job (user)
     *
     * @param job the user job that will be used to create the internal job.
     * @return the created internal job.
     * @throws JobCreationException an exception if the factory cannot create the given job.
     */
    private static InternalJob createJob(TaskFlowJob userJob) throws JobCreationException {
        if (userJob.getTasks().size() == 0) {
            logger_dev.info("Job '" + userJob.getName() + "' must contain tasks !");
            throw new JobCreationException("This job must contains tasks !");
        }

        int maxTask = PASchedulerProperties.JOB_FACTOR.getValueAsInt();
        if (userJob.getTasks().size() > maxTask) {
            logger_dev.info("Job '" + userJob.getName() + "' cannot contain more than " + maxTask +
                " tasks !");
            throw new JobCreationException("Job cannot contain more than " + maxTask + " tasks !");
        }

        // validate taskflow
        List<FlowChecker.Block> blocks = new ArrayList<FlowChecker.Block>();
        FlowError err = FlowChecker.validate(userJob, blocks);
        if (err != null) {
            String e = "";

            e += "Invalid taskflow: " + err.getMessage() + "; context: " + err.getTask();
            logger_dev.error(e);
            throw new JobCreationException(e, err);
        }
{'fraction_non_alphanumeric': 0.1119974216420917, 'fraction_numerical': 0.008621384255902023, 'mean_word_length': 3.211740753308449, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6722469', 'n_tokens_mistral': 3456, 'n_tokens_neox': 3371, 'n_words': 1031}