Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@
<packaging>war</packaging>

<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
Expand Down Expand Up @@ -43,11 +50,11 @@
<artifactId>jsf-impl</artifactId>
<version>2.2.12</version>
</dependency>
<!--<dependency>-->
<!--<groupId>javax.servlet</groupId>-->
<!--<artifactId>jstl</artifactId>-->
<!--<version>1.2</version>-->
<!--</dependency>-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>


<dependency>
Expand Down
38 changes: 0 additions & 38 deletions src/main/java/pp/ua/fame/DAO/TaskDaoImp.java

This file was deleted.

36 changes: 36 additions & 0 deletions src/main/java/pp/ua/fame/config/SpringConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package pp.ua.fame.config;

import jdk.nashorn.api.scripting.NashornScriptEngineFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;

import javax.persistence.EntityManager;
import javax.persistence.Persistence;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;

@Configuration
@ComponentScan(basePackages = "pp.ua.fame")
public class SpringConfig {
@Bean
public EntityManager entityManager(){
return Persistence.createEntityManagerFactory("TesterDB").createEntityManager();
}

public ScriptEngine nashorn(){
return new NashornScriptEngineFactory().getScriptEngine("--no-java");
}

public ScriptEngine rhino(){
return new ScriptEngineManager().getEngineByName("rhino");
}

@Bean
@Scope("prototype")
public ScriptEngine scriptEngine(){
return nashorn();
}

}
19 changes: 16 additions & 3 deletions src/main/java/pp/ua/fame/controllers/SpringController.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,28 @@
package pp.ua.fame.controllers;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import pp.ua.fame.dao.impl.TaskDaoImp;
import pp.ua.fame.model.Task;

import javax.servlet.http.HttpServletRequest;

@Controller
public class SpringController {

@Autowired
private TaskDaoImp taskDao;

@RequestMapping(value = "/main/{id}", method = RequestMethod.GET)
public String Task(ModelMap model, @PathVariable("id") Integer id){
return "/main.xhtml?id="+id;
public String Task(@PathVariable("id") Integer id, HttpServletRequest req){
Task task = taskDao.getTask(id);
if (task == null) {
return ("404page.xhtml");
}
req.setAttribute("task", task);
return "/main.xhtml";
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package pp.ua.fame.DAO;
package pp.ua.fame.dao;

import pp.ua.fame.persistence.Task;
import pp.ua.fame.model.Task;

public interface TaskDao {
Task getTask(long id);
Expand Down
40 changes: 40 additions & 0 deletions src/main/java/pp/ua/fame/dao/impl/TaskDaoImp.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package pp.ua.fame.dao.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import pp.ua.fame.dao.TaskDao;
import pp.ua.fame.model.Task;
import sun.reflect.generics.reflectiveObjects.NotImplementedException;

import javax.persistence.EntityManager;

@Component()
public class TaskDaoImp implements TaskDao {
@Autowired
private EntityManager entityManager;

@Override
public Task getTask(long id) {
return entityManager.find(Task.class, id);
}

@Override
public void addTask(String description, String source) {
throw new NotImplementedException();
}

@Override
public void deleteTask(long id) {
throw new NotImplementedException();
}

@Override
public void addTest(String test, String answer) {
throw new NotImplementedException();
}

@Override
public void deleteTest(long id) {
throw new NotImplementedException();
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package pp.ua.fame.exceptions;
package pp.ua.fame.exception;

public class TesterException extends Exception {
public TesterException() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package pp.ua.fame.exceptions;
package pp.ua.fame.exception;

public class TimeoutException extends TesterException {
public TimeoutException() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package pp.ua.fame.exceptions;
package pp.ua.fame.exception;

public class TypeMismatchException extends TesterException{
public TypeMismatchException() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,49 +1,44 @@
package pp.ua.fame.JSRuner;
package pp.ua.fame.jsRuner;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import pp.ua.fame.exceptions.TimeoutException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import pp.ua.fame.exception.TimeoutException;

import javax.faces.context.FacesContext;
import javax.script.ScriptEngine;
import javax.script.ScriptException;
import java.io.ByteArrayOutputStream;
import java.io.OutputStreamWriter;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why enum in one file with class?


enum Status{CREATE, RUN, ERROR, FINISH, TIMEOUT}

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CaMeL cAsE

public class JS {
@Component()
@Scope("prototype")
public class Js {

private final int TIMEOUT = 5000;

private Status status;
private static final int TIMEOUT = 5000;

@Autowired
private ScriptEngine engine;

private Status status = Status.CREATE;

private String source;

private ScriptException ex;

Result result;
private Result result;

private static ApplicationContext appContext;

public JS(String source) {
if (appContext == null) {
FacesContext ctx = FacesContext.getCurrentInstance();
String configLocation =
ctx.getExternalContext().getInitParameter("contextConfigLocation");
appContext = new ClassPathXmlApplicationContext(configLocation);
}
engine = (ScriptEngine)appContext.getBean("nashorn");
// engine.getContext().setWriter(new OutputStreamWriter(System.err));
public Js(String source) {
this.source = source;
status = Status.CREATE;
}

public Result eval() throws ScriptException, TimeoutException {
status = Status.RUN;
Thread calculatingThread = new Thread(() ->{
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not needed line

try {
result = new Result(engine.eval(source));
ByteArrayOutputStream myConsole = new ByteArrayOutputStream();
engine.getContext().setWriter(new OutputStreamWriter(myConsole));
result = new Result(engine.eval(source), new String(myConsole.toByteArray()));
status = Status.FINISH;
} catch (ScriptException e) {
ex = e;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,28 @@
package pp.ua.fame.JSRuner;
package pp.ua.fame.jsRuner;

import jdk.nashorn.api.scripting.ScriptObjectMirror;
import pp.ua.fame.exceptions.TypeMismatchException;
import pp.ua.fame.exception.TypeMismatchException;

import java.util.Map;

public class Result {
private Object result;

private String console;

public Result(Object result) {
this.result = result;
}

public Result(Object result, String console) {
this.result = result;
this.console = console;
}

public String getConsole() {
return console;
}

public boolean isNull(){
return result == null;
}
Expand Down Expand Up @@ -53,36 +64,41 @@ public boolean isFunction(){
public ScriptObjectMirror getScriptObject() throws TypeMismatchException {
if (isScriptObject()){
return (ScriptObjectMirror) result;
} else
} else {
throw new TypeMismatchException("Result is not ScriptObject");
}
}

public Double getNumber() throws TypeMismatchException {
if (isNumber()){
return (Double.valueOf(result.toString()));
} else
} else {
throw new TypeMismatchException("Result is not Number");
}
}

public String getString() throws TypeMismatchException {
if (isString()){
return (String) result;
} else
} else {
throw new TypeMismatchException("Result is not String");
}
}

public Boolean getBoolean() throws TypeMismatchException {
if (isBoolean()){
return (Boolean) result;
} else
} else {
throw new TypeMismatchException("Result is not Boolean");
}
}

public Map getArray() throws TypeMismatchException {
if (isArray()){
return (Map)result;
} else
} else {
throw new TypeMismatchException("Result is not Array");
}
}

public Object getObject(){
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/pp/ua/fame/jsRuner/Status.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package pp.ua.fame.jsRuner;

public enum Status{
CREATE, RUN, ERROR, FINISH, TIMEOUT
}
Loading