[jboss-user] [JNDI and Naming] New message: "Re: Context Lookup vs. Injection"

jaikiran pai do-not-reply at jboss.com
Wed Feb 3 08:09:37 EST 2010


User development,

A new message was posted in the thread "Context Lookup vs. Injection":

http://community.jboss.org/message/523890#523890

Author  : jaikiran pai
Profile : http://community.jboss.org/people/jaikiran

Message:
--------------------------------------------------------------
Sorry, forgot about this discussion.
 

> mailto:andy.miller at jboss.com wrote:
>  
> I tried that, but it didn't work.  Just get a NullPointerException when I try to reference the factory. Specifically, the following wiki shows how to do it the old way:
>  
> http://community.jboss.org/docs/DOC-11223
>  
>  
I tried this today and it worked fine for me. Here's the EJB3 SLSB into which i am injecting this as a @Resource:
 
public interface KeyGenerator
{
   Object generateKey();
}
 

 
@Stateless
@Remote(KeyGenerator.class)
@RemoteBinding(jndiBinding = SimpleSLSB.JNDI_NAME)
public class SimpleSLSB implements KeyGenerator
{
 
   public static final String JNDI_NAME = "KeyGeneratorFactoryInjectionTestBean";
   
   @Resource (mappedName="UUIDKeyGeneratorFactory")
   private KeyGeneratorFactory keygenFactory;
 
   public Object generateKey()
   {
      if (this.keygenFactory == null)
      {
         throw new RuntimeException("Key generator factory was not injected");
      }
      try
      {
         return this.keygenFactory.getKeyGenerator().generateKey();
      }
      catch (Exception e)
      {
         throw new RuntimeException("Could not generate key: ", e);
      }
 
   }
 
}

 
And then a testcase which invokes on this bean, which then internally uses the keygenfactory to create a keygen and then the key:
 
public class KeyGeneratorFactoryInjectionTestCase extends JBossTestCase
{
 
   private static Logger logger = Logger.getLogger(KeyGeneratorFactoryInjectionTestCase.class);
   
   public KeyGeneratorFactoryInjectionTestCase(String name)
   {
      super(name);
   }
   
   public static Test suite() throws Exception
   {
      return getDeploySetup(KeyGeneratorFactoryInjectionTestCase.class, "keygenfactory-injection.jar");
   }
 
   public void testKeyGeneration() throws Exception
   {
      KeyGenerator keyGenerator = (KeyGenerator) this.getInitialContext().lookup(SimpleSLSB.JNDI_NAME);
      Object key = keyGenerator.generateKey();
      logger.info("Generated key: " + key);
      assertNotNull("Key generator generated a null key", key);
   }
}

 
 
Passes:
 [junit] Running org.jboss.test.ejb3.keygenfactory.injection.unit.KeyGeneratorFactoryInjectionTestCase
    [junit] Tests run: 1, Failures: 0, Errors: 0, Time elapsed: 1.178 sec


 
And here's the generated key:
 
2010-02-03 18:31:59,388 INFO  [org.jboss.test.ejb3.keygenfactory.injection.unit.KeyGeneratorFactoryInjectionTestCase] Generated key: 93EC42DA7F00000101C47F7C5549F6EB
 


--------------------------------------------------------------

To reply to this message visit the message page: http://community.jboss.org/message/523890#523890




More information about the jboss-user mailing list