Search code examples
jettyjndicdijboss-weld

Jetty: adding <resource-env-ref> programmatically


I have a standalone application with embedded Jetty and Wicket.
I'd like to use CDI for injection.

So I've found http://docs.jboss.org/weld/reference/latest/en-US/html/environments.html#d0e5286
and now I'm trying to add this programatically, but it's quite complex.

How do I code that?

Other sources I've found are:

So far I have:

  Server server = new Server( 8080 );
  Context ctx = new Context( server, "/", Context.NO_SECURITY | Context.SESSIONS );


  try {
     //BeanManager
     new org.mortbay.jetty.plus.naming.Resource( ctx, "BeanManager", 
        new javax.naming.Reference(
           "javax.enterprise.inject.spi.BeanManager",
           "org.jboss.weld.resources.ManagerObjectFactory", null )
     );
  } catch ( NamingException ex ) {
     log.error(...);
  }


  // Wicket.
  final ServletHolder wicketSH = new ServletHolder( new MyReloadingWicketServlet() );
  wicketSH.setInitParameter( "applicationClassName", WicketApplication.class.getName() );
  ctx.addServlet( wicketSH, "/*" );

Solution

  • Adding a resource-env-ref programmatically doesn't make sense. The point of JavaEE refs is to separate the developer from the deployer: the developer declares a reference, and the deployer binds the reference to a managed resource in the environment. If you don't have or need a deployer role, then you don't need a resource-env-ref either: simply look up the target object yourself (for CDI integration, I think that would be an @Produces method).