📄 start.java
字号:
/* * $Id: Start.java 5462 2005-08-05 18:35:48Z jonesde $ * * 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.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.PrintWriter;import java.lang.reflect.Method;import java.net.ConnectException;import java.net.InetAddress;import java.net.ServerSocket;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 $Rev: 5462 $ * @since 2.1 */public class Start implements Runnable { private Classpath classPath = new Classpath(System.getProperty("java.class.path")); private ClassLoader classloader = null; private ServerSocket serverSocket = null; private Thread serverThread = null; private boolean serverRunning = true; private List loaders = null; private Config config = null; private String[] loaderArgs = null; private static final String SHUTDOWN_COMMAND = "SHUTDOWN"; private static final String STATUS_COMMAND = "STATUS"; private static final double REQUIRED_JDK = 1.4; public void init(String[] args, boolean fullInit) throws IOException { String firstArg = args.length > 0 ? args[0] : ""; String cfgFile = Start.getConfigFileName(firstArg); this.loaders = new ArrayList(); this.config = new Config(); // read the default properties first config.readConfig(cfgFile); // parse the startup arguments if (args.length > 1) { this.loaderArgs = new String[args.length - 1]; for (int i = 1; i < args.length; i++) { this.loaderArgs[i - 1] = args[i]; } } if (fullInit) { // initialize the classpath initClasspath(); // initialize the log directory initLogDirectory(); // initialize the listener thread initListenerThread(); // initialize the startup loaders initStartLoaders(); // set the shutdown hook if (config.useShutdownHook) { setShutdownHook(); } else { System.out.println("Shutdown hook disabled"); } } } public void init(String[] args) throws IOException { init(args, true); } 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 initListenerThread() throws IOException { if (config.adminPort > 0) { this.serverSocket = new ServerSocket(config.adminPort, 1, config.adminAddress); this.serverThread = new Thread(this, this.toString()); this.serverThread.setDaemon(false); System.out.println("Admin socket configured on - " + config.adminAddress + ":" + config.adminPort); } else { System.out.println("Admin socket not configured; set to port 0"); } } private void startListenerThread() { if (serverSocket != null && serverThread != null) { this.serverThread.start(); } } private void loadLibs(String path, boolean recurse) throws IOException { 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) && recurse) { loadLibs(files[i].getCanonicalPath(), recurse); } else if (fileName.endsWith(".jar") || fileName.endsWith(".zip")) { classPath.addComponent(files[i]); } } } } private void initClasspath() throws IOException { // load tools.jar if (config.toolsJar != null) { classPath.addComponent(config.toolsJar); } // load comm.jar if (config.commJar != null) { classPath.addComponent(config.commJar); } // add OFBIZ_HOME to CP & load libs classPath.addClasspath(config.ofbizHome); loadLibs(config.ofbizHome, false); // load the lib directory if (config.baseLib != null) { loadLibs(config.baseLib, true); } // load the ofbiz-base.jar if (config.baseJar != null) { classPath.addComponent(config.baseJar); } // load the base schema directory if (config.baseDtd != null) { classPath.addComponent(config.baseDtd); } // load the config directory if (config.baseConfig != null) { classPath.addComponent(config.baseConfig); } // set the classpath/classloader System.setProperty("java.class.path", classPath.toString()); this.classloader = classPath.getClassLoader(); Thread.currentThread().setContextClassLoader(classloader); if (System.getProperty("DEBUG") != null) { System.out.println("Startup Classloader: " + classloader.toString()); System.out.println("Startup Classpath: " + classPath.toString()); } } private void initLogDirectory() { // stat the log directory boolean createdDir = false; File logDir = new File(config.logDir); if (!logDir.exists()) { logDir.mkdir(); createdDir = true; } if (createdDir) { System.out.println("Created OFBiz log dir [" + logDir.getAbsolutePath() + "]"); } } private void initStartLoaders() { // initialize 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, loaderArgs); loaders.add(loader); } catch (Exception e) { e.printStackTrace(); System.exit(99); } } } private void startStartLoaders() { // start the loaders Iterator i = loaders.iterator(); while (i.hasNext()) { StartupLoader loader = (StartupLoader) i.next(); try { loader.start(); } catch (StartupException 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(); } catch (Exception e) { e.printStackTrace(); } } } serverRunning = false; } private void startServer() { // start the listener thread startListenerThread(); // start the startup loaders startStartLoaders(); } public void start() { startServer(); if (config.shutdownAfterLoad) { shutdownServer(); System.exit(0); } } public void stop() { shutdownServer(); } public void destroy() { this.serverSocket = null; this.serverThread = null; this.loaders = null; this.config = null; this.loaderArgs = null; } public String shutdown() throws IOException { return sendSocketCommand(Start.SHUTDOWN_COMMAND); } public String status() throws IOException { String status = null; try { status = sendSocketCommand(Start.STATUS_COMMAND); } catch (ConnectException e) { return "Not Running"; } catch (IOException e) { throw e; } return status; } 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; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -