Thank you very much. ;-) 2008/8/6 Clement Escoffier <[EMAIL PROTECTED]>
> > > > > -----Original Message----- > From: honnix [mailto:[EMAIL PROTECTED] > Sent: mardi 5 août 2008 16:32 > To: users@felix.apache.org > Subject: Re: Is there any way to access OSGi service from non-OSGi client > > > > clement escoffier wrote: > > > Hi, > > > > > > Are you embedding Felix inside a host ? In this case, you can access the > > > bundle context by invoking the getBundleContext() method on the Felix > > > object. This returns the System-bundle bundle context. Be aware that > Felix > > > need to be started first. > > > > > > Always if you're embedding Felix, you can use something like the > following > > > methods (using reflection) to invoke OSGi services from your host: > > > public Object invokeServiceOperation(ServiceReference ref, String > > > methodName, Class[] argTypes, Object[] args) { > > > Object svc = context.getService(ref); > > > try { > > > Method method = svc.getClass().getMethod(methodName, > argTypes); > > > return method.invoke(svc, args); > > > } catch (Exception e) { > > > System.err.println("Cannot invoke the service operation " + > > > methodName + " : " + e.getMessage()); > > > } > > > return null; > > > } > > > > > > public Object lookAndInvokeServiceOperation(String itf, String > filter, > > > String methodName, Class[] argTypes, Object[] args) { > > > ServiceReference[] refs = null; > > > try { > > > refs = context.getServiceReferences(itf, filter); > > > if (refs == null) { > > > System.err.println("Service lookup failed - no matching > > > provider " + itf + " " + filter); > > > return null; > > > } > > > } catch (InvalidSyntaxException e1) { > > > System.err.println("Service lookup failed - invalid filter " > + > > > itf + " " + filter); > > > return null; > > > } > > > > > > Object svc = context.getService(refs[0]); > > > try { > > > Method method = svc.getClass().getMethod(methodName, > argTypes); > > > return method.invoke(svc, args); > > > } catch (InvocationTargetException e) { > > > System.err.println("Cannot invoke the service operation " + > > > methodName + " : " + e.getTargetException()); > > > e.getTargetException().printStackTrace(); > > > } catch (Exception e) { > > > System.err.println("Cannot invoke the service operation " + > > > methodName + " : " + e.getMessage()); > > > } > > > return null; > > > } > > > > > > > > > If you want to access OSGi services remotely, there is several way to > expose > > > OSGi service to be accessible remotely. Some works were made around Web > > > Services (especially X-Fire). More recently, a work using Hessian sounds > > > interristing > (http://java.dzone.com/articles/hessian-service-and-client-osg). > > > > > > > > > Regards, > > > > > > Clement > > > > > > 2008/8/5 honnix <[EMAIL PROTECTED]> > > > > > > > > >> Seems that there is some classloader problem. > > >> > > >> How can I access OSGi bundle context, and how can I access OSGi service > > >> from some non-OSGi client. I searched in Google, but have no hint yet. I > can > > >> get that using reflection way, but is there any convenient way? > > >> > > >> Thank you all. > > >> > > >> --------------------------------------------------------------------- > > >> To unsubscribe, e-mail: [EMAIL PROTECTED] > > >> For additional commands, e-mail: [EMAIL PROTECTED] > > >> > > >> > > >> > > > > > > > > Hi Clement, > > > > Thank you very much for your reply. > > > > Seems that the best way to invoke service is using reflection, right? > > And consider this situation: > > > > class FelixWrapper > > { > > private static Felix felix = new Felix(...); > > > > public static Felix getFelix() > > { > > return felix; > > } > > > > ... > > } > > > > > > This wrapper is loaded by some classloader, so I have to use the same > > classloader to find FelixWrapper class, and use reflection to get Felix > > instance. > > > > Class clazz = theClassLoader.loadClass("com.honnix.osgi.FelixWrapper"); > > Method method = clazz.getMethod("getFelix", ...); > > Object felix = method.invoke(null, ...); > > > > clazz = theClassLoader.loadClass("org.apache.felix.framework.Felix"); > > Method method = clazz.getMethod("getBundleContext", ...); > > Object bundleContext = method.invoke(felix, ...); > > > > ... use reflection on bundleContext to get bundles, service references, > > services ... > > > > Am I right? > > > > Brs, > > Honnix > > > > > > Yes, it should work. Be care that if your service operations return > "non-standard" objects, you must use reflection to interact with these > objects too. > > > > Regards, > > > > Clement > > > > --------------------------------------------------------------------- > > To unsubscribe, e-mail: [EMAIL PROTECTED] > > For additional commands, e-mail: [EMAIL PROTECTED] > > > >