Skip to content

Commit

Permalink
Script engine : ScriptExecutorServiceImpl WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
alexd6631 committed Mar 27, 2015
1 parent 591a577 commit cc19c03
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 13 deletions.
55 changes: 47 additions & 8 deletions src/net/algem/script/execution/ScriptExecutorServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,41 @@
import net.algem.script.common.Script;
import net.algem.script.execution.models.ScriptUserArguments;
import net.algem.script.execution.models.ScriptResult;
import net.algem.util.DataConnection;

import javax.script.Bindings;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ScriptExecutorServiceImpl implements ScriptExecutorService {
private final DataConnection dataConnection;

public ScriptExecutorServiceImpl(DataConnection dataConnection) {
this.dataConnection = dataConnection;
}

@Override
public ScriptResult executeScript(Script script, ScriptUserArguments arguments) throws Exception {
public ScriptResult executeScript(final Script script, final ScriptUserArguments arguments) throws Exception {
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("js");
final ScriptEngine engine = manager.getEngineByName("js");

Bindings bindings = engine.createBindings();
OutInterface out = new OutInterface();
bindings.put("out", out);
bindings.put("args", arguments.getArguments());
return dataConnection.withTransaction(new DataConnection.SQLRunnable<ScriptResult>() {
@Override
public ScriptResult run(DataConnection conn) throws Exception {
Bindings bindings = engine.createBindings();
OutInterface out = new OutInterface();
bindings.put("out", out);
bindings.put("args", arguments.getArguments());
bindings.put("dc", conn);

engine.eval(script.getCode(), bindings);
return out.getResult();
engine.eval(script.getCode(), bindings);
return out.getResult();
}
});
}


Expand All @@ -45,5 +60,29 @@ public void line(List<Object> line) {
ScriptResult getResult() {
return new ScriptResult(_header, rows);
}

public void resultSet(ResultSet rs) throws Exception {
int n = rs.getMetaData().getColumnCount();
_header = new ArrayList<>();
for (int i=0; i<n; i++) {
_header.add(rs.getMetaData().getColumnName(i + 1));
}
rows = new ArrayList<>();
while (rs.next()) {
List<Object> row = new ArrayList<>();
for (int i = 0; i < n; i++) {
row.add(rs.getObject(i + 1));
}
rows.add(row);
}
}

@Override
public String toString() {
return "OutInterface{" +
"_header=" + _header +
", rows=" + rows +
'}';
}
}
}
4 changes: 2 additions & 2 deletions test/net/algem/TestProperties.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ public class TestProperties
/** User with profile 4. */
public final static String ADMIN = System.getProperty("user.name");
/** User with profile 1. */
public final static String USER = "lm"; // a modifier suivant le contexte
public final static String USER = "nobody"; // a modifier suivant le contexte
/** Pass. */
public final static String PASS = "Pigfy!"; // a modifier suivant le contexte
public final static String PASS = "nobody"; // a modifier suivant le contexte
/** JDBC driver. */
public final static String DRIVER = "org.postgresql.Driver";

Expand Down
27 changes: 26 additions & 1 deletion test/net/algem/script/execution/ScriptExecutorServiceTest.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package net.algem.script.execution;

import junit.framework.TestCase;
import net.algem.TestProperties;
import net.algem.script.common.Script;
import net.algem.script.common.ScriptArgument;
import net.algem.script.execution.models.ScriptResult;
import net.algem.script.execution.models.ScriptUserArguments;
import net.algem.util.DataConnection;
import net.algem.util.DataConnectionTest;
import net.algem.util.IOUtil;

import java.io.File;
Expand All @@ -14,11 +17,22 @@

public class ScriptExecutorServiceTest extends TestCase {
private ScriptExecutorServiceImpl executorService;
private DataConnectionTest.SimpleService simpleService;

@Override
protected void setUp() throws Exception {
super.setUp();
executorService = new ScriptExecutorServiceImpl();
DataConnection dc = TestProperties.getDataConnection();
executorService = new ScriptExecutorServiceImpl(dc);

simpleService = new DataConnectionTest.SimpleService(dc);
simpleService.initTables();
simpleService.op1();
simpleService.op2();
}

public void tearDown() throws Exception {
simpleService.dropTables();
}

public void testHelloWorld() throws Exception {
Expand All @@ -45,4 +59,15 @@ public void testWithArgs() throws Exception {

assertEquals(expected, scriptResult);
}

public void testWithDataConnection() throws Exception {
String code = IOUtil.readFile(new File("testData/scriptsSample/withdc.js"));
Script script = new Script("hello world", new ArrayList<ScriptArgument>(), "", code);
ScriptResult scriptResult = executorService.executeScript(script, new ScriptUserArguments(new HashMap<String, Object>()));
ScriptResult expected = new ScriptResult(Arrays.asList("value"),
Arrays.asList(Arrays.<Object>asList(1), Arrays.<Object>asList(2)));

assertEquals(expected, scriptResult);

}
}
4 changes: 2 additions & 2 deletions test/net/algem/util/DataConnectionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,10 @@ public Void run(DataConnection conn) throws Exception {
}


static class SimpleService {
public static class SimpleService {
final DataConnection dc;

SimpleService(DataConnection dc) {
public SimpleService(DataConnection dc) {
this.dc = dc;
}

Expand Down
3 changes: 3 additions & 0 deletions testData/scriptsSample/withdc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
var rs = dc.executeQuery("select * from test");

out.resultSet(rs);

0 comments on commit cc19c03

Please sign in to comment.