You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@servicemix.apache.org by Sergey Shcherbakov <ss...@echelon.de> on 2009/05/22 15:37:13 UTC

Get a filename of the resource referenced by the maven url

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.

 


RE: Get a filename of the resource referenced by the maven url

Posted by Sergey Shcherbakov <ss...@echelon.de>.
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:sshcherbakov@echelon.de] 
Sent: Monday, May 25, 2009 6:48 PM
To: users@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:sshcherbakov@echelon.de] 
Sent: Friday, May 22, 2009 3:56 PM
To: users@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:jb@nanthrax.net] 
Sent: Friday, May 22, 2009 3:46 PM
To: users@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.
> 
>  
> 
> 

RE: Get a filename of the resource referenced by the maven url

Posted by Sergey Shcherbakov <ss...@echelon.de>.
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:sshcherbakov@echelon.de] 
Sent: Friday, May 22, 2009 3:56 PM
To: users@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:jb@nanthrax.net] 
Sent: Friday, May 22, 2009 3:46 PM
To: users@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.
> 
>  
> 
> 

RE: Get a filename of the resource referenced by the maven url

Posted by Sergey Shcherbakov <ss...@echelon.de>.
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:jb@nanthrax.net] 
Sent: Friday, May 22, 2009 3:46 PM
To: users@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.
> 
>  
> 
> 

Re: Get a filename of the resource referenced by the maven url

Posted by Jean-Baptiste Onofré <jb...@nanthrax.net>.
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.
> 
>  
> 
>