-
1. Re: Context Lookup vs. Injection
jaikiran Jan 28, 2010 12:03 AM (in response to andy.miller)How about:
@Resource(mappedName="jndiNameOfUUIDKeyGeneratorFactory")
-
2. Re: Context Lookup vs. Injection
andy.miller Jan 28, 2010 8:05 AM (in response to jaikiran)jaikiran wrote:
How about:
@Resource(mappedName="jndiNameOfUUIDKeyGeneratorFactory")
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/wiki/KeyGenerator
InitialContext ctx = new InitialContext();
KeyGeneratorFactory kgf = (KeyGeneratorFactory) ctx.lookup("UUIDKeyGeneratorFactory");
KeyGenerator kg = kgf.getKeyGenerator();
Object uuid = kg.generateKey();Which works just fine. So, I tried the following:
@Resource(mappedName="UUIDKeyGeneratorFactory")
I tried other things as well, such as java:comp/env/UUIDKeyGeneratorFactory, and java:/UUIDKeyGeneratorFactory, and others, but none of them worked. Unfortunately, I couldn't find anything in the console that showed its JNDI name either.
-
3. Re: Context Lookup vs. Injection
jaikiran Feb 3, 2010 8:09 AM (in response to andy.miller)Sorry, forgot about this discussion.
andy.miller@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/wiki/KeyGenerator
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
-
4. Re: Context Lookup vs. Injection
andy.miller Feb 3, 2010 10:20 AM (in response to jaikiran)Okay, so it works from a stateless session bean. I am trying to use it in an entity for the primary key, which today looks like this:
@Id
@GeneratedValue(generator="system-uuid")
@GenericGenerator(name = "system-uuid", strategy = "uuid")
public String getOrderId () {
return orderId;
}These are the Hibernate specific annotation (non-JPA) that do what I want. I tried to replace those by injecting the key generator factory and using it in the default constructor of the entity.
So, code that looks like this:
@Entity
public class Order implements Serializable {
/**
*
*/
private static final long serialVersionUID = -5721274468042182248L;
private String orderId;
private long customerId;
private Address shippingAddress;
private BigDecimal totalOrderAmount;
private List<OrderLine> orderLines;
private Customer customer;@Resource(mappedName="UUIDKeyGeneratorFactory")
KeyGeneratorFactory keyGeneratorFactory;
public Order() {shippingAddress = new Address();
totalOrderAmount = new BigDecimal(0);orderId = (String) keyGeneratoryFactory.getKeyGenerator().generateKey();
}
@Id
public String getOrderId () {
return orderId;
}Is there a difference in @Resource in a stateless session bean and an entity?
-
5. Re: Context Lookup vs. Injection
jaikiran Feb 3, 2010 10:24 AM (in response to andy.miller)andy.miller@jboss.com wrote:
Is there a difference in @Resource in a stateless session bean and an entity?
JPA entities don't support injection.
-
6. Re: Context Lookup vs. Injection
andy.miller Feb 3, 2010 10:31 AM (in response to jaikiran)jaikiran wrote:
andy.miller@jboss.com wrote:
Is there a difference in @Resource in a stateless session bean and an entity?
JPA entities don't support injection.
Well, that explains it. I will have to do what you did, which is inject the resource into a stateless session bean, and then pass the generated key to the constructor instead. Thanks.