1 22 package org.jboss.ejb.plugins.cmp.jdbc.metadata; 23 24 import org.jboss.deployment.DeploymentException; 25 import org.jboss.metadata.MetaData; 26 import org.jboss.logging.Logger; 27 import org.w3c.dom.Element ; 28 29 35 public final class JDBCOptimisticLockingMetaData 36 { 37 public static final Integer FIELD_GROUP_STRATEGY = new Integer (1); 39 public static final Integer MODIFIED_STRATEGY = new Integer (2); 40 public static final Integer READ_STRATEGY = new Integer (4); 41 public static final Integer VERSION_COLUMN_STRATEGY = new Integer (8); 42 public static final Integer TIMESTAMP_COLUMN_STRATEGY = new Integer (16); 43 public static final Integer KEYGENERATOR_COLUMN_STRATEGY = new Integer (32); 44 45 47 final private Integer lockingStrategy; 48 49 50 final private String groupName; 51 52 53 final private JDBCCMPFieldMetaData lockingField; 54 55 56 final private String keyGeneratorFactory; 57 58 59 final private Logger log; 60 61 66 public JDBCOptimisticLockingMetaData(JDBCEntityMetaData entityMetaData, 67 Element element) 68 throws DeploymentException 69 { 70 log = Logger.getLogger(entityMetaData.getName()); 71 72 Element strategyEl; 73 if((strategyEl = MetaData.getOptionalChild(element, "group-name")) != null) 74 { 75 lockingStrategy = FIELD_GROUP_STRATEGY; 76 groupName = MetaData.getElementContent(strategyEl); 77 lockingField = null; 78 keyGeneratorFactory = null; 79 80 log.debug("optimistic locking: group=" + groupName); 81 } 82 else if((strategyEl = MetaData.getOptionalChild(element, "modified-strategy")) != null) 83 { 84 lockingStrategy = MODIFIED_STRATEGY; 85 groupName = null; 86 lockingField = null; 87 keyGeneratorFactory = null; 88 89 log.debug("optimistic locking: modified strategy"); 90 } 91 else if((strategyEl = MetaData.getOptionalChild(element, "read-strategy")) != null) 92 { 93 lockingStrategy = READ_STRATEGY; 94 groupName = null; 95 lockingField = null; 96 keyGeneratorFactory = null; 97 98 log.debug("optimistic locking: read strategy"); 99 } 100 else if((strategyEl = MetaData.getOptionalChild(element, "version-column")) != null) 101 { 102 String fieldType = MetaData.getOptionalChildContent(element, "field-type"); 103 if(fieldType != null) 104 throw new DeploymentException( 105 "field-type is not allowed for version column. It is implicitly set to java.lang.Long." 106 ); 107 108 lockingStrategy = VERSION_COLUMN_STRATEGY; 109 lockingField = constructLockingField(entityMetaData, element); 110 groupName = null; 111 keyGeneratorFactory = null; 112 113 log.debug("optimistic locking: version-column=" + lockingField.getFieldName()); 114 } 115 else if((strategyEl = MetaData.getOptionalChild(element, "timestamp-column")) != null) 116 { 117 String fieldType = MetaData.getOptionalChildContent(element, "field-type"); 118 if(fieldType != null) 119 throw new DeploymentException( 120 "field-type is not allowed for timestamp column. It is implicitly set to java.util.Date." 121 ); 122 123 lockingStrategy = TIMESTAMP_COLUMN_STRATEGY; 124 lockingField = constructLockingField(entityMetaData, element); 125 groupName = null; 126 keyGeneratorFactory = null; 127 128 log.debug("optimistic locking: timestamp-column=" + lockingField.getFieldName()); 129 } 130 else if((keyGeneratorFactory = 131 MetaData.getOptionalChildContent(element, "key-generator-factory")) != null) 132 { 133 lockingStrategy = KEYGENERATOR_COLUMN_STRATEGY; 134 lockingField = constructLockingField(entityMetaData, element); 135 groupName = null; 136 137 log.debug("optimistic locking: key-generator-factory=" + keyGeneratorFactory); 138 } 139 else 140 { 141 throw new DeploymentException("Unexpected error: entity " 142 + entityMetaData.getName() 143 + " has unkown/incorrect optimistic locking configuration."); 144 } 145 } 146 147 public Integer getLockingStrategy() 149 { 150 return lockingStrategy; 151 } 152 153 public String getGroupName() 154 { 155 return groupName; 156 } 157 158 public JDBCCMPFieldMetaData getLockingField() 159 { 160 return lockingField; 161 } 162 163 public String getKeyGeneratorFactory() 164 { 165 return keyGeneratorFactory; 166 } 167 168 173 private JDBCCMPFieldMetaData constructLockingField( 174 JDBCEntityMetaData entity, 175 Element element) 176 throws DeploymentException 177 { 178 String fieldName = MetaData.getOptionalChildContent(element, "field-name"); 180 if(fieldName == null || fieldName.trim().length() < 1) 181 fieldName = (lockingStrategy == VERSION_COLUMN_STRATEGY ? "version_lock" : 182 (lockingStrategy == TIMESTAMP_COLUMN_STRATEGY ? "timestamp_lock" : "generated_lock")); 183 184 String columnName = MetaData.getOptionalChildContent(element, "column-name"); 186 if(columnName == null || columnName.trim().length() < 1) 187 columnName = (lockingStrategy == VERSION_COLUMN_STRATEGY ? "version_lock" : 188 (lockingStrategy == TIMESTAMP_COLUMN_STRATEGY ? "timestamp_lock" : "generated_lock")); 189 190 Class fieldType = null; 192 if(lockingStrategy == VERSION_COLUMN_STRATEGY) 193 fieldType = java.lang.Long .class; 194 else if(lockingStrategy == TIMESTAMP_COLUMN_STRATEGY) 195 fieldType = java.util.Date .class; 196 String fieldTypeStr = MetaData.getOptionalChildContent(element, "field-type"); 197 if(fieldTypeStr != null) 198 { 199 try 200 { 201 fieldType = GetTCLAction. 202 getContextClassLoader().loadClass(fieldTypeStr); 203 } 204 catch(ClassNotFoundException e) 205 { 206 throw new DeploymentException( 207 "Could not load field type for optimistic locking field " 208 + fieldName + ": " + fieldTypeStr); 209 } 210 } 211 212 int jdbcType; 214 String sqlType; 215 String jdbcTypeName = MetaData.getOptionalChildContent(element, "jdbc-type"); 216 if(jdbcTypeName != null) 217 { 218 jdbcType = JDBCMappingMetaData.getJdbcTypeFromName(jdbcTypeName); 219 sqlType = MetaData.getUniqueChildContent(element, "sql-type"); 220 } 221 else 222 { 223 jdbcType = Integer.MIN_VALUE; 224 sqlType = null; 225 } 226 227 return new JDBCCMPFieldMetaData( 228 entity, fieldName, fieldType, columnName, jdbcType, sqlType 229 ); 230 } 231 } 232 | Popular Tags |