Skip to content

Commit

Permalink
use java assist to read bytecode
Browse files Browse the repository at this point in the history
  • Loading branch information
abd0123 committed May 17, 2024
1 parent 7ef74b4 commit d44c6e6
Showing 1 changed file with 11 additions and 28 deletions.
39 changes: 11 additions & 28 deletions controller/src/main/java/com/workup/controller/CLIHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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")
Expand All @@ -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),
Expand Down

0 comments on commit d44c6e6

Please sign in to comment.