Reputation: 35276
I have this rather "crazy" idea to have a "stored procedure" facility in my application. Basically, my app is data-centric and that it can access the datastore through some form of Restful interface.
I want to have the functionality to manipulate data (which is a JSON
string) in many ways, like
My initial implementation was to create a Java class that will do these operations. Typical.
However, I want to have the flexibility in doing such manipulations. That is, I can add a procedure to manipulate data on-the-fly, that is, like a
script
that can be selected and will process the retrieved data and then save it back
again in the datastore.
For example:
http://127.0.0.1:8888/resources?key=somekey&operation=funkyops
This will cause the server to internally fetch the Entity with such key then manipulate the data and save the Entity back to the datastore.
Retrieving and persisting logic can be hardcoded but the "stored procedure" must be stored in the datastore as a script or something like that and then when selected will be passed into some sort of processing block.
Here's a concrete example of what I am trying to do: HashBend.java
Any ideas on how I can achieve this?
Upvotes: 1
Views: 164
Reputation: 2821
Your best bet would be to simply use a scripting language and call the interpreter through Java.
If you insist on using only Java then there may be a way....
Create an interface that all your scripts conform to which specifies a method of manipulating the data. Create your specific class, serialize it and then include that in the json (probably base 64 encoded).
When you get the json, pull out the base 64 data, convert back to binary, deserialize and cast to the interface type you know about. Call the manipulation method.
I haven't tried this but I can't see any reason why it wouldn't work. What I do suggest though is implementing a signing scheme so that you can be sure that the code you execute came from where you think it came from. You don't want someone injecting malicious code.
Upvotes: 1
Reputation: 35246
You could use the javascript engine: see "Scripting for the Java Platform" http://java.sun.com/developer/technicalArticles/J2SE/Desktop/scripting/
for ex:
http://host/resources?key=somekey&operation=return+{newkey:123,data1:"hello"};
Upvotes: 1