package com.jinoos.flume;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Enumeration;
import java.util.Properties;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
/**
* socket 操作命令和文件传输
*
* @author XBF
*
*/
public class FileSkServer {
ExecutorService exec = Executors.newCachedThreadPool();
private static final String SUCCESS_MSG = "SUCCESS";// 成功标识符
private static final String FAILED_MSG = "FAILED";// 失败标识符
private static final String REPLACE = "replace";// 操作命令
private static final String RESTART = "restart";// 操作命令
private static final String STOP = "stop";// 操作命令
private static final int BUFFER_SIZE = 1024;// 缓存大小
private static final String configFile = "config.properties";// 版本号存放文件
private Properties pro = new Properties();// 加载属性文件,读取数据库连接配置信息
private String PATH = "";
private Runtime rt = Runtime.getRuntime();
private static boolean flag = true;
Socket socket = null;
public FileSkServer() {
ServerSocket server = null;
try {
pro.load(FileSkServer.class.getResourceAsStream(configFile));
String port = pro.getProperty("fileServer.port");
server = new ServerSocket(Integer.parseInt(port));// 绑定接受数据端口
System.out.println("Waiting for client connect……");
while (flag) {
socket = server.accept();
exec.execute(new Handler(socket));
}
} catch (IOException e) {
//e.printStackTrace();
if(server.isClosed()||socket.isClosed()){
new FileSkServer();
}
} finally {
}
}
public static void main(String[] args) {
new FileSkServer();
}
// 判断是否是windows操作系统
private boolean isWin() throws IOException {
boolean flag = false;
String os = System.getProperty("os.name").toLowerCase();
pro.load(FileSkServer.class.getResourceAsStream(configFile));
if (os.startsWith("win")) {// 如果是Windows操作系统
flag = true;
}else{
PATH = pro.getProperty("flume_home_linux");
}
return flag;
}
// 判断系统架构(32位或64位)
private boolean is64bit() throws IOException {
boolean flag = false;
String os = System.getProperty("os.arch");
pro.load(MonitorClient.class.getResourceAsStream(configFile));
if (os.contains("86") || os.contains("32")) {
PATH = pro.getProperty("flume_home_win_32");
} else if (os.contains("64")) {
PATH = pro.getProperty("flume_home_win_64");
flag= true;
}
return flag;
}
// 停止系统
private boolean stop() {
System.out.println("Begin to stop this application...");
try {
String command = "";
if (isWin()) {
is64bit();
command = "cmd /c " + PATH + "bin\\stopCWAgen.bat start";
} else {
command = PATH + "bin/stopCWAgen.sh";
}
if (isAlive()) {// 结束TOMCAT进程
Process p= rt.exec(command, null, new File(PATH + "bin\\"));
final BufferedReader br2 = new BufferedReader(
new InputStreamReader(p.getErrorStream(), "UTF-8"));
final BufferedReader br = new BufferedReader(
new InputStreamReader(p.getInputStream(), "UTF-8"));
Thread t1 = new Thread() {
public void run() {
String line = null;
try {
while ((line = br2.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
}
};
Thread t2 = new Thread() {
public void run() {
String line = null;
try {
while ((line = br.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
}
};
t1.start();
t2.start();
// if (p.waitFor() != 0) {
// if (p.exitValue() == 1) {// 0表示正常结束,1:非正常结束
// System.err.println("Stop Failed!");
// }
// }
if (isAlive())
kill();// 强制结束进程
}
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
// 启动系统
private String start() {
System.out.println("starting run this application...");
String result = FAILED_MSG;
try {
if (!isAlive()) {
String command = "";
Process p = null;
if (isWin()) {
if (is64bit()) {
command = "cmd /c " + PATH
+ "bin\\startCWAgen.bat start";
} else {
command = "cmd /c " + PATH
+ "bin\\startCWAgen_32.bat start";
}
} else {
command = PATH + "bin/startup.sh";
}
p = rt.exec(command, null, new File(PATH + "bin\\"));
final BufferedReader br2 = new BufferedReader(
new InputStreamReader(p.getErrorStream(), "UTF-8"));
final BufferedReader br = new BufferedReader(
new InputStreamReader(p.getInputStream(), "UTF-8"));
Thread t1 = new Thread() {
public void run() {
String line = null;
try {
while ((line = br2.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
}
};
Thread t2 = new Thread() {
public void run() {
String line = null;
try {
while ((line = br.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
}
};
t1.start();
t2.start();
// if (p.waitFor() != 0) {
// if (p.exitValue() == 1) {
// System.err.println("Start failed!");
// return FAILED_MSG;
// }
// }
if (isAlive()) {
System.out.println("Start successfully!");
return SUCCESS_MSG;
}
}
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
/**
* 判断是否运行
*
* @param rt
* @return
* @throws IOException
*/
private boolean isAlive() {
boolean isAlive = false;
try {
if (isWin()) {
String program = "CWAgen.exe";
String command = "TASKLIST.EXE /FI \"IMAGENAME EQ CWAGEN.EXE\" /FO CSV /NH";
Process p = rt.exec(command);
BufferedReader read = new BufferedReader(new InputStreamReader(
p.getInputStream()));
String line = "";
while ((line = read.readLine()) != null) {
if (line.indexOf(program) != -1) {
System.out.println("This application is running:"
+ line);
isAlive = true;
} else {
System.out.println("This application is closed");
}
}
read.close();
p.destroy();
} else {
String command = "ps -ef | grep \"CWAgen\" | wc -l";
Process p = rt.exec(command);
BufferedReader read = new BufferedReader(new InputStreamReader(
p.getInputStream()));
String line = "";
while ((line = read.readLine()) != null) {
if (line.equals("1")) {
System.out.println("This application is running:"
+ line);
isAlive = true;
} else if (line.equals("0")) {
System.out.println("This application is closed");
}
}
read.close();
p.destroy();
}
} catch (Exception e) {
e.printStackTrace();
}
return isAlive;
}
/**
* 强制结束进程
*
* @param rt
* @throws IOException
*/
public boolean kill() {
try {
System.out.println("Begin to stop this application...");
if (isAlive()) {
Process p = null;
if (isWin()) {
String cmd = "taskkill -f -im CWAgen.exe";
p = rt.exec(cmd);
评论0
最新资源