users@jersey.java.net

Re: [Jersey] LW HTTP Server and injectable providers

From: Bertold Kolics <Bertold.Kolics_at_Sun.COM>
Date: Tue, 10 Jun 2008 11:30:40 -0500
Paul Sandoz wrote:
On Jun 10, 2008, at 1:07 AM, Bertold Kolics wrote:

Hi,

Is it possible to use injectable providers with the lightweight HTTP Server? I don't see how one can invoke the addInjectable method on WebApplication when running outside a container.


In the latest build you can use @Provider, for example:

  @Provider
  public class MyInjectable implements InjectableProvider<MyAnnotation, Type>  {
    ...
  }

Thank you.

My case maybe special, because I have a constructor in my injectable provider that takes some parameters. The simplest way I can find to setup an LW HTTP Server was something like this (hacked together from the ContainerFactory and HttpServerFactory classes). Is there a better way?

Bertold

        String classPath = System.getProperty("java.class.path");
        String[] paths = classPath.split(File.pathSeparator);
        ClasspathResourceConfig config = new ClasspathResourceConfig(paths);
        WebApplication wa = WebApplicationFactory.createWebApplication();
        wa.addInjectable(new MyInjectableProvider(myParams));

        // Revese the order so that applications may override
        LinkedList<ContainerProvider> cps = new LinkedList<ContainerProvider>();
        for (ContainerProvider cp : ServiceFinder.find(ContainerProvider.class, true))
            cps.addFirst(cp);

        HttpHandler handler = null;
        for (ContainerProvider<HttpHandler> cp : cps) {
            handler = cp.createContainer(HttpHandler.class, config, wa);
            if (handler != null) {
                Object o = config.getProperties().get(
                        ResourceConfig.PROPERTY_CONTAINER_NOTIFIER);
                if (o instanceof ContainerNotifier &&
                        handler instanceof ContainerListener) {
                    ContainerNotifier crf = (ContainerNotifier)o;
                    crf.addListener((ContainerListener)handler);
                }
            }
        }

       HttpServer server =
                HttpServerFactory.create("http://localhost:9998/", handler);