Main.java 24.5 KB
Newer Older
caixiangyi's avatar
caixiangyi 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
package com.x.server.console;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.io.RandomAccessFile;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.FileChannel.MapMode;
import java.nio.channels.FileLock;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;
import org.eclipse.jetty.deploy.App;
import org.eclipse.jetty.deploy.DeploymentManager;
import org.quartz.Scheduler;

import com.x.base.core.project.config.ApplicationServer;
import com.x.base.core.project.config.Config;
import com.x.base.core.project.config.DataServer;
import com.x.base.core.project.config.StorageServer;
import com.x.base.core.project.config.WebServer;
import com.x.base.core.project.tools.DefaultCharset;
import com.x.server.console.action.ActionCompactData;
import com.x.server.console.action.ActionConfig;
import com.x.server.console.action.ActionCreateEncryptKey;
import com.x.server.console.action.ActionDumpData;
import com.x.server.console.action.ActionDumpStorage;
import com.x.server.console.action.ActionEraseContentBbs;
import com.x.server.console.action.ActionEraseContentCms;
import com.x.server.console.action.ActionEraseContentLog;
import com.x.server.console.action.ActionEraseContentProcessPlatform;
import com.x.server.console.action.ActionRestoreData;
import com.x.server.console.action.ActionRestoreStorage;
import com.x.server.console.action.ActionSetPassword;
import com.x.server.console.action.ActionShowCpu;
import com.x.server.console.action.ActionShowMemory;
import com.x.server.console.action.ActionShowOs;
import com.x.server.console.action.ActionShowThread;
import com.x.server.console.action.ActionUpdate;
R
roo00 已提交
54
import com.x.server.console.action.ActionUpdateFile;
caixiangyi's avatar
caixiangyi 已提交
55 56 57 58 59 60 61
import com.x.server.console.action.ActionVersion;
import com.x.server.console.log.LogTools;
import com.x.server.console.server.Servers;

public class Main {

	private static final String MANIFEST_FILENAME = "manifest.cfg";
liyi_hz2008's avatar
liyi_hz2008 已提交
62
	private static final String GITIGNORE_FILENAME = ".gitignore";
caixiangyi's avatar
caixiangyi 已提交
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139

	public static void main(String[] args) throws Exception {
		String base = getBasePath();
		scanWar(base);
		loadJars(base);
		/* getVersion需要FileUtils在后面运行 */
		cleanTempDir();
		createTempClassesDirectory();
		SystemOutErrorSideCopyBuilder.start();
		if (null == Config.currentNode()) {
			throw new Exception("无法找到当前节点,请检查config/node_{name}.json与local/node.cfg文件内容中的名称是否一致.");
		}
		LogTools.setSlf4jSimple();
		CommandFactory.printStartHelp();
		try (PipedInputStream pipedInput = new PipedInputStream();
				PipedOutputStream pipedOutput = new PipedOutputStream(pipedInput)) {
			new Thread() {
				/* 文件中的命令输出到解析器 */
				public void run() {
					try (RandomAccessFile raf = new RandomAccessFile(Config.base() + "/command.swap", "rw")) {
						FileChannel fc = raf.getChannel();
						MappedByteBuffer mbb = fc.map(MapMode.READ_WRITE, 0, 256);
						byte[] fillBytes = new byte[256];
						byte[] readBytes = new byte[256];
						Arrays.fill(fillBytes, (byte) 0);
						mbb.put(fillBytes);
						FileLock flock = null;
						String cmd = "";
						while (true) {
							flock = fc.lock();
							mbb.position(0);
							mbb.get(readBytes, 0, 256);
							mbb.position(0);
							mbb.put(fillBytes);
							flock.release();
							if (!Arrays.equals(readBytes, fillBytes)) {
								cmd = StringUtils.trim(new String(readBytes, DefaultCharset.charset));
								System.out.println("read command:" + cmd);
								pipedOutput.write((cmd + StringUtils.LF).getBytes(DefaultCharset.name));
							}
							Thread.sleep(4000);
						}
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			}.start();
			new Thread() {
				/* 将屏幕命令输出到解析器 */
				public void run() {
					try (BufferedReader reader = new BufferedReader(new InputStreamReader(System.in))) {
						String cmd = "";
						while (null != cmd) {
							cmd = reader.readLine();
							/** 在linux环境中当前端console窗口关闭后会导致可以立即read到一个null的input值 */
							if (null != cmd) {
								cmd = cmd + StringUtils.LF;
								pipedOutput.write(cmd.getBytes(DefaultCharset.name));
								pipedOutput.flush();
							}
							Thread.sleep(1000);
						}
						System.out.println("console input closed!");
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			}.start();
			/* 启动NodeAgent */
			if (BooleanUtils.isTrue(Config.currentNode().nodeAgentEnable())) {
				NodeAgent nodeAgent = new NodeAgent();
				nodeAgent.start();
			}

			SchedulerBuilder schedulerBuilder = new SchedulerBuilder();
			Scheduler scheduler = schedulerBuilder.start();

R
roo00 已提交
140 141 142 143
			if (Config.currentNode().autoStart()) {
				startAll();
			}

caixiangyi's avatar
caixiangyi 已提交
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
			Matcher matcher = null;
			try (BufferedReader reader = new BufferedReader(new InputStreamReader(pipedInput))) {
				String cmd = "";
				while (true) {
					try {
						cmd = reader.readLine();
					} catch (Exception e) {
						continue;
					}
					if (StringUtils.isBlank(cmd)) {
						continue;
					}

					matcher = CommandFactory.test_pattern.matcher(cmd);
					if (matcher.find()) {
						test();
						continue;
					}

					matcher = CommandFactory.show_os_pattern.matcher(cmd);
					if (matcher.find()) {
						showOs(matcher.group(1), matcher.group(2));
						continue;
					}

					matcher = CommandFactory.show_cpu_pattern.matcher(cmd);
					if (matcher.find()) {
						showCpu(matcher.group(1), matcher.group(2));
						continue;
					}

					matcher = CommandFactory.show_memory_pattern.matcher(cmd);
					if (matcher.find()) {
						showMemory(matcher.group(1), matcher.group(2));
						continue;
					}

					matcher = CommandFactory.show_thread_pattern.matcher(cmd);
					if (matcher.find()) {
						showThread(matcher.group(1), matcher.group(2));
						continue;
					}

					matcher = CommandFactory.start_pattern.matcher(cmd);
					if (matcher.find()) {
						switch (matcher.group(1)) {
						case "application":
							startApplicationServer();
							break;
						case "center":
							startCenterServer();
							break;
						case "web":
							startWebServer();
							break;
						case "storage":
							startStorageServer();
							break;
						case "data":
							startDataServer();
							break;
						default:
							startAll();
							break;
						}
						continue;
					}
					matcher = CommandFactory.stop_pattern.matcher(cmd);
					if (matcher.find()) {
						switch (matcher.group(1)) {
						case "application":
							stopApplicationServer();
							break;
						case "center":
							stopCenterServer();
							break;
						case "web":
							stopWebServer();
							break;
						case "storage":
							stopStorageServer();
							break;
						case "data":
							stopDataServer();
							break;
						default:
							stopAll();
							break;
						}
						continue;
					}
					matcher = CommandFactory.dump_pattern.matcher(cmd);
					if (matcher.find()) {
						switch (matcher.group(1)) {
						case "data":
							dumpData(matcher.group(2));
							break;
						case "storage":
							dumpStorage(matcher.group(2));
							break;
						default:
							break;
						}
						continue;
					}
					matcher = CommandFactory.restore_pattern.matcher(cmd);
					if (matcher.find()) {
						switch (matcher.group(1)) {
						case "data":
							resotreData(matcher.group(2), matcher.group(3));
							break;
						case "storage":
							resotreStorage(matcher.group(2), matcher.group(3));
							break;
						default:
							break;
						}
						continue;
					}

					matcher = CommandFactory.help_pattern.matcher(cmd);
					if (matcher.find()) {
						CommandFactory.printHelp();
						continue;
					}

					matcher = CommandFactory.version_pattern.matcher(cmd);
					if (matcher.find()) {
						version();
						continue;
					}

					matcher = CommandFactory.update_pattern.matcher(cmd);
					if (matcher.find()) {
R
roo00 已提交
278
						if (update(matcher.group(1), matcher.group(2), matcher.group(3))) {
caixiangyi's avatar
caixiangyi 已提交
279 280 281 282 283 284 285
							stopAll();
							System.exit(0);
						} else {
							continue;
						}
					}

R
roo00 已提交
286
					matcher = CommandFactory.updateFile_pattern.matcher(cmd);
caixiangyi's avatar
caixiangyi 已提交
287
					if (matcher.find()) {
R
roo00 已提交
288
						if (updateFile(matcher.group(1), matcher.group(2), matcher.group(3))) {
caixiangyi's avatar
caixiangyi 已提交
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420
							stopAll();
							System.exit(0);
						} else {
							continue;
						}
					}

					matcher = CommandFactory.setPassword_pattern.matcher(cmd);
					if (matcher.find()) {
						setPassword(matcher.group(1), matcher.group(2));
						if (config()) {
							break;
						} else {
							continue;
						}
					}

					matcher = CommandFactory.erase_content_pattern.matcher(cmd);
					if (matcher.find()) {
						switch (matcher.group(1)) {
						case "pp":
							eraseContentProcessPlatform(matcher.group(2));
							break;
						case "cms":
							eraseContentCms(matcher.group(2));
							break;
						case "log":
							eraseContentLog(matcher.group(2));
							break;
						case "bbs":
							eraseContentBbs(matcher.group(2));
							break;

						default:
							break;
						}
						continue;
					}

					matcher = CommandFactory.compact_data_pattern.matcher(cmd);
					if (matcher.find()) {
						compactData(matcher.group(1));
						continue;
					}

					// matcher = CommandFactory.convert_dataItem_pattern.matcher(cmd);
					// if (matcher.find()) {
					// convertDataItem(matcher.group(1));
					// continue;
					// }

					matcher = CommandFactory.create_encrypt_key_pattern.matcher(cmd);
					if (matcher.find()) {
						createEncryptKey(matcher.group(1));
						continue;
					}

					matcher = CommandFactory.exit_pattern.matcher(cmd);
					if (matcher.find()) {
						exit();
					}

					System.out.println("unknown command:" + cmd);
				}
			}
			/* 关闭定时器 */
			scheduler.shutdown();
			// scheduler.shutdown();
		}
		SystemOutErrorSideCopyBuilder.stop();
	}

	private static boolean test() {
		try {
			DeploymentManager deployer = Servers.applicationServer.getBean(DeploymentManager.class);
			for (App app : deployer.getApps()) {
				System.out.println(app.getContextPath());
				if (StringUtils.equals("/x_query_assemble_designer", app.getContextPath())) {
					app.getContextHandler().stop();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return true;
	}

	private static boolean showOs(String interval, String repeat) {
		try {
			return new ActionShowOs().execute(Integer.parseInt(interval, 10), Integer.parseInt(repeat, 10));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return true;
	}

	private static boolean showCpu(String interval, String repeat) {
		try {
			return new ActionShowCpu().execute(Integer.parseInt(interval, 10), Integer.parseInt(repeat, 10));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return true;
	}

	private static boolean showMemory(String interval, String repeat) {
		try {
			return new ActionShowMemory().execute(Integer.parseInt(interval, 10), Integer.parseInt(repeat, 10));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return true;
	}

	private static boolean showThread(String interval, String repeat) {
		try {
			return new ActionShowThread().execute(Integer.parseInt(interval, 10), Integer.parseInt(repeat, 10));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return true;
	}

	private static boolean createEncryptKey(String password) {
		try {
			return new ActionCreateEncryptKey().execute(password);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return true;
	}

R
roo00 已提交
421
	private static boolean update(String password, String backup, String latest) {
caixiangyi's avatar
caixiangyi 已提交
422
		try {
R
roo00 已提交
423 424
			return new ActionUpdate().execute(password, BooleanUtils.toBoolean(backup),
					BooleanUtils.toBoolean(latest));
caixiangyi's avatar
caixiangyi 已提交
425 426 427 428 429 430
		} catch (Exception e) {
			e.printStackTrace();
		}
		return true;
	}

R
roo00 已提交
431
	private static boolean updateFile(String path, String backup, String password) {
caixiangyi's avatar
caixiangyi 已提交
432
		try {
R
roo00 已提交
433
			return new ActionUpdateFile().execute(path, BooleanUtils.toBoolean(backup), password);
caixiangyi's avatar
caixiangyi 已提交
434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705
		} catch (Exception e) {
			e.printStackTrace();
		}
		return true;
	}

	private static void version() {
		try {
			new ActionVersion().execute();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private static boolean config() {
		try {
			return new ActionConfig().execute();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return true;
	}

	private static void startDataServer() {
		try {
			if (Servers.dataServerIsRunning()) {
				System.out.println("data server is running.");
			} else {
				Servers.startDataServer();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private static void stopDataServer() {
		try {
			if (!Servers.dataServerIsRunning()) {
				System.out.println("data server is not running.");
			} else {
				Servers.stopDataServer();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private static void startStorageServer() {
		try {
			if (Servers.storageServerIsRunning()) {
				System.out.println("storage server is running.");
			} else {
				Servers.startStorageServer();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private static void stopStorageServer() {
		try {
			if (!Servers.storageServerIsRunning()) {
				System.out.println("storage server is not running.");
			} else {
				Servers.stopStorageServer();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private static void startApplicationServer() {
		try {
			if (Servers.applicationServerIsRunning()) {
				System.out.println("application server is running.");
			} else {
				Servers.startApplicationServer();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private static void stopApplicationServer() {
		try {
			if (!Servers.applicationServerIsRunning()) {
				System.out.println("application server is not running.");
			} else {
				Servers.stopApplicationServer();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private static void startCenterServer() {
		try {
			if (Servers.centerServerIsRunning()) {
				System.out.println("center server is running.");
			} else {
				Servers.startCenterServer();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private static void stopCenterServer() {
		try {
			if (!Servers.centerServerIsRunning()) {
				System.out.println("center server is not running.");
			} else {
				Servers.stopCenterServer();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private static void startWebServer() {
		try {
			if (Servers.webServerIsRunning()) {
				System.out.println("web server is running.");
			} else {
				Servers.startWebServer();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private static void stopWebServer() {
		try {
			if (!Servers.webServerIsRunning()) {
				System.out.println("web server is not running.");
			} else {
				Servers.stopWebServer();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private static void startAll() {
		try {
			DataServer dataServer = Config.currentNode().getData();
			if (null != dataServer) {
				if (BooleanUtils.isTrue(dataServer.getEnable())) {
					startDataServer();
				}
			}
			StorageServer storageServer = Config.currentNode().getStorage();
			if (null != storageServer) {
				if (BooleanUtils.isTrue(storageServer.getEnable())) {
					startStorageServer();
				}
			}
			if (Config.currentNode().getIsPrimaryCenter()) {
				startCenterServer();
			}
			ApplicationServer applicationServer = Config.currentNode().getApplication();
			if (null != applicationServer) {
				if (BooleanUtils.isTrue(applicationServer.getEnable())) {
					startApplicationServer();
				}
			}
			WebServer webServer = Config.currentNode().getWeb();
			if (null != webServer) {
				if (BooleanUtils.isTrue(webServer.getEnable())) {
					startWebServer();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private static void exit() {
		stopAll();
		System.exit(0);
	}

	private static void stopAll() {
		try {
			WebServer webServer = Config.currentNode().getWeb();
			if (null != webServer) {
				if (BooleanUtils.isTrue(webServer.getEnable())) {
					stopWebServer();
				}
			}
			ApplicationServer applicationServer = Config.currentNode().getApplication();
			if (null != applicationServer) {
				if (BooleanUtils.isTrue(applicationServer.getEnable())) {
					stopApplicationServer();
				}
			}
			if (Config.currentNode().getIsPrimaryCenter()) {
				stopCenterServer();
			}
			StorageServer storageServer = Config.currentNode().getStorage();
			if (null != storageServer) {
				if (BooleanUtils.isTrue(storageServer.getEnable())) {
					stopStorageServer();
				}
			}
			DataServer dataServer = Config.currentNode().getData();
			if (null != dataServer) {
				if (BooleanUtils.isTrue(dataServer.getEnable())) {
					stopDataServer();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private static void dumpData(String password) {
		try {
			(new ActionDumpData()).execute(password);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private static void dumpStorage(String password) {
		try {
			(new ActionDumpStorage()).execute(password);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private static void resotreData(String dateString, String password) {
		try {
			SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
			Date date = format.parse(dateString);
			File file = new File(Config.base(), "local/dump/dumpData_" + format.format(date));
			if (file.exists() && file.isDirectory()) {
				(new ActionRestoreData()).execute(date, password);
			} else {
				System.out.println("directory " + file.getAbsolutePath() + " not existed.");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private static void resotreStorage(String dateString, String password) {
		try {
			SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
			Date date = format.parse(dateString);
			File file = new File(Config.base(), "local/dump/dumpStorage_" + format.format(date));
			if (file.exists() && file.isDirectory()) {
				ActionRestoreStorage restoreStorage = new ActionRestoreStorage();
				restoreStorage.execute(date, password);
			} else {
				System.out.println("directory " + file.getAbsolutePath() + " not existed.");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private static void createTempClassesDirectory() throws Exception {
		File tempDir = new File(Config.base(), "local/temp/classes");
		FileUtils.forceMkdir(tempDir);
		FileUtils.cleanDirectory(tempDir);
	}

	/**
	 * 检查store目录下的war文件是否全部在manifest.cfg中
	 * 
liyi_hz2008's avatar
liyi_hz2008 已提交
706
	 * @param base o2server的根目录
caixiangyi's avatar
caixiangyi 已提交
707 708 709 710 711 712 713 714 715 716 717 718 719 720 721
	 */
	private static void scanWar(String base) throws Exception {
		File dir = new File(base, "store");
		File manifest = new File(dir, MANIFEST_FILENAME);
		if ((!manifest.exists()) || manifest.isDirectory()) {
			throw new Exception("can not find " + MANIFEST_FILENAME + " in store.");
		}
		List<String> manifestNames = readManifest(manifest);
		for (File o : dir.listFiles()) {
			if (o.isDirectory() && o.getName().equals("jars")) {
				continue;
			}
			if (o.getName().equals(MANIFEST_FILENAME)) {
				continue;
			}
liyi_hz2008's avatar
liyi_hz2008 已提交
722 723 724
			if (o.getName().equals(GITIGNORE_FILENAME)) {
				continue;
			}
caixiangyi's avatar
caixiangyi 已提交
725
			if (!manifestNames.contains(o.getName())) {
liyi_hz2008's avatar
liyi_hz2008 已提交
726
				System.out.println("扫描 store 过程中删除无效的文件:" + o.getName());
caixiangyi's avatar
caixiangyi 已提交
727 728 729 730 731 732 733 734 735 736 737
				o.delete();
			}
		}
	}

	private static void loadJars(String base) throws Exception {
		URLClassLoader urlClassLoader = (URLClassLoader) ClassLoader.getSystemClassLoader();
		Class<?> urlClass = URLClassLoader.class;
		Method method = urlClass.getDeclaredMethod("addURL", new Class[] { URL.class });
		method.setAccessible(true);
		/* loading ext */
R
roo00 已提交
738 739 740
		File commons_ext_dir = new File(base, "commons/ext");
		File commons_ext_manifest_file = new File(commons_ext_dir, MANIFEST_FILENAME);
		if (!commons_ext_manifest_file.exists()) {
caixiangyi's avatar
caixiangyi 已提交
741 742
			throw new Exception("can not find " + MANIFEST_FILENAME + " in commons/ext.");
		}
R
roo00 已提交
743 744
		List<String> commons_ext_manifest_names = readManifest(commons_ext_manifest_file);
		if (commons_ext_manifest_names.isEmpty()) {
caixiangyi's avatar
caixiangyi 已提交
745 746
			throw new Exception("commons/ext manifest is empty.");
		}
R
roo00 已提交
747
		for (File file : commons_ext_dir.listFiles()) {
liyi_hz2008's avatar
liyi_hz2008 已提交
748
			if ((!file.getName().equals(MANIFEST_FILENAME)) && (!file.getName().equals(GITIGNORE_FILENAME))) {
R
roo00 已提交
749
				if (!commons_ext_manifest_names.remove(file.getName())) {
R
roo00 已提交
750
					System.out.println("载入 commons/ext 过程中删除无效的文件:" + file.getName());
caixiangyi's avatar
caixiangyi 已提交
751 752 753 754 755 756
					file.delete();
				} else {
					method.invoke(urlClassLoader, new Object[] { file.toURI().toURL() });
				}
			}
		}
R
roo00 已提交
757 758
		for (String str : commons_ext_manifest_names) {
			System.out.println("载入 commons/ext 过程中无法找到文件:" + str);
R
roo00 已提交
759
		}
caixiangyi's avatar
caixiangyi 已提交
760
		/* loading jars */
R
roo00 已提交
761 762 763
		File store_jars_dir = new File(base, "store/jars");
		File store_jars_manifest_file = new File(store_jars_dir, MANIFEST_FILENAME);
		if (!store_jars_manifest_file.exists()) {
caixiangyi's avatar
caixiangyi 已提交
764 765
			throw new Exception("can not find " + MANIFEST_FILENAME + " in store/jars.");
		}
R
roo00 已提交
766 767
		List<String> store_jars_manifest_names = readManifest(store_jars_manifest_file);
		for (File file : store_jars_dir.listFiles()) {
liyi_hz2008's avatar
liyi_hz2008 已提交
768
			if ((!file.getName().equals(MANIFEST_FILENAME)) && (!file.getName().equals(GITIGNORE_FILENAME))) {
R
roo00 已提交
769
				if (!store_jars_manifest_names.remove(file.getName())) {
R
roo00 已提交
770
					System.out.println("载入 store/jars 过程中删除无效的文件:" + file.getName());
caixiangyi's avatar
caixiangyi 已提交
771 772 773 774 775 776
					file.delete();
				} else {
					method.invoke(urlClassLoader, new Object[] { file.toURI().toURL() });
				}
			}
		}
R
roo00 已提交
777 778 779 780 781 782
		for (String str : store_jars_manifest_names) {
			System.out.println("载入 store/jars 过程中无法找到文件:" + str);
		}
		/* load custom jar */
		File custom_jars_dir = new File(base, "custom/jars");
		if (custom_jars_dir.exists() && custom_jars_dir.isDirectory()) {
R
roo00 已提交
783
			for (File file : custom_jars_dir.listFiles()) {
R
roo00 已提交
784
				method.invoke(urlClassLoader, new Object[] { file.toURI().toURL() });
R
roo00 已提交
785 786
			}
		}
R
roo00 已提交
787 788 789 790 791 792 793
		File dynamic_jars_dir = new File(base, "dynamic/jars");
		if (dynamic_jars_dir.exists() && dynamic_jars_dir.isDirectory()) {
			for (File file : dynamic_jars_dir.listFiles()) {
				method.invoke(urlClassLoader, new Object[] { file.toURI().toURL() });
			}
		}

R
roo00 已提交
794 795
		/* load temp class */
		method.invoke(urlClassLoader, new Object[] { Config.dir_local_temp_classes().toURI().toURL() });
caixiangyi's avatar
caixiangyi 已提交
796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886
	}

	private static String getBasePath() throws Exception {
		String path = Main.class.getProtectionDomain().getCodeSource().getLocation().getPath();
		File file = new File(path);
		if (!file.isDirectory()) {
			file = file.getParentFile();
		}
		while (null != file) {
			File versionFile = new File(file, "version.o2");
			if (versionFile.exists()) {
				return file.getAbsolutePath();
			}
			file = file.getParentFile();
		}
		throw new Exception("can not define o2server base directory.");
	}

	private static void cleanTempDir() throws Exception {
		File file = new File(Config.base(), "local/temp");
		FileUtils.forceMkdir(file);
		FileUtils.cleanDirectory(file);
	}

	private static List<String> readManifest(File file) throws Exception {
		List<String> list = new ArrayList<>();
		try (FileReader fileReader = new FileReader(file);
				BufferedReader bufferedReader = new BufferedReader(fileReader)) {
			String line;
			while ((line = bufferedReader.readLine()) != null) {
				list.add(line);
			}
		}
		return list;
	}

	private static boolean setPassword(String oldPassword, String newPassword) throws Exception {
		try {
			return new ActionSetPassword().execute(oldPassword, newPassword);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	private static boolean compactData(String password) throws Exception {
		try {
			return new ActionCompactData().execute(password);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	private static boolean eraseContentProcessPlatform(String password) throws Exception {
		try {
			return new ActionEraseContentProcessPlatform().execute(password);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	private static boolean eraseContentCms(String password) throws Exception {
		try {
			return new ActionEraseContentCms().execute(password);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	private static boolean eraseContentBbs(String password) throws Exception {
		try {
			return new ActionEraseContentBbs().execute(password);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	private static boolean eraseContentLog(String password) throws Exception {
		try {
			return new ActionEraseContentLog().execute(password);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

}