Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Equinox » How to reference Bundle Service in web resource like JSP, Servlet?
How to reference Bundle Service in web resource like JSP, Servlet? [message #100497] Mon, 29 October 2007 06:03 Go to next message
Eclipse UserFriend
Originally posted by: lifesting.gmail.com

I created a bundle and bound a service *SmsService* on it's context,
below is the class body:

public class Activator implements BundleActivator {

private ServiceRegistration serviceReg;

public void start(BundleContext context) throws Exception {
SmsService service = xxx;
serviceReg = context.registerService(SmsService.class.getName(),
service,new Hashtable());
}


public void stop(BundleContext context) throws Exception {
serviceReg.unregister();
}

}

I also created a Servlet in this bundle, in which there's a method
invocation on the registered service above:

public class SendMessageServlet extends HttpServlet {
protected void doPost(HttpServletRequest req,
HttpServletResponse resp)
throws ServletException, IOException {
.....
SmsService sms = ....//Get the registered service
sms.sendMessage(XXX);
}
}

The only valid method after try is to add a field references
BundleContext and set it's value in Servlet constructor:

public class SendMessageServlet extends HttpServlet {
private BundleContext context;
public SendMessageServlet(BundleContext context)
{
this.context =context;
}
......
}

As you see, this way need every Servlet import Bundle explicitly, is
there a more elegant method?

Thanks a lot!
Re: How to reference Bundle Service in web resource like JSP, Servlet? [message #100632 is a reply to message #100497] Tue, 30 October 2007 12:54 Go to previous messageGo to next message
Eclipse UserFriend
>
> public class SendMessageServlet extends HttpServlet {
> private BundleContext context;
> public SendMessageServlet(BundleContext context)
> {
> this.context =context;
> }
> ......
> }
>
> As you see, this way need every Servlet import Bundle explicitly, is
> there a more elegant method?
>
> Thanks a lot!

David,

There a no *special elegant* way to do that.

Of course you can supress importing Bundle explicitly by setting the
service directly. Then only the Activator holds the BundleContext and
calls bind/unbind on the servlet (if your service is dynamic).

I prefer this approach.



public class MyServlet ...... {
private IFooService fooService;

bindFooService(IFooService fooService) {
...
}

unbindFooService(IFooService fooService) {
...
}

protected void doPost(HttpServletRequest req,
HttpServletResponse resp)
throws ServletException, IOException {

...
if(fooService == null {
// some strategy to handle this situation
}

// do something with fooService

}
Re: How to reference Bundle Service in web resource like JSP, Servlet? [message #101262 is a reply to message #100497] Thu, 08 November 2007 01:59 Go to previous message
Eclipse UserFriend
Originally posted by: bob.yang.unison.net.cn

Hello David,

I have the similar issue, because I want to use the
'org.eclipse.equinox.http.registry' bundle to registry servlet by using
eclipse extension point mechanism. But I can not find a way to supply
BundleContext object to my servlet when using
'org.eclipse.equinox.http.registry' bundle.

any idea?


David BY Chan wrote:
> I created a bundle and bound a service *SmsService* on it's context,
> below is the class body:
>
> public class Activator implements BundleActivator {
>
> private ServiceRegistration serviceReg;
>
> public void start(BundleContext context) throws Exception {
> SmsService service = xxx;
> serviceReg = context.registerService(SmsService.class.getName(),
> service,new Hashtable());
> }
>
>
> public void stop(BundleContext context) throws Exception {
> serviceReg.unregister();
> }
>
> }
>
> I also created a Servlet in this bundle, in which there's a method
> invocation on the registered service above:
>
> public class SendMessageServlet extends HttpServlet {
> protected void doPost(HttpServletRequest
> req, HttpServletResponse resp)
> throws ServletException, IOException {
> .....
> SmsService sms = ....//Get the registered service
> sms.sendMessage(XXX);
> }
> }
>
> The only valid method after try is to add a field references
> BundleContext and set it's value in Servlet constructor:
>
> public class SendMessageServlet extends HttpServlet {
> private BundleContext context;
> public SendMessageServlet(BundleContext context)
> {
> this.context =context;
> }
> ......
> }
>
> As you see, this way need every Servlet import Bundle explicitly, is
> there a more elegant method?
>
> Thanks a lot!
Previous Topic:How to append classpath to other bundles?
Next Topic:Adding/replacing classloader
Goto Forum:
  


Current Time: Mon Sep 23 17:29:36 GMT 2024

Powered by FUDForum. Page generated in 0.03938 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top