You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@felix.apache.org by Bhaskar Maddala <ma...@gmail.com> on 2008/06/04 22:44:27 UTC

Searching class spaces - noob

Hello,

    Couple of days back I sent the email with the same subject to the
users mailing list, to which Richard Hall did reply, I gather Richard
is also part of this mailing list, so firstly thanks for the reply
Richard.

I summarize the question and Richard's response below [1]

I had a couple of questions that I would like to follow-up with, which
I thought would be more appropriate on the dev mailing list. Are there
any plans to provide such functionality (in the web console sub
project perhaps)? From the response, I gather that this would not be
an impossible task, however what would be the feasibility of
implementing this functionality? Would the solution be specific to
felix or could the bundle be deployed in other OSGi implementations
(equinox, knopflerfish etc)?

I ask these questions this is functionality that I am used to from
application server administration consoles.

Thanks
Bhaskar



[1]

Question : Does anyone know of a way to search class spaces for the
location (bundle/jar file) from which a specified class is being
loaded?

Reply : It might not be completely impossible, but it would require a
lot of reflection over all of your bundles' content as well as the
current set of wirings among them...it would not be pretty.


Thanks
Bhaskar

Re: Searching class spaces - noob

Posted by "Richard S. Hall" <he...@ungoverned.org>.
Bhaskar Maddala wrote:
> I frequently (seem to) run into issues that result in Class loading
> related exceptions, doing a manual inspection of the manifest headers,
> I often come to the conclusion that the class should have been in the
> class space of the bundle that is causing the exception, at this state
> it would be great to have a tool to visually inspect the classes that
> are available in the bundle class space along with the jars from which
> the classes are being loaded, this diagnostic information would, I
> think, help in determining the error in the Manifest headers sooner
> and lessen my pain attempting to understand OSGi.
>   

Felix tries to give some of this diagnostic information when a CNFE 
occurs...it will tell you if the package is available and who is 
providing, for example.

> Having understood the scope of the work that needs to be implemented I
> think I have decided to settle for option 2
>
>  -> user does a search by class name and a bundle id
> I intend to provide class meta information in the event that the class
> is available in the bundle class space, else use
> PackageAdmin.getBundle(java.lang.Class clazz) to retreive the bundle
> and display its information.
>
>
> From Karl's and Niclas responses above, my initial though was that
> Niclas is correct, in that
>
> I want to know where a particular class comes from for a specific
> bundle class space. Based on what I understand, a class may be loaded
> from 2 separate locations, in different class spaces, so I think I
> need step 3.
>
> 1) Get the package name from the class name.
> 2) Locate all bundles exporting that package.
> 3) Check which Bundle Classloader equals the class classloader.
>
> From Karl's response I took a look at the Javadoc for PackageAdmin
> ___
> public Bundle getBundle(java.lang.Class clazz)
>
> Returns the bundle from which the specified class is loaded. The class
> loader of the returned bundle must have been used to load the
> specified class. If the class was not loaded by a bundle class loader
> then null is returned.
> __
>
> This seems to imply that my understanding of class being (potentially)
> loaded from multiple bundles in different class spaces is incorrect,
> what exactly am I missing?
>   

No, your assumption is correct. Any given class can only be loaded by 
one class loader (and thus one bundle), so the above API returns the 
bundle that loaded the class instance you passed in. It is possible that 
Foo.class was loaded from two different bundles, so in that case there 
really are two different class instances, but you only pass in one class 
instance to the above method, thus only one bundle will be returned -- 
the bundle that loaded that class. (Don't be confused when I say class 
instance, I am not talking about instances of Foo, but rather instances 
of Class.)

-> richard

> My approach to try to implement this is as follows
>
> Try and fix
> https://issues.apache.org/jira/browse/FELIX-574
> https://issues.apache.org/jira/browse/FELIX-566
>
> to get an understanding of how the web console code is organized, I
> took a first stab at 574 and know what to do to fix this. On 566, I
> think I should be able to address it, once I have 574 addressed.
>
> Only after then do I intend to try implementing the additional servlet
> for performing the class and bundle id based search.
>
> Once again thank you all for chiming in, greatly appreciated as it
> makes my understand a little better each time.
>
> Thanks
> Bhaskar
>
>
> On Fri, Jun 6, 2008 at 9:40 AM, Richard S. Hall <he...@ungoverned.org> wrote:
>   
>> Niclas Hedhman wrote:
>>     
>>> On Friday 06 June 2008 01:47, Richard S. Hall wrote:
>>>
>>>       
>>>> In some
>>>> cases, this won't be so easy, because you might have to look into the
>>>> contents of embedded JAR files...
>>>>
>>>>         
>>> Well, if he has the class (which I believe was the initial question), then
>>> I think the process would be much easier.
>>>
>>> 1) Get the package name from the class name.
>>> 2) Locate all bundles exporting that package.
>>> 3) Check which Bundle Classloader equals the class' classloader.
>>>
>>> No?
>>>
>>>       
>> Yes, if he has the class. However, I thought his original question on users@
>> said that he wanted to be able to list all class available in a class space.
>> I could be remembering incorrectly. Nevertheless, you are correct, and as
>> Karl points out, if that is all he wants to do, it is really easy.
>>
>> -> richard
>>
>>     
>>> Cheers
>>>
>>>       

Re: Searching class spaces - noob

Posted by Bhaskar Maddala <ma...@gmail.com>.
>> Yes, if he has the class. However, I thought his original question on users@ said that he wanted to be able to list all class available in a class space.

Yes this is my goal-plated requirement, and what I ultimately would
want to do, but partially reimplementing the OSGi class loader search
process seems beyond my scope/ability.

Here is the development time problem I would like to solve, again the
problem might very well be a beginner problem that developers with
OSGi knowledge might not face.

I frequently (seem to) run into issues that result in Class loading
related exceptions, doing a manual inspection of the manifest headers,
I often come to the conclusion that the class should have been in the
class space of the bundle that is causing the exception, at this state
it would be great to have a tool to visually inspect the classes that
are available in the bundle class space along with the jars from which
the classes are being loaded, this diagnostic information would, I
think, help in determining the error in the Manifest headers sooner
and lessen my pain attempting to understand OSGi.

Having understood the scope of the work that needs to be implemented I
think I have decided to settle for option 2

 -> user does a search by class name and a bundle id
I intend to provide class meta information in the event that the class
is available in the bundle class space, else use
PackageAdmin.getBundle(java.lang.Class clazz) to retreive the bundle
and display its information.


>From Karl's and Niclas responses above, my initial though was that
Niclas is correct, in that

I want to know where a particular class comes from for a specific
bundle class space. Based on what I understand, a class may be loaded
from 2 separate locations, in different class spaces, so I think I
need step 3.

1) Get the package name from the class name.
2) Locate all bundles exporting that package.
3) Check which Bundle Classloader equals the class classloader.

>From Karl's response I took a look at the Javadoc for PackageAdmin
___
public Bundle getBundle(java.lang.Class clazz)

Returns the bundle from which the specified class is loaded. The class
loader of the returned bundle must have been used to load the
specified class. If the class was not loaded by a bundle class loader
then null is returned.
__

This seems to imply that my understanding of class being (potentially)
loaded from multiple bundles in different class spaces is incorrect,
what exactly am I missing?

My approach to try to implement this is as follows

Try and fix
https://issues.apache.org/jira/browse/FELIX-574
https://issues.apache.org/jira/browse/FELIX-566

to get an understanding of how the web console code is organized, I
took a first stab at 574 and know what to do to fix this. On 566, I
think I should be able to address it, once I have 574 addressed.

Only after then do I intend to try implementing the additional servlet
for performing the class and bundle id based search.

Once again thank you all for chiming in, greatly appreciated as it
makes my understand a little better each time.

Thanks
Bhaskar


On Fri, Jun 6, 2008 at 9:40 AM, Richard S. Hall <he...@ungoverned.org> wrote:
> Niclas Hedhman wrote:
>>
>> On Friday 06 June 2008 01:47, Richard S. Hall wrote:
>>
>>>
>>> In some
>>> cases, this won't be so easy, because you might have to look into the
>>> contents of embedded JAR files...
>>>
>>
>> Well, if he has the class (which I believe was the initial question), then
>> I think the process would be much easier.
>>
>> 1) Get the package name from the class name.
>> 2) Locate all bundles exporting that package.
>> 3) Check which Bundle Classloader equals the class' classloader.
>>
>> No?
>>
>
> Yes, if he has the class. However, I thought his original question on users@
> said that he wanted to be able to list all class available in a class space.
> I could be remembering incorrectly. Nevertheless, you are correct, and as
> Karl points out, if that is all he wants to do, it is really easy.
>
> -> richard
>
>> Cheers
>>
>

Re: Searching class spaces - noob

Posted by "Richard S. Hall" <he...@ungoverned.org>.
Niclas Hedhman wrote:
> On Friday 06 June 2008 01:47, Richard S. Hall wrote:
>   
>> In some
>> cases, this won't be so easy, because you might have to look into the
>> contents of embedded JAR files...
>>     
>
> Well, if he has the class (which I believe was the initial question), then I 
> think the process would be much easier.
>
> 1) Get the package name from the class name.
> 2) Locate all bundles exporting that package.
> 3) Check which Bundle Classloader equals the class' classloader.
>
> No?
>   

Yes, if he has the class. However, I thought his original question on 
users@ said that he wanted to be able to list all class available in a 
class space. I could be remembering incorrectly. Nevertheless, you are 
correct, and as Karl points out, if that is all he wants to do, it is 
really easy.

-> richard

> Cheers
>   

Re: Searching class spaces - noob

Posted by Karl Pauls <ka...@gmail.com>.
On Fri, Jun 6, 2008 at 6:40 AM, Niclas Hedhman <ni...@hedhman.org> wrote:
> On Friday 06 June 2008 01:47, Richard S. Hall wrote:
>> In some
>> cases, this won't be so easy, because you might have to look into the
>> contents of embedded JAR files...
>
> Well, if he has the class (which I believe was the initial question), then I
> think the process would be much easier.
>
> 1) Get the package name from the class name.
> 2) Locate all bundles exporting that package.
> 3) Check which Bundle Classloader equals the class' classloader.
>
> No?

It's more easy in this case because the PackageAdmin.getBundle(Class)
can tell you the bundle that exports the package the class is coming
from.

regards,

Karl

> Cheers
> --
> Niclas Hedhman, Software Developer
>
> I  live here; http://tinyurl.com/2qq9er
> I  work here; http://tinyurl.com/2ymelc
> I relax here; http://tinyurl.com/2cgsug
>



-- 
Karl Pauls
karlpauls@gmail.com

Re: Searching class spaces - noob

Posted by Niclas Hedhman <ni...@hedhman.org>.
On Friday 06 June 2008 01:47, Richard S. Hall wrote:
> In some
> cases, this won't be so easy, because you might have to look into the
> contents of embedded JAR files...

Well, if he has the class (which I believe was the initial question), then I 
think the process would be much easier.

1) Get the package name from the class name.
2) Locate all bundles exporting that package.
3) Check which Bundle Classloader equals the class' classloader.

No?

Cheers
-- 
Niclas Hedhman, Software Developer

I  live here; http://tinyurl.com/2qq9er
I  work here; http://tinyurl.com/2ymelc
I relax here; http://tinyurl.com/2cgsug

Re: Searching class spaces - noob

Posted by "Richard S. Hall" <he...@ungoverned.org>.
Bhaskar Maddala wrote:
> Got it thank you, all I care about is bundle 15 (for now at least), do
> not see why I care about the jar (since we are not using fragments at
> this time, using the bundle id should be sufficient).
>
> That will be all for now, until I get stuck again ;)
>   

Ok, well, you should be able to calculate this sort of information by 
using PackageAdmin to determine all wirings for the bundle (i.e., 
imports, required bundles, and fragments). Then for a given class, you 
would have to search the wiring to find out from where it comes. In some 
cases, this won't be so easy, because you might have to look into the 
contents of embedded JAR files...

Essentially, you will have to partial reimplement the OSGi class loader 
search process.

-> richard

> On Thu, Jun 5, 2008 at 10:31 AM, Richard S. Hall <he...@ungoverned.org> wrote:
>   
>> Bhaskar Maddala wrote:
>>     
>>> Richard : I am not entirely certain I understand the distinction
>>> between logical bundle and physical JAR (I get physical JAR but have
>>> no idea what a logical bundle means - noob in subject). As a second
>>> attempt in case I find the information I get from public classes to be
>>> insufficient, I might try to do the "lot of not easy calculations",
>>> would you (or anyone else) care to point me to resources I can look up
>>> to understand what would need to be done.
>>>
>>>       
>> Do you want to know that org.foo.FooClass comes from Bundle 15 (i.e., the
>> installed bundle with bundle ID 15) or do you want to know that it came from
>> /home/myhome/.felix/myprofile/bundle15/version0.0/bundle.jar ?
>>
>> -> richard
>>
>>     
>>> Thanks
>>> Bhaskar
>>>
>>> On Thu, Jun 5, 2008 at 12:12 AM, Richard S. Hall <he...@ungoverned.org>
>>> wrote:
>>>
>>>       
>>>> Niclas Hedhman wrote:
>>>>
>>>>         
>>>>> On Thursday 05 June 2008 04:44, Bhaskar Maddala wrote:
>>>>>
>>>>>
>>>>>           
>>>>>> Question : Does anyone know of a way to search class spaces for the
>>>>>> location (bundle/jar file) from which a specified class is being
>>>>>> loaded?
>>>>>>
>>>>>>
>>>>>>             
>>>>> If you are talking about Exported/Imported (i.e. public classes)
>>>>> packages,
>>>>> then you should be able to retrieve that info very easily from the
>>>>> Package
>>>>> Admin service.
>>>>>
>>>>> For private packages, the answer from Richard seems to reflect(!)
>>>>> reality,
>>>>> and will be framework dependent.
>>>>>
>>>>>           
>>>> Also, I guess my answer depends on what you mean by "location". If by
>>>> "location" you mean the actual provider (i.e., the logical bundle) of the
>>>> class or you mean the physical JAR file in the system. The former would
>>>> be
>>>> possible (but with a lot of not easy calculations), the latter would not
>>>> be
>>>> possible in a standard way.
>>>>
>>>> -> richard
>>>>
>>>>
>>>>         

Re: Searching class spaces - noob

Posted by Bhaskar Maddala <ma...@gmail.com>.
Got it thank you, all I care about is bundle 15 (for now at least), do
not see why I care about the jar (since we are not using fragments at
this time, using the bundle id should be sufficient).

That will be all for now, until I get stuck again ;)

On Thu, Jun 5, 2008 at 10:31 AM, Richard S. Hall <he...@ungoverned.org> wrote:
> Bhaskar Maddala wrote:
>>
>> Richard : I am not entirely certain I understand the distinction
>> between logical bundle and physical JAR (I get physical JAR but have
>> no idea what a logical bundle means - noob in subject). As a second
>> attempt in case I find the information I get from public classes to be
>> insufficient, I might try to do the "lot of not easy calculations",
>> would you (or anyone else) care to point me to resources I can look up
>> to understand what would need to be done.
>>
>
> Do you want to know that org.foo.FooClass comes from Bundle 15 (i.e., the
> installed bundle with bundle ID 15) or do you want to know that it came from
> /home/myhome/.felix/myprofile/bundle15/version0.0/bundle.jar ?
>
> -> richard
>
>> Thanks
>> Bhaskar
>>
>> On Thu, Jun 5, 2008 at 12:12 AM, Richard S. Hall <he...@ungoverned.org>
>> wrote:
>>
>>>
>>> Niclas Hedhman wrote:
>>>
>>>>
>>>> On Thursday 05 June 2008 04:44, Bhaskar Maddala wrote:
>>>>
>>>>
>>>>>
>>>>> Question : Does anyone know of a way to search class spaces for the
>>>>> location (bundle/jar file) from which a specified class is being
>>>>> loaded?
>>>>>
>>>>>
>>>>
>>>> If you are talking about Exported/Imported (i.e. public classes)
>>>> packages,
>>>> then you should be able to retrieve that info very easily from the
>>>> Package
>>>> Admin service.
>>>>
>>>> For private packages, the answer from Richard seems to reflect(!)
>>>> reality,
>>>> and will be framework dependent.
>>>>
>>>
>>> Also, I guess my answer depends on what you mean by "location". If by
>>> "location" you mean the actual provider (i.e., the logical bundle) of the
>>> class or you mean the physical JAR file in the system. The former would
>>> be
>>> possible (but with a lot of not easy calculations), the latter would not
>>> be
>>> possible in a standard way.
>>>
>>> -> richard
>>>
>>>
>

Re: Searching class spaces - noob

Posted by "Richard S. Hall" <he...@ungoverned.org>.
Bhaskar Maddala wrote:
> Richard : I am not entirely certain I understand the distinction
> between logical bundle and physical JAR (I get physical JAR but have
> no idea what a logical bundle means - noob in subject). As a second
> attempt in case I find the information I get from public classes to be
> insufficient, I might try to do the "lot of not easy calculations",
> would you (or anyone else) care to point me to resources I can look up
> to understand what would need to be done.
>   

Do you want to know that org.foo.FooClass comes from Bundle 15 (i.e., 
the installed bundle with bundle ID 15) or do you want to know that it 
came from /home/myhome/.felix/myprofile/bundle15/version0.0/bundle.jar ?

-> richard

> Thanks
> Bhaskar
>
> On Thu, Jun 5, 2008 at 12:12 AM, Richard S. Hall <he...@ungoverned.org> wrote:
>   
>> Niclas Hedhman wrote:
>>     
>>> On Thursday 05 June 2008 04:44, Bhaskar Maddala wrote:
>>>
>>>       
>>>> Question : Does anyone know of a way to search class spaces for the
>>>> location (bundle/jar file) from which a specified class is being
>>>> loaded?
>>>>
>>>>         
>>> If you are talking about Exported/Imported (i.e. public classes) packages,
>>> then you should be able to retrieve that info very easily from the Package
>>> Admin service.
>>>
>>> For private packages, the answer from Richard seems to reflect(!) reality,
>>> and will be framework dependent.
>>>       
>> Also, I guess my answer depends on what you mean by "location". If by
>> "location" you mean the actual provider (i.e., the logical bundle) of the
>> class or you mean the physical JAR file in the system. The former would be
>> possible (but with a lot of not easy calculations), the latter would not be
>> possible in a standard way.
>>
>> -> richard
>>
>>     

Re: Searching class spaces - noob

Posted by Bhaskar Maddala <ma...@gmail.com>.
Thank you Niclas and Richard for the responses.

Niclas : I think for the initial attempt I think I would like to have
the public classes (Exported/Imported packages), and I can code
something that retrieves this from PackageAdmin, that should solve 80%
of my requirements.

Richard : I am not entirely certain I understand the distinction
between logical bundle and physical JAR (I get physical JAR but have
no idea what a logical bundle means - noob in subject). As a second
attempt in case I find the information I get from public classes to be
insufficient, I might try to do the "lot of not easy calculations",
would you (or anyone else) care to point me to resources I can look up
to understand what would need to be done.

Thanks
Bhaskar

On Thu, Jun 5, 2008 at 12:12 AM, Richard S. Hall <he...@ungoverned.org> wrote:
>
>
> Niclas Hedhman wrote:
>>
>> On Thursday 05 June 2008 04:44, Bhaskar Maddala wrote:
>>
>>>
>>> Question : Does anyone know of a way to search class spaces for the
>>> location (bundle/jar file) from which a specified class is being
>>> loaded?
>>>
>>
>> If you are talking about Exported/Imported (i.e. public classes) packages,
>> then you should be able to retrieve that info very easily from the Package
>> Admin service.
>>
>> For private packages, the answer from Richard seems to reflect(!) reality,
>> and will be framework dependent.
>
> Also, I guess my answer depends on what you mean by "location". If by
> "location" you mean the actual provider (i.e., the logical bundle) of the
> class or you mean the physical JAR file in the system. The former would be
> possible (but with a lot of not easy calculations), the latter would not be
> possible in a standard way.
>
> -> richard
>

Re: Searching class spaces - noob

Posted by "Richard S. Hall" <he...@ungoverned.org>.

Niclas Hedhman wrote:
> On Thursday 05 June 2008 04:44, Bhaskar Maddala wrote:
>   
>> Question : Does anyone know of a way to search class spaces for the
>> location (bundle/jar file) from which a specified class is being
>> loaded?
>>     
>
> If you are talking about Exported/Imported (i.e. public classes) packages, 
> then you should be able to retrieve that info very easily from the Package 
> Admin service.
>
> For private packages, the answer from Richard seems to reflect(!) reality, and 
> will be framework dependent.

Also, I guess my answer depends on what you mean by "location". If by 
"location" you mean the actual provider (i.e., the logical bundle) of 
the class or you mean the physical JAR file in the system. The former 
would be possible (but with a lot of not easy calculations), the latter 
would not be possible in a standard way.

-> richard

Re: Searching class spaces - noob

Posted by Niclas Hedhman <ni...@hedhman.org>.
On Thursday 05 June 2008 04:44, Bhaskar Maddala wrote:
> Question : Does anyone know of a way to search class spaces for the
> location (bundle/jar file) from which a specified class is being
> loaded?

If you are talking about Exported/Imported (i.e. public classes) packages, 
then you should be able to retrieve that info very easily from the Package 
Admin service.

For private packages, the answer from Richard seems to reflect(!) reality, and 
will be framework dependent.


Cheers
-- 
Niclas Hedhman, Software Developer

I  live here; http://tinyurl.com/2qq9er
I  work here; http://tinyurl.com/2ymelc
I relax here; http://tinyurl.com/2cgsug