You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@karaf.apache.org by Milan Tomic <to...@yahoo.com> on 2014/08/08 14:04:20 UTC

Karaf client

Hello,

I am total beginer with OSGi and Karaf and I am begging here for some help.


I have followed one tutorial and created one OSGi module, packet it in JAR and installed it in Karaf with hot deploy:

public interface ParserService {
    String parse(String s);
}

public class ParserImpl implements ParserService {
    public String parse(String s) {
        return s.replaceAll("AA", "BB");
    }
 }

Now I have no idea how to call this service from outside of Karaf. Let's say from an Servlet running inside some Tomcat on the same Windows Server maching. So, how do you create OSGi/Karaf client app?


Thank you in advance,
Milan

Re: Karaf client

Posted by Kevin Schmidt <kt...@gmail.com>.
Milan,

The short answer is that if you want to call this from outside of Karaf,
the easiest way to do it is to expose it as a REST or SOAP Web service
using CXF and call it that way.

But, if you want to call it from a servlet, why not run that servlet in
Karaf?  Then you can make the call as a native method call inside Karaf by
either just using the class or registering ParserImpl as an OSGi service
and performing a service lookup from the Servlet to get a handle on that
service.

Kevin


On Fri, Aug 8, 2014 at 5:04 AM, Milan Tomic <to...@yahoo.com> wrote:

> Hello,
>
> I am total beginer with OSGi and Karaf and I am begging here for some help.
>
> I have followed one tutorial and created one OSGi module, packet it in JAR
> and installed it in Karaf with hot deploy:
>
> public interface ParserService {
>     String parse(String s);
> }
>
> public class ParserImpl implements ParserService {
>     public String parse(String s) {
>         return s.replaceAll("AA", "BB");
>     }
>  }
>
> Now I have no idea how to call this service from outside of Karaf. Let's
> say from an Servlet running inside some Tomcat on the same Windows Server
> maching. So, how do you create OSGi/Karaf client app?
>
> Thank you in advance,
> Milan
>

Re: Karaf client

Posted by Jean-Baptiste Onofré <jb...@nanthrax.net>.
Hi Milan,

as you understood, a service is just a interface (and a bean 
implementing this interface).

So a service itself is not available directly from outside. It's up to 
you to expose the service as a servlet, REST service, or whatever.

Regards
JB

On 08/08/2014 02:04 PM, Milan Tomic wrote:
> Hello,
>
> I am total beginer with OSGi and Karaf and I am begging here for some help.
>
> I have followed one tutorial and created one OSGi module, packet it in
> JAR and installed it in Karaf with hot deploy:
>
> public interface ParserService {
>      String parse(String s);
> }
>
> public class ParserImpl implements ParserService {
>      public String parse(String s) {
>          return s.replaceAll("AA", "BB");
>      }
>   }
>
> Now I have no idea how to call this service from outside of Karaf. Let's
> say from an Servlet running inside some Tomcat on the same Windows
> Server maching. So, how do you create OSGi/Karaf client app?
>
> Thank you in advance,
> Milan

-- 
Jean-Baptiste Onofré
jbonofre@apache.org
http://blog.nanthrax.net
Talend - http://www.talend.com

Re: Karaf client

Posted by Jean-Baptiste Onofré <jb...@nanthrax.net>.
I guess that you have a bundle providing MyService interface 
(Export-Package) and another bundle that import the package containing 
the interface (Import-Package).

Check if you don't have MyService two times (embedded instead of imported).

Regards
JB

On 08/11/2014 03:09 PM, Milan Tomic wrote:
> Hi Jean,
>
> Yes, MyService is an interface.
>
> No, I am not using blueprint, but I will search for some example now. :)
>
> Thank you
> Milan
>
>
> On Monday, August 11, 2014 3:05 PM, Jean-Baptiste Onofré
> <jb...@nanthrax.net> wrote:
>
>
> Hi Milan,
>
> MyService is the interface ?
>
> You register the service using blueprint ?
>
> Regards
> JB
>
> On 08/11/2014 02:55 PM, Milan Tomic wrote:
>  >
>  > Thank you very much for your answers. Due to my bug my Activator didn't
>  > activated my service and that was causing problems.
>  >
>  > Now I got this exception:
>  >
>  > ClassCastException: Proxy68687b14_9ba8_4734_b559_b199ee1ac482 cannot be
>  > cast to myPackage.MyService
>  >
>  > when I do this:
>  >
>  > MyService myService = (MyService) ic.lookup("osgi:service/" +
>  > MyService.class.getName());
>  >
>  > what could be a problem?
>  >
>  > Thank you in advance,
>  > Milan
>  >
>  >
>  >
>  >
>  >
>  > On Monday, August 11, 2014 2:50 PM, Milan Tomic <tomicmilan@yahoo.com
> <ma...@yahoo.com>>
>  > wrote:
>  >
>  >
>  >
>  > Oh :) I didn't noticed that I sent this only to you :) I wanted to send
>  > it to the whole user mailing list. Sorry and discard this email because
>  > it is solved already.
>  >
>  > Br, Milan
>  >
>  >
>  > On Monday, August 11, 2014 9:39 AM, Milan Tomic <tomicmilan@yahoo.com
> <ma...@yahoo.com>>
>  > wrote:
>  >
>  >
>  >
>  > Thank you very much for your responses.
>  >
>  > Do I have to register OSGi service with JNDI or all OSGi services
>  > areautoregistered? I am trying this:
>  >
>  > MyService myService = (MyService) ic.lookup("osgi:service/" +
>  > MyService.class.getName());
>  >
>  > and I got this exception:
>  >
>  > javax.naming.NameNotFoundException: osgi:service/mypackage.MyService
>  >
>  > I have already instaled feature:install jndi
>  >
>  > Thank you in advance,
>  > Milan
>  >
>  >
>  >
>  > On Saturday, August 9, 2014 10:03 AM, "chris.gray@kiffer.ltd.uk
> <ma...@kiffer.ltd.uk>"
>  > <chris.gray@kiffer.ltd.uk <ma...@kiffer.ltd.uk>> wrote:
>  >
>  >
>  > Hello Milan,
>  >
>  > You don't say which tutorial you followed: if you use Eclipse then I
>  > suggest you take a look at <http://bndtools.org/tutorial.html>. Step 6
>  > won't work for you because it's designed for a different shell than the
>  > one Karaf uses by default, but you should learn something from the
> earlier
>  > steps.
>  >
>  >  > public interface ParserService {
>  >  >    String parse(String s);
>  >  > }
>  >  >
>  >  > public class ParserImpl implements ParserService {
>  >  >    public String parse(String s) {
>  >  >        return s.replaceAll("AA", "BB");
>  >  >    }
>  >  >  }
>  >
>  > I hope these are two separate files, BTW otherwise I will have to
> send you
>  > to bed without any supper. :)
>  >
>  >
>  >  > Now I have no idea how to call this service from outside of Karaf.
> Let's
>  >  > say from an Servlet running inside some Tomcat on the same Windows
> Server
>  >  > maching. So, how do you create OSGi/Karaf client app?
>  >
>  >
>  > At this stage you can't even call the service from *inside* Karaf,
> because
>  > you didn't register it with the framework. There are many ways to do
> this,
>  > from the painstakingly manual to the mysteriously magical - the
> @Component
>  > annotation falls into the latter category, so once you have it
> working you
>  > might like to read up on "bnd" and "scr" to see how the magic works.
>  >
>  > If you want to present a web interface then the easiest way is to install
>  > the Karaf "web" feature and have your module register a service which
>  > implements HttpServlet and has a property called "alias". This will
>  > automatically be picked up by something called PAX Web, which will
> wire it
>  > into Jetty for you.
>  >
>  > HTH, Chris
>
>  >
>  >
>  >
>  >
>  >
>  >
>  >
>  >
>  >
>
> --
> Jean-Baptiste Onofré
> jbonofre@apache.org <ma...@apache.org>
> http://blog.nanthrax.net <http://blog.nanthrax.net/>
> Talend - http://www.talend.com <http://www.talend.com/>
>

-- 
Jean-Baptiste Onofré
jbonofre@apache.org
http://blog.nanthrax.net
Talend - http://www.talend.com

Re: Karaf client

Posted by Milan Tomic <to...@yahoo.com>.
Hi Jean,

Yes, MyService is an interface.

No, I am not using blueprint, but I will search for some example now. :)

Thank you
Milan




On Monday, August 11, 2014 3:05 PM, Jean-Baptiste Onofré <jb...@nanthrax.net> wrote:
 


Hi Milan,

MyService is the interface ?

You register the service using blueprint ?

Regards
JB


On 08/11/2014 02:55 PM, Milan Tomic wrote:
>
> Thank you very much for your answers. Due to my bug my Activator didn't
> activated my service and that was causing problems.
>
> Now I got this exception:
>
> ClassCastException: Proxy68687b14_9ba8_4734_b559_b199ee1ac482 cannot be
> cast to myPackage.MyService
>
> when I do this:
>
> MyService myService = (MyService) ic.lookup("osgi:service/" +
> MyService.class.getName());
>
> what could be a problem?
>
> Thank you in advance,
> Milan
>
>
>
>
>
> On Monday, August 11, 2014 2:50 PM, Milan Tomic <to...@yahoo.com>
> wrote:
>
>
>
> Oh :) I didn't noticed that I sent this only to you :) I wanted to send
> it to the whole user mailing list. Sorry and discard this email because
> it is solved already.
>
> Br, Milan
>
>
> On Monday, August 11, 2014 9:39 AM, Milan Tomic <to...@yahoo.com>
> wrote:
>
>
>
> Thank you very much for your responses.
>
> Do I have to register OSGi service with JNDI or all OSGi services
> areautoregistered? I am trying this:
>
> MyService myService = (MyService) ic.lookup("osgi:service/" +
> MyService.class.getName());
>
> and I got this exception:
>
> javax.naming.NameNotFoundException: osgi:service/mypackage.MyService
>
> I have already instaled feature:install jndi
>
> Thank you in advance,
> Milan
>
>
>
> On Saturday, August 9, 2014 10:03 AM, "chris.gray@kiffer.ltd.uk"
> <ch...@kiffer.ltd.uk> wrote:
>
>
> Hello Milan,
>
> You don't say which tutorial you followed: if you use Eclipse then I
> suggest you take a look at <http://bndtools.org/tutorial.html>. Step 6
> won't work for you because it's designed for a different shell than the
> one Karaf uses by default, but you should learn something from the earlier
> steps.
>
>  > public interface ParserService {
>  >     String parse(String s);
>  > }
>  >
>  > public class ParserImpl implements ParserService {
>  >     public String parse(String s) {
>  >         return s.replaceAll("AA", "BB");
>  >     }
>  >  }
>
> I hope these are two separate files, BTW otherwise I will have to send you
> to bed without any supper. :)
>
>
>  > Now I have no idea how to call this service from outside of Karaf. Let's
>  > say from an Servlet running inside some Tomcat on the same Windows Server
>  > maching. So, how do you create OSGi/Karaf client app?
>
>
> At this stage you can't even call the service from *inside* Karaf, because
> you didn't register it with the framework. There are many ways to do this,
> from the painstakingly manual to the mysteriously magical - the @Component
> annotation falls into the latter category, so once you have it working you
> might like to read up on "bnd" and "scr" to see how the magic works.
>
> If you want to present a web interface then the easiest way is to install
> the Karaf "web" feature and have your module register a service which
> implements HttpServlet and has a property called "alias". This will
> automatically be picked up by something called PAX Web, which will wire it
> into Jetty for you.
>
> HTH, Chris
>
>
>
>
>
>
>
>
>

-- 
Jean-Baptiste Onofré
jbonofre@apache.org
http://blog.nanthrax.net
Talend - http://www.talend.com

Re: Karaf client

Posted by Jean-Baptiste Onofré <jb...@nanthrax.net>.
Hi Milan,

MyService is the interface ?

You register the service using blueprint ?

Regards
JB

On 08/11/2014 02:55 PM, Milan Tomic wrote:
>
> Thank you very much for your answers. Due to my bug my Activator didn't
> activated my service and that was causing problems.
>
> Now I got this exception:
>
> ClassCastException: Proxy68687b14_9ba8_4734_b559_b199ee1ac482 cannot be
> cast to myPackage.MyService
>
> when I do this:
>
> MyService myService = (MyService) ic.lookup("osgi:service/" +
> MyService.class.getName());
>
> what could be a problem?
>
> Thank you in advance,
> Milan
>
>
>
>
>
> On Monday, August 11, 2014 2:50 PM, Milan Tomic <to...@yahoo.com>
> wrote:
>
>
>
> Oh :) I didn't noticed that I sent this only to you :) I wanted to send
> it to the whole user mailing list. Sorry and discard this email because
> it is solved already.
>
> Br, Milan
>
>
> On Monday, August 11, 2014 9:39 AM, Milan Tomic <to...@yahoo.com>
> wrote:
>
>
>
> Thank you very much for your responses.
>
> Do I have to register OSGi service with JNDI or all OSGi services
> areautoregistered? I am trying this:
>
> MyService myService = (MyService) ic.lookup("osgi:service/" +
> MyService.class.getName());
>
> and I got this exception:
>
> javax.naming.NameNotFoundException: osgi:service/mypackage.MyService
>
> I have already instaled feature:install jndi
>
> Thank you in advance,
> Milan
>
>
>
> On Saturday, August 9, 2014 10:03 AM, "chris.gray@kiffer.ltd.uk"
> <ch...@kiffer.ltd.uk> wrote:
>
>
> Hello Milan,
>
> You don't say which tutorial you followed: if you use Eclipse then I
> suggest you take a look at <http://bndtools.org/tutorial.html>. Step 6
> won't work for you because it's designed for a different shell than the
> one Karaf uses by default, but you should learn something from the earlier
> steps.
>
>  > public interface ParserService {
>  >     String parse(String s);
>  > }
>  >
>  > public class ParserImpl implements ParserService {
>  >     public String parse(String s) {
>  >         return s.replaceAll("AA", "BB");
>  >     }
>  >  }
>
> I hope these are two separate files, BTW otherwise I will have to send you
> to bed without any supper. :)
>
>
>  > Now I have no idea how to call this service from outside of Karaf. Let's
>  > say from an Servlet running inside some Tomcat on the same Windows Server
>  > maching. So, how do you create OSGi/Karaf client app?
>
>
> At this stage you can't even call the service from *inside* Karaf, because
> you didn't register it with the framework. There are many ways to do this,
> from the painstakingly manual to the mysteriously magical - the @Component
> annotation falls into the latter category, so once you have it working you
> might like to read up on "bnd" and "scr" to see how the magic works.
>
> If you want to present a web interface then the easiest way is to install
> the Karaf "web" feature and have your module register a service which
> implements HttpServlet and has a property called "alias". This will
> automatically be picked up by something called PAX Web, which will wire it
> into Jetty for you.
>
> HTH, Chris
>
>
>
>
>
>
>
>
>

-- 
Jean-Baptiste Onofré
jbonofre@apache.org
http://blog.nanthrax.net
Talend - http://www.talend.com

Re: Karaf client

Posted by Milan Tomic <to...@yahoo.com>.

Thank you very much for your answers. Due to my bug my Activator didn't activated my service and that was causing problems.

Now I got this exception:


ClassCastException: Proxy68687b14_9ba8_4734_b559_b199ee1ac482 cannot be cast to myPackage.MyService

when I do this:

MyService myService = (MyService) ic.lookup("osgi:service/" + MyService.class.getName());

what could be a problem?

Thank you in advance,
Milan






On Monday, August 11, 2014 2:50 PM, Milan Tomic <to...@yahoo.com> wrote:
 




Oh :) I didn't noticed that I sent this only to you :) I wanted to send it to the whole user mailing list. Sorry and discard this email because it is solved already.

Br, Milan


On Monday, August 11, 2014 9:39 AM, Milan Tomic <to...@yahoo.com> wrote:
 




Thank you very much for your responses.

Do I have to register OSGi service with JNDI or all OSGi services areautoregistered? I am trying this:

MyService myService = (MyService) ic.lookup("osgi:service/" + MyService.class.getName());

and I got this exception:

javax.naming.NameNotFoundException: osgi:service/mypackage.MyService

I have already instaled feature:install jndi

Thank you in advance,
Milan



On Saturday, August 9, 2014 10:03 AM, "chris.gray@kiffer.ltd.uk" <ch...@kiffer.ltd.uk> wrote:
 


Hello Milan,

You don't say which tutorial you followed: if you use Eclipse then I
suggest you take a look at <http://bndtools.org/tutorial.html>. Step 6
won't work for you because it's designed for a different shell than the
one Karaf uses by default, but you should learn something from the earlier
steps.

> public interface ParserService {
>     String parse(String s);
> }
>
> public class ParserImpl implements ParserService {
>     public String parse(String s) {
>         return s.replaceAll("AA", "BB");
>     }
>  }

I hope these are two separate files, BTW otherwise I will have to send you
to bed without any supper. :)


> Now I have no idea how to call this service from outside of Karaf. Let's
> say from an Servlet running inside some Tomcat on the same Windows Server
> maching. So, how do you create OSGi/Karaf client app?

At this stage you can't even call the service from *inside* Karaf, because
you didn't register it with the framework. There are many ways to do this,
from the painstakingly manual to the mysteriously magical - the @Component
annotation falls into the latter category, so once you have it working you
might like to read up on "bnd" and "scr" to see how the magic works.

If you want to present a web interface then the easiest way is to install
the Karaf "web" feature
 and have your module register a service which
implements HttpServlet and has a property called "alias". This will
automatically be picked up by something called PAX Web, which will wire it
into Jetty for you.

HTH, Chris