English 中文(简体)
Conversion of bytes into a type without changing the application (ie storing the conversion method in DB) with Spring 3.0
原标题:

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.

问题回答

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.

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





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签