Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problem with web application Classloader

 
Francesco Marchioni
author
Ranch Hand
Posts: 194
  • Mark post as helpful
  • send pies
  • 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
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • 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
Ranch Hand
Posts: 194
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your answer. It was quite clear.
Regards Francesco
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic