Skip to content

Commit

Permalink
UPDATEE
Browse files Browse the repository at this point in the history
  • Loading branch information
ziadsadek999 committed May 17, 2024
1 parent d44c6e6 commit 27e066b
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 47 deletions.
52 changes: 41 additions & 11 deletions controller/src/main/java/com/workup/controller/CLIHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
import com.workup.shared.commands.payments.wallet.requests.CreateWalletRequest;
import com.workup.shared.enums.ControllerQueueNames;
import com.workup.shared.enums.ServiceQueueNames;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;
import javassist.ClassClassPath;
import javassist.ClassPool;
import javassist.CtClass;
import org.apache.logging.log4j.Level;
Expand Down Expand Up @@ -114,19 +116,47 @@ 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!";
}
String jarFilePath = app + ".jar";
String fullClassName = "BOOT-INF.classes.com.workup." + app + ".commands." + className;
updateCommand(commandName, className);
return "Command sent!!";
}

public void updateCommand(String commandName, String className)
throws InstantiationException, IllegalAccessException {
ClassPool pool = ClassPool.getDefault();
pool.insertClassPath(jarFilePath);
CtClass cc = pool.get(fullClassName);
byte[] classBytes = cc.toBytecode();
int x = 0;
rabbitTemplate.convertAndSend(
appQueueMap.get(app),
"",
UpdateCommandRequest.builder().withName(commandName).withByteCode(classBytes).build());
return "Command sent!!";
// this line defines the root class used to query classes. so when
// we access a command to edit it, we write its name relative to the Main class,
// that's why the format is commands.CommandName
pool.insertClassPath(new ClassClassPath(ControllerApplication.class));
try {
CtClass ctClass = pool.get(className);
// That's the compiled class byte code
byte[] classBytes = ctClass.toBytecode();
// I encoded the byte code to a string to be able
// to send it in a json and send it over the network,
// not sure if it could be done better.
String byteString = java.util.Base64.getEncoder().encodeToString(classBytes);
// decoding the string back to byte array
byte[] byteArray = Base64.getDecoder().decode(byteString);
MyClassLoader loader = new MyClassLoader();
// This method loads a class on the fly given its byte code.
// Usually it is called when an object is created as it reads
// the byte code from the memory to load the class,
// create an instance of it then run it.
// But here we give it the byte code directly instead of reading it from memory.
Class<com.workup.shared.commands.Command> newCommand =
(Class<com.workup.shared.commands.Command>) loader.loadClass(byteArray, className);
com.workup.shared.commands.Command command = newCommand.newInstance();
command.Run(null);
System.out.println("Command updated!");
} catch (Exception ex) {
ex.printStackTrace();
}
}

static class MyClassLoader extends ClassLoader {
public Class<?> loadClass(byte[] byteCode, String className) {
return defineClass(className, byteCode, 0, byteCode.length);
}
}

@Command(description = "Deletes an existing command")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
package com.workup.jobs.commands;

import com.workup.jobs.models.Job;
import com.workup.shared.commands.jobs.requests.CreateJobRequest;
import com.workup.shared.commands.jobs.responses.CreateJobResponse;
import com.workup.shared.enums.HttpStatusCode;
import java.util.Date;
import java.util.UUID;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

Expand All @@ -14,37 +10,40 @@ public class CreateJobCommand extends JobCommand<CreateJobRequest, CreateJobResp

@Override
public CreateJobResponse Run(CreateJobRequest request) {
logger.info("Information");
logger.error("Error");
System.out.println("THIS IS AN UPDATEDDDDD VERSIONNNNNNNNNNNN!");
try {
Job job =
Job.builder()
.withId(UUID.randomUUID())
.withTitle(request.getTitle())
.withDescription(request.getDescription())
.withLocation(request.getLocation())
.withBudget(request.getBudget())
.withClientId(request.getUserId())
.withSkills(request.getSkills())
.withIsActive(true)
.withExperienceLevel(request.getExperience())
.withCreatedAt(new Date())
.withUpdatedAt(new Date())
.build();
Job savedJob = jobRepository.save(job);
System.out.println(" [x] Saved Job '" + savedJob.getTitle());
return CreateJobResponse.builder()
.withStatusCode(HttpStatusCode.CREATED)
.withJobId(savedJob.getId().toString())
.build();
} catch (Exception e) {
e.printStackTrace();
return CreateJobResponse.builder()
.withStatusCode(HttpStatusCode.INTERNAL_SERVER_ERROR)
.withErrorMessage("An error occurred while saving job")
.withJobId(null)
.build();
}

System.out.println("ABDOOOOOO!");
return null;
// logger.info("Information");
// logger.error("Error");
// System.out.println("THIS IS AN UPDATEDDDDD VERSIONNNNNNNNNNNN!");
// try {
// Job job =
// Job.builder()
// .withId(UUID.randomUUID())
// .withTitle(request.getTitle())
// .withDescription(request.getDescription())
// .withLocation(request.getLocation())
// .withBudget(request.getBudget())
// .withClientId(request.getUserId())
// .withSkills(request.getSkills())
// .withIsActive(true)
// .withExperienceLevel(request.getExperience())
// .withCreatedAt(new Date())
// .withUpdatedAt(new Date())
// .build();
// Job savedJob = jobRepository.save(job);
// System.out.println(" [x] Saved Job '" + savedJob.getTitle());
// return CreateJobResponse.builder()
// .withStatusCode(HttpStatusCode.CREATED)
// .withJobId(savedJob.getId().toString())
// .build();
// } catch (Exception e) {
// e.printStackTrace();
// return CreateJobResponse.builder()
// .withStatusCode(HttpStatusCode.INTERNAL_SERVER_ERROR)
// .withErrorMessage("An error occurred while saving job")
// .withJobId(null)
// .build();
// }
}
}

0 comments on commit 27e066b

Please sign in to comment.