You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@felix.apache.org by Vinicius Carvalho <vi...@caravelatech.com> on 2009/02/03 17:31:32 UTC

Re: Starting out with OSGi

Hello there! Rob following your instructions when we try to load a resource:

httpService.registerResource("/OSLApplication.html","/OSLApplication.html",
webcontext); //This is the GWT entry-point

And when the method from webcontext is finally called when the user points
to localhost:8080/myapp/OSLApplication.html:

public URL getResource(String name) {
        // TODO Auto-generated method stub
        return GWTService.class.getResource("/resources/www/"+baseURI+name);
    }

We are always getting null

We tried:

"resources/www/"+baseURI+name

And also did not work

We tried using
GWTService.class.getClassLoader.getResource...
even
Thread.currentThread().getContextClassLoader().getResource

It always returns null.

The jar is packaged:

org/
resources/
META-INF

But it seems that resources/ is not being loaded by the class loader, do we
need any special directive on the MANIFEST?

Regards


>

Re: Starting out with OSGi

Posted by Vinicius Carvalho <vi...@caravelatech.com>.
Yeah, they have different classloaders :(

Well, one "easy" way out was to move resources/www to the
WAR/WEB-INF/classes



It now does load the OSLApplication.html, problem is the other resources...
like

 <script type="text/javascript" language="javascript"
src="org.openspotlight.OSLApplication.nocache.js"></script>

  </head>

not counting the images and other static resources. Do you need to register
each one of them?

Regards

On Tue, Feb 3, 2009 at 3:09 PM, Stuart McCulloch <mc...@gmail.com> wrote:

> 2009/2/4 Vinicius Carvalho <vi...@caravelatech.com>
>
> > Hello there! Rob following your instructions when we try to load a
> > resource:
> >
> >
> httpService.registerResource("/OSLApplication.html","/OSLApplication.html",
> > webcontext); //This is the GWT entry-point
> >
> > And when the method from webcontext is finally called when the user
> points
> > to localhost:8080/myapp/OSLApplication.html:
> >
> > public URL getResource(String name) {
> >         // TODO Auto-generated method stub
> >        return
> GWTService.class.getResource("/resources/www/"+baseURI+name);
> >    }
> >
> > We are always getting null
> >
> > We tried:
> >
> > "resources/www/"+baseURI+name
> >
> > And also did not work
> >
> > We tried using
> > GWTService.class.getClassLoader.getResource...
> > even
> > Thread.currentThread().getContextClassLoader().getResource
> >
>
> Hi Vinicius,
>
> have you tried printing out:
>
>  GWTService.class.getClassLoader()   and
> Thread.currentThread().getContextClassLoader()
>
> to see if these match the classloader of the bundle with the resource?
> ( most OSGi frameworks put the bundle id in the classloader name )
>
> if you're using the same classloader as the bundle with the resource
> then it should find it (just like a normal jar) - otherwise there are OSGi
> APIs that can load resources from Bundle instances, for situations
> where you don't know what the bundle classloader is but you know
> the Bundle (note that you can't get the classloader instance directly
> from the Bundle, as that would be too tempting!)
>
> if you want to share a resource with other bundles then you need to add
> exports and imports, just as with classes - if you have several bundles
> providing resources in the same package then you might want to look
> at the extender pattern, rather than attempt to merge these packages
> together (because by default OSGi doesn't merge package contents -
> instead it picks one bundle to be the 'exporter' and uses the package
> contents from this bundle - to merge package contents you need to
> use advanced techniques like Require-Bundle...)
>
> [ http://www.osgi.org/blog/2007/02/osgi-extender-model.html ]
>
> HTH
>
> It always returns null.
> >
> > The jar is packaged:
> >
> > org/
> > resources/
> > META-INF
> >
> > But it seems that resources/ is not being loaded by the class loader, do
> we
> > need any special directive on the MANIFEST?
> >
> > Regards
> > >
> >
>
> --
> Cheers, Stuart
>

Re: Starting out with OSGi

Posted by Stuart McCulloch <mc...@gmail.com>.
2009/2/4 Vinicius Carvalho <vi...@caravelatech.com>

> Hello there! Rob following your instructions when we try to load a
> resource:
>
> httpService.registerResource("/OSLApplication.html","/OSLApplication.html",
> webcontext); //This is the GWT entry-point
>
> And when the method from webcontext is finally called when the user points
> to localhost:8080/myapp/OSLApplication.html:
>
> public URL getResource(String name) {
>         // TODO Auto-generated method stub
>        return GWTService.class.getResource("/resources/www/"+baseURI+name);
>    }
>
> We are always getting null
>
> We tried:
>
> "resources/www/"+baseURI+name
>
> And also did not work
>
> We tried using
> GWTService.class.getClassLoader.getResource...
> even
> Thread.currentThread().getContextClassLoader().getResource
>

Hi Vinicius,

have you tried printing out:

  GWTService.class.getClassLoader()   and
Thread.currentThread().getContextClassLoader()

to see if these match the classloader of the bundle with the resource?
( most OSGi frameworks put the bundle id in the classloader name )

if you're using the same classloader as the bundle with the resource
then it should find it (just like a normal jar) - otherwise there are OSGi
APIs that can load resources from Bundle instances, for situations
where you don't know what the bundle classloader is but you know
the Bundle (note that you can't get the classloader instance directly
from the Bundle, as that would be too tempting!)

if you want to share a resource with other bundles then you need to add
exports and imports, just as with classes - if you have several bundles
providing resources in the same package then you might want to look
at the extender pattern, rather than attempt to merge these packages
together (because by default OSGi doesn't merge package contents -
instead it picks one bundle to be the 'exporter' and uses the package
contents from this bundle - to merge package contents you need to
use advanced techniques like Require-Bundle...)

[ http://www.osgi.org/blog/2007/02/osgi-extender-model.html ]

HTH

It always returns null.
>
> The jar is packaged:
>
> org/
> resources/
> META-INF
>
> But it seems that resources/ is not being loaded by the class loader, do we
> need any special directive on the MANIFEST?
>
> Regards
> >
>

-- 
Cheers, Stuart

Re: Starting out with OSGi

Posted by Rob Walker <ro...@ascert.com>.
Vinicius

First part I'd check is whether you really mean this:

    httpService.registerResource("/OSLApplication.html","/OSLApplication.html",webcontext);
    //This is the GWT entry-point

I think that means all names will get resolved to a base alias of 
"/OSLApplication.html" - I wonder whether you really want the ".html". I 
suspect what you really want is something like:

    httpService.registerResource("/com.mycompany.myapp.OSLApplication","/OSLApplication",webcontext);
    //This is the GWT entry-point

This will ensure your alias starts at the "root" of your application 
resources, and will let you resolve any and all resources including the 
.html "index page"

Your 2nd part of resource resolving looks pretty much the same as ours:

                    retVal = 
GwtServlet.class.getClassLoader().getResource("resources/www" + baseUri 
+ name);

Note that we have no leading / though - it's just /resources"

I would suggest putting a System.out trace statement in your routine to 
see what the "name" param value is when it comes in.

You need to make sure that the String you construct:

    resources/www" + baseUri + name

matches exactly the path to the resource in your bundle JAR. It could be 
that the "name" param coming in may have something like a leading / or 
some other unexpected prefix. We have to do a couple of "massages" to 
the received name in our model on the way in:

        public URL getResource(String name)
        {
            // makes sure a "/" or empty URL resolves to our index page
            if (name.equals("/") || name.length() == 0)
            {
                name = indexPage;
            }
           
            //Not sure why we need this - but under Jetty6/GWT1.5.2 there
            //seems to be a direct call with alias still attached
            if (name.startsWith(alias + "/"))
            {
                name = name.substring(alias.length());
            }


Also note - that the "resource" you're looking for *must* be in the same 
bundle JAR as your GWTService class.

resources/www/<your URI base>/<your name>

I found that putting copious trace statements in the resource handling 
code helped me get over the initial hump of things not resolving.

Regards

-- Rob

Vinicius Carvalho wrote:
>
> And when the method from webcontext is finally called when the user points
> to localhost:8080/myapp/OSLApplication.html:
>
> public URL getResource(String name) {
>         // TODO Auto-generated method stub
>         return GWTService.class.getResource("/resources/www/"+baseURI+name);
>     }
>
> We are always getting null
>
> We tried:
>
> "resources/www/"+baseURI+name
>
> And also did not work
>
> We tried using
> GWTService.class.getClassLoader.getResource...
> even
> Thread.currentThread().getContextClassLoader().getResource
>
> It always returns null.
>
> The jar is packaged:
>
> org/
> resources/
> META-INF
>
> But it seems that resources/ is not being loaded by the class loader, do we
> need any special directive on the MANIFEST?
>
> Regards
>
>
>   
>
>   

-- 


Ascert - Taking systems to the Edge
robw@ascert.com
+44 (0)20 7488 3470
www.ascert.com