1 22 package org.jboss.ejb.plugins.cmp.jdbc.bridge; 23 24 import org.jboss.ejb.EntityEnterpriseContext; 25 import org.jboss.ejb.plugins.cmp.jdbc.JDBCStoreManager; 26 import org.jboss.ejb.plugins.cmp.jdbc.metadata.JDBCCMPFieldMetaData; 27 import org.jboss.ejb.plugins.keygenerator.KeyGeneratorFactory; 28 import org.jboss.ejb.plugins.keygenerator.KeyGenerator; 29 import org.jboss.deployment.DeploymentException; 30 31 import javax.naming.InitialContext ; 32 import javax.naming.NamingException ; 33 34 38 public class JDBCKeyGenVersionFieldBridge extends JDBCCMP2xVersionFieldBridge 39 { 40 private final KeyGenerator keyGenerator; 41 42 public JDBCKeyGenVersionFieldBridge(JDBCStoreManager manager, 43 JDBCCMPFieldMetaData metadata, 44 String keygenFactoryName) 45 throws DeploymentException 46 { 47 super(manager, metadata); 48 keyGenerator = initKeyGenerator(keygenFactoryName); 49 } 50 51 public JDBCKeyGenVersionFieldBridge(JDBCCMP2xFieldBridge cmpField, String keygenFactoryName) 52 throws DeploymentException 53 { 54 super(cmpField); 55 keyGenerator = initKeyGenerator(keygenFactoryName); 56 } 57 58 private KeyGenerator initKeyGenerator(String keygenFactoryName) throws DeploymentException 59 { 60 try 61 { 62 InitialContext ctx = new InitialContext (); 63 KeyGeneratorFactory keygenFactory = (KeyGeneratorFactory)ctx.lookup(keygenFactoryName); 64 return keygenFactory.getKeyGenerator(); 65 } 66 catch(NamingException e) 67 { 68 throw new DeploymentException("Could not lookup key generator factory: " 69 + keygenFactoryName, e); 70 } 71 catch(Exception e) 72 { 73 throw new DeploymentException("Could not create KeyGenerator instance.", e); 74 } 75 } 76 77 public void setFirstVersion(EntityEnterpriseContext ctx) 78 { 79 Object version = keyGenerator.generateKey(); 80 setInstanceValue(ctx, version); 81 } 82 83 public Object updateVersion(EntityEnterpriseContext ctx) 84 { 85 Object next = keyGenerator.generateKey(); 86 setInstanceValue(ctx, next); 87 return next; 88 } 89 } 90 | Popular Tags |