ClassLoader / Threads etc.

Hi,

I am trying to use MyCustomClassLoader to load java classes (my app is running under WebLogic).

Right now I am facing two problems I don't know what to do about:

1. I have tried to load classes through MyCustomClassLoader and all of them got loaded, but when it's turn to load java.lang.Object, a SecurityException is thrown saying "prohibited package name: java/lang". I have read it somewhere that I will not be able to load java.* package. Is that true? Is there a way around it?

2. While my classloader is running fine (except for the problem # 1), I am still trying to make it the default classloader for my app. The approach I chose (and tell my if this approach is wrong) is to make MyCustomClassLoader the default one for the threads in the threadpool serving my app (ExecuteThread 'number' for the application 'my application name'). I change all of them, verify that they have been changed, launch my first servlet, chech them again and notice that threads used to load my servlet have been changed to sun.something.something (the default classloader). What am I doing wrong? Is the approach I chose wrong?

I will give you 500 and an A grade if you help me make it work.

Thanks
LVL 1
mkaplunovAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
moduloConnect With a Mentor Commented:
Saving this Q as a PAQ and refunding the 500 points to the questionner

modulo

Community Support Moderator
Experts Exchange
0
 
wide_awakeCommented:
My understanding of the class loader architecture is that it uses a sort of delegation model to load classes.  It delegates the class loading to the class loader closes to the root ni the hierarchy (and therefore most trusted one) to actually load the class.  When you make your own class loader, it will only load classes that can't be loaded by the built-in classloaders.  

The reason for this is security.  If you could force the JVM to use your class loader, you would basically have total control over security.  The model they use means that the most trusted classloader will load the bootstrap classes, ensuring that security is not compromised.

It's been a while since I've messed around with classloaders, so I may be wrong :)
0
 
objectsCommented:
1. I don't think your custom classloader should be loading system classes.
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
mkaplunovAuthor Commented:
You may be right, but I would like to know if it is possible at all.
0
 
Venci75Commented:
1. yes - loading java.* classes causes SecurityException and there is no way to around this. Why do you want to load java.lang.Object? It is already loaded by the system class loader and you don't need to do this?
2. instead of trying to load your servlet with a custom class loader - try to move the servlet functionality to another class and load this class using your class loader.

0
 
nir2002Commented:
Hi,

>>I am trying to use MyCustomClassLoader to load java classes (my app is running under WebLogic).

If this is in EJB, this situation is illegal by the EJB spec.

Best regards
Nir
0
 
girionisCommented:
 1) As already stated it is not allowed since the system classes are already loaded (if they weren't you couldn't run your application :-) ). Loading them again could cause serious problems to the VM (for instance you could end up with two instances of static variables) hence it is not allowed and there isn't any workaround AFAIK.

  2) Again Servlets are loaded by a different classloader. Classloaders always ask the parent classloader up to the hierarchy tree (and the parent classloader delegates the request to the "grandparent" classloader etc) to load a class and only if a) the parent classloder cannot load the class and b) the class is not already loaded, then the classloader is allowed to load a class. If you already have written a custom classloader I suggest you move it up to the hierarchy tree and see (since, hopefully, the default Servlet classloader will ask your custom classloader to load the Servlet classes) if this works. If you could "override" WebLogic's default Servlet classloader then your custom classloader will precede it and will be able to load the classes. If you haven't already done so you might want to take a look here: http://edocs.bea.com/wls/docs81/programming/classloading.html
0
 
itzsureshCommented:
1. almost all posts above have answered this question: you cannot and must not load java* classes. if u are allowed to do this, then it is a severe security problem, coz someone else can write a java.lang.Object class with a malicious intent and if that is loaded, then u are in all sorts of problems.

2. try to remove the classes that you would like to load thru your classloaded from the classpath. this makes it inaccessible to the default classloader. now try to load these classes from a fixed location thru ur classloader. i think this could solve your problem.

regards,
0
 
mkaplunovAuthor Commented:
Venchi75:
I am trying to decrypt my class files, so I have to use my customised class loader

nir2002:
I agree

girionis:
I have read that document and as far as I understand it shows one how to specify a custom classloader's hierarchy, and not how to include one's custom classloader. Do you know how to move MyCustomClassLoader up the hierarchy?

itzsuresh:
Regarding #2 - that's exactly what I am doing. None of the classloader can locate requested APIs, so MyCustomCLassLoader gets to load them. The only problem here, I need to make my ClassLoader the default one. Otherwise you can find yourself in a situation where you have to go through your entire application (and we have lots of files) and change every instance of all of your classes to be instantiated through MyCustomClassLoader...
0
 
objectsCommented:
0
 
mkaplunovAuthor Commented:
I don't know if I should laugh or cry, but I have read the same documents (almost all of them and much more), before posting this question...
0
 
objectsCommented:
Have you had a look at the tutorial on ibm's site?
0
 
mkaplunovAuthor Commented:
I found this tutorial in a PDF file on some other website
0
 
girionisCommented:
>  Do you know how to move MyCustomClassLoader up the hierarchy?

  No sorry, don't know. The thing is that if none of the classloaders can load the requested APIs then the only one that can is your custom classloader so there might not be the need to put it up the hierarchy tree, because they will be loaded by your own classloader anyway.
0
 
mkaplunovAuthor Commented:
girionis:

True. I could exclude the location of my class files from the CLASSPATH, but then I'd be back to the square one (loading classes by implicitly calling my class loader every time a class has to be loaded) and it’s gonna be very ugly and not very scalable...
0
 
girionisCommented:
 That's how things work with the default classloaders as well. A class is loaded on demand. What you could do if you wanted is to load all of your custom classes at once and then they will be available if another class requests them.
0
 
mkaplunovAuthor Commented:
I have though about it and I don't think it will work. If my custom classes are are loaded by my custom classloader and then requested/referenced by some other classes later on, they will be reloaded by the default classloader...


0
 
objectsCommented:
I'm confused, where is your classloader currently in the hierarchy, and which way are you referring to as up?
In fact how are you currently using your class loader to load classes?
0
 
girionisCommented:
 This could happen with any classes. The only way to make sure that a class will not be loaded twice is if it is loaded by the bootstrap classloader. If classloaders *above* your custom classloader do not request your classes then they are guaranteed that they will never be loaded twice. And frankly, I do not thing they will, it is like telling me that the String class, or the HttpServlet calss could make a call to your custom classes.
0
 
mkaplunovAuthor Commented:
Remember I said I was running my app on WebLogic? There are multiple classloaders created (by WebLogic). Each threadgroup has a default classloader assigned to it, depending on the context served by the threads in this group.
0
 
nir2002Commented:
Do your classes compiled with the WebLogic compiler?
if not try to compile it with them and let me know what happen.
Which verson of WebLogic do you use?

best regards
Nir
0
 
mkaplunovAuthor Commented:
girionis:

You're right that String would never call my class, but if class A was loaded by MyCustomClassLoader and then class B references/instantiates A, then the default class loader for this threadgroup is going to pick it up and then:

1. Check if it has loaded A before, and if it hasn't
2. Delegate class A up the hierarchy, if if neither of its parents can load A
3. Try to load A

MyCustomClassLoader is not a member and cannot be "inserted" into the classloader hierarchy for my app (default WebLogic ClassLoaders hierarchy). Am I wrong here?
0
 
mkaplunovAuthor Commented:
WL6.1 SP4, JDK1.3.1
I am using javac that came with the WL
0
 
nir2002Commented:
How and when you use the class loader?
0
 
girionisCommented:
 mkaplunov:

  I am not sure how WebLogic classloading works but, your classloader, should follow the delegation model like all java classloaders do (apart from bootstrap). If you look at the classloading document of WebLogc you will see that the weblogic-application.xml file has a <classloader-structure> element. Maybe you could use that to move your classloader up or down, but I am not sure if it can be done.

  As for your other question, it depends where class B is. If class B is loaded by some other classloader then, yes, you will end up with class A loaded two times. For this to happen class B needs to be in a place that the rest of the classloaders have access. If class B is anywhere else then you shouldn't have problems since the only classloder that could access it is your custom classloader.
0
 
mkaplunovAuthor Commented:
nir2002:
*I am trying to make MyCustomClassLoader the default one for my app*, so I am trying to find out how and when it should be used.

girionis:
<class-loader> structure cannot be used, as it defines the structure but doesn't allow you to specify which custom classloader, if any, to use for a particular component.

If class B is available to MyCustomClassLoader only, then if I don't call the latter explicitly, I will get a ClassNotFoundException. I cannot add MyCustomClassLoader to the tree of the classloaders used, I just create a new branch and therefore if this branch is not made dafault for my app, it will never be used.

0
 
girionisCommented:
> In fact how are you currently using your class loader to load classes?

  Objects asked a very valid question. Can you give us some more information?
0
 
mkaplunovAuthor Commented:
Right now I have a final class, let's call it class A, which is launched at startup. A has a static method called getClassLoader, which instantiates an instance of MyCustomClassLoader, so whenever I need to load a class, I do something like this:

Class toLoad = A.getClassLoader().loadClass("classname", boolean);
0
 
Venci75Commented:
hm... I thought that the startup classes are loaded by the system class loader.
If you use a startup class - you can execute this:
Class.forName("servlet class", myCustomClassLoader);

unfortunately - if you want to re-deploy your application after that - you will have to restart the server

0
 
girionisCommented:
 mkaplunov:

  Your classloader is alerady loaded so it can load any classes that can't be loaded by any other classloaders. As long as these classes reside in a location other than the default ones or are not acquired by other classes loaded by the default classloaders then there really shouldn't be any problem.

  Have you actually tried the system and you saw that there are issues?
0
 
mkaplunovAuthor Commented:
girionis:

Yes, I have. As I said before just because MyCustomClassLoader is loaded and the rest of classloaders doesn't know where my class files are, doesn't mean that the job of loading my classes will be delegated to MyCustomClassLoader (unless I explicitly call MyCustomClassLoader to load my classes). Remember that classloader hierarchy "talks" one way only (up the chain) and I can't make MyCustomClassLoader to be the parent of an existing AppClassLoader, which is the default one for my app.
0
 
objectsCommented:
You don't need to be telling other classloader about yours, your classloader needs to be the top (or bottom depending on semantics) of the chain, and it needs to be delegating to the default class loader.
0
 
mkaplunovAuthor Commented:
Do you know how to make it the top of the chain?
0
 
objectsCommented:
for an app I do, but haven't used wl.
Saw some doc on a later version of wl, but not for 6.1
0
 
girionisCommented:
 mkaplunov:

  Top level calssloaders will not delegate to your classloader if it is below them. You can only delegate a request to the classloaders above.  

  I have just gone through the WebLogic documentation briefly. What I noticed is this:

  "You can create custom classloader hierarchies for an application allowing for better control over class visibility and reloadability"

  and this:
 

  "The top-level element in weblogic-application.xml includes an optional classloader-structure element. If you do not specify this element, then the standard classloader is used. Also, if you do not include a particular module in the definition, it is assigned a classloader, as in the standard hierarchy. That is, EJB modules are associated with the application Root classloader, and Web application modules have their own classloaders"

  Pay particular attention to the following line:

  "If you do not specify this element, then the standard classloader is used."

  My guess is that if you specify a classloader on the top-level element then the classloader you specified will be used for your application.

  Have you actualyl tried to use the <classloader-structure> element? Any luck?
0
 
mkaplunovAuthor Commented:
girionis:

Please, read my comments made on 04/02/2003 09:57AM PST
0
 
moduloCommented:
Dear expert(s),

A request has been made to close this Q in CS:
http://www.experts-exchange.com/Community_Support/CleanUp/Q_20590122.html

Without a response in 72 hrs, a moderator will finalize this question by:

 - Saving this Q as a PAQ and refunding the points to the questionner

When you agree or disagree, please add a comment here.

Thank you.

modulo

Community Support Moderator
Experts Exchange
0
All Courses

From novice to tech pro — start learning today.