1 package org.openejb.alt.containers.castor_cmp11; 2 3 import java.util.HashMap ; 4 5 import org.openejb.core.DeploymentInfo; 6 import org.openejb.util.Logger; 7 8 public abstract class KeyGeneratorFactory { 9 protected final static Logger logger = Logger.getInstance("CastorCMP", "org.openejb.alt.util.resources"); 10 11 private static class PrimitiveKey implements org.openejb.alt.containers.castor_cmp11.KeyGenerator{ 12 private final java.lang.reflect.Field field; 13 14 PrimitiveKey(DeploymentInfo di) throws org.openejb.OpenEJBException{ 15 field = di.getPrimaryKeyField(); 16 } 17 18 public java.lang.Object getPrimaryKey(javax.ejb.EntityBean bean){ 19 try{ 20 return field.get(bean); 21 }catch(Exception e) { 22 e.printStackTrace(); 23 throw new IllegalStateException ("Don't have access to field "+field+": received "+e); 24 } 25 } 26 27 public org.exolab.castor.persist.spi.Complex getJdoComplex(java.lang.Object primaryKey){ 28 throw new IllegalStateException ("This method must not be called on primitive primary keys"); 29 } 30 31 public boolean isKeyComplex( ){ 32 return false; 33 } 34 } 35 36 private static class ComplexKey implements org.openejb.alt.containers.castor_cmp11.KeyGenerator{ 37 private final java.lang.Class pkClass; 38 private final Object [] cmFields; 39 private final HashMap pkFieldMap; 40 private final HashMap beanFieldMap; 41 42 ComplexKey(DeploymentInfo di) throws org.openejb.OpenEJBException{ 43 pkClass = di.getPrimaryKeyClass(); 44 Class beanClass= di.getBeanClass(); 45 java.util.List v= new java.util.ArrayList (); 46 java.lang.reflect.Field [] fields=pkClass.getFields(); 47 pkFieldMap = new HashMap (); 48 beanFieldMap = new HashMap (); 49 for(int i=0; i<fields.length; ++i) { 50 String fieldName=fields[i].getName(); 51 try{ 52 beanFieldMap.put(fieldName, beanClass.getField(fieldName)); 53 pkFieldMap.put(fieldName, fields[i]); 54 v.add(fieldName); 55 }catch(java.lang.NoSuchFieldException e) { 56 } 58 } 59 cmFields=new Object [v.size()]; 60 v.toArray(cmFields); 61 } 62 63 64 public java.lang.Object getPrimaryKey(javax.ejb.EntityBean bean){ 65 try{ 66 Object pk = pkClass.newInstance(); 67 for(int i=0; i<cmFields.length; ++i) { 68 Object fieldName=cmFields[i]; 69 Object value = ((java.lang.reflect.Field ) beanFieldMap.get(fieldName)).get(bean); 70 ((java.lang.reflect.Field ) pkFieldMap.get(fieldName)).set(pk, value); 71 } 72 return pk; 73 }catch(Exception e) { 74 logger.error("getPrimaryKey() received exception "+e, e); 75 throw new IllegalStateException (); 76 } 77 } 78 79 public org.exolab.castor.persist.spi.Complex getJdoComplex(java.lang.Object primaryKey){ 80 int len = cmFields.length; 81 java.lang.Object [] args = new java.lang.Object [len]; 82 try{ 83 for(int i=0; i<len; ++i) { 84 args[i] = ((java.lang.reflect.Field ) pkFieldMap.get(cmFields[i])).get(primaryKey); 85 } 86 return new org.exolab.castor.persist.spi.Complex(len, args); 87 }catch(Exception e) { 88 logger.error("getJdoComplex() received exception "+e, e); 89 throw new IllegalStateException (); 90 } 91 } 92 93 public boolean isKeyComplex( ){ 94 return true; 95 } 96 } 97 98 public static KeyGenerator createKeyGenerator(DeploymentInfo di) 99 throws org.openejb.OpenEJBException{ 100 101 if(di.getPrimaryKeyField()!=null) { 102 return new PrimitiveKey(di); 103 } else { 104 return new ComplexKey(di); 105 } 106 } 107 } 108 109 | Popular Tags |