File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Problem with web application Classloader Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Problem with web application Classloader" Watch "Problem with web application Classloader" New topic
Author

Problem with web application Classloader

Francesco Marchioni
author
Ranch Hand

Joined: Sep 22, 2003
Posts: 190
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


WildFly 8 Administration Book - JBoss Tutorials
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

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

Joined: Sep 22, 2003
Posts: 190
Thanks for your answer. It was quite clear.
Regards Francesco
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem with web application Classloader