RunPhp.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.IO;
using System.Xml;
 
namespace Tomcat
{
    public class RunPhp
    {
        /// <summary>
        /// Function to start php exe
        /// </summary>
        /// <param name="phpLocation"></param>
        /// <param name="phpPort"></param>
        public void StartPhp(string tomcatLocation, string phpPort)
        {
            string response;
            // Call the RunCommand function to change the port in web.xml
            //response = RunCommand(Environment.GetEnvironmentVariable("RoleRoot") + @"\approot\setupPhp.bat"
            //    , phpLocation, phpPort, Environment.GetEnvironmentVariable("RoleRoot") + @"\approot");
            // Call the StartPhpProcess to start the php process
            response = RunPhpProcess(tomcatLocation, phpPort);
 
            //setting the webxml Configpath
            string serverConfigPath = tomcatLocation + @"webapps\php\WEB-INF\web.xml";
            Trace.WriteLine(serverConfigPath, "Information");
 
            //Calling ConfigTomcatPort method to configure the port in web.xml
            ConfigPhpPort(serverConfigPath, phpPort);
        }
 
        /// <summary>
        /// Function to start php Process
        /// </summary>
        /// <param name="phpLocation"></param>
        /// <returns></returns>
 
        private string RunPhpProcess(string tomcatLocation,string phpPort)
        {
            // This is a sample worker implementation. Replace with your logic.
            Trace.WriteLine("PHP entry point called", "Information");
 
            // initiating stream reader to get the output details
//            StreamReader sr;
            // string variable to store the output details
            string returnDetails;
 
            // Run Phpcgi
            Process phpProc = new Process();
            try
            {
                phpProc.StartInfo.UseShellExecute = false;
                phpProc.StartInfo.RedirectStandardOutput = false;
 
                // setting the localsource path tomcatlocation to the environment variable catalina_home 
                //phpProc.StartInfo.EnvironmentVariables.Remove("XXXX");
                //phpProc.StartInfo.EnvironmentVariables.Add("XXXX", "");
 
                string phpini =  tomcatLocation+@"php\php.ini" ;
 
                // setting the file name  bin\startup.bat in tomcatlocation of localresourcepath 
                phpProc.StartInfo.Arguments = String.Format("{0} {1} {2} {3}", "-b", phpPort, "-c", phpini);
                phpProc.StartInfo.FileName = tomcatLocation + @"php\php-cgi.exe";
                Trace.WriteLine(phpProc.StartInfo.FileName, "Information");
//                phpProc.EnableRaisingEvents = false;
                // starting process
                phpProc.Start();
 
                // getting the process output
//                sr = phpProc.StandardOutput;
 
                // storing the output in the string variable
//                returnDetails = sr.ReadToEnd();
 
                // Logging the output details
//                Trace.TraceInformation("Information", returnDetails);
                returnDetails = null;
 
            }
            catch (Exception ex)
            {
                returnDetails = ex.Message;
                // Logging the exceptiom
                Trace.TraceError(ex.Message);
            }
            return returnDetails;
        }
        /// <summary>
        /// ConfigphpPort method is to configure the port in server.xml
        /// </summary>
        /// <param name="serverConfigPath">Full path location of server.xml</param>
        /// <param name="newPort">New value for connector</param>
        private void ConfigPhpPort(string serverConfigPath, string newPort)
        {
            try
            {
                XmlDocument config = new XmlDocument();
 
                // Load \conf\server.xml
                config.Load(serverConfigPath);
 
                Trace.TraceInformation("Original Value = {0}"
                  , config.SelectSingleNode("/web-app/servlet/init-param/param-value").InnerText);
 
                //Change the port with the new port Arg 1
                config.SelectSingleNode("/web-app/servlet/init-param/param-value").InnerText = newPort;
 
                Trace.TraceInformation("Updating web.xml...");
 
                config.Save(serverConfigPath);
 
                Trace.TraceInformation("web.xml updated...");
            }
            catch (Exception ex)
            {
                Trace.TraceError(ex.StackTrace);
            }
        }
 
    }
}
最終更新:2010年12月06日 19:34