?? lpservlet.java
字號:
package com.power.pipeengine;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
public class LPServlet extends HttpServlet{//extends HttpServlet{
public boolean isWait = false;
public String _str = "";
public LPServlet() {
super();
initialize();
}
/**
* Process incoming HTTP GET requests
*
* @param request Object that encapsulates the request to the servlet
* @param response Object that encapsulates the response from the servlet
*/
public void doGet(
javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response)
throws ServletException, IOException {
doPost(request,response);
System.out.println("Nothing to do when GET method.");
}
/**
* Process incoming HTTP POST requests
*
* @param request Object that encapsulates the request to the servlet
* @param response Object that encapsulates the response from the servlet
*/
public void doPost(
javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response)
throws ServletException, IOException {
/**** Initialize Writer ****/
response.setContentType("text/html");
PrintWriter out = response.getWriter();
response.setStatus(HttpServletResponse.SC_OK);
/**** get Running Parameters ****/
_str = "<<<<Start ENGINE Running>>>>\n";
System.out.println();
System.out.println(_str);
out.println(_str);
String _exePath = request.getParameter("exePath");
String _outPath = request.getParameter("outPath");
String _paraPath = request.getParameter("paraPath");
String _testPath = request.getParameter("testPath");
String _workPath = request.getParameter("workPath");
String _logPath = request.getParameter("logPath");
if (_exePath == null || _outPath == null || _paraPath == null ||
_testPath == null || _workPath == null || _logPath == null) {
_str = "**Error: Missing parameters.\n";
out.println(_str);
System.out.println(_str);
return;
}
/**** DELETE .out & .log file before Running ***/
try {
File bbb = new File(_outPath);
_str = "CLEAR file --> '"+_outPath+"' "+(bbb.delete()?"OK.":"not exists.")+"\n";
out.println(_str);
System.out.println(_str);
bbb = new File(_logPath);
_str = "CLEAR file --> '"+_logPath+"' "+(bbb.delete()?"OK.":"not exists."+"\n");
out.println(_str);
System.out.println(_str);
}
catch (Exception e) {}
/**** WAITING .mps file before Running ****/
if (!waitFile(_testPath, 5, "Waiting INTERMEDIATE file", "1", out)) return;
/**** EXECUTE Engine COMMAND ****/
try {
Runtime r = Runtime.getRuntime();
Process p = null;
String cmd[] = {_exePath,_paraPath};
_str = "EXECUTE engine --> "+cmd[0]+" "+cmd[1]+"\n";
out.println(_str);
System.out.println(_str);
p=r.exec(cmd, null, new File(_workPath));
//r.runFinalization();
//r.gc();
} catch (Exception e) {
_str = "**Error: Engine running failed --> "+e.toString()+"\n";
out.println(_str);
System.out.println(_str);
return;
}
finally {
System.gc();
}
/**** WAITING .out file when Running ****/
// if (!waitFile(_outPath, 10, "Waiting RESULT file", "2", out)) return;
/**** WAITING .log file after Running ****/
// if (!waitFile(_logPath, 2, "Waiting TEMPORARY file", "3", out)) return;
/**** DELETE .log file before Running ***/
/* try {
File bbb = new File(_logPath);
_str = "Delete Temporary file --> '"+_logPath+"' "+(bbb.delete()?"OK.":"**Failed.")+"\n";
out.println(_str);
System.out.println(_str);
}
catch (Exception e) {}
*/
_str = "<<<<End ENGINE Running>>>>\n";
out.println(_str);
System.out.println(_str);
System.out.println();
}
/**
* Returns the servlet info string.
*/
public String getServletInfo() {
return super.getServletInfo();
}
/**
* Called whenever the part throws an exception.
* @param exception java.lang.Throwable
*/
private void handleException(java.lang.Throwable exception) {
System.out.println("--------- UNCAUGHT EXCEPTION ---------");
exception.printStackTrace(System.out);
}
/**
* Initializes the servlet.
*/
public void init(ServletConfig config) throws ServletException {
// insert code to initialize the servlet here
super.init(config);
}
/**
* Initialize the class.
*/
/* WARNING: THIS METHOD WILL BE REGENERATED. */
private void initialize() {
try {
} catch (java.lang.Throwable ivjExc) {
handleException(ivjExc);
}
}
/**
* Process incoming requests for information
*
* @param request Object that encapsulates the request to the servlet
* @param response Object that encapsulates the response from the servlet
*/
public void performTask(
javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response) {
//System.out.println("performTask() method is called.");
}
/**
* service method comment.
*/
public void service(HttpServletRequest request, HttpServletResponse response)
throws javax.servlet.ServletException, java.io.IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String sMethod;
sMethod = request.getMethod();
if (sMethod.equals("GET"))
doGet(request, response);
else
doPost(request, response);
}
public boolean waitFile(String _FileName, int _interval,
String _MsgStr, String _ErrorCode,PrintWriter out )
{
try {
_str = _MsgStr + " --> "+_FileName + "\n";
System.out.println(_str);
out.println(_str);
int count = 1;
RandomAccessFile ccc = null;
int interval = _interval;
while (true) {
try {
ccc = new RandomAccessFile(_FileName,"r");
if (ccc.length() < 1)
throw new FileNotFoundException("");
else
break;
}
catch (FileNotFoundException ex) {
if (ccc != null ) ccc.close();
_str = "waiting..."+interval*count++ + " secs.\n";
out.println(_str);
System.out.println(_str);
isWait = true;
waitThread wT = new waitThread(interval);
wT.start();
while (isWait) {};
if (interval*count >= 3600) {
_str = "**ERROR " + _ErrorCode + ": " +
"ENGINE running timeout, has been canceled.\n";
out.println(_str);
System.out.println(_str);
return false;
}
}
finally {
if (ccc != null ) ccc.close();
}
}
}
catch(Exception e) {
_str = "**Error"+_ErrorCode+":"+e.toString()+"\n";
out.println(_str);
System.out.println(_str);
return false;
}
return true;
}
public class waitThread extends Thread {
int interval = 0;
public waitThread(int secs) { this.interval = secs; }
public void run()
{
try {
waitThread.sleep(interval*1000);
isWait = false;
}
catch(Exception e){}
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -