From d44c6e64556e1fc3b52a0c76f41a3d008d1d6f7f Mon Sep 17 00:00:00 2001 From: Abdelrahman Elsalh Date: Fri, 17 May 2024 22:35:50 +0300 Subject: [PATCH] use java assist to read bytecode --- .../com/workup/controller/CLIHandler.java | 39 ++++++------------- 1 file changed, 11 insertions(+), 28 deletions(-) diff --git a/controller/src/main/java/com/workup/controller/CLIHandler.java b/controller/src/main/java/com/workup/controller/CLIHandler.java index 2f213520..9c6478d4 100644 --- a/controller/src/main/java/com/workup/controller/CLIHandler.java +++ b/controller/src/main/java/com/workup/controller/CLIHandler.java @@ -3,15 +3,13 @@ import asg.cliche.CLIException; import asg.cliche.Command; import com.workup.shared.commands.controller.*; -import com.workup.shared.commands.jobs.requests.CreateJobRequest; +import com.workup.shared.commands.payments.wallet.requests.CreateWalletRequest; import com.workup.shared.enums.ControllerQueueNames; import com.workup.shared.enums.ServiceQueueNames; -import java.io.IOException; -import java.io.InputStream; import java.util.HashMap; import java.util.Map; -import java.util.jar.JarEntry; -import java.util.jar.JarFile; +import javassist.ClassPool; +import javassist.CtClass; import org.apache.logging.log4j.Level; import org.springframework.amqp.core.AmqpTemplate; import org.springframework.beans.factory.annotation.Autowired; @@ -100,8 +98,9 @@ public String setLoggingLevel(String app, String level) { @Command(description = "test") public void test() { - rabbitTemplate.convertSendAndReceive( - ServiceQueueNames.JOBS, CreateJobRequest.builder().withTitle("Ziko").build()); + CreateWalletRequest request = + CreateWalletRequest.builder().withFreelancerId("123").withUserId("123").build(); + rabbitTemplate.convertSendAndReceive(ServiceQueueNames.PAYMENTS, request); } @Command(description = "Creates a new command") @@ -116,28 +115,12 @@ public String updateCommand(String app, String commandName, String className) th return "Error: app can only be jobs, users, contracts or payments!"; } String jarFilePath = app + ".jar"; - String fullClassName = - "BOOT-INF/classes/com/workup/" + app + "/commands/" + className + ".class"; - - byte[] classBytes = null; - try (JarFile jarFile = new JarFile(jarFilePath)) { - JarEntry entry = jarFile.getJarEntry(fullClassName); - if (entry != null) { - try (InputStream inputStream = jarFile.getInputStream(entry)) { - classBytes = inputStream.readAllBytes(); - System.out.println("Class file extracted successfully."); - } - } else { - System.out.println("Class file not found in the JAR." + fullClassName); - } - } catch (IOException e) { - e.printStackTrace(); - } - - if (classBytes == null) { - return "Error: Class file not found in the JAR."; - } + String fullClassName = "BOOT-INF.classes.com.workup." + app + ".commands." + className; + ClassPool pool = ClassPool.getDefault(); + pool.insertClassPath(jarFilePath); + CtClass cc = pool.get(fullClassName); + byte[] classBytes = cc.toBytecode(); int x = 0; rabbitTemplate.convertAndSend( appQueueMap.get(app),