Inject a CDI managed bean into a class that is instantiated with 'new'

Solution Verified - Updated -

Issue

We are trying to inject a managed bean into a CallbackHandler which is being instantiated by the JBossWS framework. The Callback handler is defined in @EndpointConfig on our web service, so we don't have control over how it is created.

How can we inject CDI managed beans into it? eg:

public class KeystorePasswordCallback implements CallbackHandler {
   private Map<String, String> passwords = new HashMap<String, String>();

   @Inject
   private MyService service;

   public KeystorePasswordCallback() {
      passwords.put("alice", "password");
      passwords.put("bob", "password");
   }

   /**
    * It attempts to get the password from the private
    * alias/passwords map.
    */
   public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
      for (int i = 0; i < callbacks.length; i++) {
         WSPasswordCallback pc = (WSPasswordCallback)callbacks[i];

         String pass = passwords.get(pc.getIdentifier());
         if (pass != null) {
            pc.setPassword(pass);
            return;
         }
      }

      service.postProcessing();     // NULL POINTER EXCEPTION HERE.
   }

   /**
    * Add an alias/password pair to the callback mechanism.
    */
   public void setAliasPassword(String alias, String password) {
      passwords.put(alias, password);
   }
}

Environment

JBoss Enterprise Application Platform (EAP) 6.0

Subscriber exclusive content

A Red Hat subscription provides unlimited access to our knowledgebase of over 48,000 articles and solutions.

Current Customers and Partners

Log in for full access

Log In
Close

Welcome! Check out the Getting Started with Red Hat page for quick tours and guides for common tasks.