Skip to content

Commit

Permalink
adding service class
Browse files Browse the repository at this point in the history
  • Loading branch information
sh0derun committed Feb 13, 2020
1 parent 0aa4090 commit ab7cd68
Show file tree
Hide file tree
Showing 10 changed files with 140 additions and 36 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
*.py
/target

Empty file added file.txt
Empty file.
25 changes: 25 additions & 0 deletions src/main/java/com/oracle/challenge/common/Enums.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.oracle.challenge.common;

public class Enums {
public enum InterpreterIdentifier{
PYTHON("python");
private String value;

InterpreterIdentifier(String value) {
this.value = value;
}

public String toString() {
return String.valueOf(value);
}

public static InterpreterIdentifier fromValue(String text) {
for (InterpreterIdentifier b : InterpreterIdentifier.values()) {
if (String.valueOf(b.value).equals(text)) {
return b;
}
}
return null;
}
}
}
7 changes: 7 additions & 0 deletions src/main/java/com/oracle/challenge/common/Interpreter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.oracle.challenge.common;

public interface Interpreter {

public void interpret(String code);

}
9 changes: 9 additions & 0 deletions src/main/java/com/oracle/challenge/common/Python.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.oracle.challenge.common;

public class Python implements Interpreter{

public void interpret(String code) {
System.out.println(code);
}

}
85 changes: 85 additions & 0 deletions src/main/java/com/oracle/challenge/service/NoteBookService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package com.oracle.challenge.service;

import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;

import com.oracle.challenge.common.Enums;
import com.oracle.challenge.common.Interpreter;
import com.oracle.challenge.common.Python;
import com.oracle.challenge.model.NoteBookInput;
import com.oracle.challenge.model.NoteBookOutput;

import antlr.PythonCodeGenerator;

@Service
public class NoteBookService {

@Autowired
ApplicationContext context;

private static Map<Enums.InterpreterIdentifier, Class<?>> interpretersMap = null;

static {
interpretersMap = new HashMap<>();
interpretersMap.put(Enums.InterpreterIdentifier.PYTHON, Python.class);
}

public NoteBookOutput HandelNoteBookInput(NoteBookInput noteBookInput) {
NoteBookOutput noteBookOutput = new NoteBookOutput();
try {
String[] splitedInput = noteBookInput.getCode().split(" ", 2);
String interpreter = splitedInput[0].substring(1);
String code = splitedInput[1];

System.out.println(interpreter);
System.out.println(code);
/*
interpretersMap.get(Enums.InterpreterIdentifier.fromValue(interpreter));
*/
//script that will hold all inputs, and gets fed with new input when ever the endpoint is called
BufferedWriter out = new BufferedWriter(new FileWriter("script.py", true));
out.append("\n"+code);
out.close();

Process process = null;

//script execution
if(System.getProperty("os.name").toLowerCase().indexOf("windows") >= 0) {
process = Runtime.getRuntime().exec(String.format("cmd.exe /c python script.py"));
}
else {
//other os
}

//preparing result of script execution to be binded in the output
ByteArrayOutputStream result = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int length;
while ((length = process.getInputStream().read(buffer)) != -1) {
result.write(buffer, 0, length);
}

noteBookOutput.setResult(result.toString().replaceAll("(\\r|\\n)", "|"));
} catch (IOException e) {
e.printStackTrace();
System.out.println(e.getMessage());
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

return noteBookOutput;
}

}
38 changes: 7 additions & 31 deletions src/main/java/com/oracle/challenge/ws/NoteBookWs.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
import java.io.ByteArrayOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
Expand All @@ -17,47 +17,23 @@

import com.oracle.challenge.model.NoteBookInput;
import com.oracle.challenge.model.NoteBookOutput;
import com.oracle.challenge.service.NoteBookService;

@Controller
public class NoteBookWs {

@Autowired
private NoteBookService noteBookService;

@RequestMapping(value = "/execute",
produces = { "application/json;charset=utf-8" },
consumes = { "application/json;charset=utf-8" },
method = RequestMethod.POST)
ResponseEntity<NoteBookOutput> executeScript(@Valid @RequestBody NoteBookInput noteBookInput) {
NoteBookOutput noteBookOutput = new NoteBookOutput();
try {
//script that will hold all inputs, and gets fed with new input when ever the endpoint is called
BufferedWriter out = new BufferedWriter(new FileWriter("script.py"));
out.write("print "+noteBookInput.getCode());
out.close();

Process process = null;

//script execution
if(System.getProperty("os.name").toLowerCase().indexOf("windows") >= 0) {
process = Runtime.getRuntime().exec(String.format("cmd.exe /c python script.py"));
}
else {
//other os
}

//preparing result of script execution to be binded in the output
ByteArrayOutputStream result = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int length;
while ((length = process.getInputStream().read(buffer)) != -1) {
result.write(buffer, 0, length);
}

noteBookOutput.setResult(result.toString().replaceAll("(\\r|\\n)", ""));
} catch (IOException e) {
e.printStackTrace();
System.out.println(e.getMessage());
}

NoteBookOutput noteBookOutput = this.noteBookService.HandelNoteBookInput(noteBookInput);
return new ResponseEntity<NoteBookOutput>(noteBookOutput, HttpStatus.CREATED);

}

}
2 changes: 1 addition & 1 deletion target/classes/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Manifest-Version: 1.0
Build-Jdk-Spec: 11
Implementation-Title: BackendNoteBookChallenge
Implementation-Version: 0.0.1-SNAPSHOT
Build-Jdk-Spec: 1.8
Created-By: Maven Integration for Eclipse

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#Generated by Maven Integration for Eclipse
#Wed Feb 12 23:16:14 CET 2020
m2e.projectLocation=D\:\\shadrunWorks\\oracleChallenge\\BackendNoteBookChallenge
m2e.projectName=BackendNoteBookChallenge
#Thu Feb 13 08:31:09 WET 2020
version=0.0.1-SNAPSHOT
groupId=com.oracle
m2e.projectName=BackendNoteBookChallenge
m2e.projectLocation=C\:\\Users\\arazik\\Downloads\\AnassGithub\\BackendNoteBookChallenge
artifactId=BackendNoteBookChallenge
version=0.0.1-SNAPSHOT
Binary file modified target/classes/com/oracle/challenge/ws/NoteBookWs.class
Binary file not shown.

0 comments on commit ab7cd68

Please sign in to comment.