Sorry for the false alarm.
Looks like the usual way works for me now:

URL url = new URL(id);
return url.openStream();


Best Regards,
Sergey Shcherbakov.

-----Original Message-----
From: Sergey Shcherbakov [mailto:sshcherba...@echelon.de] 
Sent: Tuesday, May 26, 2009 10:17 AM
To: users@felix.apache.org
Subject: Get a filename of the resource referenced by the maven url

Hi all,

I would like to ask the question below here in the Felix user list.

How can I make use of the Felix URL protocol handling from within application 
that embeds Felix instance?

Below is a code snippet which shows how I could achieve that meanwhile. But I 
believe there should be more elegant way to do the same.


Best Regards,
Sergey Shcherbakov.

-----Original Message-----
From: Sergey Shcherbakov [mailto:sshcherba...@echelon.de] 
Sent: Monday, May 25, 2009 6:48 PM
To: us...@servicemix.apache.org
Subject: RE: Get a filename of the resource referenced by the maven url

Here is a way I found it possible to use PAX Url to solve my problem:

...
        urlServiceTracker = new ServiceTracker(
                hostActivator.getContext(), 
URLStreamHandlerService.class.getName(), null);
        urlServiceTracker.open();

...

        public InputStream getResource(String id) {

                // id = "mvn:my.group.id/my_artifact/0.0.1-SNAPSHOT/zip/bin";
                try{
                        URL url = new URL(id);
                        Object[] services = urlServiceTracker.getServices();
                        URLConnection conn = null;
                        for( Object service : services ){
                                try{
                                        URLStreamHandlerService shs = 
(URLStreamHandlerService) service;
                                        conn = shs.openConnection(url);
                                        break;
                                }
                                catch(MalformedURLException ex){
                                }
                        }
                        if( conn != null )
                                return conn.getInputStream();
                }
                catch(IOException ex){
                        ex.printStackTrace();
                }
                catch(Throwable t){
                        t.printStackTrace();
                }
                return null;
        }

Isn't there more elegant way to use PAX Url except iterating through the 
available URLStreamHandlerService implementations in order to find one that 
supports needed protocol?

In the PAX Url package I could not find Handler.class that I could use to make 
java.net.URL to work automatically with different protocols. 
Thus, I have ended up by the iteration loop :(

Can anybody recommend better approach?

Best Regards,
Sergey Shcherbakov.

-----Original Message-----
From: Sergey Shcherbakov [mailto:sshcherba...@echelon.de] 
Sent: Friday, May 22, 2009 3:56 PM
To: us...@servicemix.apache.org
Subject: RE: Get a filename of the resource referenced by the maven url

Hi Jean-Baptiste,

Eh.. actually not on the class path but in the local maven folder (I am storing 
non-java resources in the maven-like structured folder layout using Maven and 
Ivy).
But in principle this is the same:
classpath:/mybundle/file.conf
or
mvn:myorg/myartifact/version/type/classifier
If I'd know how to get resource using one URL I would know how to get it using 
another.

The link you are referring to is the same as I was talking about. It doesn't 
contain examples of getting resources using URLs programmatically.

Best Regards,
Sergey Shcherbakov.


-----Original Message-----
From: Jean-Baptiste Onofré [mailto:j...@nanthrax.net] 
Sent: Friday, May 22, 2009 3:46 PM
To: us...@servicemix.apache.org
Subject: Re: Get a filename of the resource referenced by the maven url

Hi Sergey,

I don't know if I have right understood your needs :).

I guess that you need to read a file located in your application 
classpath, correct ?

In this case, using PAX, you can use classpath protocol like this:

classpath:/mybundle/file.conf

You can find some documentation here:
http://wiki.ops4j.org/display/paxurl/Classpath+Protocol

Regards
JB

Sergey Shcherbakov wrote:
> Hello all,
> 
>  
> 
> I am very new to OSGi and Servicemix.
> 
>  
> 
> Let's say, I have a running Servicemix instance. A [local] maven
> repository can be and is usually used to find resources required by
> dependencies etc.
> 
>  
> 
> From my bundle or better to say from my application that embeds
> Servicemix I need to figure out a full file name of the resource
> referenced by the maven url (mvn:...).
> 
>  
> 
> That is, I have an URL string that uses maven protocol and want to get a
> local file name of the artifact referenced by this URL or even an input
> stream representing that resource (that would be OK too).
> 
>  
> 
> I believe this is possible with help of the Servicemix preinstalled
> bundle services.
> 
>  
> 
> Could you please give a hint in which direction should I look?
> 
>  
> 
> In the out of the box running Servicemix instance I have found several
> bundles that provide 
> 
> org.osgi.service.url.URLStreamHandlerService
> 
>  
> 
> Do I need a maven implementation one of them? This interface requires a
> further a reference to the org.osgi.service.url.URLStreamHandlerSetter
> implementation. Should I implement it by myself?
> 
> Is there a more simple way to convert maven URL into the local file
> system path?
> 
>  
> 
> The PAX documentation
> 
> http://wiki.ops4j.org/display/paxurl/Mvn+Protocol
> 
> is very brief and I could not find examples of how to use it.
> 
>  
> 
> The way I found to be recommended for use from inside bundles:
> 
>  
> 
> URL url = new URL(s);
> 
> InputStream in = url.openStream();
> 
>  
> 
> Doesn't work for me since I have an embedded Servicemix instance.
> 
> I have only a Felix, BundleContext and a ServiceTracker references and
> the code above throws "unknown protocol" Runtime exception.
> 
>  
> 
> Thank you!
> 
>  
> 
> Best Regards,
> 
> Sergey Shcherbakov.
> 
>  
> 
> 

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@felix.apache.org
For additional commands, e-mail: users-h...@felix.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@felix.apache.org
For additional commands, e-mail: users-h...@felix.apache.org

Reply via email to