/// <summary>
        /// Main entrypoint for Activation Filter
        /// </summary>
        /// <param name="args">
        /// Expect only a single argument containing an XML file
        /// </param>
        /// <returns>
        /// int value from Activation Filter
        /// </returns>
        static int Main(string[] args)
        {
            string clusterName = Environment.GetEnvironmentVariable("CCP_SCHEDULER");

            if (setupLogFile() != 0)
            {
                return((int)AFReturnValue.FilterFailure);
            }

            int retval = (int)AFReturnValue.FilterFailure;

            try {
                // If the job is submitted outside peak business hours, no change is necessary
                if (DuringOffHours())
                {
                    logFile.WriteLine("AF: During Off Peak Hours, job starting");
                    return((int)AFReturnValue.StartJob);
                }

                // Currently during peak business hours
                // Check if user is authorized to start a job during these hours
                // If not, delay the start of the job until off peak hours are in play

                // Check that there is only one argument.  If more than 1 argument exists,
                // put a warning in the log file, but try to process it anyway
                if (args.Length != 1)
                {
                    logFile.WriteLine("Only 1 parameter expected containing the name of the job xml file");
                    logFile.WriteLine("Received {0} parameters", args.Length);
                    // If no parameters exist, cannot parse XML file
                    if (args.Length == 0)
                    {
                        return((int)AFReturnValue.FilterFailure);
                    }
                }

                String fileName = args[0];

                // Load the job file as an XmlDocument.
                XmlDocument doc = new XmlDocument();
                doc.Load(fileName);

                XmlNamespaceManager nsMgr = new XmlNamespaceManager(doc.NameTable);
                nsMgr.AddNamespace("hpc", xmlNameSpace);

                // Find the job node in the XML document.
                XmlNode jobXML = doc.SelectSingleNode("/hpc:Job", nsMgr);

                if (jobXML == null)
                {
                    throw new Exception("No job in the xml file");
                }

                // Find the User attribute for the job.
                XmlAttributeCollection attrCol  = jobXML.Attributes;
                XmlAttribute           userAttr = attrCol["UserName"];
                string user = userAttr.Value;

                // If user does not have permission to run jobs during peak hours, adjust HoldUntil if needed
                if (!PeakHoursUser(user))
                {
                    string jobIdString = attrCol["Id"].Value;
                    int    jobId;
                    Int32.TryParse(jobIdString, out jobId);
                    if (jobId != 0)
                    {
                        using (IScheduler scheduler = new Scheduler()) {
                            scheduler.Connect(clusterName);
                            ISchedulerJob job = scheduler.OpenJob(jobId);

                            DateTime peakEnd = DateTime.Today.AddHours((double)endhours);

                            // If the job is not already set to delay until off peak hours, set it
                            // This property should be null, but could be non-null if some other
                            // thread has set it after scheduling called the activation filter
                            if ((job.HoldUntil == null) || (job.HoldUntil < peakEnd))
                            {
                                job.SetHoldUntil(peakEnd);
                                job.Commit();
                                logFile.WriteLine("Delay job {0} until off peak hours", jobId);
                            }
                            else
                            {
                                logFile.WriteLine("Job {0} already set to {1}", jobId, job.HoldUntil);
                            }
                            scheduler.Close();
                        } // using scheduler
                    }
                    else
                    {
                        logFile.WriteLine("jobId == 0, delaying job by default duration");
                    }

                    retval = (int)AFReturnValue.HoldJobUntil;
                }
                else
                {
                    logFile.WriteLine("Job to run during peak hours");
                    retval = (int)AFReturnValue.StartJob;
                }
            } catch (IOException e) {
                logFile.WriteLine("Error Loading the XmlFile");
                logFile.WriteLine(e.ToString());
                retval = (int)AFReturnValue.FilterFailure;
            } catch (Exception e) {
                logFile.WriteLine(e.ToString());
                retval = (int)AFReturnValue.FilterFailure;
            } finally {
                logFile.Close();
            }

            return(retval);
        }
        /// <summary>
        /// Entry point for an activation filter.
        /// </summary>
        /// <param name="jobXml"></param>
        /// XML stream containing the job in question.
        /// <param name="schedulerPass"></param>
        /// <param name="jobIndex"></param>
        /// <param name="backfill"></param>
        /// <param name="resourceCount"></param>
        /// <returns></returns>
        public ActivationFilterResponse FilterActivation(Stream jobXml, int schedulerPass, int jobIndex, bool backfill, int resourceCount)
        {
            if (setupLogFile() != 0)
            {
                return(ActivationFilterResponse.FailJob);
            }

            ActivationFilterResponse retval = ActivationFilterResponse.FailJob;

            try {
                // If the job is submitted outside peak business hours, no change is necessary
                if (DuringOffHours())
                {
                    logFile.WriteLine("AF: During Off Peak Hours, job starting");
                    return(ActivationFilterResponse.StartJob);
                }

                // Currently during peak business hours
                // Check if user is authorized to start a job during these hours
                // If not, delay the start of the job until off peak hours are in play

                // Load the job file as an XmlDocument.
                XmlDocument doc = new XmlDocument();
                doc.Load(jobXml);

                XmlNamespaceManager nsMgr = new XmlNamespaceManager(doc.NameTable);
                nsMgr.AddNamespace("hpc", xmlNameSpace);

                // Find the job node in the XML document.
                XmlNode jobXML = doc.SelectSingleNode("/hpc:Job", nsMgr);

                if (jobXML == null)
                {
                    throw new Exception("No job in the xml file");
                }

                // Find the User attribute for the job.
                XmlAttributeCollection attrCol  = jobXML.Attributes;
                XmlAttribute           userAttr = attrCol["User"];
                string user = userAttr.Value;

                // If user does not have permission to run jobs during peak hours, adjust HoldUntil if needed
                if (!PeakHoursUser(user))
                {
                    string jobIdString = attrCol["Id"].Value;
                    int    jobId;
                    Int32.TryParse(jobIdString, out jobId);
                    if (jobId != 0)
                    {
                        using (IScheduler scheduler = new Scheduler()) {
                            scheduler.Connect("localhost");
                            ISchedulerJob job = scheduler.OpenJob(jobId);

                            DateTime peakEnd = DateTime.Today.AddHours((double)endhours);

                            // If the job is not already set to delay until off peak hours, set it
                            // This property should be null, but could be non-null if some other
                            // thread has set it after scheduling called the activation filter
                            if ((job.HoldUntil == null) || (job.HoldUntil < peakEnd))
                            {
                                job.SetHoldUntil(peakEnd);
                                job.Commit();
                                logFile.WriteLine("Delay job {0} until off peak hours", jobId);
                            }
                            else
                            {
                                logFile.WriteLine("Job {0} already set to {1}", jobId, job.HoldUntil);
                            }
                            scheduler.Close();
                        } // using scheduler
                    }
                    else
                    {
                        logFile.WriteLine("jobId == 0, delaying job by default duration");
                    }

                    retval = ActivationFilterResponse.HoldJobReleaseResourcesAllowOtherJobsToSchedule;
                }
                else
                {
                    logFile.WriteLine("Job to run during peak hours");
                    retval = ActivationFilterResponse.StartJob;
                }
            } catch (IOException e) {
                logFile.WriteLine("Error Loading the XmlFile");
                logFile.WriteLine(e.ToString());
                retval = ActivationFilterResponse.FailJob;
            } catch (Exception e) {
                logFile.WriteLine(e.ToString());
                retval = ActivationFilterResponse.FailJob;
            } finally {
                logFile.Close();
            }

            return(retval);
        }