views:

45

answers:

2

Is there a way to store a conversion strategy (for converting some bytes) into a database and then execute it on the run time.

If one were to store a complete java file, you would need to compile it, store the class and some how inject into the already running system. I am not sure how this would be possible.

But using some kind of dynamic language on JVM would be nice. I see an example of execution of groovy from within spring context here http://www.devx.com/tips/Tip/42789

but this is still static in nature as application context contains the reference to the implementation and cannot be changed by database.

This is kind of like giving the end use a DSL (domain specific language) to store and execute conversion strategies. The end user would be able to put in their custom rules

Perhaps with JavaConfig of context it is possible. I am exploring options now, specifically with Spring 3.0. Your suggestions in any direction would be welcome.

+1  A: 

To me this sounds like you want to serialize Java classes to the DB. To my knowledge it is perfectly OK, although I have never done it myself. Here is a link to a tutorial, but you can easily google for more.

OTOH at second thought, why do you actually need to store a full class instance in the DB? Isn't it enough to store the fully qualified class name, then load the class and instantiate it (with desired properties which may also come from configuration) via Spring?

Update: OK, now I understand your case better.

Serialization could be a viable option if you can constrain your end users to provide serializable strategy classes. Note though that serialization has its own quirks, so doing it properly requires deeper than average Java knowledge. Josh Bloch in Effective Java 2nd Ed. dedicates the entire Chapter 11 to serialization. Executing code provided by external parties could pose a security risk as well.

Another possible option could be implementing the elements of your DSL yourself, then letting the end users put together what they want from the elements provided by you. This would be safer and more robust, as you still had control over what gets into the DB and what gets executed.

Péter Török
I want to give the end user the ability to store a custom conversion logic.Kind of like giving the end use a DSL (domain specific language) to store and execute conversion strategies.
geoaxis
Keep in mind serialising an object only serialises its state - you can of course serialise the actual class itself, which is probably what you mean.
Michael Neale
@Michael Good point, thanks.
Péter Török
A: 

If you want script-like behaviour of what you store in the DB, consider something like http://mvel.codehaus.org/ - very easy to do.

Michael Neale