Пример #1
0
        /**
         * Parses a crontab-like line.
         * 
         * @param table
         *            The table on which the parsed task will be stored, by
         *            side-effect.
         * @param line
         *            The crontab-like line.
         * @throws Exception
         *             The supplied line doesn't represent a valid task line.
         */
        public static void parseLine(TaskTable table, string line)
        {
            if (string.IsNullOrEmpty(line))
                return;

            line = line.Trim();
            if (line.Length == 0 || line[0] == '#')
            {
                return;
            }
            // Detecting the pattern.
            int size = line.Length;
            string pattern = null;
            for (int i = size; i >= 0; i--)
            {
                string aux = line.Substring(0, i);
                if (SchedulingPattern.Validate(aux))
                {
                    pattern = aux;
                    break;
                }
            }
            if (pattern == null)
            {
                throw new Exception("Invalid cron line: " + line);
            }
            line = line.Substring(pattern.Length);
            size = line.Length;
            // Splitting the line
            List<string> splitted = new List<string>();
            StringBuilder current = null;
            bool quotes = false;
            for (int i = 0; i < size; i++)
            {
                char c = line[i];
                if (current == null)
                {
                    if (c == '"')
                    {
                        current = new StringBuilder();
                        quotes = true;
                    }
                    else if (c > ' ')
                    {
                        current = new StringBuilder();
                        current.Append(c);
                        quotes = false;
                    }
                }
                else
                {
                    bool closeCurrent;
                    if (quotes)
                    {
                        closeCurrent = (c == '"');
                    }
                    else
                    {
                        closeCurrent = (c <= ' ');
                    }
                    if (closeCurrent)
                    {
                        if (current != null && current.Length > 0)
                        {
                            string str = current.ToString();
                            if (quotes)
                            {
                                str = escape(str);
                            }
                            splitted.Add(str);
                        }
                        current = null;
                    }
                    else
                    {
                        current.Append(c);
                    }
                }
            }
            if (current != null && current.Length > 0)
            {
                string str = current.ToString();
                if (quotes)
                {
                    str = escape(str);
                }
                splitted.Add(str);
                current = null;
            }
            // Analyzing
            size = splitted.Count;
            int status = 0;
            // Status values:
            // 0 -> fetching environment variables, working directory and channels
            // 1 -> fetching the command and its arguments
            string dirString = null;
            FileInfo stdinFile = null;
            FileInfo stdoutFile = null;
            FileInfo stderrFile = null;
            List<string> envsList = new List<string>();
            string command = null;
            List<string> argsList = new List<string>();
            for (int i = 0; i < size; i++)
            {
                string tk = (string)splitted[i];
                // Check the local status.
                if (status == 0)
                {
                    // Environment variables, working directory and channels
                    if (tk.StartsWith("ENV:"))
                    {
                        envsList.Add(tk.Substring(4));
                        continue;
                    }
                    else if (tk.StartsWith("DIR:"))
                    {
                        dirString = tk.Substring(4);
                        continue;
                    }
                    else if (tk.StartsWith("IN:"))
                    {
                        stdinFile = new FileInfo(tk.Substring(3));
                        continue;
                    }
                    else if (tk.StartsWith("OUT:"))
                    {
                        stdoutFile = new FileInfo(tk.Substring(4));
                        continue;
                    }
                    else if (tk.StartsWith("ERR:"))
                    {
                        stderrFile = new FileInfo(tk.Substring(4));
                        continue;
                    }
                    else
                    {
                        status = 1;
                    }
                }
                if (status == 1)
                {
                    // Command or argument?
                    if (command == null)
                    {
                        command = tk;
                    }
                    else
                    {
                        argsList.Add(tk);
                    }
                }
            }
            // Task preparing.
            Task task = null;
            // Command evaluation.
            if (command == null)
            {
                // No command!
                throw new Exception("Invalid cron line: " + line);
            }
            else if (command.StartsWith("dotnet:")) //INFO in cron4net use dotnet mark .NET Type
            {
                // Java inner-process.
                string className = command.Substring(7);
                if (className.Length == 0)
                {
                    throw new Exception("Invalid .NET type name on line: " + line);
                }
                string methodName;
                int sep = className.IndexOf('#');
                if (sep == -1)
                {
                    methodName = "Main";
                }
                else
                {
                    methodName = className.Substring(sep + 1);
                    className = className.Substring(0, sep);
                    if (methodName.Length == 0)
                    {
                        throw new Exception("Invalid .NET method name on line: "
                                + line);
                    }
                }
                string[] args = argsList.ToArray();

                task = new StaticMethodTask(className, methodName, args);
            }
            else
            {
                // External command.
                string[] cmdarray = new string[1 + argsList.Count];
                cmdarray[0] = command;
                for (int i = 0; i < argsList.Count; i++)
                {
                    cmdarray[i + 1] = argsList[i];
                }
                // Environments.
                string[] envs = null;
                size = envsList.Count;
                if (size > 0)
                {
                    envs = envsList.ToArray();
                }
                // Working directory.
                DirectoryInfo dir = null;
                if (dirString != null)
                {
                    dir = new DirectoryInfo(dirString);
                    if (!dir.Exists)
                    {
                        throw new Exception(
                                "Invalid cron working directory parameter at line: "
                                        + line,
                                new FileNotFoundException(dirString
                                        + " doesn't exist or it is not a directory"));
                    }
                }
                //TODO  ProcessTask
                //// Builds the task.
                //ProcessTask process = new ProcessTask(cmdarray, envs, dir);
                //// Channels.
                //if (stdinFile != null)
                //{
                //    process.setStdinFile(stdinFile);
                //}
                //if (stdoutFile != null)
                //{
                //    process.setStdoutFile(stdoutFile);
                //}
                //if (stderrFile != null)
                //{
                //    process.setStderrFile(stderrFile);
                //}
                //task = process;
            }
            // End.
            table.Add(new SchedulingPattern(pattern), task);
        }
Пример #2
0
        /**
         * Parses a crontab-like line.
         *
         * @param table
         *            The table on which the parsed task will be stored, by
         *            side-effect.
         * @param line
         *            The crontab-like line.
         * @throws Exception
         *             The supplied line doesn't represent a valid task line.
         */
        public static void parseLine(TaskTable table, string line)
        {
            if (string.IsNullOrEmpty(line))
            {
                return;
            }

            line = line.Trim();
            if (line.Length == 0 || line[0] == '#')
            {
                return;
            }
            // Detecting the pattern.
            int    size    = line.Length;
            string pattern = null;

            for (int i = size; i >= 0; i--)
            {
                string aux = line.Substring(0, i);
                if (SchedulingPattern.Validate(aux))
                {
                    pattern = aux;
                    break;
                }
            }
            if (pattern == null)
            {
                throw new Exception("Invalid cron line: " + line);
            }
            line = line.Substring(pattern.Length);
            size = line.Length;
            // Splitting the line
            List <string> splitted = new List <string>();
            StringBuilder current  = null;
            bool          quotes   = false;

            for (int i = 0; i < size; i++)
            {
                char c = line[i];
                if (current == null)
                {
                    if (c == '"')
                    {
                        current = new StringBuilder();
                        quotes  = true;
                    }
                    else if (c > ' ')
                    {
                        current = new StringBuilder();
                        current.Append(c);
                        quotes = false;
                    }
                }
                else
                {
                    bool closeCurrent;
                    if (quotes)
                    {
                        closeCurrent = (c == '"');
                    }
                    else
                    {
                        closeCurrent = (c <= ' ');
                    }
                    if (closeCurrent)
                    {
                        if (current != null && current.Length > 0)
                        {
                            string str = current.ToString();
                            if (quotes)
                            {
                                str = escape(str);
                            }
                            splitted.Add(str);
                        }
                        current = null;
                    }
                    else
                    {
                        current.Append(c);
                    }
                }
            }
            if (current != null && current.Length > 0)
            {
                string str = current.ToString();
                if (quotes)
                {
                    str = escape(str);
                }
                splitted.Add(str);
                current = null;
            }
            // Analyzing
            size = splitted.Count;
            int status = 0;
            // Status values:
            // 0 -> fetching environment variables, working directory and channels
            // 1 -> fetching the command and its arguments
            string        dirString  = null;
            FileInfo      stdinFile  = null;
            FileInfo      stdoutFile = null;
            FileInfo      stderrFile = null;
            List <string> envsList   = new List <string>();
            string        command    = null;
            List <string> argsList   = new List <string>();

            for (int i = 0; i < size; i++)
            {
                string tk = (string)splitted[i];
                // Check the local status.
                if (status == 0)
                {
                    // Environment variables, working directory and channels
                    if (tk.StartsWith("ENV:"))
                    {
                        envsList.Add(tk.Substring(4));
                        continue;
                    }
                    else if (tk.StartsWith("DIR:"))
                    {
                        dirString = tk.Substring(4);
                        continue;
                    }
                    else if (tk.StartsWith("IN:"))
                    {
                        stdinFile = new FileInfo(tk.Substring(3));
                        continue;
                    }
                    else if (tk.StartsWith("OUT:"))
                    {
                        stdoutFile = new FileInfo(tk.Substring(4));
                        continue;
                    }
                    else if (tk.StartsWith("ERR:"))
                    {
                        stderrFile = new FileInfo(tk.Substring(4));
                        continue;
                    }
                    else
                    {
                        status = 1;
                    }
                }
                if (status == 1)
                {
                    // Command or argument?
                    if (command == null)
                    {
                        command = tk;
                    }
                    else
                    {
                        argsList.Add(tk);
                    }
                }
            }
            // Task preparing.
            Task task = null;

            // Command evaluation.
            if (command == null)
            {
                // No command!
                throw new Exception("Invalid cron line: " + line);
            }
            else if (command.StartsWith("dotnet:")) //INFO in cron4net use dotnet mark .NET Type
            {
                // Java inner-process.
                string className = command.Substring(7);
                if (className.Length == 0)
                {
                    throw new Exception("Invalid .NET type name on line: " + line);
                }
                string methodName;
                int    sep = className.IndexOf('#');
                if (sep == -1)
                {
                    methodName = "Main";
                }
                else
                {
                    methodName = className.Substring(sep + 1);
                    className  = className.Substring(0, sep);
                    if (methodName.Length == 0)
                    {
                        throw new Exception("Invalid .NET method name on line: "
                                            + line);
                    }
                }
                string[] args = argsList.ToArray();

                task = new StaticMethodTask(className, methodName, args);
            }
            else
            {
                // External command.
                string[] cmdarray = new string[1 + argsList.Count];
                cmdarray[0] = command;
                for (int i = 0; i < argsList.Count; i++)
                {
                    cmdarray[i + 1] = argsList[i];
                }
                // Environments.
                string[] envs = null;
                size = envsList.Count;
                if (size > 0)
                {
                    envs = envsList.ToArray();
                }
                // Working directory.
                DirectoryInfo dir = null;
                if (dirString != null)
                {
                    dir = new DirectoryInfo(dirString);
                    if (!dir.Exists)
                    {
                        throw new Exception(
                                  "Invalid cron working directory parameter at line: "
                                  + line,
                                  new FileNotFoundException(dirString
                                                            + " doesn't exist or it is not a directory"));
                    }
                }
                //TODO  ProcessTask
                //// Builds the task.
                //ProcessTask process = new ProcessTask(cmdarray, envs, dir);
                //// Channels.
                //if (stdinFile != null)
                //{
                //    process.setStdinFile(stdinFile);
                //}
                //if (stdoutFile != null)
                //{
                //    process.setStdoutFile(stdoutFile);
                //}
                //if (stderrFile != null)
                //{
                //    process.setStderrFile(stderrFile);
                //}
                //task = process;
            }
            // End.
            table.Add(new SchedulingPattern(pattern), task);
        }