1 25 26 27 package org.objectweb.jonas_ejb.deployment.api; 28 29 import java.util.ArrayList ; 30 import java.util.Collection ; 31 import java.util.Iterator ; 32 import java.lang.reflect.Field ; 33 import java.lang.reflect.Method ; 34 import org.objectweb.jonas_ejb.deployment.xml.AssemblyDescriptor; 35 import org.objectweb.jonas_ejb.deployment.xml.Entity; 36 import org.objectweb.jonas_ejb.deployment.xml.JonasEntity; 37 import org.objectweb.jonas_ejb.deployment.xml.Query; 38 import org.objectweb.jonas_ejb.deployment.ejbql.ParseException; 39 import org.objectweb.jonas_ejb.lib.BeanNaming; 40 import org.objectweb.jonas_lib.deployment.api.DeploymentDescException; 41 import org.objectweb.jonas_lib.deployment.xml.JLinkedList; 42 import org.objectweb.jorm.metainfo.api.MetaObject; 43 44 45 52 public abstract class EntityCmp2Desc extends EntityCmpDesc { 53 54 private ArrayList ejbRelationshipRoleDesc = new ArrayList (); 55 private ArrayList jormMOList = new ArrayList (); 56 protected String abstractSchemaName; 57 58 private static final String JORM_PACKAGE = "jorm"; 59 60 private String jormClassName = null; 61 private String jormFQClassName = null; 62 private String jormAccessorClassName = null; 63 private String jormPKClassName = null; 64 private String jormPNameGetterClassName = null; 65 private String jormBinderClassName = null; 66 67 68 protected String factoryClassName = null; 69 protected DeploymentDescEjb2 dc2d = null; 70 71 75 public EntityCmp2Desc(ClassLoader classLoader, 76 Entity ent, 77 AssemblyDescriptor asd, 78 JonasEntity jEnt, 79 DeploymentDescEjb2 dc2d, 80 JLinkedList jMDRList, 81 String fileName) 82 throws DeploymentDescException { 83 84 super(classLoader, ent, asd, jEnt, jMDRList, fileName); 85 this.dc2d = dc2d; 86 87 if ((ent.getAbstractSchemaName() == null) 89 || (ent.getAbstractSchemaName().length() == 0)) { 90 throw new DeploymentDescException("abstract-schema-name must be provided for bean " + this.ejbName); 91 } 92 abstractSchemaName = ent.getAbstractSchemaName(); 93 94 if (fieldDesc.isEmpty()) { 96 throw new DeploymentDescException("No cmp-field defined in bean " + this.ejbName); 97 } 98 for (Iterator i = fieldDesc.keySet().iterator(); i.hasNext();) { 100 String fn = (String ) i.next(); 101 102 try { 104 Field f = ejbClass.getField(fn); 105 throw new DeploymentDescException("In cmp-version 2.x, field-name " + fn + " should not be defined in bean " + this.ejbName); 106 } catch (NoSuchFieldException e) { 107 } catch (SecurityException e) { 109 throw new DeploymentDescException("Cannot use java reflexion on " + this.ejbClass.getName()); 110 } 111 112 try { 113 Method getter = null; 114 try { 115 getter = ejbClass.getMethod(FieldDesc.getGetterName(fn), (Class []) null); 116 ((FieldDesc) (fieldDesc.get(fn))).setFieldType(getter.getReturnType()); 117 } catch (NoSuchMethodException e) { 118 throw new DeploymentDescException("Getter method not found for field-name " + fn + " in bean " + this.ejbName, e); 119 } 120 try { 121 ejbClass.getMethod(FieldDesc.getSetterName(fn), new Class []{getter.getReturnType()}); 122 } catch (NoSuchMethodException e) { 123 throw new DeploymentDescException("Setter method not found for field-name " + fn + " in bean " + this.ejbName, e); 124 } 125 } catch (SecurityException e) { 126 throw new DeploymentDescException("Cannot use java reflexion on " + this.ejbClass.getName()); 127 } 128 } 129 130 if (jEnt.getIsModifiedMethodName() != null) { 132 throw new DeploymentDescException("use of is-modified-method-name deprecated for CMP 2.x"); 133 } 134 135 if (ent.getQueryList() != null) { 137 for (Iterator i = ent.getQueryList().iterator(); i.hasNext();) { 138 Query q = (Query) i.next(); 139 boolean foundMatch = false; 140 for (Iterator j = getMethodDescIterator(); j.hasNext();) { 141 MethodDesc methd = (MethodDesc) j.next(); 142 String methName = q.getQueryMethod().getMethodName(); 143 if (methd.matchPattern(null, methName, q.getQueryMethod().getMethodParams()) 144 != MethodDesc.APPLY_TO_NOTHING) { 145 foundMatch = true; 146 String query = q.getEjbQl(); 147 if (!(methd instanceof MethodCmp2Desc)) { 148 throw new DeploymentDescException("ejbql query " + query + " can't apply to method " 149 + methName + " in bean " + ejbName); 150 } 151 try { 152 ((MethodCmp2Desc) methd).setQuery(query); 153 } catch (ParseException e) { 154 throw new DeploymentDescException("Invalid ejbql syntax for bean " + ejbName + ":\n" 155 + e.getMessage(query)); 156 } 157 if (q.getResultTypeMapping() != null) { 158 ((MethodCmp2Desc) methd).setResultTypeMapping(q.getResultTypeMapping()); 159 } 160 } 161 } 162 if (!foundMatch) { 163 throw new DeploymentDescException("invalid query-method definition for bean " + ejbName + "\nno such method as " 164 + MethodCmp2Desc.queryMethodElementToString(q.getQueryMethod()) + "\ncheck method name and method parameters"); 165 } 166 } 167 } 168 for (Iterator j = getMethodDescIterator(); j.hasNext();) { 170 MethodDesc md = (MethodDesc) j.next(); 171 if ((md.isFinder() || md.isEjbSelect()) && !md.isFindByPrimaryKey()) { 172 if (((MethodCmp2Desc) md).getQuery() == null) { 173 throw new DeploymentDescException("query not defined for method " + MethodDesc.toString(md.getMethod()) 174 + " of bean" + ejbName); 175 176 } 177 } 178 } 179 if (isUndefinedPK()) { 180 FieldDesc fd = this.newFieldDescInstance(); 181 fd.setName("JONASAUTOPKFIELD"); 182 fd.setPrimaryKey(true); 183 fieldDesc.put("JONASAUTOPKFIELD", fd); 184 ((FieldDesc) (fieldDesc.get("JONASAUTOPKFIELD"))).setFieldType(java.lang.Integer .class); 185 ((FieldJdbcDesc) (fieldDesc.get("JONASAUTOPKFIELD"))).setJdbcFieldName(this.getJdbcAutomaticPkFieldName()); 186 } 187 } 188 189 public DeploymentDescEjb2 getDeploymentDescEjb2() { 190 return dc2d; 191 } 192 193 196 public String getAbstractSchemaName() { 197 return abstractSchemaName; 198 } 199 200 205 private String getJormCName() { 206 if (jormClassName == null) { 207 jormClassName = BeanNaming.firstToUpperCase(abstractSchemaName); 208 } 209 return jormClassName; 210 } 211 212 217 public String getJormClassName() { 218 if (jormFQClassName == null) { 219 jormFQClassName = BeanNaming.getClassName(JORM_PACKAGE, getJormCName()); 220 } 221 return jormFQClassName; 222 } 223 224 228 public Collection getJormList() { 229 return jormMOList; 230 } 231 232 236 public void addToJormList(MetaObject jobject) { 237 jormMOList.add(jobject); 238 } 239 240 243 public void addEjbRelationshipRoleDesc(EjbRelationshipRoleDesc ersrd) { 244 ejbRelationshipRoleDesc.add(ersrd); 245 } 246 247 251 public Iterator getEjbRelationshipRoleDescIterator() { 252 return ejbRelationshipRoleDesc.iterator(); 253 } 254 255 259 public EjbRelationshipRoleDesc getEjbRelationshipRoleDesc(String cmr) { 260 for (Iterator i = ejbRelationshipRoleDesc.iterator(); i.hasNext();) { 261 EjbRelationshipRoleDesc rsr = (EjbRelationshipRoleDesc) i.next(); 262 if (rsr.hasCmrField() && cmr.equals(rsr.getCmrFieldName())) { 263 return rsr; 264 } 265 } 266 return null; 267 } 268 269 273 protected MethodDesc newMethodDescInstance(Method meth, Class classDef, int index) { 274 return new MethodCmp2Desc(this, meth, classDef, index); 275 } 276 277 282 public String getJormAccessorClassName() { 283 if (jormAccessorClassName == null) { 284 jormAccessorClassName = BeanNaming.getClassName(JORM_PACKAGE, getJormCName() + "Accessor"); 285 } 286 return jormAccessorClassName; 287 } 288 289 294 public String getJormBindingClassName() { 295 return BeanNaming.getClassName(JORM_PACKAGE, getJormCName() + "Binding"); 296 } 297 298 303 public String getFactoryClassName() { 304 return BeanNaming.getClassName(JORM_PACKAGE, "rdb." + getJormCName() + "Mapping"); 305 } 306 307 312 public String getJormPKClassName() { 313 if (! hasPrimaryKeyField() && jormPKClassName == null) { 314 jormPKClassName = BeanNaming.getClassName(JORM_PACKAGE, 315 BeanNaming.getBaseName(getPrimaryKeyClass().getName())); 316 } 317 return jormPKClassName; 318 } 319 320 325 public String getJormPNameGetterClassName() { 326 if (! hasPrimaryKeyField() && jormPNameGetterClassName == null) { 327 jormPNameGetterClassName = getJormPKClassName() + "PNG"; 328 } 329 return jormPNameGetterClassName; 330 } 331 332 337 public String getJormBinderClassName() { 338 if (jormBinderClassName == null) { 339 if (hasPrimaryKeyField()) { 340 jormBinderClassName = "org.objectweb.jorm.facility.naming.basidir.BasidBinder"; 341 } else { 342 jormBinderClassName = getJormPKClassName() + "Binder"; 343 } 344 } 345 return jormBinderClassName; 346 } 347 348 351 public boolean needJormCoherenceHelper() { 352 return ejbRelationshipRoleDesc.iterator().hasNext(); 353 } 354 355 public String getJormCoherenceHelperItfName() { 356 return "JOnAS" + ejbName + "CoherenceHelper"; 357 } 358 359 public String getJormCoherenceHelperPackageName() { 360 return BeanNaming.getPackageName(getFullDerivedBeanName()); 361 } 362 363 public String getJormCoherenceHelperFQItfName() { 364 String pn = getJormCoherenceHelperPackageName(); 365 return (pn != null && pn.length() > 0 366 ? pn + "." + getJormCoherenceHelperItfName() 367 : getJormCoherenceHelperItfName()); 368 } 369 370 374 public String toString() { 375 StringBuffer ret = new StringBuffer (); 376 ret.append(super.toString()); 377 for (Iterator i = ejbRelationshipRoleDesc.iterator(); i.hasNext(); ) { 378 ret.append("\nejbRelationshipRoleDesc[]=" + i.next()); 379 } 380 ret.append("\ngetAbstractSchemaName()=" + getAbstractSchemaName()); 381 ret.append("\ngetJormAccessorClassName() = " + getJormAccessorClassName()); 382 ret.append("\nneedJormCoherenceHelper() = " + needJormCoherenceHelper()); 383 return ret.toString(); 384 } 385 386 } 387 388 | Popular Tags |