<commit_before>// ----------------------------------------------------------------------
// This code is developed as part of the Java CoG Kit project
// The terms of the license can be found at http://www.cogkit.org/license
// This message may not be removed or altered.
// ----------------------------------------------------------------------

package org.globus.cog.abstraction.impl.execution.gt2;

import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Vector;

import org.apache.log4j.Logger;
import org.globus.cog.abstraction.impl.common.StatusImpl;
import org.globus.cog.abstraction.impl.common.task.IllegalSpecException;
import org.globus.cog.abstraction.impl.common.task.InvalidSecurityContextException;
import org.globus.cog.abstraction.impl.common.task.InvalidServiceContactException;
import org.globus.cog.abstraction.impl.common.task.TaskSubmissionException;
import org.globus.cog.abstraction.interfaces.DelegatedTaskHandler;
import org.globus.cog.abstraction.interfaces.Delegation;
import org.globus.cog.abstraction.interfaces.FileLocation;
import org.globus.cog.abstraction.interfaces.JobSpecification;
import org.globus.cog.abstraction.interfaces.SecurityContext;
import org.globus.cog.abstraction.interfaces.ServiceContact;
import org.globus.cog.abstraction.interfaces.Status;
import org.globus.cog.abstraction.interfaces.Task;
import org.globus.gram.GramException;
import org.globus.gram.GramJob;
import org.globus.gram.GramJobListener;
import org.globus.gram.internal.GRAMConstants;
import org.globus.io.gass.server.GassServer;
import org.globus.io.gass.server.JobOutputListener;
import org.globus.io.gass.server.JobOutputStream;
import org.globus.rsl.Binding;
import org.globus.rsl.Bindings;
import org.globus.rsl.NameOpValue;
import org.globus.rsl.ParseException;
import org.globus.rsl.RSLParser;
import org.globus.rsl.RslNode;
import org.globus.rsl.Value;
import org.globus.rsl.VarRef;
import org.gridforum.jgss.ExtendedGSSManager;
import org.ietf.jgss.GSSCredential;
import org.ietf.jgss.GSSException;
import org.ietf.jgss.GSSManager;

public class JobSubmissionTaskHandler implements DelegatedTaskHandler,
        GramJobListener, JobOutputListener {
    static Logger logger = Logger.getLogger(JobSubmissionTaskHandler.class
            .getName());
    private Task task = null;
    private GramJob gramJob;
    private Vector jobList = null;
    private boolean startGassServer = false;
    private GassServer gassServer = null;
    private SecurityContext securityContext;
    private JobOutputStream stdoutStream;
    private JobOutputStream stderrStream;
    private GSSCredential credential;

    public void submit(Task task) throws IllegalSpecException,
            InvalidSecurityContextException, InvalidServiceContactException,
            TaskSubmissionException {
        if (this.task != null) {
            throw new TaskSubmissionException(
                    "JobSubmissionTaskHandler cannot handle two active jobs simultaneously");
        }
        if (task.getStatus().getStatusCode() != Status.UNSUBMITTED) {
            throw new TaskSubmissionException("This task is already submitted");
        }
        this.task = task;
        this.task.setStatus(Status.SUBMITTING);
        this.securityContext = getSecurityContext(task);
        this.credential = (GSSCredential) securityContext.getCredentials();
        String rsl;
        JobSpecification spec;
        try {
            spec = (JobSpecification) this.task.getSpecification();
        }
        catch (Exception e) {
            throw new IllegalSpecException(
                    "Exception while retreiving Job Specification", e);
        }
        RslNode rslTree = null;
        try {
            rslTree = prepareSpecification(spec);
        }
        catch (Throwable e) {
            throw new IllegalSpecException("Cannot parse the given RSL", e);
        }
        if (logger.isDebugEnabled()) {
            logger.debug("RSL: " + rslTree);
        }

        if (rslTree.getOperator() == RslNode.MULTI) {
            this.task.setAttribute("jobCount", "multiple");
            submitMultipleJobs(rslTree, spec);
        }
        else {
            this.task.setAttribute("jobCount", "single");
            submitSingleJob(rslTree.toString(), spec);
        }
    }

    private void submitSingleJob(String rsl, JobSpecification spec)
            throws IllegalSpecException, InvalidSecurityContextException,
            InvalidServiceContactException, TaskSubmissionException {
        this.gramJob = new GramJob(rsl);
        try {
            this.gramJob.setCredentials(this.credential);
        }
        catch (IllegalArgumentException iae) {
            throw new InvalidSecurityContextException(
                    "Cannot set the SecurityContext twice", iae);
        }

        if (!spec.isBatchJob()) {
            CallbackHandlerManager.increaseUsageCount(this.credential);
            this.gramJob.addListener(this);
        }

        ServiceContact serviceContact = this.task.getService(0)
                .getServiceContact();
        String server = serviceContact.getContact();

        // if the jobmanager attribute is specified, handle it
        String jobmanager = (String) this.task.getService(0).getAttribute(
                "jobmanager");
        if (jobmanager != null) {
            server = handleJobManager(server, jobmanager);
        }
        if (logger.isDebugEnabled()) {
            logger.debug("Execution server: " + server);
        }
        boolean limitedDeleg = isLimitedDelegation(this.securityContext);
        if (spec.getDelegation() == Delegation.FULL_DELEGATION) {
            limitedDeleg = false;
        }
        try {
            // check if the task has not been canceled after it was
            // submitted for execution

            this.gramJob.request(server, spec.isBatchJob(), limitedDeleg);<commit_msg>properly handle job managers<commit_after>// ----------------------------------------------------------------------
// This code is developed as part of the Java CoG Kit project
// The terms of the license can be found at http://www.cogkit.org/license
// This message may not be removed or altered.
// ----------------------------------------------------------------------

package org.globus.cog.abstraction.impl.execution.gt2;

import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Vector;

import org.apache.log4j.Logger;
import org.globus.cog.abstraction.impl.common.StatusImpl;
import org.globus.cog.abstraction.impl.common.task.IllegalSpecException;
import org.globus.cog.abstraction.impl.common.task.InvalidSecurityContextException;
import org.globus.cog.abstraction.impl.common.task.InvalidServiceContactException;
import org.globus.cog.abstraction.impl.common.task.TaskSubmissionException;
import org.globus.cog.abstraction.interfaces.DelegatedTaskHandler;
import org.globus.cog.abstraction.interfaces.Delegation;
import org.globus.cog.abstraction.interfaces.ExecutionService;
import org.globus.cog.abstraction.interfaces.FileLocation;
import org.globus.cog.abstraction.interfaces.JobSpecification;
import org.globus.cog.abstraction.interfaces.SecurityContext;
import org.globus.cog.abstraction.interfaces.Service;
import org.globus.cog.abstraction.interfaces.ServiceContact;
import org.globus.cog.abstraction.interfaces.Status;
import org.globus.cog.abstraction.interfaces.Task;
import org.globus.gram.GramException;
import org.globus.gram.GramJob;
import org.globus.gram.GramJobListener;
import org.globus.gram.internal.GRAMConstants;
import org.globus.io.gass.server.GassServer;
import org.globus.io.gass.server.JobOutputListener;
import org.globus.io.gass.server.JobOutputStream;
import org.globus.rsl.Binding;
import org.globus.rsl.Bindings;
import org.globus.rsl.NameOpValue;
import org.globus.rsl.ParseException;
import org.globus.rsl.RSLParser;
import org.globus.rsl.RslNode;
import org.globus.rsl.Value;
import org.globus.rsl.VarRef;
import org.gridforum.jgss.ExtendedGSSManager;
import org.ietf.jgss.GSSCredential;
import org.ietf.jgss.GSSException;
import org.ietf.jgss.GSSManager;

public class JobSubmissionTaskHandler implements DelegatedTaskHandler,
        GramJobListener, JobOutputListener {
    static Logger logger = Logger.getLogger(JobSubmissionTaskHandler.class
            .getName());
    private Task task = null;
    private GramJob gramJob;
    private Vector jobList = null;
    private boolean startGassServer = false;
    private GassServer gassServer = null;
    private SecurityContext securityContext;
    private JobOutputStream stdoutStream;
    private JobOutputStream stderrStream;
    private GSSCredential credential;

    public void submit(Task task) throws IllegalSpecException,
            InvalidSecurityContextException, InvalidServiceContactException,
            TaskSubmissionException {
        if (this.task != null) {
            throw new TaskSubmissionException(
                    "JobSubmissionTaskHandler cannot handle two active jobs simultaneously");
        }
        if (task.getStatus().getStatusCode() != Status.UNSUBMITTED) {
            throw new TaskSubmissionException("This task is already submitted");
        }
        this.task = task;
        this.task.setStatus(Status.SUBMITTING);
        this.securityContext = getSecurityContext(task);
        this.credential = (GSSCredential) securityContext.getCredentials();
        String rsl;
        JobSpecification spec;
        try {
            spec = (JobSpecification) this.task.getSpecification();
        }
        catch (Exception e) {
            throw new IllegalSpecException(
                    "Exception while retreiving Job Specification", e);
        }
        RslNode rslTree = null;
        try {
            rslTree = prepareSpecification(spec);
        }
        catch (Throwable e) {
            throw new IllegalSpecException("Cannot parse the given RSL", e);
        }
        if (logger.isDebugEnabled()) {
            logger.debug("RSL: " + rslTree);
        }

        if (rslTree.getOperator() == RslNode.MULTI) {
            this.task.setAttribute("jobCount", "multiple");
            submitMultipleJobs(rslTree, spec);
        }
        else {
            this.task.setAttribute("jobCount", "single");
            submitSingleJob(rslTree.toString(), spec);
        }
    }

    private void submitSingleJob(String rsl, JobSpecification spec)
            throws IllegalSpecException, InvalidSecurityContextException,
            InvalidServiceContactException, TaskSubmissionException {
        this.gramJob = new GramJob(rsl);
        try {
            this.gramJob.setCredentials(this.credential);
        }
        catch (IllegalArgumentException iae) {
            throw new InvalidSecurityContextException(
                    "Cannot set the SecurityContext twice", iae);
        }

        if (!spec.isBatchJob()) {
            CallbackHandlerManager.increaseUsageCount(this.credential);
            this.gramJob.addListener(this);
        }

        ServiceContact serviceContact = this.task.getService(0)
                .getServiceContact();
        String server = serviceContact.getContact();

        // if the jobmanager attribute is specified, handle it
        Service service = this.task.getService(0);
        String jobmanager = null;
        if (service instanceof ExecutionService) {
            jobmanager = ((ExecutionService) service).getJobManager();
        }
        if (jobmanager != null) {
            server = handleJobManager(server, jobmanager);
        }
        if (logger.isDebugEnabled()) {
            logger.debug("Execution server: " + server);
        }
        boolean limitedDeleg = isLimitedDelegation(this.securityContext);
        if (spec.getDelegation() == Delegation.FULL_DELEGATION) {
            limitedDeleg = false;
        }
        try {
            // check if the task has not been canceled after it was
            // submitted for execution

            this.gramJob.request(server, spec.isBatchJob(), limitedDeleg);
{'fraction_non_alphanumeric': 0.11249074911602665, 'fraction_numerical': 0.000657840638105419, 'mean_word_length': 2.7444581280788176, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '146331', 'n_tokens_mistral': 3359, 'n_tokens_neox': 3325, 'n_words': 686}