Skip to content

Commit

Permalink
remove /script(/jruby) in request-location
Browse files Browse the repository at this point in the history
restarting will only occur after post to /restart/{endpoint}
always remove Gemfile.lock to enforce recompile git-resources
  • Loading branch information
tbaum committed Jul 24, 2011
1 parent 087d597 commit 97a8de2
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 106 deletions.
5 changes: 3 additions & 2 deletions example/backend/deploy.sh
@@ -1,4 +1,5 @@
#!/bin/bash
server=${1-'localhost:7474'}
curl -XPOST --data-binary @config.ru http://$server/script/jruby/config
curl -XPOST --data-binary @Gemfile http://$server/script/jruby/gemfile
curl -XPOST --data-binary @config.ru http://$server/script/config
curl -XPOST --data-binary @Gemfile http://$server/script/gemfile
curl -XPOST http://$server/script/restart
6 changes: 3 additions & 3 deletions example/simple/deploy.sh
@@ -1,5 +1,5 @@
#!/bin/bash
server=${1-'localhost:7474'}
curl -XDELETE @Gemfile http://$server/script/jruby/gemfile
curl -XPUT --data-binary @Gemfile http://$server/script/jruby/gemfile
curl -XPOST --data-binary @config.ru http://$server/script/jruby/config
curl -XPOST --data-binary @config.ru http://$server/script/config
curl -XPOST --data-binary @Gemfile http://$server/script/gemfile
curl -XPOST http://$server/script/restart
6 changes: 3 additions & 3 deletions lib/neo4j_server.rb
Expand Up @@ -16,21 +16,21 @@ def server_url
def upload_gemfile(file_location)
file = File.read(file_location)
res = Net::HTTP.start(server_url.host, server_url.port) do |http|
http.post('/script/jruby/gemfile', file)
http.post('/script/gemfile', file)
end
[res.code, res.body]
end

def delete_gemfile
res = Net::HTTP.start(server_url.host, server_url.port) do |http|
http.delete('/script/jruby/gemfile')
http.delete('/script/gemfile')
end
res.code
end

def eval(s)
res = Net::HTTP.start(server_url.host, server_url.port) do |http|
http.post('/script/jruby/eval', s)
http.post('/script/eval', s)
end
[res.code, res.body]
end
Expand Down
138 changes: 53 additions & 85 deletions src/main/java/org/neo4j/server/extension/script/JRubyResource.java
Expand Up @@ -20,6 +20,7 @@
package org.neo4j.server.extension.script;

import org.jruby.Ruby;
import org.jruby.internal.runtime.GlobalVariables;
import org.jruby.runtime.builtin.IRubyObject;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.server.extension.script.resources.ServerResource;
Expand All @@ -38,9 +39,8 @@
import static javax.ws.rs.core.Response.Status.OK;
import static org.jruby.javasupport.JavaUtil.convertJavaToRuby;

@Path("/jruby")
@Path("/")
public class JRubyResource {

private static final Logger LOG = new Logger(JRubyResource.class);
private final GraphDatabaseService database;

Expand All @@ -49,7 +49,8 @@ public JRubyResource(@Context GraphDatabaseService database) {
}

@POST @Consumes("application/x-www-form-urlencoded") @Path("/call")
public Response call(@Context EmbeddedRackApplicationFactory f, MultivaluedMap<String, String> formParams) {
public Response call(@Context EmbeddedRackApplicationFactory applicationFactory,
MultivaluedMap<String, String> formParams) {
LOG.info("Call2");
try {
for (String key : formParams.keySet()) {
Expand All @@ -59,19 +60,20 @@ public Response call(@Context EmbeddedRackApplicationFactory f, MultivaluedMap<S
String rubymethod = formParams.getFirst("method");
String nbrArgs = formParams.getFirst("args");
LOG.info("Call class: '" + rubyclass + "' method: '" + rubymethod + "' #args: " + nbrArgs);
final Ruby container = f.getRuntime();

// container.put("$NEO4J_SERVER", database); // looks like the initialization is not always run ???
Ruby container = applicationFactory.getRuntime();

int size = Integer.parseInt(nbrArgs);

StringBuilder script = new StringBuilder();
script.append(rubyclass).append(".send(:").append(rubymethod);
GlobalVariables globalVariables = container.getGlobalVariables();

for (int i = 0; i < size; i++) {
script.append(",");
String argName = "arg" + i;
LOG.info("Set '" + argName + "' = '" + formParams.get(argName) + "'");
container.getGlobalVariables().set(argName, convertJavaToRuby(container, formParams.getFirst(argName)));
String value = formParams.getFirst(argName);
LOG.info("Set '" + argName + "' = '" + value + "'");
globalVariables.set(argName, convertJavaToRuby(container, value));
script.append(argName);
}
script.append(")");
Expand All @@ -84,120 +86,86 @@ public Response call(@Context EmbeddedRackApplicationFactory f, MultivaluedMap<S
}
}

@PUT @Produces(TEXT_PLAIN) @Path("/config")
public Response createConfigFile(@Context JRubyRackContextMap cont, String txt) throws IOException {
final String singleEndpoint = cont.getSingleEndpointName();
return createConfigFile(cont, singleEndpoint, txt);
}

@POST @Produces(TEXT_PLAIN) @Path("/config")
public Response createConfigFileAndRestart(@Context JRubyRackContextMap cont, String txt) throws IOException {
final String singleEndpoint = cont.getSingleEndpointName();
return createConfigFileAndRestart(cont, singleEndpoint, txt);
public Response createConfigFile(@Context JRubyRackContextMap contextMap,
String data) throws IOException {
String endpoint = contextMap.getSingleEndpointName();
return createConfigFile(contextMap, endpoint, data);
}

@POST @Produces(TEXT_PLAIN) @Path("/config/{endpoint}")
public Response createConfigFileAndRestart(@Context JRubyRackContextMap cont, @PathParam("endpoint") String endpoint,
String txt) throws IOException {
public Response createConfigFile(@Context JRubyRackContextMap contextMap,
@PathParam("endpoint") String endpoint,
String data) throws IOException {
LOG.info("Create new config for " + endpoint);
Date start = new Date();

if (!endpoint.startsWith("/")) {
endpoint = "/" + endpoint;
}

JRubyRackContext container = cont.getEndpoint(endpoint);
createConfigFile(cont, endpoint, txt);
container.restart();

LOG.info("Initialized");
return Response.status(OK).entity(container.getLog(start)).build();
}

@PUT @Produces(TEXT_PLAIN) @Path("/config/{endpoint}")
public Response createConfigFile(@Context JRubyRackContextMap cont, @PathParam("endpoint") String endpoint,
String txt) throws IOException {
LOG.info("Create new config for " + endpoint);

if (!endpoint.startsWith("/")) {
endpoint = "/" + endpoint;
}

JRubyRackContext container = cont.getEndpoint(endpoint);
ServerResource res = container.getConfigRu();
res.store(txt, database);

JRubyRackContext container = contextMap.getEndpoint(endpoint);
container.getConfigRu().store(data, database);
return Response.status(OK).build();
}

@POST @Produces(TEXT_PLAIN) @Path("/gemfile")
public Response createGemFileAndRestart(@Context JRubyRackContextMap container, @Context EmbeddedRackApplicationFactory f,
String txt) throws IOException {
Date start = new Date();
createGemFile(f, txt);
container.restartAll();

return Response.status(OK).entity(container.getLogAll(start)).build();
}

@PUT @Produces(TEXT_PLAIN) @Path("/gemfile")
public Response createGemFile(@Context EmbeddedRackApplicationFactory f, String txt) throws IOException {
public Response createGemFile(@Context EmbeddedRackApplicationFactory applicationFactory,
String data) throws IOException {
LOG.info("Create new Gemfile");
ServerResource gemFile = f.getGemFile();
gemFile.store(txt, database);

applicationFactory.getGemFile().store(data, database);
return Response.status(OK).build();
}

@DELETE @Path("/config")
public Response deleteConfigFile(@Context JRubyRackContextMap cont) throws IOException {
final String singleEndpoint = cont.getSingleEndpointName();
return deleteConfigFile(cont, singleEndpoint);
public Response deleteConfigFile(@Context JRubyRackContextMap contextMap) throws IOException {
String singleEndpoint = contextMap.getSingleEndpointName();
return deleteConfigFile(contextMap, singleEndpoint);
}

@DELETE @Path("/config/{endpoint}")
public Response deleteConfigFile(@Context JRubyRackContextMap cont, @PathParam("endpoint") String endpoint) throws IOException {
Date start = new Date();
JRubyRackContext container = cont.getEndpoint("/" + endpoint);

ServerResource config = container.getConfigRu();
config.delete(database);
container.restart();
return Response.status(OK).entity(container.getLog(start)).build();
public Response deleteConfigFile(@Context JRubyRackContextMap contextMap,
@PathParam("endpoint") String endpoint) throws IOException {
JRubyRackContext container = contextMap.getEndpoint("/" + endpoint);
container.getConfigRu().delete(database);
return Response.status(OK).build();
}

@DELETE @Path("/gemfile")
public Response deleteGemFile(@Context JRubyRackContextMap container, @Context EmbeddedRackApplicationFactory f,
String txt) throws IOException {
Date start = new Date();
ServerResource gemFile = f.getGemFile();
gemFile.delete(database);
container.restartAll();
return Response.status(OK).entity(container.getLogAll(start)).build();
public Response deleteGemFile(@Context EmbeddedRackApplicationFactory applicationFactory) throws IOException {
applicationFactory.getGemFile().delete(database);
return Response.status(OK).build();
}

@POST @Produces(TEXT_PLAIN) @Path("/eval")
public Response eval(@Context EmbeddedRackApplicationFactory f, String script) throws IOException {
public Response eval(@Context EmbeddedRackApplicationFactory applicationFactory, String script) {
LOG.info("Eval: '" + script + "'");
// container.put("$NEO4J_SERVER", database);
final Ruby container = f.getRuntime();
Ruby container = applicationFactory.getRuntime();
IRubyObject result = container.evalScriptlet(script);
return Response.status(OK).entity((result.toString()).getBytes()).build();
}

@GET @Produces({TEXT_PLAIN, APPLICATION_JSON}) @Path("/log")
public Response log(@Context JRubyRackContextMap cont) {
final String singleEndpoint = cont.getSingleEndpointName();
JRubyRackContext endpoint = cont.getEndpoint(singleEndpoint);

return Response.status(OK).entity(endpoint.getLog(null)).build();
public Response log(@Context JRubyRackContextMap contextMap) {
return Response.status(OK).entity(contextMap.getLogAll(null)).build();
}

@GET @Produces({TEXT_PLAIN, APPLICATION_JSON}) @Path("/log/{since}")
public Response log(@Context JRubyRackContextMap cont, @PathParam("since") long since) {
final String singleEndpoint = cont.getSingleEndpointName();
JRubyRackContext endpoint = cont.getEndpoint(singleEndpoint);
public Response log(@Context JRubyRackContextMap contextMap, @PathParam("since") long since) {
return Response.status(OK).entity(contextMap.getLogAll(new Date(since))).build();
}

return Response.status(OK).entity(endpoint.getLog(new Date(since))).build();
@POST @Path("/restart")
public Response restart(@Context JRubyRackContextMap contextMap) {
Date start = new Date();
contextMap.restartAll();
return Response.status(OK).entity(contextMap.getLogAll(start)).build();
}

@POST @Path("/restart/{endpoint}")
public Response restart(@Context JRubyRackContextMap contextMap, @PathParam("endpoint") String endpoint) {
Date start = new Date();
JRubyRackContext container = contextMap.getEndpoint("/" + endpoint);
container.restart();
return Response.status(OK).entity(container.getLog(start)).build();
}
}
Expand Up @@ -42,9 +42,7 @@ public GemfileServerResource(File file, String property) {

@Override public boolean updateFileSystem(final GraphDatabaseService gds) throws IOException {
final boolean changed = super.updateFileSystem(gds);
if (changed) {
new File(getFile().getCanonicalPath() + ".lock").delete();
}
new File(getFile().getCanonicalPath() + ".lock").delete();
return changed;
}
}
Expand Up @@ -37,7 +37,7 @@
public class JRubyExtensionServerTest {
private static final String HOSTNAME = "localhost";
private static final int PORT = 7473;
public static final String URI = "http://" + HOSTNAME + ":" + PORT + "/script/jruby/";
public static final String URI = "http://" + HOSTNAME + ":" + PORT + "/script/";
private static LocalTestServer server = new LocalTestServer(HOSTNAME,PORT).withPropertiesFile("test-db.properties");
private static RestRequest request;

Expand Down
Expand Up @@ -61,24 +61,24 @@ public static void stopServer() {
public void cleanup() {
final GraphDatabaseService gds = server.getGraphDatabase();
final Transaction tx = gds.beginTx();
gds.getReferenceNode().removeProperty("script/jruby/gemfile");
gds.getReferenceNode().removeProperty("script/gemfile");
tx.success();
tx.finish();
server.cleanDb();
}

@Test
public void testEval() throws Exception {
final ClientResponse resp1 = request.put("script/jruby/gemfile", "" +
final ClientResponse resp1 = request.post("script/gemfile", "" +
"source :gemcutter\n" +
"gem 'sinatra'\n");
final String result1 = resp1.getEntity(String.class);
System.out.println("= script/jruby/gemfile ================================================");
System.out.println("= script/gemfile ================================================");
System.out.println(result1);
resp1.close();


final ClientResponse resp2 = request.post("script/jruby/config", "" +
final ClientResponse resp2 = request.post("script/config", "" +
"require 'sinatra'\n" +
"class App < Sinatra::Base\n" +
" get '/' do\n" +
Expand All @@ -93,10 +93,17 @@ public void testEval() throws Exception {
"run App");

String result2 = resp2.getEntity(String.class);
System.out.println("= script/jruby/config ================================================");
System.out.println("= script/config ================================================");
System.out.println(result2);
resp2.close();

final ClientResponse resp2a = request.post("script/restart", "");

String result2a = resp2a.getEntity(String.class);
System.out.println("= script/restart ================================================");
System.out.println(result2a);
resp2a.close();


Date now = new Date();

Expand All @@ -106,9 +113,9 @@ public void testEval() throws Exception {
System.out.println(result3);
response3.close();

final ClientResponse response4 = request.get("script/jruby/log/" + now.getTime());
final ClientResponse response4 = request.get("script/log/" + now.getTime());
String result4 = response4.getEntity(String.class);
System.out.println("= script/jruby/log/" + now.getTime() + " ================================================");
System.out.println("= script/log/" + now.getTime() + " ================================================");
System.out.println(result4);
response4.close();

Expand All @@ -121,9 +128,9 @@ public void testEval() throws Exception {
response5.close();


final ClientResponse response6 = request.get("script/jruby/log/" + now.getTime() );
final ClientResponse response6 = request.get("script/log/" + now.getTime());
String result6 = response6.getEntity(String.class);
System.out.println("= script/jruby/log/" + now.getTime() + " ================================================");
System.out.println("= script/jruby/log/" + now.getTime() + " ================================================");
System.out.println(result6);
response6.close();
}
Expand Down

0 comments on commit 97a8de2

Please sign in to comment.