Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse Platform » Absolute path of file in bundle classpath
Absolute path of file in bundle classpath [message #529379] Sat, 24 April 2010 10:20 Go to next message
Kai Schlamp is currently offline Kai SchlampFriend
Messages: 344
Registered: July 2009
Senior Member
Hello.

I have to write an OSGi bundle/Eclipse plugin where some parts of the code can also be executed in normal Java SDK environment.
The problem is that inside my code are some resources that I need the absolute path to. So I need to fetch that path without the use of OSGi or Eclipse stuff.

This here works fine ... but I need the path, not a stream:
InputStream in = MaxMatcher.class.getClassLoader().getResourceAsStream("configure.xml ");
in.read()

But this fails ... I don't get the real path, just "/configure.xml":
URL url = MaxMatcher.class.getClassLoader().getResource("configure.xml ");
File f = new File(url.toURI()); // already throws an exception here
f.getAbsolutePath());

Any suggestions

Regards,
Kai
Re: Absolute path of file in bundle classpath [message #529387 is a reply to message #529379] Sat, 24 April 2010 12:41 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33133
Registered: July 2009
Senior Member
Kai,

Comments below.

Kai Schlamp wrote:
> Hello.
>
> I have to write an OSGi bundle/Eclipse plugin where some parts of the
> code can also be executed in normal Java SDK environment.
> The problem is that inside my code are some resources that I need the
> absolute path to. So I need to fetch that path without the use of OSGi
> or Eclipse stuff.
I don't see how this conclusion follows from the statement about needing
an absolute path.
>
> This here works fine ... but I need the path, not a stream:
> InputStream in =
> MaxMatcher.class.getClassLoader().getResourceAsStream("configure.xml ");
> in.read()
>
> But this fails ... I don't get the real path, just "/configure.xml":
> URL url = MaxMatcher.class.getClassLoader().getResource("configure.xml
> ");
> File f = new File(url.toURI()); // already throws an exception here
> f.getAbsolutePath());
What kind of path are you hoping to get? Shouldn't you expect to get a
path into a jar in general? It seems to me that you'll need to use
FileLocator.toFileURL.
>
> Any suggestions
>
> Regards,
> Kai


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Absolute path of file in bundle classpath [message #530819 is a reply to message #529387] Sat, 01 May 2010 10:52 Go to previous messageGo to next message
Kai Schlamp is currently offline Kai SchlampFriend
Messages: 344
Registered: July 2009
Senior Member
Hy Ed,

I took a closer look at my problem (and the external framework I'd like to integrate in my plugin project).
The external framework expects that myObject.class.getClassLoader().getResource("sample.dat").toURI() does not
return a bundleresource ("bundleresource:..."), but a file ("file:..."). The sample.dat won't be in a jar, but in an external directory on the classpath.
Is there a way to force the plugin to use the normal Java application classloader (not sure about the correct name)? Maybe a Manifest entry for that task?

Regards,
Kai
Re: Absolute path of file in bundle classpath [message #530836 is a reply to message #530819] Sat, 01 May 2010 15:20 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33133
Registered: July 2009
Senior Member
Kai,

Specialized class loaders are key to how OSGi supports modularization
into bundles so I don't think there is a way to force a regular class
loader into the picture.


Kai Schlamp wrote:
> Hy Ed,
>
> I took a closer look at my problem (and the external framework I'd
> like to integrate in my plugin project).
> The external framework expects that
> myObject.class.getClassLoader().getResource("sample.dat").toURI() does
> not
> return a bundleresource ("bundleresource:..."), but a file
> ("file:..."). The sample.dat won't be in a jar, but in an external
> directory on the classpath.
> Is there a way to force the plugin to use the normal Java application
> classloader (not sure about the correct name)? Maybe a Manifest entry
> for that task?
>
> Regards,
> Kai
>


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Absolute path of file in bundle classpath [message #530838 is a reply to message #530836] Sat, 01 May 2010 16:12 Go to previous messageGo to next message
Kai Schlamp is currently offline Kai SchlampFriend
Messages: 344
Registered: July 2009
Senior Member
Ed Merks wrote on Sat, 01 May 2010 11:20
Kai,
Specialized class loaders are key to how OSGi supports modularization
into bundles so I don't think there is a way to force a regular class
loader into the picture.



Ed,

I absolutely agree with you. But for some rare compatibility reasons, it would be nice to have a switch (maybe in the Manifest) to turn that feature on for a certain Plug-In. Otherwise such external libraries (like the one I am trying to integrate) can't be part of a Plug-In.
Re: Absolute path of file in bundle classpath [message #530874 is a reply to message #530838] Sun, 02 May 2010 09:43 Go to previous messageGo to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 33133
Registered: July 2009
Senior Member
Kai,

I doubt that makes sense given the plugin has to run in a well behaved
way within OSGi. It might be a little like wanting pi to be a rational
number, preferably equal to 3, because it makes the math so much easier...


Kai Schlamp wrote:
> Ed Merks wrote on Sat, 01 May 2010 11:20
>> Kai,
>> Specialized class loaders are key to how OSGi supports modularization
>> into bundles so I don't think there is a way to force a regular class
>> loader into the picture.
>
>
> Ed,
>
> I absolutely agree with you. But for some rare compatibility reasons,
> it would be nice to have a switch (maybe in the Manifest) to turn that
> feature on for a certain Plug-In. Otherwise such external libraries
> (like the one I am trying to integrate) can't be part of a Plug-In.


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: Absolute path of file in bundle classpath [message #530919 is a reply to message #530874] Sun, 02 May 2010 19:04 Go to previous messageGo to next message
Kai Schlamp is currently offline Kai SchlampFriend
Messages: 344
Registered: July 2009
Senior Member
Ed,

this is indeed what a calculator does ... it only uses the first 10 digits of Pi to make calculations possible Razz
Re: Absolute path of file in bundle classpath [message #531016 is a reply to message #529379] Mon, 03 May 2010 12:00 Go to previous message
Paul Webster is currently offline Paul WebsterFriend
Messages: 6859
Registered: July 2009
Location: Ottawa
Senior Member

Kai Schlamp wrote:
> Hello.
>
> I have to write an OSGi bundle/Eclipse plugin where some parts of the
> code can also be executed in normal Java SDK environment.
> The problem is that inside my code are some resources that I need the
> absolute path to. So I need to fetch that path without the use of OSGi
> or Eclipse stuff.

First let me say: Bad 3rdparty library! seriously, to have a URL and
then switch to a File instead of just using InputStream. Not mock
testable is the least of their problems :-)

But as for integrating into eclipse, you are limited to parts of the
classpath you can change.

Option 1: you do the work. Load your 3rd party jar in a URLClassLoader
that also contains your extra directory. This option will involve a lot
of hand waving to get it correct.

Option 2: Generate a configuration plugin on the fly:
config plugin - config - config.txt
- META-INF - MANIFEST.MF
export config, and add an option Require-Bundle on the config plugin
from the plugin where you are managing your 3rd party code. When you
load config/config.txt it should appear (well, mostly :-)

Option 3: use buddy classloading and modify framework properties. It is
possible to specify different classloaders be available to the framework
or packages [1]. Then you use the buddy classloading policy to open up
your eclipse plugin to some of the other classpaths [2].




[1]
http://help.eclipse.org/galileo/index.jsp?topic=/org.eclipse .platform.doc.isv/reference/misc/runtime-options.html

[2]
http://help.eclipse.org/galileo/index.jsp?topic=/org.eclipse .platform.doc.isv/reference/misc/buddy_loading.html

--
Paul Webster
http://wiki.eclipse.org/Platform_Command_Framework
http://wiki.eclipse.org/Command_Core_Expressions
http://wiki.eclipse.org/Menu_Contributions
http://wiki.eclipse.org/Menus_Extension_Mapping
http://help.eclipse.org/galileo/index.jsp?topic=/org.eclipse .platform.doc.isv/guide/workbench.htm


Previous Topic:P2 and Feature Patch
Next Topic:how can i minimize a specific editor
Goto Forum:
  


Current Time: Thu Apr 18 08:19:52 GMT 2024

Powered by FUDForum. Page generated in 0.01948 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top