You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by David Wall <d....@computer.org> on 2008/04/07 05:52:13 UTC

Adding transactional classloader for dynamic code in webapp?

I'd like to allow a web app to define custom code, such as for custom 
data validators or data formatters to be added by a user through 
configuration in my web app. 

That is, I'd like to have a user of my web app specify some Java code 
snippets (either a few lines of code, or perhaps a complete method) that 
my base code would call back to do customized data validations if they 
have been configured in my web app.  An example would be a form that 
needs to validate a phone number, but some want a strict NPA-NXX-9999 
style format, others allow phone extensions, others may allow an 
optional country code prefix, etc.  So I'd like my based code to be able 
to do something like call a method the user has defined that follows a 
naming convention, such as

boolean validatePhone(String phone)

and it would then do the code check set by the user who perhaps only 
filled in the code snippet into a textarea in my JSP and I've saved to 
the database.

My guess is I'd need to develop some sort of transactional class loader, 
one that would be created after dynamically generating the Java code, or 
compiling the Java code entered and storing the resulting .class file, 
etc.  But since they could change the code through my web app, I would 
need to be able to auto-recompile (sort like a JSP does in Tomcat) and 
by recreating a new classloader on the fly, it would obsolete the old 
code during garbage collection and would use the new code going forward.

Is there any place that I can read about some proven approaches to this 
sort of dynamic Java class creation, compilation, class loader?  Tips? 

Thanks,
David

---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Adding transactional classloader for dynamic code in webapp?

Posted by Christopher Schultz <ch...@christopherschultz.net>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

David,

David Wall wrote:
| So I'd like my based code to be able
| to do something like call a method the user has defined that follows a
| naming convention, such as
|
| boolean validatePhone(String phone)

Could you get away with allowing users to specific regular expressions
that could be run over a String? That would make your life sooo much easier.

Another option would be to allow them to use a scripting language like
Javascript, and then run their "method" through something like Rhino.
Then you don't have to deal with ClassLoaders at all.

- -chris

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.8 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iEYEARECAAYFAkf6N+gACgkQ9CaO5/Lv0PAkBACgp5xG/pKUan7CbYCwN7D1ru1M
kSYAnRzS+f+YSBpVUqV1ZfhJl0B2pp+g
=XDQ8
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org