* @ BarryWang
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.EOFException;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.BindException;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import envmanager.util.StaticConstantUtil;
public class ServerEntry {
protected int
fListenPort = 3000;
private static
Properties fProperties = null;
private final static
byte[] fLock = new byte[0];
private final
ExecutorService fExecutor = Executors.newFixedThreadPool(3);
private final Map
fCommandThreadMap = new HashMap();
private final Map
fCommandProcessIdMap = new HashMap();
public void
acceptConnections() {
try {
ServerSocket serverSocket = new ServerSocket(fListenPort);
Socket
socket = null;
if(serverSocket.isBound()){
System.out.println("Server
has started!");
Properties properties =
readConfFile();
while (true) {
socket = serverSocket.accept();
if(socket.isBound()&&
socket.isConnected()){
String
command = getCommandFromClient(socket);
System.out.println(" -----------------"+command);
if(command.startsWith(StaticConstantUtil.EXE_COMMAND_PREFIX)){
this.executeCommand(socket,
command, properties);
}
else
if(command.startsWith(StaticConstantUtil.KILL_COMMAND_PREFIX)){
this.killProcess(socket,
command, properties);
}
else
{
new CommandHandle(socket,
command, properties, null).start();
}
}
}
}
} catch (BindException e) {
System.out.println("Unable to bind to port " + fListenPort);
} catch (IOException e) {
System.out.println("Unable to instantiate a ServerSocket on port:
"+ fListenPort);
}
}
private void
executeCommand(Socket socket, final String command, final
Properties properties){
final String key = command.substring(4);
if(fCommandThreadMap.get(key) == null){
CommandHandle ch = new CommandHandle(socket, command, properties,
null);
final List
procListBefore =
getProdessIdList(properties.getProperty(StaticConstantUtil.KILL_COMMAND_PREFIX+StaticConstantUtil.COMMAND_SEPARATOR+key));
System.out.println("---procListBefore: "+procListBefore);
@SuppressWarnings("unchecked")
Future f =
(Future)fExecutor.submit(ch);
try
{
Thread.sleep(500);
} catch
(InterruptedException e) {
e.printStackTrace();
}
new
Thread(){
@Override
public void run(){
List procListAfter =
getProdessIdList(properties.getProperty(StaticConstantUtil.KILL_COMMAND_PREFIX+StaticConstantUtil.COMMAND_SEPARATOR+key));
System.out.println("---procListAfter:
"+procListAfter);
addTheNewProcessIdIntoMap(procListBefore,
procListAfter, key);
fCommandThreadMap.put(key, command);
}
}.start();
if(f.isDone()){
System.out.println(key+" is
done!");
fCommandProcessIdMap.remove(key);
fCommandThreadMap.remove(key);
}
}
else {
new
MsgReturn(socket,
command+":"+StaticConstantUtil.EXE_COMMAND_STATUS_RUNNING).start();
}
}
private void
killProcess(Socket socket, String command, Properties
properties){
String key = command.substring(5);
if(fCommandThreadMap.get(key) != null){
fCommandThreadMap.remove(key);
if(fCommandProcessIdMap.get(key)!= null){
new CommandHandle(socket,
command, properties, fCommandProcessIdMap).start();
}
}
else {
new
MsgReturn(socket,
command+":"+StaticConstantUtil.KILL_COMMAND_STATUS_KILLED).start();
}
}
public void
addTheNewProcessIdIntoMap(List listBefore, List listAfter, String
command){
int size = listAfter.size();
for(int i=0; i
String
processId = listAfter.get(i);
if(!listBefore.contains(processId)){
fCommandProcessIdMap.put(command, processId);
System.out.println("------and
into map: "+command+processId);
break;
}
}
}
public static List
getProdessIdList(String processName){
Process process = null;
BufferedReader reader = null;
List processIdList = null;
try {
process =
Runtime.getRuntime().exec("tasklist");
reader =
new BufferedReader(new
InputStreamReader(process.getInputStream()));
String
processLine = "";
processIdList = new ArrayList();
processName += ".exe";
while((processLine = reader.readLine())!= null){
System.out.println(processLine);
String upperCaseLine =
processLine.trim().toUpperCase();
if
(upperCaseLine.startsWith(processName.toUpperCase())) {
String tempStr =
upperCaseLine.substring(processName.length());
boolean isProcessIdStart = false;
StringBuffer buf = new StringBuffer();
for (int i = 0; i < tempStr.length(); i++)
{
char ch =
tempStr.charAt(i);
if(ch != '
'){
isProcessIdStart =
true;
buf.append(ch);
}
if(isProcessIdStart && ch == ' '){
break;
}
}
processIdList.add(buf.toString().trim());
}
}
} catch(IOException e){
e.printStackTrace();
}
return processIdList;
}
private String
getCommandFromClient(Socket socket){
String command = "";
try {
command =
new DataInputStream(socket.getInputStream()).readUTF();
} catch (EOFException e){
System.out.println("Client has shutdown!");
} catch (IOException e) {
e.printStackTrace();
}
return command;
}
private static
Properties readConfFile(){
synchronized(fLock){
if(fProperties == null || fProperties.isEmpty()){
fProperties =
readConfigFile();
}
}
return fProperties;
}
private static
Properties readConfigFile(){
Properties properties = new Properties();
try {
FileInputStream fileInput = new
FileInputStream(ServerEntry.class.getResource("conf.properties").toURI().getPath());
properties.load(fileInput);
} catch (IOException e) {
System.out.println("Can't find the file:
'conf.properties'!");
e.printStackTrace();
} catch(URISyntaxException e){
System.out.println("Can't find the file:
'conf.properties'!");
e.printStackTrace();
}
return properties;
}
private boolean
addTheAppIntoRegedit(){
RegeditManager regManager = new
RegeditManager();
//String forderFor64 =
"HKEY_LOCAL_MACHINE\\SOFTWARE\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Run";//For
64bit system.
String forderFor32 =
"HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run";//For
32bit system.
String keyName = "EnvManager";
String dataType = "REG_SZ";
String dataValue = "D:\\Program
Files\\7thonline\\EnvManager.exe";
try {
if(regManager.query(forderFor32, keyName) == null){
return
regManager.add(forderFor32, keyName, dataType, dataValue);
}
else
{
return true;
}
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
public static void
main(String[] args) {
ServerEntry server = new ServerEntry();
// server.addTheAppIntoRegedit();
server.acceptConnections();
}
}