• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Problem with web application Classloader

 
author
Posts: 194
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
I have a web application which uses several Utility classes
for business tasks. I'm going to distribute this web application
but I don't want to distribute the byte codes of the Utility classes.

So I've crypted the class and built a custom classloaders
which decrypts the bytes and load the class. So far so good.

Now the matter is: all in the code I have hardcoded the Utility Classes,
while on the other hand I simply have an Object in return from

class.newInstance();

How can have my classes loaded with a custom classloader without having to rewrite all the code ? (for example using reflection)

I thought to write a common superclass with a factory method which returns instances of the Utility classes.

public class Utility extends Master

{
void secretMethod() {
}

}

public class Master

{
getUtilityInstance() {
....
return new Utility();
}

void secretMethod() {
}

}

But then the "Master" class too must be loaded with the same classloader or the Utility class of the web application won't see it. But if the "Master"
class is not loaded from the standard web classloader how can I compile the code, when "Master" is used ? It's like the dog who bites its tail!

How can I solve the problem ?
Thanks a lot
Francesco
 
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yopu should be able to have two JARs, one encrypted and loaded by your custom ClassLoader, and the second unencrypted and loaded by the standard WebApp ClassLoader (ie placed in the WEB-INF/lib directory.

The second JAR can contain Interfaces implemented by you classes, and you can cast happily as long as you construct your custom ClassLoader as a child of the web application ClassLoader. In your Servlets you can put ClassLoader loader = new CustomClassLoader(Servlet.class.getClassLoader()), but there may be a cleaner way to do that bit.

Usinf Interfaces rather than 'Master' classes is easier since you only need a type for the compiler, not actually any concrete implementations.
 
Francesco Marchioni
author
Posts: 194
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your answer. It was quite clear.
Regards Francesco
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic