On 26/12/2007, David Leangen <[EMAIL PROTECTED]> wrote:
>
> On Wed, 2007-12-26 at 13:48 +0800, Stuart McCulloch wrote:
> > On 26/12/2007, David Leangen <[EMAIL PROTECTED]> wrote:
> >
> >         Hiya,
> >
> >         Is there a way to import a class into a bundle at runtime when
> >         the
> >         imported class is not declared as imported in the manifest?
> >
> >         I want to be able to use and work with certain classes without
> >         knowing
> >         what they are at build time.
> >
> > have you looked at using DynamicImport-Package?  (section 3.8.2 of R4
> > spec)
> >
> >
> > you can use wildcards, so you can load classes without knowing the
> > details in
> > advance (ie. they are resolved when needed, not when the bundle is
> > resolved)
>
> Thanks, Stuart.
>
> Yeah, I had looked at that section.
>
>
> So, are you saying that the only way to do this is to declare something
> in the manifest (possibly a wildcard dynamic import)?


Yes, for example:

   DynamicImport-Package: org.foo.*, com.bar.*

or to access anything provided by other bundles:

   DynamicImport-Package: *

Note that you don't have to use wildcards in DynamicImport-Package. You
could
list exact packages - say if you already know them, but they're optional
imports.
You can also add various attributes and constraints to fine-tune the
results.

BTW, for the explicit case you could also use the resolution:=optional
attribute:

   Import-Package: org.foo.bar;resolution:=optional

which will only add a wire during the bundle resolution (whereas a dynamic
import
will continue to check each time it's needed, as long as the import is
unsatisfied)

There's no runtime
> method for importing a single class at runtime?


Well it's still Java so if you can find a classloader that can provide the
class then
you can load it. For example, you could create your own custom classloader
that
streamed classes off the internet - but then you're back to the non-OSGi
world,
with all the potential management problems...

I didn't notice anything in the api or the spec, but was just wondering
> if there was some kind of trick somewhere...


tricks aren't always good ;)  if you take a step back and explain 'why' you
want to
import a single, not-known-before-hand, unversioned class into your
application
then it would help others suggest solutions

HTH

Thanks!
> David
>
>
>
> _______________________________________________
> OSGi Developer Mail List
> [email protected]
> http://www2.osgi.org/mailman/listinfo/osgi-dev
>



-- 
Cheers, Stuart
_______________________________________________
OSGi Developer Mail List
[email protected]
http://www2.osgi.org/mailman/listinfo/osgi-dev

Reply via email to