EntityCmp2Desc.java

00001 
00027 package org.objectweb.jonas_ejb.deployment.api;
00028 
00029 import java.util.ArrayList;
00030 import java.util.Collection;
00031 import java.util.Iterator;
00032 import java.lang.reflect.Field;
00033 import java.lang.reflect.Method;
00034 import org.objectweb.jonas_ejb.deployment.xml.AssemblyDescriptor;
00035 import org.objectweb.jonas_ejb.deployment.xml.Entity;
00036 import org.objectweb.jonas_ejb.deployment.xml.JonasEntity;
00037 import org.objectweb.jonas_ejb.deployment.xml.Query;
00038 import org.objectweb.jonas_ejb.deployment.ejbql.ParseException;
00039 import org.objectweb.jonas_ejb.lib.BeanNaming;
00040 import org.objectweb.jonas_lib.deployment.api.DeploymentDescException;
00041 import org.objectweb.jonas_lib.deployment.xml.JLinkedList;
00042 import org.objectweb.jorm.metainfo.api.MetaObject;
00043 
00044 
00052 public abstract class EntityCmp2Desc extends EntityCmpDesc {
00053 
00054     private ArrayList ejbRelationshipRoleDesc = new ArrayList();
00055     private ArrayList jormMOList = new ArrayList();
00056     protected String abstractSchemaName;
00057 
00058     private static final String JORM_PACKAGE = "jorm";
00059 
00060     private String jormClassName = null;
00061     private String jormFQClassName = null;
00062     private String jormAccessorClassName = null;
00063     private String jormPKClassName = null;
00064     private String jormPNameGetterClassName = null;
00065     private String jormBinderClassName = null;
00066 
00067 
00068     protected String factoryClassName = null;
00069     protected DeploymentDescEjb2 dc2d = null;
00070 
00075     public EntityCmp2Desc(ClassLoader classLoader,
00076                           Entity ent,
00077                           AssemblyDescriptor asd,
00078                           JonasEntity jEnt,
00079                           DeploymentDescEjb2 dc2d,
00080                           JLinkedList jMDRList,
00081                                       String fileName)
00082         throws DeploymentDescException {
00083 
00084         super(classLoader, ent, asd, jEnt, jMDRList, fileName);
00085         this.dc2d = dc2d;
00086 
00087         // An abstract schema name is required
00088         if ((ent.getAbstractSchemaName() == null)
00089             || (ent.getAbstractSchemaName().length() == 0)) {
00090             throw new DeploymentDescException("abstract-schema-name must be provided for bean " + this.ejbName);
00091         }
00092         abstractSchemaName = ent.getAbstractSchemaName();
00093 
00094         // check if persistent fields exist
00095         if (fieldDesc.isEmpty()) {
00096             throw new DeploymentDescException("No cmp-field defined in bean " + this.ejbName);
00097         }
00098         // check if persistent fields map to getter and setter
00099         for (Iterator i = fieldDesc.keySet().iterator(); i.hasNext();) {
00100             String fn = (String) i.next();
00101 
00102             // field should not be defined
00103             try {
00104                 Field f = ejbClass.getField(fn);
00105                 throw new DeploymentDescException("In cmp-version 2.x, field-name " + fn + " should not be defined in bean " + this.ejbName);
00106             } catch (NoSuchFieldException e) {
00107                 // this is what we expect: nothing to do
00108             } catch (SecurityException e) {
00109                 throw new DeploymentDescException("Cannot use java reflexion on " + this.ejbClass.getName());
00110             }
00111 
00112             try {
00113                 Method getter = null;
00114                 try {
00115                     getter = ejbClass.getMethod(FieldDesc.getGetterName(fn), (Class[]) null);
00116                     ((FieldDesc) (fieldDesc.get(fn))).setFieldType(getter.getReturnType());
00117                 } catch (NoSuchMethodException e) {
00118                     throw new DeploymentDescException("Getter method not found for field-name " + fn + " in bean " + this.ejbName, e);
00119                 }
00120                 try {
00121                     ejbClass.getMethod(FieldDesc.getSetterName(fn), new Class[]{getter.getReturnType()});
00122                 } catch (NoSuchMethodException e) {
00123                     throw new DeploymentDescException("Setter method not found for field-name " + fn + " in bean " + this.ejbName, e);
00124                 }
00125             } catch (SecurityException e) {
00126                 throw new DeploymentDescException("Cannot use java reflexion on " + this.ejbClass.getName());
00127             }
00128         }
00129         
00130         // isModifiedMethod deprecated for CMP 2.x
00131         if (jEnt.getIsModifiedMethodName() != null) {
00132             throw new DeploymentDescException("use of is-modified-method-name deprecated for CMP 2.x");
00133         }  
00134 
00135         // EJB-QL query
00136         if (ent.getQueryList() != null) {
00137             for (Iterator i = ent.getQueryList().iterator(); i.hasNext();) {
00138                 Query q = (Query) i.next();
00139                 boolean foundMatch = false;
00140                 for (Iterator j = getMethodDescIterator(); j.hasNext();) {
00141                     MethodDesc methd = (MethodDesc) j.next();
00142                     String methName = q.getQueryMethod().getMethodName();
00143                     if (methd.matchPattern(null, methName, q.getQueryMethod().getMethodParams())
00144                         != MethodDesc.APPLY_TO_NOTHING) {
00145                         foundMatch = true;
00146                         String query = q.getEjbQl();
00147                         if (!(methd instanceof MethodCmp2Desc)) {
00148                             throw new DeploymentDescException("ejbql query " + query + " can't apply to method "
00149                                                               + methName + " in bean " + ejbName);
00150                         }
00151                         try {
00152                             ((MethodCmp2Desc) methd).setQuery(query);
00153                         } catch (ParseException e) {
00154                             throw new DeploymentDescException("Invalid ejbql syntax for bean " + ejbName + ":\n"
00155                                                               + e.getMessage(query));
00156                         }
00157                         if (q.getResultTypeMapping() != null) {
00158                             ((MethodCmp2Desc) methd).setResultTypeMapping(q.getResultTypeMapping());
00159                         }
00160                     }
00161                 }
00162                 if (!foundMatch) {
00163                     throw new DeploymentDescException("invalid query-method definition for bean " + ejbName + "\nno such method as "
00164                                                       + MethodCmp2Desc.queryMethodElementToString(q.getQueryMethod()) + "\ncheck method name and method parameters");
00165                 }
00166             }
00167         }
00168         // check that all finder/selecter methods but findByPrimaryKey have a non null query
00169         for (Iterator j = getMethodDescIterator(); j.hasNext();) {
00170             MethodDesc md = (MethodDesc) j.next();
00171             if ((md.isFinder() || md.isEjbSelect()) && !md.isFindByPrimaryKey()) {
00172                 if (((MethodCmp2Desc) md).getQuery() == null) {
00173                     throw new DeploymentDescException("query not defined for method " + MethodDesc.toString(md.getMethod())
00174                                                       + " of bean" + ejbName);
00175 
00176                 }
00177             }
00178         }
00179         if (isUndefinedPK()) {
00180            FieldDesc fd = this.newFieldDescInstance();
00181            fd.setName("JONASAUTOPKFIELD");
00182            fd.setPrimaryKey(true);
00183            fieldDesc.put("JONASAUTOPKFIELD", fd);
00184            ((FieldDesc) (fieldDesc.get("JONASAUTOPKFIELD"))).setFieldType(java.lang.Integer.class); 
00185            ((FieldJdbcDesc) (fieldDesc.get("JONASAUTOPKFIELD"))).setJdbcFieldName(this.getJdbcAutomaticPkFieldName());
00186         } 
00187     }
00188 
00189     public DeploymentDescEjb2 getDeploymentDescEjb2() {
00190         return dc2d;
00191     }
00192 
00196     public String getAbstractSchemaName() {
00197         return abstractSchemaName;
00198     }
00199 
00205     private String getJormCName() {
00206         if (jormClassName == null) {
00207             jormClassName = BeanNaming.firstToUpperCase(abstractSchemaName);
00208         }
00209         return jormClassName;
00210     }
00211 
00217     public String getJormClassName() {
00218         if (jormFQClassName == null) {
00219             jormFQClassName = BeanNaming.getClassName(JORM_PACKAGE, getJormCName());
00220         }
00221         return jormFQClassName;
00222     }
00223 
00228     public Collection getJormList() {
00229         return jormMOList;
00230     }
00231 
00236     public void addToJormList(MetaObject jobject) {
00237         jormMOList.add(jobject);
00238     }
00239 
00243     public void addEjbRelationshipRoleDesc(EjbRelationshipRoleDesc ersrd) {
00244         ejbRelationshipRoleDesc.add(ersrd);
00245     }
00246 
00251     public Iterator getEjbRelationshipRoleDescIterator() {
00252         return ejbRelationshipRoleDesc.iterator();
00253     }
00254 
00259     public EjbRelationshipRoleDesc getEjbRelationshipRoleDesc(String cmr) {
00260         for (Iterator i = ejbRelationshipRoleDesc.iterator(); i.hasNext();) {
00261             EjbRelationshipRoleDesc rsr = (EjbRelationshipRoleDesc) i.next();
00262             if (rsr.hasCmrField() && cmr.equals(rsr.getCmrFieldName())) {
00263                 return rsr;
00264             }
00265         }
00266         return null;
00267     }
00268 
00273     protected MethodDesc newMethodDescInstance(Method meth, int index) {
00274         return new MethodCmp2Desc(this, meth, index);
00275     }
00276 
00282     public String getJormAccessorClassName() {
00283         if (jormAccessorClassName == null) {
00284             jormAccessorClassName = BeanNaming.getClassName(JORM_PACKAGE, getJormCName() + "Accessor");
00285         }
00286         return jormAccessorClassName;
00287     }
00288 
00294     public String getJormBindingClassName() {
00295         return BeanNaming.getClassName(JORM_PACKAGE, "rdb." + getJormCName() + "Binding");
00296     }
00297 
00303     public String getFactoryClassName() {
00304         return BeanNaming.getClassName(JORM_PACKAGE, "rdb." + getJormCName() + "Mapping");
00305     }
00306 
00312     public String getJormPKClassName() {
00313         if (! hasPrimaryKeyField() && jormPKClassName == null) {
00314             jormPKClassName = BeanNaming.getClassName(JORM_PACKAGE, 
00315                                                       BeanNaming.getBaseName(getPrimaryKeyClass().getName()));
00316         }
00317         return jormPKClassName;
00318     }
00319 
00325     public String getJormPNameGetterClassName() {
00326         if (! hasPrimaryKeyField() && jormPNameGetterClassName == null) {
00327             jormPNameGetterClassName = getJormPKClassName() + "PNG";
00328         }
00329         return jormPNameGetterClassName;
00330     }
00331 
00337     public String getJormBinderClassName() {
00338         if (jormBinderClassName == null) {
00339             if (hasPrimaryKeyField()) {
00340                 jormBinderClassName = "org.objectweb.jorm.facility.naming.basidir.BasidBinder";
00341             } else {
00342                 jormBinderClassName = getJormPKClassName() + "Binder";
00343             }
00344         }
00345         return jormBinderClassName;
00346     }
00347 
00351     public boolean needJormCoherenceHelper() {
00352         return ejbRelationshipRoleDesc.iterator().hasNext();
00353     }
00354 
00355     public String getJormCoherenceHelperItfName() {
00356         return "JOnAS" + ejbName + "CoherenceHelper";
00357     }
00358 
00359     public String getJormCoherenceHelperPackageName() {
00360         return BeanNaming.getPackageName(getFullDerivedBeanName());
00361     }
00362 
00363     public String getJormCoherenceHelperFQItfName() {
00364         String pn = getJormCoherenceHelperPackageName();
00365         return (pn != null && pn.length() > 0
00366                 ? pn + "." + getJormCoherenceHelperItfName()
00367                 : getJormCoherenceHelperItfName());
00368     }
00369 
00374     public String toString() {
00375         StringBuffer ret = new StringBuffer();
00376         ret.append(super.toString());
00377         for (Iterator i = ejbRelationshipRoleDesc.iterator(); i.hasNext(); ) {
00378             ret.append("\nejbRelationshipRoleDesc[]=" + i.next());
00379         }
00380         ret.append("\ngetAbstractSchemaName()=" + getAbstractSchemaName());
00381         ret.append("\ngetJormAccessorClassName() = " + getJormAccessorClassName());
00382         ret.append("\nneedJormCoherenceHelper() = " + needJormCoherenceHelper());
00383         return ret.toString();
00384     }
00385 
00386 }
00387 

Generated on Tue Feb 15 15:05:44 2005 for JOnAS by  doxygen 1.3.9.1