/**
  * This method is called by the acceptancePolicy of the site, if a remote Job is taken over and
  * should be given to the LRMS
  */
 public void manageNewJob(Job j) {
   // save the exchange in the Job-Object
   j.getProvenance().addEpisode(this.site);
   // The job is managed by this side now
   getListOfManagedJobs().add(j);
   this.getSite().getScheduler().putNextJob(j);
 }
 /** This method is used for delegation of a Job, e.g. when the job is delegated to another site */
 public void delegate(Job job) throws AcceptanceVetoException {
   // try to remove the Job from Local SchedulingSystem
   if (job.getLifecycle().getLastCondition() == State.QUEUED) {
     try {
       getSite().getScheduler().dequeue(this, job);
     } catch (DequeuingVetoException e) {
       throw new AcceptanceVetoException(
           "The job cannot be dequeued from local Scheduling-System");
     }
   }
   // Try to give up the responsibility for the Job
   if (!managedJobs.remove(job))
     throw new AcceptanceVetoException("The Job is no longer available for acception");
 }
  /**
   * This method is used to make an offer of Jobs to this Broker by giving it
   *
   * @param ActivityDelegationSession session
   */
  public void offer(ActivityDelegationSession session) throws OfferingVetoException {
    // Add the offered session to the list of activeSessions, maybe for later use
    getActiveSessions().add(session);

    if (session.getCreator() instanceof Dispatcher) {
      // Must create the jobReleaseEvent
      for (Job j : session.getJoblist()) {
        // if (acceptancePolicy.decideRemoteJob(j,session.getCreator())){
        try {
          session.accept(j);
          manageNewJob(j);
          /* INSTRUMENTATION: Set accounting information. Begin */
          ComputeSiteInformation info = (ComputeSiteInformation) this.site.getSiteInformation();
          info.preLRMSWaitingJobs++;
          info.foreignJobs++;
          /* INSTRUMENTATION: End */

        } catch (AcceptanceVetoException e) {
          // Maybe another site accepted the Job first
          // or the job owner itself decides to execute the Job
        } catch (SessionInvalidException e) {
          // The session is to old, so it has to be removed from the list of activeSessions
          // In other implementations, the acceptancePolicy could make a query to another site
          // to get a new valid session for their Jobs
          getActiveSessions().remove(session);
        }
        // } //End if acceptance policy
      } // end for
      return;
    } // end if instance of GridactivityBroker

    if (session.getCreator() instanceof ActivityBroker) {
      // The session has been offered by another activityBroker and must be handled as a remote Job
      List<Job> jobs = session.getJoblist();
      for (Job j : jobs) {
        if (acceptancePolicy.decideRemoteJob(j, session.getCreator())) {
          try {

            /* INSTRUMENTATION: Set accounting information. Begin */
            ComputeSiteInformation info = (ComputeSiteInformation) this.site.getSiteInformation();
            info.preLRMSWaitingJobs++;
            info.foreignJobs++;
            /* INSTRUMENTATION: End */

            // Try to accept the Job
            session.accept(j);
            // if job is accepted it can be scheduled locally
            // In this Implementation every overtaken job is directly given to the LRMS
            // There is no distribution of accepted jobs to further sites, which is possible in
            // principle
            manageNewJob(j);
          } catch (AcceptanceVetoException e) {
            // Maybe another site accepted the Job first
            // or the jobowner itself decides to execute the Job
          } catch (SessionInvalidException e) {
            // The session is to old, so it has to be removed from the list of activeSessions
            // In other implementations, the acceptancePolicy could make a query to another site
            // to get a new valid session for theire Jobs
            getActiveSessions().remove(session);
          }
        }
      }

    } else {
      if (session.getCreator() instanceof LocalSubmissionComponent) {
        // Local submission of Jobs
        for (Job j : session.getJoblist()) {
          j.setReleasedSite(getSite());
          j.getLifecycle().addEpisode(State.RELEASED);
          j.getProvenance().addEpisode(getSite());
          if (!acceptancePolicy.decideLocalJob(j)) {
            // A local job was declined
            throw new OfferingVetoException("Local Job was declined");
          }
          // in this Implementation use the DistributionPolicy for locally submitted
          // jobs to decide, whether the Job should be executed locally or beeing delegated to other
          // sites
          List<ActivityBroker> activityBrokerToAsk =
              locationPolicy.getOrder(getKnownActivityBroker());
          boolean accepted = false;

          for (ActivityBroker ab : activityBrokerToAsk) {
            // Check for one Partner after another
            if ((distributionPolicy.decide(j, ab))) {
              // Check, if the Job is still scheduable locally or maybe taken over by a remote site
              if (getListOfManagedJobs().contains(j)) {

                /* INSTRUMENTATION: Set accounting information. Begin */
                ComputeSiteInformation info =
                    (ComputeSiteInformation) this.site.getSiteInformation();
                info.preLRMSWaitingJobs++;
                info.localPreLRMSWaitingJobs++;
                /* INSTRUMENTATION: End */

                // the job is still scheduable
                site.getScheduler().putNextJob(j);
                accepted = true;
              }
              // The Job is scheduled, so the loop can be breaked
              break;
            } else {
              try {
                // The Job should be scheduled remotely
                ActivityDelegationSession remoteSession =
                    DelegationFabric.generateActivityDelegationSession(j, this);
                ab.offer(remoteSession);
              } catch (OfferingVetoException e) {
                e.printStackTrace();
              }
            }
          } // end for activityBroker

          // Check, if no other Broker has accepted the Job
          // In this Implementation a job, that has not been taken directly is not been accepted
          // may be different in other Implementations e.g. using webservices with time for
          // Communication
          if (!accepted && getListOfManagedJobs().contains(j)) {

            /* INSTRUMENTATION: Set accounting information. Begin */
            ComputeSiteInformation info = (ComputeSiteInformation) this.site.getSiteInformation();
            info.preLRMSWaitingJobs++;
            info.localPreLRMSWaitingJobs++;
            /* INSTRUMENTATION: End */

            // The job must be scheduled locally
            site.getScheduler().putNextJob(j);
          }
        } // end for job
      }
      /* else
      {
      	//Other Components must be implemented by subclass
      	//The two Delegationcomponents activityBroker and localSubmissionComponent are
      	//only the standardcase
      	throw new OfferingVetoException("Submission of job by this Component: " + session.getCreator().toString() + " is not implemented by activityBroker");
      } //end localSubmissionComponent is creator */
    }
  }