Skip to main content



      Home
Home » Eclipse Projects » Equinox » Read configuration file in the bundle
Read configuration file in the bundle [message #93340] Mon, 30 July 2007 17:01 Go to next message
Eclipse UserFriend
Hi all,

I've read in Knopflerfish forum that it's possible to read a config file,
that it's compressed in the bundle, with the following code:
FileInputStream is = (FileInputStream)
getClass().getResourceAsStream("/cfg/file.properties");
where the bundle structure is:
/META-INF
MANIFEST.MF
/cfg
file.properties
"CLASSES"

However, I'm using Equinox and the previous sentence always returns null.
Perhaps it's because I'm debugging with Equinox and the .JAR file (of the
bundle) is not generated yet? (I don't think so).

How could I read a file inside my bundle?.

Thanks,
Jaime
Re: Read configuration file in the bundle [message #93350 is a reply to message #93340] Mon, 30 July 2007 18:18 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: jacek.pospychala.pl.ibm.com

Jaime,
I think that getResourceAsStream actually looks at classpath so unless
your classpath includes "." (bundle root) it won't work.
Also getClass() must be called on a class that comes from bundle you are
trying to access.
Instead you could try Bundle.getEntry("/cfg/file.properties")

Jaime wrote:
> Hi all,
>
> I've read in Knopflerfish forum that it's possible to read a config file,
> that it's compressed in the bundle, with the following code:
> FileInputStream is = (FileInputStream)
> getClass().getResourceAsStream("/cfg/file.properties");
> where the bundle structure is:
> /META-INF
> MANIFEST.MF
> /cfg
> file.properties
> "CLASSES"
>
> However, I'm using Equinox and the previous sentence always returns null.
> Perhaps it's because I'm debugging with Equinox and the .JAR file (of the
> bundle) is not generated yet? (I don't think so).
>
> How could I read a file inside my bundle?.
>
> Thanks,
> Jaime
>
>
>
Re: Read configuration file in the bundle [message #93363 is a reply to message #93350] Tue, 31 July 2007 03:30 Go to previous messageGo to next message
Eclipse UserFriend
Thanks Jacek,

The problem was the . classpath. It works after including:
Bundle-Classpath: .

Best regards,
Jaime
Re: Read configuration file in the bundle [message #93505 is a reply to message #93363] Tue, 31 July 2007 15:43 Go to previous messageGo to next message
Eclipse UserFriend
The default Bundle-Classpath is '.' if you do not define one so I am
surprised that fixed the problem for you. Could you open a bug against
Equinox->Framework and attach a sample project to reproduce?

With that said I would still use Bundle.getEntry in this case to get URL
to the resource. This avoids any classpath confusion. Also in your
original post you casted the inputstream to a FileInputStream. This
will not work when the bundle is jar'ed because the input stream will
come from a ZipFile.

Tom.
Re: Read configuration file in the bundle [message #93520 is a reply to message #93505] Tue, 31 July 2007 16:58 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: alex_blewitt.yahoo.com

Benefits of using getResourceAsStream() over Bundle.getEntry() include (a) being able to load the config file outside of an OSGi environment as well as within, and (b) being able to load the config from a dependent bundle or contributed fragment. In fact, I'd go as far as saying that Bundle.getEntry() is only particularly useful in writing tools such as the Equinox Extension Registry which explicitly needs to state which bundle a resource comes from.

Alex.
Re: Read configuration file in the bundle [message #93601 is a reply to message #93520] Wed, 01 August 2007 03:59 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: alex_blewitt.yahoo.com

See for example http://www.eclipsezone.com/eclipse/forums/t99588.html which would be impossible to do if the caller used Bundle.getEntry().

Alex.
Re: Read configuration file in the bundle [message #93656 is a reply to message #93505] Wed, 01 August 2007 12:04 Go to previous messageGo to next message
Eclipse UserFriend
Hi Tom,

The real thing is that I already had a "Bundle-Classpath" header but without
the ".". I thought that it was always included, but it's not. So, there's no
need to add a bug.

Besides, I disagree with the Bundle.getEntry. I try to develop my code as
much independent as possible, and Bundle binds that part of the code to
OSGi. So I prefer the use of ResourceStream.

Jaime.

"Tom Watson" <tjwatson@us.ibm.com> escribi
Re: Read configuration file in the bundle [message #93716 is a reply to message #93656] Wed, 01 August 2007 13:23 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: alex_blewitt.yahoo.com

Yeah, you only get the default of '.' if you don't have a Bundle-ClassPath header. As soon as you provide one, you get whatever you ask for :-)

Alex.
Re: Read configuration file in the bundle [message #93730 is a reply to message #93656] Wed, 01 August 2007 15:44 Go to previous message
Eclipse UserFriend
> Besides, I disagree with the Bundle.getEntry. I try to develop my code as
> much independent as possible, and Bundle binds that part of the code to
> OSGi. So I prefer the use of ResourceStream.
>

OK, that is a fine reason. I would suggest that you place the
configuration resource in the same source folder as your code. In your
example it seemed like your config file was at the root of your bundle
and your code was in an inner library jar which was included on the
Bundle-ClassPath. Since '.' was not on the classpath your code could
not find the resource.

As a best practice bundles should try not having any inner jars and
place all their code at the root of the bundle so that the default '.'
Bundle-ClassPath can be used. Inner jars have a number of draw backs.
For example, if you deliver your bundle as a jar'ed bundle then the
inner jar must be extracted at runtime to load classes. Currently
PDE/JDT/javac cannot handle inner jars in jar'ed bundles when compiling
clients that use packages exported by your bundle. If your bundle is
always delivered as an "expanded" bundle then this is not such an issue.
Note that "expanded" bundles are not really standard in OSGi but are
supported in Equinox for backwards compatibility reasons and for
self-hosting in a development environment.

Tom
Previous Topic:Configuration Admin
Next Topic:Does Bundle-NativeCode support external dlls?
Goto Forum:
  


Current Time: Sun Jun 08 16:22:48 EDT 2025

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

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

Back to the top