1. Scene
Recently, a project customer asked to configure relevant permissions by themselves. Due to historical reasons, this project uses the company's previous authority system. This permission system is very powerful, but there is a disadvantage, that is, every time the permission menu is added, it must be restarted before it can take effect, otherwise it will have to wait for one day before its cache expires. Due to the project progress, we cannot overturn this permission system and start over.
The feasible way is to add the permission system restart button to the administrator operation interface. In this way, customers can easily restart Tomcat after modifying permissions.
2. Technical analysis
Since the administrator system is based on .net BS. So we need to be able to control the shutdown and startup of Tomcat through the web page and load it into the administrator system.
First, analyze the startup method of Tomcat. There are two batch files in the bin directory of Tomcat 6.0. and control the start and shutdown of Tomcat respectively. To control the startup and shutdown of Tomcat, we have to call these two batch files.
How to adjust it? Where to adjust? It is impossible to directly tune the browser. A web page must be deployed on the web server and these two commands are called on the server. It is best if deployed inside the permission system, but this creates a problem, we can turn off Tomcat but it cannot start. The final solution is to deploy a .net-implemented webservice on the same machine, and use this webservice to enable the startup and shutdown of the permission system.
3. Specific implementation
Create a web service project in VS2008.
Create two WebMethods.
1. Start the Tomcat method
/// <summary>
/// Start the permission system
/// </summary>
/// <returns>true:success; false:failed</returns>
[WebMethod]
public bool StartPM()
{
try
{
// Create process startup information
ProcessStartInfo sinfo = new ProcessStartInfo( + "");
// Get system environment variables
IDictionary dics = ();
foreach (string key in )
{// Add system environment variables to new process environment variables
if ((key)) continue;
(key, dics[key].ToString());
}
// No need to create it from the system shell, directly from the file
= false;
(sinfo);
}
catch (Exception ex)
{
//TODO: Write a log
return false;
}
return true;
}
.Close the Tomcat method
/// <summary>
/// Turn off the permission system
/// </summary>
/// <returns>true:success; false:failed</returns>
[WebMethod]
public bool StopPM()
{
try
{
// Create process startup information
ProcessStartInfo sinfo = new ProcessStartInfo( + "");
// Get system environment variables
IDictionary dics = ();
foreach (string key in )
{// Add system environment variables to new process environment variables
if ((key)) continue;
(key, dics[key].ToString());
}
// No need to create it from the system shell, directly from the file
= false;
(sinfo);
}
catch (Exception ex)
{
//TODO: Write a log
return false;
}
return true;
}
To facilitate future configuration, I have created two new bat files. In these two files, the sum of Tomcat is done. The details are as follows:
4. Summary
During the entire development process, an error occurred that the CATALINA_HOME and JAVA_HOME environment variables could not be found. That is because the parent process, that is, the environment variables of the web service, do not have these two items, so when creating the child process (the environment variables of the child process are inherited from the parent process). You have to get the environment variable from the system environment variable and add it to the newly started process. At the same time, it must be clear that the working directory of the process is not equal to the startup directory of the process. for example
If the error that cannot be found is removed, it is because the working directory and the startup directory are inconsistent.
Recently, a project customer asked to configure relevant permissions by themselves. Due to historical reasons, this project uses the company's previous authority system. This permission system is very powerful, but there is a disadvantage, that is, every time the permission menu is added, it must be restarted before it can take effect, otherwise it will have to wait for one day before its cache expires. Due to the project progress, we cannot overturn this permission system and start over.
The feasible way is to add the permission system restart button to the administrator operation interface. In this way, customers can easily restart Tomcat after modifying permissions.
2. Technical analysis
Since the administrator system is based on .net BS. So we need to be able to control the shutdown and startup of Tomcat through the web page and load it into the administrator system.
First, analyze the startup method of Tomcat. There are two batch files in the bin directory of Tomcat 6.0. and control the start and shutdown of Tomcat respectively. To control the startup and shutdown of Tomcat, we have to call these two batch files.
How to adjust it? Where to adjust? It is impossible to directly tune the browser. A web page must be deployed on the web server and these two commands are called on the server. It is best if deployed inside the permission system, but this creates a problem, we can turn off Tomcat but it cannot start. The final solution is to deploy a .net-implemented webservice on the same machine, and use this webservice to enable the startup and shutdown of the permission system.
3. Specific implementation
Create a web service project in VS2008.
Create two WebMethods.
1. Start the Tomcat method
Copy the codeThe code is as follows:
/// <summary>
/// Start the permission system
/// </summary>
/// <returns>true:success; false:failed</returns>
[WebMethod]
public bool StartPM()
{
try
{
// Create process startup information
ProcessStartInfo sinfo = new ProcessStartInfo( + "");
// Get system environment variables
IDictionary dics = ();
foreach (string key in )
{// Add system environment variables to new process environment variables
if ((key)) continue;
(key, dics[key].ToString());
}
// No need to create it from the system shell, directly from the file
= false;
(sinfo);
}
catch (Exception ex)
{
//TODO: Write a log
return false;
}
return true;
}
.Close the Tomcat method
Copy the codeThe code is as follows:
/// <summary>
/// Turn off the permission system
/// </summary>
/// <returns>true:success; false:failed</returns>
[WebMethod]
public bool StopPM()
{
try
{
// Create process startup information
ProcessStartInfo sinfo = new ProcessStartInfo( + "");
// Get system environment variables
IDictionary dics = ();
foreach (string key in )
{// Add system environment variables to new process environment variables
if ((key)) continue;
(key, dics[key].ToString());
}
// No need to create it from the system shell, directly from the file
= false;
(sinfo);
}
catch (Exception ex)
{
//TODO: Write a log
return false;
}
return true;
}
To facilitate future configuration, I have created two new bat files. In these two files, the sum of Tomcat is done. The details are as follows:
Copy the codeThe code is as follows:
K:\apache-tomcat-6.0.30\bin\
Copy the codeThe code is as follows:
K:\apache-tomcat-6.0.30\bin\
4. Summary
During the entire development process, an error occurred that the CATALINA_HOME and JAVA_HOME environment variables could not be found. That is because the parent process, that is, the environment variables of the web service, do not have these two items, so when creating the child process (the environment variables of the child process are inherited from the parent process). You have to get the environment variable from the system environment variable and add it to the newly started process. At the same time, it must be clear that the working directory of the process is not equal to the startup directory of the process. for example
Copy the codeThe code is as follows:
ProcessStartInfo sinfo = new ProcessStartInfo( + "");
If the error that cannot be found is removed, it is because the working directory and the startup directory are inconsistent.