1 29 30 package com.caucho.ejb.amber; 31 32 import com.caucho.amber.field.AmberField; 33 import com.caucho.amber.field.CompositeId; 34 import com.caucho.amber.field.Id; 35 import com.caucho.amber.field.IdField; 36 import com.caucho.amber.field.PropertyField; 37 import com.caucho.amber.field.StubMethod; 38 import com.caucho.amber.manager.AmberPersistenceUnit; 39 import com.caucho.amber.table.Column; 40 import com.caucho.amber.table.Table; 41 import com.caucho.amber.type.EntityType; 42 import com.caucho.amber.type.Type; 43 import com.caucho.bytecode.JClass; 44 import com.caucho.bytecode.JMethod; 45 import com.caucho.config.ConfigException; 46 import com.caucho.ejb.cfg.CmpField; 47 import com.caucho.ejb.cfg.CmpProperty; 48 import com.caucho.ejb.cfg.CmrRelation; 49 import com.caucho.ejb.cfg.EjbConfig; 50 import com.caucho.ejb.cfg.EjbEntityBean; 51 import com.caucho.java.gen.JavaClassGenerator; 52 import com.caucho.util.L10N; 53 import com.caucho.vfs.PersistentDependency; 54 55 import java.io.IOException ; 56 import java.util.ArrayList ; 57 import java.util.HashMap ; 58 59 62 public class AmberConfig { 63 private static final L10N L = new L10N(AmberConfig.class); 64 65 private EjbConfig _ejbConfig; 66 private AmberPersistenceUnit _manager; 67 68 private ArrayList <EjbEntityBean> _beans = new ArrayList <EjbEntityBean>(); 69 70 private HashMap <String ,EntityType> _entityMap = 71 new HashMap <String ,EntityType>(); 72 73 76 public AmberConfig(EjbConfig ejbConfig) 77 { 78 _ejbConfig = ejbConfig; 79 _manager = _ejbConfig.getEJBManager().getAmberManager(); 80 } 81 82 public void init() 83 throws ConfigException, IOException 84 { 85 } 86 87 90 public AmberPersistenceUnit getManager() 91 { 92 return _manager; 93 } 94 95 98 public void addBean(EjbEntityBean bean) 99 throws ConfigException 100 { 101 _beans.add(bean); 102 103 EntityType type = bean.getEntityType(); 104 105 type.setInstanceClassName(bean.getFullImplName()); 106 type.setProxyClass(bean.getLocal()); 107 108 String sqlTable; 109 110 if (bean.getAbstractSchemaName() != null) { 111 type.setName(bean.getAbstractSchemaName()); 112 sqlTable = bean.getAbstractSchemaName(); 113 } 114 else { 115 String name = bean.getEJBName(); 116 int p = name.lastIndexOf('/'); 117 if (p > 0) 118 sqlTable = name.substring(p + 1); 119 else 120 sqlTable = name; 121 } 122 123 if (bean.getSQLTable() != null) 124 sqlTable = bean.getSQLTable(); 125 126 Table table = _manager.createTable(sqlTable); 127 table.setConfigLocation(bean.getLocation()); 128 129 type.setTable(table); 130 131 type.setReadOnly(bean.isReadOnly()); 132 table.setReadOnly(bean.isReadOnly()); 133 type.setCacheTimeout(bean.getCacheTimeout()); 134 135 if (hasMethod(bean.getEJBClassWrapper(), "ejbLoad", new JClass[0])) { 136 type.setHasLoadCallback(true); 137 } 138 139 _entityMap.put(bean.getEJBName(), type); 140 141 ArrayList <CmpProperty> ids = new ArrayList <CmpProperty>(); 142 143 for (CmpField cmpField : bean.getCmpFields()) { 144 if (cmpField.isId()) 145 ids.add(cmpField); 146 else 147 configureField(type, cmpField); 148 } 149 150 for (CmrRelation cmrRelation : bean.getRelations()) { 151 if (cmrRelation.isId()) 152 ids.add(cmrRelation); 153 } 154 155 configureId(bean, type, ids); 156 157 for (JMethod method : bean.getStubMethods()) { 158 type.addStubMethod(new StubMethod(method)); 159 } 160 161 for (PersistentDependency depend : bean.getDependList()) { 162 type.addDependency(depend); 163 } 164 } 165 166 169 private void configureField(EntityType type, CmpField cmpField) 170 throws ConfigException 171 { 172 String fieldName = cmpField.getName(); 173 String sqlName = cmpField.getSQLColumn(); 174 175 if (sqlName == null) 176 sqlName = fieldName; 177 178 JClass dataType = cmpField.getJavaType(); 179 180 if (dataType == null) 181 throw new NullPointerException (L.l("'{0}' is an unknown field", 182 fieldName)); 183 184 Type amberType = _manager.createType(dataType); 185 Column column = type.getTable().createColumn(sqlName, amberType); 186 187 column.setConfigLocation(cmpField.getLocation()); 188 189 PropertyField field = new PropertyField(type, fieldName); 190 field.setLazy(false); 191 field.setColumn(column); 192 193 type.addField(field); 194 } 195 196 199 private void configureId(EjbEntityBean bean, 200 EntityType type, 201 ArrayList <CmpProperty> fields) 202 throws ConfigException 203 { 204 ArrayList <IdField> keys = new ArrayList <IdField>(); 205 206 for (CmpProperty cmpProperty : fields) { 207 IdField idField = cmpProperty.createId(_manager, type); 208 if (fields.size() > 1 || bean.getCompositeKeyClass() != null) 209 idField.setKeyField(true); 210 211 keys.add(idField); 212 } 213 214 if (keys.size() == 1 && bean.getCompositeKeyClass() == null) { 215 Id id = new Id(type, keys.get(0)); 216 type.setId(id); 217 } 218 else { 219 CompositeId id = new CompositeId(type, keys); 220 id.setKeyClass(bean.getPrimKeyClass()); 221 type.setId(id); 222 } 223 } 224 225 public void configureRelations() 226 throws ConfigException 227 { 228 for (EjbEntityBean bean : _beans) { 229 configureRelations(bean); 230 } 231 232 for (EjbEntityBean bean : _beans) { 233 linkRelations(bean); 234 } 235 } 236 237 private void configureRelations(EjbEntityBean bean) 238 throws ConfigException 239 { 240 EntityType type = bean.getEntityType(); 241 242 for (CmrRelation rel : bean.getRelations()) { 243 if (! rel.isId()) 244 configureRelation(type, rel); 245 } 246 } 247 248 private void linkRelations(EjbEntityBean bean) 249 throws ConfigException 250 { 251 EntityType type = _entityMap.get(bean.getEJBName()); 252 253 for (CmrRelation rel : bean.getRelations()) { 254 rel.linkAmber(); 255 } 256 } 257 258 261 private void configureRelation(EntityType type, CmrRelation rel) 262 throws ConfigException 263 { 264 String fieldName = rel.getName(); 265 String targetName = rel.getTargetBean().getEJBName(); 266 267 EntityType targetType = _entityMap.get(targetName); 268 269 if (targetType == null) 270 throw new ConfigException(L.l("'{0}' is an unknown entity type", 271 targetName)); 272 273 AmberField field = rel.assembleAmber(type); 274 275 if (field != null) 276 type.addField(field); 277 } 278 279 282 public void generate(JavaClassGenerator javaGen) 283 throws Exception 284 { 285 _manager.generate(javaGen); 286 } 287 288 private static boolean hasMethod(JClass cl, String name, JClass []param) 289 { 290 try { 291 return cl.getMethod(name, param) != null; 292 } catch (Throwable e) { 293 return false; 294 } 295 } 296 } 297 | Popular Tags |