/** Initialize ResourceBroker and all subPolicys */
  public void initialize() throws InstantiationException {
    Configuration c =
        RuntimeEnvironment.getInstance().getConfiguration().subset(Site.CONFIGURATION_SECTION);
    // check, if only one Transferpolicy should be used
    TransferPolicy tp = null;
    String key =
        ConfigurationHelper.retrieveRelevantKey(
            c,
            getSite().getName(),
            Constants.CONFIGURATION_SITES_ACTIVITYBROKER_TRANSFERPOLICY_CLASS);
    if (key != null) {
      String className = c.getString(key);
      tp = ClassLoaderHelper.loadInterface(className, TransferPolicy.class);
      tp.setActivityBroker(this);
      tp.initialize(key.replace(".class", ""));
    }

    // Initialize DistributionPolicy
    key =
        ConfigurationHelper.retrieveRelevantKey(
            c,
            getSite().getName(),
            Constants.CONFIGURATION_SITES_ACTIVITYBROKER_DISTRIBUTIONPOLICY_CLASS);
    if (key == null) {
      String msg =
          "ResourceBroker entry ("
              + Site.CONFIGURATION_SECTION
              + "["
              + getSite().getName()
              + "]"
              + Constants.CONFIGURATION_SITES_ACTIVITYBROKER_DISTRIBUTIONPOLICY_CLASS
              + ") not found in configuration";
      throw new InstantiationException(msg);
    } // if
    String className = c.getString(key);
    DistributionPolicy dp = ClassLoaderHelper.loadInterface(className, DistributionPolicy.class);
    dp.setActivityBroker(this);
    distributionPolicy = dp;
    dp.setActivityBroker(this);

    if (tp == null) {
      dp.initializeTransferPolicy();
    } else {
      dp.setTransferPolicy(tp);
    }

    // initialize AcceptancePolicy
    key =
        ConfigurationHelper.retrieveRelevantKey(
            c,
            getSite().getName(),
            Constants.CONFIGURATION_SITES_ACTIVITYBROKER_ACCEPTANCEPOLICY_CLASS);
    if (key == null) {
      String msg =
          "ResourceBroker entry ("
              + Site.CONFIGURATION_SECTION
              + "["
              + getSite().getName()
              + "]"
              + Constants.CONFIGURATION_SITES_ACTIVITYBROKER_ACCEPTANCEPOLICY_CLASS
              + ") not found in configuration";
      throw new InstantiationException(msg);
    } // if
    className = c.getString(key);
    AcceptancePolicy ap = ClassLoaderHelper.loadInterface(className, AcceptancePolicy.class);
    ap.setActivityBroker(this);
    acceptancePolicy = ap;
    ap.setActivityBroker(this);
    if (tp == null) {
      ap.initializeTransferPolicy();
    } else {
      ap.setTransferPolicy(tp);
    }

    // initialize LocationPolicy
    key =
        ConfigurationHelper.retrieveRelevantKey(
            c,
            getSite().getName(),
            Constants.CONFIGURATION_SITES_ACTIVITYBROKER_LOCATIONPOLICY_CLASS);
    if (key == null) {
      String msg =
          "ResourceBroker entry ("
              + Site.CONFIGURATION_SECTION
              + "["
              + getSite().getName()
              + "]"
              + Constants.CONFIGURATION_SITES_ACTIVITYBROKER_LOCATIONPOLICY_CLASS
              + ") not found in configuration";
      throw new InstantiationException(msg);
    } // if
    className = c.getString(key);
    LocationPolicy lp = ClassLoaderHelper.loadInterface(className, LocationPolicy.class);
    lp.setActivityBroker(this);
    locationPolicy = lp;
    lp.setActivityBroker(this);
    lp.initialize();

    // Initialize public Information here
    key =
        ConfigurationHelper.retrieveRelevantKey(
            c,
            getSite().getName(),
            Constants.CONFIGURATION_SITES_ACTIVITYBROKER_PUBLIC_INFORMATION);
    if (key != null) {
      List<String> infos = c.getList(key);

      // Fill Public Information with Standardparts
      if (infos.contains("size")) {
        publicInformation.put(
            "size", this.getSite().getSiteInformation().getProvidedResources().size());
      }
    } // if
  }
  /**
   * 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 */
    }
  }