📄 start.java
字号:
} catch (Exception e) {
e.printStackTrace();
}
}
}
serverRunning = false;
}
public void start() throws Exception {
startServer();
}
public String status() throws Exception {
String status = null;
try {
status = sendSocketCommand(Start.STATUS_COMMAND);
} catch (ConnectException e) {
return "Not Running";
} catch (IOException e) {
throw e;
}
return status;
}
public String shutdown() throws Exception {
return sendSocketCommand(Start.SHUTDOWN_COMMAND);
}
private String sendSocketCommand(String command) throws IOException, ConnectException {
Socket socket = new Socket(config.adminAddress, config.adminPort);
// send the command
PrintWriter writer = new PrintWriter(socket.getOutputStream(), true);
writer.println(config.adminKey + ":" + command);
writer.flush();
// read the reply
BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String response = null;
if (reader.ready()) {
response = reader.readLine();
}
reader.close();
// close the socket
writer.close();
socket.close();
return response;
}
public static void main(String[] args) throws Exception {
Start start = new Start(args.length == 2 ? args[1] : null);
String firstArg = args.length > 0 ? args[0] : "";
if (firstArg.equals("-help") || firstArg.equals("-?") || args.length > 2) {
System.out.println("");
System.out.println("Usage: java -jar ofbiz.jar [command] [config]");
System.out.println("-help, -? ----> This screen");
System.out.println("-start -------> Start the server");
System.out.println("-status ------> Status of the server");
System.out.println("-shutdown ----> Shutdown the server");
System.out.println("[no config] --> Use default config");
System.out.println("[no command] -> Start the server w/ default config");
} else if (firstArg.equals("-status")) {
System.out.println("Current Status : " + start.status());
} else if (firstArg.equals("-shutdown")) {
System.out.println("Shutting down server : " + start.shutdown());
} else {
start.start();
}
}
public static class Config {
public String containerConfig;
public InetAddress adminAddress;
public int adminPort;
public String adminKey;
public String ofbizHome;
public String baseJar;
public String baseLib;
public String baseConfig;
public String logDir;
public List loaders;
public String awtHeadless;
public Config(String config) {
try {
init(config);
} catch (IOException e) {
e.printStackTrace();
System.exit(-1);
}
}
public void init(String config) throws IOException {
InputStream propsStream = getClass().getClassLoader().getResourceAsStream(config);
if (propsStream == null) {
throw new IOException("Cannot load configuration properties : " + config);
}
Properties props = new Properties();
props.load(propsStream);
// set the ofbiz.home
if (ofbizHome == null) {
ofbizHome = props.getProperty("ofbiz.home", ".");
// get a full path
if (ofbizHome.equals(".")) {
ofbizHome = System.getProperty("user.dir");
ofbizHome = ofbizHome.replace('\\', '/');
}
}
System.setProperty("ofbiz.home", ofbizHome);
// base config directory
baseConfig = System.getProperty("ofbiz.base.config");
if (baseConfig == null) {
baseConfig = ofbizHome + "/" + props.getProperty("ofbiz.base.config", "config");
}
// base lib directory
baseLib = System.getProperty("ofbiz.base.lib");
if (baseLib == null) {
baseLib = ofbizHome + "/" + props.getProperty("ofbiz.base.lib", "lib");
}
// base jar file
baseJar = System.getProperty("ofbiz.base.jar");
if (baseJar == null) {
baseJar = ofbizHome + "/" + props.getProperty("ofbiz.base.jar", "base/build/lib/ofbiz-base.jar");
}
// log directory
logDir = System.getProperty("ofbiz.log.dir");
if (logDir == null) {
logDir = ofbizHome + "/" + props.getProperty("ofbiz.log.dir", "logs");
}
// container configuration
containerConfig = System.getProperty("ofbiz.container.config");
if (containerConfig == null) {
containerConfig = ofbizHome + "/" + props.getProperty("ofbiz.container.config", "base/config/ofbiz-containers.xml");
}
// get the admin server info
String serverHost = System.getProperty("ofbiz.admin.host");
if (serverHost == null) {
serverHost = props.getProperty("ofbiz.admin.host", "127.0.0.1");
}
String adminPortStr = System.getProperty("ofbiz.admin.port");
if (adminPortStr == null) {
adminPortStr = props.getProperty("ofbiz.admin.port", "10523");
}
// set the admin key
adminKey = System.getProperty("ofbiz.admin.key");
if (adminKey == null) {
adminKey = props.getProperty("ofbiz.admin.key", "NA");
}
// create the host InetAddress
adminAddress = InetAddress.getByName(serverHost);
// parse the port number
try {
adminPort = Integer.parseInt(adminPortStr);
} catch (Exception e) {
adminPort = 10523;
}
// set the property to tell Log4J to use debug.properties
String log4jConfig = System.getProperty("log4j.configuration");
if (log4jConfig == null) {
log4jConfig = props.getProperty("log4j.configuration");
}
// build a default log4j configuration based on ofbizHome
if (log4jConfig == null) {
log4jConfig = ofbizHome + "/config/debug.properties";
}
// set the log4j configuration property so we don't pick up one inside jars by mistake
System.setProperty("log4j.configuration", log4jConfig);
awtHeadless = System.getProperty("java.awt.headless");
if (awtHeadless == null) {
awtHeadless = props.getProperty("java.awt.headless");
}
if (awtHeadless != null) {
System.setProperty("java.awt.headless", awtHeadless);
}
// set the property to tell Jetty to use 2.4 SessionListeners
System.setProperty("org.mortbay.jetty.servlet.AbstractSessionManager.24SessionDestroyed", "true");
// loader classes
loaders = new ArrayList();
int currentPosition = 1;
while(true) {
String loaderClass = props.getProperty("ofbiz.start.loader" + currentPosition);
if (loaderClass == null || loaderClass.length() == 0) {
break;
} else {
loaders.add(loaderClass);
currentPosition++;
}
}
// close the stream
propsStream.close();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -