Skip to content

Commit

Permalink
send byte code
Browse files Browse the repository at this point in the history
  • Loading branch information
abd0123 committed May 16, 2024
1 parent 8705b5d commit 36e78ec
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 6 deletions.
2 changes: 2 additions & 0 deletions controller/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
FROM eclipse-temurin:21-jre-alpine
VOLUME /tmp
ARG JAR_FILE=target/*.jar
ARG JOBS_JAR_FILE=target/dependency/jobs-0.0.1-SNAPSHOT.jar
COPY ${JAR_FILE} controller.jar
COPY ${JOBS_JAR_FILE} jobs.jar
ENTRYPOINT ["tail", "-f", "/dev/null"]
17 changes: 17 additions & 0 deletions controller/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,23 @@

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.2.0</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<!-- Configure the plugin as needed -->
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
Expand Down
33 changes: 27 additions & 6 deletions controller/src/main/java/com/workup/controller/CLIHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@

import asg.cliche.CLIException;
import asg.cliche.Command;
import com.workup.jobs.JobsApplication;
import com.workup.shared.commands.controller.*;
import com.workup.shared.commands.jobs.requests.CreateJobRequest;
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 javassist.*;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import org.apache.logging.log4j.Level;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -102,10 +104,29 @@ public String updateCommand(String app, String commandName, String className) th
if (!appQueueMap.containsKey(app)) {
return "Error: app can only be jobs, users, contracts or payments!";
}
ClassPool pool = ClassPool.getDefault();
pool.insertClassPath(new ClassClassPath(JobsApplication.class));
CtClass ctClass = pool.get(className);
byte[] classBytes = ctClass.toBytecode();
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.";
}

int x = 0;
rabbitTemplate.convertAndSend(
appQueueMap.get(app),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ public void receive(ContinueRequest in) throws Exception {
public void receive(UpdateCommandRequest in) throws Exception {
try {
String className = commandMap.getCommand(in.getName()).getClass().getName();
System.out.println("Updating command: " + in.getName());
System.out.println("Class: " + className);
Class newClass = new MyClassLoader().loadClass(in.getByteCode(), className);
commandMap.replaceCommand(in.getName(), newClass);
} catch (Exception e) {
Expand All @@ -99,6 +101,7 @@ private void setThreads(int threads) throws NoSuchFieldException, IllegalAccessE

class MyClassLoader extends ClassLoader {
public Class<?> loadClass(byte[] byteCode, String className) {
System.out.println("Loading class: " + className);
return defineClass(className, byteCode, 0, byteCode.length);
}
}

0 comments on commit 36e78ec

Please sign in to comment.