1 22 package org.jboss.ejb.plugins.cmp.jdbc.bridge; 23 24 import java.lang.reflect.Field ; 25 26 import javax.ejb.EJBException ; 27 28 import org.jboss.deployment.DeploymentException; 29 import org.jboss.ejb.EntityEnterpriseContext; 30 31 import org.jboss.ejb.plugins.cmp.jdbc.JDBCContext; 32 import org.jboss.ejb.plugins.cmp.jdbc.JDBCStoreManager; 33 import org.jboss.ejb.plugins.cmp.jdbc.metadata.JDBCCMPFieldMetaData; 34 35 36 53 public class JDBCCMP1xFieldBridge extends JDBCAbstractCMPFieldBridge 54 { 55 private Field field; 56 57 public JDBCCMP1xFieldBridge(JDBCStoreManager manager, 58 JDBCCMPFieldMetaData metadata) 59 throws DeploymentException 60 { 61 super(manager, metadata); 62 63 try 64 { 65 field = manager.getMetaData().getEntityClass().getField(getFieldName()); 66 } 67 catch(NoSuchFieldException e) 68 { 69 throw new DeploymentException("No field named '" + getFieldName() + 71 "' found in entity class."); 72 } 73 } 74 75 public Object getInstanceValue(EntityEnterpriseContext ctx) 76 { 77 FieldState fieldState = getFieldState(ctx); 78 if(!fieldState.isLoaded()) 79 { 80 throw new EJBException ("CMP 1.1 field not loaded: " + getFieldName()); 81 } 82 83 try 84 { 85 return field.get(ctx.getInstance()); 86 } 87 catch(Exception e) 88 { 89 throw new EJBException ("Internal error getting instance field " + 91 getFieldName(), e); 92 } 93 } 94 95 public void setInstanceValue(EntityEnterpriseContext ctx, Object value) 96 { 97 try 98 { 99 field.set(ctx.getInstance(), value); 100 FieldState fieldState = getFieldState(ctx); 101 fieldState.setLoaded(); 102 fieldState.setCheckDirty(); 103 } 104 catch(Exception e) 105 { 106 throw new EJBException ("Internal error setting instance field " + 108 getFieldName(), e); 109 } 110 } 111 112 public Object getLockedValue(EntityEnterpriseContext ctx) 113 { 114 throw new UnsupportedOperationException ("Optimistic locking is not supported in CMP1.1."); 115 } 116 117 public void lockInstanceValue(EntityEnterpriseContext ctx) 118 { 119 } 121 122 public boolean isLoaded(EntityEnterpriseContext ctx) 123 { 124 return getFieldState(ctx).isLoaded(); 125 } 126 127 130 public boolean isDirty(EntityEnterpriseContext ctx) 131 { 132 if(isReadOnly() || isPrimaryKeyMember()) 134 { 135 return false; 136 } 137 138 return isLoaded(ctx) && !stateFactory.isStateValid(getInstanceValue(ctx), getFieldState(ctx).originalValue); 140 } 141 142 147 public void setClean(EntityEnterpriseContext ctx) 148 { 149 FieldState fieldState = getFieldState(ctx); 150 fieldState.originalValue = getInstanceValue(ctx); 151 152 if(isReadOnly()) 154 { 155 fieldState.lastRead = System.currentTimeMillis(); 156 } 157 } 158 159 public boolean isReadTimedOut(EntityEnterpriseContext ctx) 160 { 161 if(!isReadOnly()) 163 { 164 return true; 165 } 166 167 if(getReadTimeOut() == -1) 169 { 170 return false; 171 } 172 173 long readInterval = System.currentTimeMillis() - 174 getFieldState(ctx).lastRead; 175 return readInterval >= getReadTimeOut(); 176 } 177 178 public void resetPersistenceContext(EntityEnterpriseContext ctx) 179 { 180 if(isReadTimedOut(ctx)) 181 { 182 JDBCContext jdbcCtx = (JDBCContext)ctx.getPersistenceContext(); 183 FieldState fieldState = (FieldState)jdbcCtx.getFieldState(jdbcContextIndex); 184 if(fieldState != null) 185 fieldState.reset(); 186 } 187 } 188 189 protected void setDirtyAfterGet(EntityEnterpriseContext ctx) 190 { 191 getFieldState(ctx).setCheckDirty(); 192 } 193 194 private FieldState getFieldState(EntityEnterpriseContext ctx) 195 { 196 JDBCContext jdbcCtx = (JDBCContext)ctx.getPersistenceContext(); 197 FieldState fieldState = (FieldState)jdbcCtx.getFieldState(jdbcContextIndex); 198 if(fieldState == null) 199 { 200 fieldState = new FieldState(jdbcCtx); 201 jdbcCtx.setFieldState(jdbcContextIndex, fieldState); 202 } 203 return fieldState; 204 } 205 206 private class FieldState 207 { 208 private Object originalValue; 209 private long lastRead = -1; 210 private JDBCEntityBridge.EntityState entityState; 211 212 public FieldState(JDBCContext jdbcContext) 213 { 214 this.entityState = jdbcContext.getEntityState(); 215 } 216 217 public boolean isLoaded() 218 { 219 return entityState.isLoaded(tableIndex); 220 } 221 222 public void setLoaded() 223 { 224 entityState.setLoaded(tableIndex); 225 } 226 227 public void setCheckDirty() 228 { 229 entityState.setCheckDirty(tableIndex); 230 } 231 232 public void reset() 233 { 234 originalValue = null; 235 lastRead = -1; 236 entityState.resetFlags(tableIndex); 237 log.debug("reset field state"); 238 } 239 } 240 } 241 | Popular Tags |