Hi,

On 21/04/2015 05:59, Thusitha Thilina Dayaratne wrote:
> Hi,
>
>> Try addURL().
>> Sorry for the inconvenience.
>> As I understand addURL() method is defined in WebAppClassLoaderBase. So
>> should I obtain the WebAppClassLoaderBase using getClassLoader() method
> and
>> use reflections to call the addURL() method?
>
>>> No need for reflection. Cast to URLClassloader.
>
>>> Note the cast *should* always work but if someone is using a strange
>>> custom class loader it will fail. Note that Tomcat 7 required the class
>>> loader to be an instance of WebappClassLoader.
>> Thanks for quick explanation Mark.
>> But still addURL() method is defined as protected. So it is not possible
> to call that method by casting right?
> This is my implementation with Tomcat 7
>
> public class CarbonWebappLoader extends WebappLoader {
>
> @Override
> protected void startInternal() throws LifecycleException {
> WebappClassloadingContext webappClassloadingContext;
> try {
> webappClassloadingContext =
> ClassloadingContextBuilder.buildClassloadingContext(getWebappFilePath());
> } catch (Exception e) {
> throw new LifecycleException(e.getMessage(), e);
> }
>
> for (String repository :
> webappClassloadingContext.getProvidedRepositories()) {
> addRepository(repository);
> }
>  super.startInternal();
>
> //Adding the WebappClassloadingContext to the WebappClassloader
> ((CarbonWebappClassLoader)
> getClassLoader()).setWebappCC(webappClassloadingContext);
> }
>
> In Tomcat 8 don't have addRepository() in the WebAppLoader. Suggestion was
> to use addURL() method.
> But that can be access through WebAppClassLoader. So I must get the
> classloader using getClassLoader() and use reflections to call addURL
> method.
> That would be really costly operation since it will get call for each and
> every application.
>
> Is there a better approach than that? Or Should I move this logic to
> somewhere else?

Thanks for the response.
>>It is worth considering other options. E.g.
>>a) Use the WebResources to map the JARs into WEB-INF/lib
I've tried to do so as follows.

File dir = new File(webappClassloadingContext.getProvidedRepositories()[0]);
WebResourceRoot resources = getContext().getResources();
resources.addJarResources(new DirResourceSet(resources, "/WEB-INF/lib",
dir.getAbsolutePath(), getContext().getPath()));
getContext().setResources(resources);

But then I get IllegalStateException

org.apache.catalina.LifecycleException: Failed to start component
[StandardEngine[Catalina].StandardHost[localhost].StandardContext[/jaxrs_basic]]
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:154)
Mail Thread ->
http://mail-archives.apache.org/mod_mbox/tomcat-users/201504.mbox/%3CCANVncXDD7h49OztF=6a3u=+MR7=_wkdssjyvqh98kios8zq...@mail.gmail.com%3E

What did I do wrong here?

>>b) Cache the method object so reflection calls are faster.
>>c) Ignore the performance issue since it only occurs on application start

I was able to fix that by overriding the addURL method and use that to add
repositories.

Thanks
Best Regards


2015-04-21 16:43 GMT+05:30 Mark Thomas <ma...@apache.org>:

> On 21/04/2015 05:59, Thusitha Thilina Dayaratne wrote:
> > Hi,
> >
> >> Try addURL().
> >> Sorry for the inconvenience.
> >> As I understand addURL() method is defined in WebAppClassLoaderBase. So
> >> should I obtain the WebAppClassLoaderBase using getClassLoader() method
> > and
> >> use reflections to call the addURL() method?
> >
> >>> No need for reflection. Cast to URLClassloader.
> >
> >>> Note the cast *should* always work but if someone is using a strange
> >>> custom class loader it will fail. Note that Tomcat 7 required the class
> >>> loader to be an instance of WebappClassLoader.
> >> Thanks for quick explanation Mark.
> >> But still addURL() method is defined as protected. So it is not possible
> > to call that method by casting right?
> > This is my implementation with Tomcat 7
> >
> > public class CarbonWebappLoader extends WebappLoader {
> >
> > @Override
> > protected void startInternal() throws LifecycleException {
> > WebappClassloadingContext webappClassloadingContext;
> > try {
> > webappClassloadingContext =
> > ClassloadingContextBuilder.buildClassloadingContext(getWebappFilePath());
> > } catch (Exception e) {
> > throw new LifecycleException(e.getMessage(), e);
> > }
> >
> > for (String repository :
> > webappClassloadingContext.getProvidedRepositories()) {
> > addRepository(repository);
> > }
> >  super.startInternal();
> >
> > //Adding the WebappClassloadingContext to the WebappClassloader
> > ((CarbonWebappClassLoader)
> > getClassLoader()).setWebappCC(webappClassloadingContext);
> > }
> >
> > In Tomcat 8 don't have addRepository() in the WebAppLoader. Suggestion
> was
> > to use addURL() method.
> > But that can be access through WebAppClassLoader. So I must get the
> > classloader using getClassLoader() and use reflections to call addURL
> > method.
> > That would be really costly operation since it will get call for each and
> > every application.
> >
> > Is there a better approach than that? Or Should I move this logic to
> > somewhere else?
>
> It is worth considering other options. E.g.
>
> a) Use the WebResources to map the JARs into WEB-INF/lib
> b) Cache the method object so reflection calls are faster.
> c) Ignore the performance issue since it only occurs on application start
>
> Mark
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
> For additional commands, e-mail: users-h...@tomcat.apache.org
>
>


--

Reply via email to