Просмотр исходного кода

新版应用市场应用安装优化

o2sword 5 лет назад
Родитель
Сommit
fa2ed7fb64

+ 52 - 60
o2server/x_console/src/main/java/com/x/server/console/NodeAgent.java

@@ -152,7 +152,7 @@ public class NodeAgent extends Thread {
 							logger.info("文件名path:" + tempFile.getAbsolutePath() + File.separator + filename);
 							File file = new File(tempFile.getAbsolutePath() + File.separator + filename);
 
-							filename = filename.substring(0, filename.lastIndexOf("."));
+							filename = filename.substring(0, filename.indexOf("."));
 							// uninstall
 							boolean result = this.customWarUninstall(filename);
 
@@ -162,41 +162,25 @@ public class NodeAgent extends Thread {
 
 						matcher = redeploy_pattern.matcher(commandObject.getCommand());
 						if (matcher.find()) {
-							String strCommand = commandObject.getCommand();
-							strCommand = strCommand.trim();
-							strCommand = strCommand.substring(strCommand.indexOf(":") + 1, strCommand.length());
+							String strCommand = commandObject.getCommand().trim();
+							strCommand = StringUtils.substringAfter(strCommand, ":");
 							logger.info("收接到命令:" + strCommand);
 							String filename = dis.readUTF();
-							File tempFile = null;
-							switch (strCommand) {
-							case "storeWar":
-								tempFile = Config.dir_store();
-								break;
-							case "storeJar":
-								tempFile = Config.dir_store_jars();
-								break;
-							case "customWar":
-								tempFile = Config.dir_custom();
-								break;
-							case "customJar":
-								tempFile = Config.dir_custom_jars();
-								break;
-							}
-							FileTools.forceMkdir(tempFile);
-							logger.info("文件名path:" + tempFile.getAbsolutePath() + File.separator + filename);
-							File file = new File(tempFile.getAbsolutePath() + File.separator + filename);
-							fos = new FileOutputStream(file);
-							byte[] bytes = new byte[1024];
-							int length = 0;
-							while ((length = dis.read(bytes, 0, bytes.length)) != -1) {
-								fos.write(bytes, 0, length);
-								fos.flush();
+
+							byte[] bytes;
+							try (ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
+								byte[] onceBytes = new byte[1024];
+								int length = 0;
+								while ((length = dis.read(onceBytes, 0, onceBytes.length)) != -1) {
+									bos.write(onceBytes, 0, length);
+									bos.flush();
+								}
+								bytes = bos.toByteArray();
 							}
-							fos.close();
-							bytes = FileUtils.readFileToByteArray(file);
-							filename = filename.substring(0, filename.lastIndexOf("."));
+
+							filename = filename.substring(0, filename.indexOf("."));
 							// 部署
-							String result = this.redeploy(filename, bytes, true);
+							String result = this.redeploy(strCommand, filename, bytes, true);
 							logger.info("部署:" + result);
 							continue;
 
@@ -405,11 +389,11 @@ public class NodeAgent extends Thread {
 		return result;
 	}
 
-	private String redeploy(String name, byte[] bytes, boolean rebootApp) {
+	private String redeploy(String type, String name, byte[] bytes, boolean rebootApp) {
 		String result = "success";
 		try {
 			logger.print("redeploy:{}.", name);
-			switch (this.type(name)) {
+			switch (type) {
 			case "storeWar":
 				storeWar(name, bytes);
 				break;
@@ -422,6 +406,9 @@ public class NodeAgent extends Thread {
 			case "customJar":
 				customJar(name, bytes, rebootApp);
 				break;
+			case "customZip":
+				customZip(name, bytes, rebootApp);
+				break;
 			}
 		} catch (Exception e) {
 			e.printStackTrace();
@@ -634,35 +621,40 @@ public class NodeAgent extends Thread {
 	private void customJar(String simpleName, byte[] bytes, boolean rebootApp) throws Exception {
 		File jar = new File(Config.dir_custom_jars(true), simpleName + ".jar");
 		FileUtils.writeByteArrayToFile(jar, bytes, false);
-		List<String> contexts = new ArrayList<>();
-		boolean isStartApplication = false;
-		for (String s : Config.dir_custom().list(new WildcardFileFilter("*.war"))) {
-			contexts.add("/" + FilenameUtils.getBaseName(s));
-		}
-		if (Servers.applicationServerIsRunning()) {
-			GzipHandler gzipHandler = (GzipHandler) Servers.applicationServer.getHandler();
-			HandlerList hanlderList = (HandlerList) gzipHandler.getHandler();
-			for (Handler handler : hanlderList.getHandlers()) {
-				if (QuickStartWebApp.class.isAssignableFrom(handler.getClass())) {
-					QuickStartWebApp app = (QuickStartWebApp) handler;
-					if (contexts.contains(app.getContextPath())) {
-						app.stop();
-						logger.print("{} need restart because {} redeployed.", app.getDisplayName(), simpleName);
-						Thread.sleep(3000);
-						app.start();
-						isStartApplication = true;
-					}
-				}
-			}
+		/*if (rebootApp) {
+			Servers.stopApplicationServer();
+			Thread.sleep(3000);
+			Servers.startApplicationServer();
+		}*/
+	}
 
-			if (rebootApp) {
-				if (!isStartApplication) {
-					Servers.stopApplicationServer();
-					Thread.sleep(1000);
-					Servers.startApplicationServer();
+	private void customZip(String simpleName, byte[] bytes, boolean rebootApp) throws Exception {
+		logger.print("start deploy customZip app {} ", simpleName);
+		File tempFile = new File(Config.base(), "local/temp/redeploy");
+		FileTools.forceMkdir(tempFile);
+		FileUtils.cleanDirectory(tempFile);
+
+		File zipFile = new File(tempFile.getAbsolutePath(), simpleName+".zip");
+		FileUtils.writeByteArrayToFile(zipFile, bytes);
+		File dist = Config.dir_custom(true);
+		List<String> subs = new ArrayList<>();
+		JarTools.unjar(zipFile, subs, dist, false);
+
+		FileUtils.cleanDirectory(tempFile);
+
+		/*if (rebootApp) {
+			Servers.stopApplicationServer();
+			int i = 0;
+			while (i++<6){
+				try {
+					if(Servers.applicationServerIsRunning()){
+						Thread.sleep(2000);
+					}
+				} catch (Exception e) {
 				}
 			}
-		}
+			Servers.startApplicationServer();
+		}*/
 	}
 
 	private List<ClassInfo> listModuleDependencyWith(String name) throws Exception {

+ 7 - 10
o2server/x_program_center/src/main/java/com/x/program/center/jaxrs/market/ActionInstallOrUpdate.java

@@ -126,9 +126,12 @@ class ActionInstallOrUpdate extends BaseAction {
 					WrapModule module = this.convertToWrapIn(jsonElement, WrapModule.class);
 					this.installModule(module);
 					installData.setWrapModule(module);
-
-				}
-				if(file.getName().toLowerCase().endsWith(".zip")){
+				}else if(file.getName().toLowerCase().endsWith(".app.zip")){
+					logger.print("开始安装自定义应用:{}", file.getName());
+					this.installCustomApp(file.getName(), FileUtils.readFileToByteArray(file));
+					installData.setCustomApp(file.getName());
+					logger.print("完成自定义应用安装:{}", file.getName());
+				}else if(file.getName().toLowerCase().endsWith(".zip")){
 					logger.print("开始安装静态资源");
 					try {
 						Business.dispatch(false, file.getName(), "", FileUtils.readFileToByteArray(file));
@@ -137,12 +140,6 @@ class ActionInstallOrUpdate extends BaseAction {
 						logger.print("模块安装成功但静态资源安装失败:{}",e.getMessage());
 					}
 				}
-				if(file.getName().toLowerCase().endsWith(".war")){
-					logger.print("开始安装自定义应用:{}", file.getName());
-					this.installCustomApp(file.getName(), FileUtils.readFileToByteArray(file));
-					installData.setCustomApp(file.getName());
-					logger.print("完成自定义应用安装:{}", file.getName());
-				}
 			}
 		}
 		FileUtils.cleanDirectory(tempFile);
@@ -245,7 +242,7 @@ class ActionInstallOrUpdate extends BaseAction {
 					try (DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
 						 DataInputStream dis = new DataInputStream(socket.getInputStream())) {
 						Map<String, Object> commandObject = new HashMap<>();
-						commandObject.put("command", "redeploy:customWar");
+						commandObject.put("command", "redeploy:customZip");
 						commandObject.put("credential", Crypto.rsaEncrypt("o2@", Config.publicKey()));
 
 						dos.writeUTF(XGsonBuilder.toJson(commandObject));