You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Goren Il <go...@hotmail.com> on 2010/07/05 09:44:20 UTC

How to call an external class from a webapp (i.e. - implement a plugin)

  I would like my webapp to call an external class, which will be provided (as a name) in an XML file at run time.
The external class will be developed by 3rd party, and might include additional JARs.
I will refer to the external class and JARs as the "plugin".
My webapp is provided as a WAR, so it is not possible to add the plugin into the same WAR (unless the customer performs it himself).
I do not want to put the plugin's JARs in the common lib of Tomcat, so that it will not affect other webapps (the plugin might include JARs of different versions than the webapps use).
I considered putting the plugin in a separate folder, and implement my own class loader that will look at that folder as well. It seems like too much of an effort for a simple task.
What is the recommended way to achieve "plugin" capabilities for a webapp?
Thanks
G.

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


Re: How to call an external class from a webapp (i.e. - implement a plugin)

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

Chuck,

On 7/7/2010 3:48 PM, Caldarale, Charles R wrote:
>> From: Christopher Schultz [mailto:chris@christopherschultz.net] 
>> Subject: Re: How to call an external class from a webapp (i.e. - 
>> implement a plugin)
>> 
>> Another option is to implement your own ClassLoader that scans a
>> directory (such as /my/great/plugins) for JAR files and will load
>> them alongside your webapp's classes.
> 
> Tomcat already has a form of this capability: 
> org.apache.catalina.loader.VirtualWebappLoader
> 
> If you declare this in the className attribute of a <Loader> element
> nested inside the <Context> of interest, you can specify additional
> directories to search for class references.  It exists in all
> supported Tomcat levels, but is only documented in Tomcat 7:
> 
> http://tomcat.apache.org/tomcat-7.0-doc/config/loader.html#VirtualWebappLoader_Implementation

Good to know. I didn't know it was available before TC 7.0 or without
compiling it yourself... I did know that it existed, at least somewhere.

I would tend to favor a ClassLoader that was explicitly used for only
plug-in class loading. I thought you could set a SecurityManager on a
ClassLoader, but my mind must be remembering something else, 'cause it
certainly not in the documentation. :(

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

iEYEARECAAYFAkw04rEACgkQ9CaO5/Lv0PA39QCfVO+whzxqUmTyhJ0/z8dU8IEw
0/UAn3G3FA4sNIkX7m7Jd+A39ZB97+Qa
=aS5e
-----END PGP SIGNATURE-----

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


RE: How to call an external class from a webapp (i.e. - implement a plugin)

Posted by "Caldarale, Charles R" <Ch...@unisys.com>.
> From: Christopher Schultz [mailto:chris@christopherschultz.net]
> Subject: Re: How to call an external class from a webapp (i.e. -
> implement a plugin)
> 
> Another option is to implement your own ClassLoader that 
> scans a directory (such as /my/great/plugins) for JAR files
> and will load them alongside your webapp's classes.

Tomcat already has a form of this capability:
  org.apache.catalina.loader.VirtualWebappLoader

If you declare this in the className attribute of a <Loader> element nested inside the <Context> of interest, you can specify additional directories to search for class references.  It exists in all supported Tomcat levels, but is only documented in Tomcat 7:

http://tomcat.apache.org/tomcat-7.0-doc/config/loader.html#VirtualWebappLoader_Implementation

 - Chuck


THIS COMMUNICATION MAY CONTAIN CONFIDENTIAL AND/OR OTHERWISE PROPRIETARY MATERIAL and is thus for use only by the intended recipient. If you received this in error, please contact the sender and delete the e-mail and its attachments from all computers.


Re: How to call an external class from a webapp (i.e. - implement a plugin)

Posted by Goren Il <go...@hotmail.com>.
  Thanks Chris for your response.
It seems that you are thinking in the same direction that we had in mind.
About your suggestion to create a new class loader - this is the direction that we started checking.
We considered the following:
1. Creating a new class, that extends WebappClassLoader.
2. Create an instance of this class, and copy all the configuration from the wepassp's class loader. (Question to the forum - other than the URL's - what other properties should we copy)?
3. Add the extra directory to the URLs
4. Load the class by name, using the new class loader.

Will this work?
Is there some example code for that?
(BTW, we are using Tomcat 5.5, in case it matters).

About your last question - the webapp loads an XML from an external directory, which includes all configuration parameters.

Thanks
G.



On 07-Jul-10 22:36, Christopher Schultz wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> Goren,
>
> On 7/6/2010 10:51 AM, Goren Il wrote:
>>   There are 2 issues (not in order of priority):
>> 1. The steps are not straight forward, and may cause the system to stop
>> working (the customer should change the WAR suffix to "zip", open it,
>> add the files and rename it back)
> There isn't really a reason why the above shouldn't work.
>
>> 2. When we provide a new version, we will replace the WAR and all the
>> changes are gone.
> Fair enough, but the customer can re-install their plug-ins.
>
> Another option is to implement your own ClassLoader that scans a
> directory (such as /my/great/plugins) for JAR files and will load them
> alongside your webapp's classes.
>
> You'll either have to chain that ClassLoader into the standard webapp
> ClassLoader provided by Tomcat for transparent class-loading by your
> webapp, or write a wrapper for plug-in loading and execution by
> explicitly managing and invoking that ClassLoader as necessary.
>
> You should seriously consider how to implement security mechanisms when
> loading classes that you do not provide yourself.
>
>> I would like my webapp to call an external class, which will be
>> provided (as a name) in an XML file at run time.
> How does your webapp load this XML file if it isn't packaged in the WAR
> file?
>
> - -chris
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.4.10 (MingW32)
> Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/
>
> iEYEARECAAYFAkw011gACgkQ9CaO5/Lv0PAO9QCfRRfexlNsGjxh5pg2pHKlzbWC
> XVEAoKaBL2jk5i10EIY2DDQt6wcP4vc/
> =VS3H
> -----END PGP SIGNATURE-----
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>
>
>


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


Re: How to call an external class from a webapp (i.e. - implement a plugin)

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

Goren,

On 7/6/2010 10:51 AM, Goren Il wrote:
>  There are 2 issues (not in order of priority):
> 1. The steps are not straight forward, and may cause the system to stop
> working (the customer should change the WAR suffix to "zip", open it,
> add the files and rename it back)

There isn't really a reason why the above shouldn't work.

> 2. When we provide a new version, we will replace the WAR and all the
> changes are gone.

Fair enough, but the customer can re-install their plug-ins.

Another option is to implement your own ClassLoader that scans a
directory (such as /my/great/plugins) for JAR files and will load them
alongside your webapp's classes.

You'll either have to chain that ClassLoader into the standard webapp
ClassLoader provided by Tomcat for transparent class-loading by your
webapp, or write a wrapper for plug-in loading and execution by
explicitly managing and invoking that ClassLoader as necessary.

You should seriously consider how to implement security mechanisms when
loading classes that you do not provide yourself.

> I would like my webapp to call an external class, which will be
> provided (as a name) in an XML file at run time.

How does your webapp load this XML file if it isn't packaged in the WAR
file?

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

iEYEARECAAYFAkw011gACgkQ9CaO5/Lv0PAO9QCfRRfexlNsGjxh5pg2pHKlzbWC
XVEAoKaBL2jk5i10EIY2DDQt6wcP4vc/
=VS3H
-----END PGP SIGNATURE-----

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


Re: [OT] How to call an external class from a webapp (i.e. - implement a plugin)

Posted by André Warnier <aw...@ice-sa.com>.
Goren Il wrote:
> 
>  so the basic idea is:
> 1. Define an interface (WSDL or named parameters for a "post" message)
> 2. Enable specifying the URL of the plugin in the context of the webapp
> 3. As part of the flow, call that URL with the defined format
> 
> Did I follow your thought?
> 
> It seems like an overhead in terms of performance, but if there is no 
> other possibility - this is what we shall do.
> Thanks
> 
Basically, I believe that this is what Chuck meant.  The "plugin" would be another 
separate webapp, and your webapp would make a HTTP request to it.

In Apache httpd, there is a concept of an "internal sub-request". This allows one 
"context" (the concept is a bit different there), to momentarily suspend its own execution 
while it is itself making an internal request to the server for another URL/resource, then 
collecting the result of this "sub-request", and continue its own execution.
Because this sub-request is purely internal to the webserver, it avoids a lot of the 
overhead that would result from a separate full HTTP request for instance (such as having 
to create its own HTTP loopback connection etc..).

It would be interesting to learn from the experts here, if there would exist an efficient 
and servlet-spec compatible way to implement such a mechanism in Tomcat.



> On 06-Jul-10 18:15, Caldarale, Charles R wrote:
>>> From: Goren Il [mailto:gorenil@hotmail.com]
>>> Subject: Re: How to call an external class from a webapp (i.e. -
>>> implement a plugin)
>>>
>>> Aren't there other webapps that have plugin capabilities?
>> The philosophy of the servlet spec is to have self-contained webapps, 
>> so plugins aren't really catered for.  (Might be something in the 3.0 
>> spec, but I haven't really studied it yet.)
>>
>> As an alternative to direct class calls, can you package the add-on as 
>> a separate context and reference it via forwarding or as a web service?
>>
>>   - Chuck
>>
>>
>> THIS COMMUNICATION MAY CONTAIN CONFIDENTIAL AND/OR OTHERWISE 
>> PROPRIETARY MATERIAL and is thus for use only by the intended 
>> recipient. If you received this in error, please contact the sender 
>> and delete the e-mail and its attachments from all computers.
>>
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
>> For additional commands, e-mail: users-help@tomcat.apache.org
>>
>>
>>
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
> 
> 


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


Re: How to call an external class from a webapp (i.e. - implement a plugin)

Posted by Goren Il <go...@hotmail.com>.
  so the basic idea is:
1. Define an interface (WSDL or named parameters for a "post" message)
2. Enable specifying the URL of the plugin in the context of the webapp
3. As part of the flow, call that URL with the defined format

Did I follow your thought?

It seems like an overhead in terms of performance, but if there is no other possibility - this is what we shall do.
Thanks

On 06-Jul-10 18:15, Caldarale, Charles R wrote:
>> From: Goren Il [mailto:gorenil@hotmail.com]
>> Subject: Re: How to call an external class from a webapp (i.e. -
>> implement a plugin)
>>
>> Aren't there other webapps that have plugin capabilities?
> The philosophy of the servlet spec is to have self-contained webapps, so plugins aren't really catered for.  (Might be something in the 3.0 spec, but I haven't really studied it yet.)
>
> As an alternative to direct class calls, can you package the add-on as a separate context and reference it via forwarding or as a web service?
>
>   - Chuck
>
>
> THIS COMMUNICATION MAY CONTAIN CONFIDENTIAL AND/OR OTHERWISE PROPRIETARY MATERIAL and is thus for use only by the intended recipient. If you received this in error, please contact the sender and delete the e-mail and its attachments from all computers.
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>
>
>


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


RE: How to call an external class from a webapp (i.e. - implement a plugin)

Posted by "Caldarale, Charles R" <Ch...@unisys.com>.
> From: Goren Il [mailto:gorenil@hotmail.com]
> Subject: Re: How to call an external class from a webapp (i.e. -
> implement a plugin)
> 
> Aren't there other webapps that have plugin capabilities?

The philosophy of the servlet spec is to have self-contained webapps, so plugins aren't really catered for.  (Might be something in the 3.0 spec, but I haven't really studied it yet.)

As an alternative to direct class calls, can you package the add-on as a separate context and reference it via forwarding or as a web service?

 - Chuck


THIS COMMUNICATION MAY CONTAIN CONFIDENTIAL AND/OR OTHERWISE PROPRIETARY MATERIAL and is thus for use only by the intended recipient. If you received this in error, please contact the sender and delete the e-mail and its attachments from all computers.



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


Re: How to call an external class from a webapp (i.e. - implement a plugin)

Posted by Goren Il <go...@hotmail.com>.
  There are 2 issues (not in order of priority):
1. The steps are not straight forward, and may cause the system to stop working (the customer should change the WAR suffix to "zip", open it, add the files and rename it back)
2. When we provide a new version, we will replace the WAR and all the changes are gone.

Aren't there other webapps that have plugin capabilities? How do they implement this capability?

Thanks
G.

On 06-Jul-10 15:45, Joseph Morgan wrote:
> What's wrong with providing instructions to the customer on how to
> install the "plugin" with a particular web app?
>
>
> -----Original Message-----
> From: Goren Il [mailto:gorenil@hotmail.com]
> Sent: Monday, July 05, 2010 2:44 AM
> To: users@tomcat.apache.org
> Subject: How to call an external class from a webapp (i.e. - implement a
> plugin)
>
>
>    I would like my webapp to call an external class, which will be
> provided (as a name) in an XML file at run time.
> The external class will be developed by 3rd party, and might include
> additional JARs.
> I will refer to the external class and JARs as the "plugin".
> My webapp is provided as a WAR, so it is not possible to add the plugin
> into the same WAR (unless the customer performs it himself).
> I do not want to put the plugin's JARs in the common lib of Tomcat, so
> that it will not affect other webapps (the plugin might include JARs of
> different versions than the webapps use).
> I considered putting the plugin in a separate folder, and implement my
> own class loader that will look at that folder as well. It seems like
> too much of an effort for a simple task.
> What is the recommended way to achieve "plugin" capabilities for a
> webapp?
> Thanks
> G.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>
>
>


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


RE: How to call an external class from a webapp (i.e. - implement a plugin)

Posted by Joseph Morgan <jo...@ignitesales.com>.
What's wrong with providing instructions to the customer on how to
install the "plugin" with a particular web app?


-----Original Message-----
From: Goren Il [mailto:gorenil@hotmail.com] 
Sent: Monday, July 05, 2010 2:44 AM
To: users@tomcat.apache.org
Subject: How to call an external class from a webapp (i.e. - implement a
plugin)


  I would like my webapp to call an external class, which will be
provided (as a name) in an XML file at run time.
The external class will be developed by 3rd party, and might include
additional JARs.
I will refer to the external class and JARs as the "plugin".
My webapp is provided as a WAR, so it is not possible to add the plugin
into the same WAR (unless the customer performs it himself).
I do not want to put the plugin's JARs in the common lib of Tomcat, so
that it will not affect other webapps (the plugin might include JARs of
different versions than the webapps use).
I considered putting the plugin in a separate folder, and implement my
own class loader that will look at that folder as well. It seems like
too much of an effort for a simple task.
What is the recommended way to achieve "plugin" capabilities for a
webapp?
Thanks
G.

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


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