📄 start.java
字号:
/*
* $Id: Start.java,v 1.12 2003/12/22 03:41:28 ajzeneski Exp $
*
* Copyright (c) 2003 The Open For Business Project - www.ofbiz.org
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
* OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
* THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
package org.ofbiz.base.start;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.lang.reflect.Method;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.ConnectException;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;
/**
* Start - OFBiz Container(s) Startup Class
*
* @author <a href="mailto:jaz@ofbiz.org">Andy Zeneski</a>
*@version $Revision: 1.12 $
* @since 2.1
*/
public class Start implements Runnable {
public static final String CONFIG_FILE = "org/ofbiz/base/start/start.properties";
public static final String SHUTDOWN_COMMAND = "SHUTDOWN";
public static final String STATUS_COMMAND = "STATUS";
private Classpath classPath = new Classpath(System.getProperty("java.class.path"));
private ServerSocket serverSocket = null;
private Thread serverThread = null;
private boolean serverRunning = true;
private List loaders = null;
private Config config = null;
public Start(String configFile) throws IOException {
if (configFile == null) {
configFile = CONFIG_FILE;
}
this.config = new Config(configFile);
this.loaders = new ArrayList();
}
public void startListenerThread() throws IOException {
this.serverSocket = new ServerSocket(config.adminPort, 1, config.adminAddress);
this.serverThread = new Thread(this, this.toString());
this.serverThread.setDaemon(false);
this.serverThread.start();
System.out.println("Admin socket listening on - " + config.adminAddress + ":" + config.adminPort);
}
public void run() {
while (serverRunning) {
try {
Socket clientSocket = serverSocket.accept();
System.out.println("Received connection from - " + clientSocket.getInetAddress() + " : " + clientSocket.getPort());
processClientRequest(clientSocket);
clientSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
shutdownServer();
System.exit(0);
}
private void processClientRequest(Socket client) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(client.getInputStream()));
String request = reader.readLine();
PrintWriter writer = new PrintWriter(client.getOutputStream(), true);
writer.println(processRequest(request, client));
writer.flush();
writer.close();
reader.close();
}
private String processRequest(String request, Socket client) {
if (request != null) {
String key = request.substring(0, request.indexOf(':'));
String command = request.substring(request.indexOf(':')+1);
if (!key.equals(config.adminKey)) {
return "FAIL";
} else {
if (command.equals(Start.SHUTDOWN_COMMAND)) {
System.out.println("Shutdown initiated from: " + client.getInetAddress().getHostAddress() + ":" + client.getPort());
serverRunning = false;
} else if (command.equals(Start.STATUS_COMMAND)) {
return serverRunning ? "Running" : "Stopped";
}
return "OK";
}
} else {
return "FAIL";
}
}
private void loadLibs(String path) throws Exception {
File libDir = new File(path);
if (libDir.exists()) {
File files[] = libDir.listFiles();
for (int i = 0; i < files.length; i++) {
String fileName = files[i].getName();
if (files[i].isDirectory() && !"CVS".equals(fileName)) {
loadLibs(files[i].getCanonicalPath());
} else if (fileName.endsWith(".jar") || fileName.endsWith(".zip")) {
classPath.addComponent(files[i]);
}
}
}
}
private void startServer() throws Exception {
// load the lib directory
loadLibs(config.baseLib);
// load the ofbiz-base.jar
classPath.addComponent(config.baseJar);
// load the config directory
classPath.addComponent(config.baseConfig);
// set the classpath/classloader
System.setProperty("java.class.path", classPath.toString());
ClassLoader classloader = classPath.getClassLoader();
Thread.currentThread().setContextClassLoader(classloader);
// set the shutdown hook
setShutdownHook();
// start the listener thread
startListenerThread();
// stat the log directory
boolean createdDir = false;
File logDir = new File(config.logDir);
if (!logDir.exists()) {
logDir.mkdir();
createdDir = true;
}
// start the loaders
Iterator li = config.loaders.iterator();
while (li.hasNext()) {
String loaderClassName = (String) li.next();
try {
Class loaderClass = classloader.loadClass(loaderClassName);
StartupLoader loader = (StartupLoader) loaderClass.newInstance();
loader.load(config);
loaders.add(loader);
} catch (Exception e) {
e.printStackTrace();
System.exit(99);
}
}
}
private void setShutdownHook() {
try {
Method shutdownHook = java.lang.Runtime.class.getMethod("addShutdownHook", new Class[] { java.lang.Thread.class });
Thread hook = new Thread() {
public void run() {
setName("OFBiz_Shutdown_Hook");
shutdownServer();
// Try to avoid JVM crash
try {
Thread.sleep(1000);
} catch (Exception e) {
e.printStackTrace();
}
}
};
shutdownHook.invoke(Runtime.getRuntime(), new Object[] { hook });
} catch (Exception e) {
// VM Does not support shutdown hook
e.printStackTrace();
}
}
private void shutdownServer() {
if (loaders != null && loaders.size() > 0) {
Iterator i = loaders.iterator();
while (i.hasNext()) {
StartupLoader loader = (StartupLoader) i.next();
try {
loader.unload();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -