KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas_ejb > deployment > api > EntityCmp2Desc


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999-2004 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: EntityCmp2Desc.java,v 1.30 2005/03/23 08:24:48 joaninh Exp $
23  * --------------------------------------------------------------------------
24  */

25
26
27 package org.objectweb.jonas_ejb.deployment.api;
28
29 import java.util.ArrayList JavaDoc;
30 import java.util.Collection JavaDoc;
31 import java.util.Iterator JavaDoc;
32 import java.lang.reflect.Field JavaDoc;
33 import java.lang.reflect.Method JavaDoc;
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 /**
46  * Class to hold meta-information related to an entity of type CMP version 2.
47  * Created on Jun 24, 2002
48  * @author Christophe Ney [cney@batisseurs.com] : Initial developper
49  * @author Helene Joanin : code cleanup on May 2003
50  * @author Helene Joanin : complement for legacy first version on May 2003
51  */

52 public abstract class EntityCmp2Desc extends EntityCmpDesc {
53
54     private ArrayList JavaDoc ejbRelationshipRoleDesc = new ArrayList JavaDoc();
55     private ArrayList JavaDoc jormMOList = new ArrayList JavaDoc();
56     protected String JavaDoc abstractSchemaName;
57
58     private static final String JavaDoc JORM_PACKAGE = "jorm";
59
60     private String JavaDoc jormClassName = null;
61     private String JavaDoc jormFQClassName = null;
62     private String JavaDoc jormAccessorClassName = null;
63     private String JavaDoc jormPKClassName = null;
64     private String JavaDoc jormPNameGetterClassName = null;
65     private String JavaDoc jormBinderClassName = null;
66
67
68     protected String JavaDoc factoryClassName = null;
69     protected DeploymentDescEjb2 dc2d = null;
70
71     /**
72      * constructor: called when the DeploymentDescriptor is read.
73      * Currently, called by both GenIC and createContainer.
74      */

75     public EntityCmp2Desc(ClassLoader JavaDoc classLoader,
76                           Entity ent,
77                           AssemblyDescriptor asd,
78                           JonasEntity jEnt,
79                           DeploymentDescEjb2 dc2d,
80                           JLinkedList jMDRList,
81                           String JavaDoc fileName)
82         throws DeploymentDescException {
83
84         super(classLoader, ent, asd, jEnt, jMDRList, fileName);
85         this.dc2d = dc2d;
86
87         // An abstract schema name is required
88
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         // check if persistent fields exist
95
if (fieldDesc.isEmpty()) {
96             throw new DeploymentDescException("No cmp-field defined in bean " + this.ejbName);
97         }
98         // check if persistent fields map to getter and setter
99
for (Iterator JavaDoc i = fieldDesc.keySet().iterator(); i.hasNext();) {
100             String JavaDoc fn = (String JavaDoc) i.next();
101
102             // field should not be defined
103
try {
104                 Field JavaDoc 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 JavaDoc e) {
107                 // this is what we expect: nothing to do
108
} catch (SecurityException JavaDoc e) {
109                 throw new DeploymentDescException("Cannot use java reflexion on " + this.ejbClass.getName());
110             }
111
112             try {
113                 Method JavaDoc getter = null;
114                 try {
115                     getter = ejbClass.getMethod(FieldDesc.getGetterName(fn), (Class JavaDoc[]) null);
116                     ((FieldDesc) (fieldDesc.get(fn))).setFieldType(getter.getReturnType());
117                 } catch (NoSuchMethodException JavaDoc 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 JavaDoc[]{getter.getReturnType()});
122                 } catch (NoSuchMethodException JavaDoc e) {
123                     throw new DeploymentDescException("Setter method not found for field-name " + fn + " in bean " + this.ejbName, e);
124                 }
125             } catch (SecurityException JavaDoc e) {
126                 throw new DeploymentDescException("Cannot use java reflexion on " + this.ejbClass.getName());
127             }
128         }
129
130         // isModifiedMethod deprecated for CMP 2.x
131
if (jEnt.getIsModifiedMethodName() != null) {
132             throw new DeploymentDescException("use of is-modified-method-name deprecated for CMP 2.x");
133         }
134
135         // EJB-QL query
136
if (ent.getQueryList() != null) {
137             for (Iterator JavaDoc i = ent.getQueryList().iterator(); i.hasNext();) {
138                 Query q = (Query) i.next();
139                 boolean foundMatch = false;
140                 for (Iterator JavaDoc j = getMethodDescIterator(); j.hasNext();) {
141                     MethodDesc methd = (MethodDesc) j.next();
142                     String JavaDoc methName = q.getQueryMethod().getMethodName();
143                     if (methd.matchPattern(null, methName, q.getQueryMethod().getMethodParams())
144                         != MethodDesc.APPLY_TO_NOTHING) {
145                         foundMatch = true;
146                         String JavaDoc 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         // check that all finder/selecter methods but findByPrimaryKey have a non null query
169
for (Iterator JavaDoc 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 JavaDoc.class);
185            ((FieldJdbcDesc) (fieldDesc.get("JONASAUTOPKFIELD"))).setJdbcFieldName(this.getJdbcAutomaticPkFieldName());
186         }
187     }
188
189     public DeploymentDescEjb2 getDeploymentDescEjb2() {
190         return dc2d;
191     }
192
193     /**
194      * getter for field abstractSchemaName
195      */

196     public String JavaDoc getAbstractSchemaName() {
197         return abstractSchemaName;
198     }
199
200     /**
201      * Get the Jorm Class name in JOrm Meta Info
202      * It is built from the Abstract Shema Name.
203      * @return the Jorm Class Name
204      */

205     private String JavaDoc getJormCName() {
206         if (jormClassName == null) {
207             jormClassName = BeanNaming.firstToUpperCase(abstractSchemaName);
208         }
209         return jormClassName;
210     }
211
212     /**
213      * Get the Jorm Fully Qualified Class name in JOrm Meta Info
214      * It is built from the Abstract Shema Name.
215      * @return the Jorm Class Name
216      */

217     public String JavaDoc getJormClassName() {
218         if (jormFQClassName == null) {
219             jormFQClassName = BeanNaming.getClassName(JORM_PACKAGE, getJormCName());
220         }
221         return jormFQClassName;
222     }
223
224     /**
225      * Get the list of Jorm MetaObjects to be generated
226      * @return a Collection of Jorm Meta Objects
227      */

228     public Collection JavaDoc getJormList() {
229         return jormMOList;
230     }
231
232     /**
233      * add a Jorm MetaObject in the list of Jorm Meta Objects to generate
234      * @param jobject the Jorm MetaObject to add in the list
235      */

236     public void addToJormList(MetaObject jobject) {
237         jormMOList.add(jobject);
238     }
239
240     /**
241      * Add meta-info of a relationship in which the bean is involved
242      */

243     public void addEjbRelationshipRoleDesc(EjbRelationshipRoleDesc ersrd) {
244         ejbRelationshipRoleDesc.add(ersrd);
245     }
246
247     /**
248      * Get Iterator on meta-info for all relationships for which the bean
249      * is involved in.
250      */

251     public Iterator JavaDoc getEjbRelationshipRoleDescIterator() {
252         return ejbRelationshipRoleDesc.iterator();
253     }
254
255     /**
256      * Get the EjbRelationshipRoleDesc corresponding to the given cmr field name.
257      * Return null if it doesn't exist.
258      */

259     public EjbRelationshipRoleDesc getEjbRelationshipRoleDesc(String JavaDoc cmr) {
260         for (Iterator JavaDoc 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     /**
270      * Factory method for MethodDesc.
271      * Only method with EJBQL queries are of type <code>MethodCmp2Desc</code>
272      */

273     protected MethodDesc newMethodDescInstance(Method JavaDoc meth, Class JavaDoc classDef, int index) {
274         return new MethodCmp2Desc(this, meth, classDef, index);
275     }
276
277     /**
278      * It retrieves the class name of the generated Jorm accessor interface.
279      * Used in the templates
280      * @return the fully qualified class name
281      */

282     public String JavaDoc getJormAccessorClassName() {
283         if (jormAccessorClassName == null) {
284             jormAccessorClassName = BeanNaming.getClassName(JORM_PACKAGE, getJormCName() + "Accessor");
285         }
286         return jormAccessorClassName;
287     }
288
289     /**
290      * It retrieves the class name of the generated Jorm binding.
291      * Used by the JormFactory
292      * @return the fully qualified class name
293      */

294     public String JavaDoc getJormBindingClassName() {
295         return BeanNaming.getClassName(JORM_PACKAGE, getJormCName() + "Binding");
296     }
297
298     /**
299      * It retrieves the class name of the generated Jorm mapping.
300      * Used in the JContainer to instanciate the JEntityFactory
301      * @return the fully qualified class name
302      */

303     public String JavaDoc getFactoryClassName() {
304         return BeanNaming.getClassName(JORM_PACKAGE, "rdb." + getJormCName() + "Mapping");
305     }
306
307     /**
308      * Retrieves the Jorm PK Class, in case of composite PK
309      * Used by Jorm to generate PNameGetter and Binder classes
310      * @return the fully qualified class name
311      */

312     public String JavaDoc getJormPKClassName() {
313         if (! hasPrimaryKeyField() && jormPKClassName == null) {
314             jormPKClassName = BeanNaming.getClassName(JORM_PACKAGE,
315                                                       BeanNaming.getBaseName(getPrimaryKeyClass().getName()));
316         }
317         return jormPKClassName;
318     }
319
320     /**
321      * It retrieves the class name of the generated Jorm PNameGetter interface.
322      * Used in the templates to generate CMP2 classes.
323      * @return the fully qualified class name
324      */

325     public String JavaDoc getJormPNameGetterClassName() {
326         if (! hasPrimaryKeyField() && jormPNameGetterClassName == null) {
327             jormPNameGetterClassName = getJormPKClassName() + "PNG";
328         }
329         return jormPNameGetterClassName;
330     }
331
332     /**
333      * It retrieves the class name of the generated Jorm Binder.
334      * Used in the Jorm Factory
335      * @return the fully qualified class name
336      */

337     public String JavaDoc 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     /**
349      * @return true if at least one relation
350      */

351     public boolean needJormCoherenceHelper() {
352         return ejbRelationshipRoleDesc.iterator().hasNext();
353     }
354
355     public String JavaDoc getJormCoherenceHelperItfName() {
356         return "JOnAS" + ejbName + "CoherenceHelper";
357     }
358
359     public String JavaDoc getJormCoherenceHelperPackageName() {
360         return BeanNaming.getPackageName(getFullDerivedBeanName());
361     }
362
363     public String JavaDoc getJormCoherenceHelperFQItfName() {
364         String JavaDoc pn = getJormCoherenceHelperPackageName();
365         return (pn != null && pn.length() > 0
366                 ? pn + "." + getJormCoherenceHelperItfName()
367                 : getJormCoherenceHelperItfName());
368     }
369
370     /**
371      * String representation of the object for test and debug purpose
372      * @return String representation of this object
373      */

374     public String JavaDoc toString() {
375         StringBuffer JavaDoc ret = new StringBuffer JavaDoc();
376         ret.append(super.toString());
377         for (Iterator JavaDoc 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