KickJava   Java API By Example, From Geeks To Geeks.

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


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: EntityCmpDesc.java,v 1.25 2005/04/29 21:35:54 ashah Exp $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.jonas_ejb.deployment.api;
27
28 import java.lang.reflect.Field JavaDoc;
29 import java.lang.reflect.Modifier JavaDoc;
30 import java.util.HashMap JavaDoc;
31 import java.util.Iterator JavaDoc;
32
33 import org.objectweb.jonas_ejb.deployment.xml.AssemblyDescriptor;
34 import org.objectweb.jonas_ejb.deployment.xml.Entity;
35 import org.objectweb.jonas_ejb.deployment.xml.CmpField;
36 import org.objectweb.jonas_ejb.deployment.xml.JonasEntity;
37 import org.objectweb.jonas_ejb.deployment.xml.JdbcMapping;
38 import org.objectweb.jonas_ejb.lib.BeanNaming;
39 import org.objectweb.jonas_lib.deployment.api.DeploymentDescException;
40 import org.objectweb.jonas_lib.deployment.xml.JLinkedList;
41
42 /**
43  * Base class to hold meta-information related to an entity of type CMP.
44  * @author Christophe Ney [cney@batisseurs.com] : Initial developer
45  * @author Helene Joanin
46  * @author Jerome Camilleri : automatic PK
47  */

48 public abstract class EntityCmpDesc extends EntityDesc {
49
50     protected HashMap JavaDoc fieldDesc = new HashMap JavaDoc();
51     protected String JavaDoc primaryKeyField = null;
52     protected String JavaDoc jdbcAutomaticPkFieldName = null;
53
54     /**
55      * constructor to be used by parent node.
56      * @param classLoader class loader to use to laod bean's classes
57      * @param ent entity in the standard deployment descriptor
58      * @param asd assembly-descriptor in the standard deployment descriptor
59      * @param jEnt jonas-entity in the JOnAS deployment descriptor
60      * @param fileName jar or directory containing ejb
61      * @throws DeploymentDescException in error case.
62      */

63     public EntityCmpDesc(ClassLoader JavaDoc classLoader, Entity ent,
64              AssemblyDescriptor asd, JonasEntity jEnt,
65              JLinkedList jMDRList, String JavaDoc fileName)
66         throws DeploymentDescException {
67
68         super(classLoader, ent, asd, jEnt, jMDRList, fileName);
69
70         // field descriptors for persistent fields
71
for (Iterator JavaDoc i = ent.getCmpFieldList().iterator(); i.hasNext();) {
72             String JavaDoc fn = ((CmpField) i.next()).getFieldName();
73             FieldDesc fd = this.newFieldDescInstance();
74             fd.setName(fn);
75             fd.setPrimaryKey(false);
76             fieldDesc.put(fn, fd);
77
78         }
79
80         // jdbc pk auto field name
81
JdbcMapping jm = jEnt.getJdbcMapping();
82         if ((jm != null) && (jm.getJdbcAutomaticPkFieldName() != null)) {
83             if (jm.getJdbcAutomaticPkFieldName().length() != 0) {
84                 jdbcAutomaticPkFieldName = jm.getJdbcAutomaticPkFieldName();
85             }
86         }
87         if (jdbcAutomaticPkFieldName == null) {
88             // Default value
89
jdbcAutomaticPkFieldName = "JPK_";
90         }
91
92         if (isUndefinedPK()) { // Automatic PK
93
// If Primary Field is not declared (auto generated key field)
94
primaryKeyField = "JONASAUTOPKFIELD";
95         } else if (ent.getPrimkeyField() != null) {
96             // primary key field
97
primaryKeyField = ent.getPrimkeyField();
98             FieldDesc fd = (FieldDesc) fieldDesc.get(primaryKeyField);
99             if (fd == null) {
100                 throw new DeploymentDescException("primkey-field " + primaryKeyField + " is not listed as cmp-field in bean " + this.ejbName);
101             }
102             fd.setPrimaryKey(true);
103         } else {
104             // public fields of primary key class
105
Field JavaDoc[] pcf = primaryKeyClass.getFields();
106             for (int i = 0; i < pcf.length; i++) {
107                 if (Modifier.isPublic(pcf[i].getModifiers())) {
108                     String JavaDoc pn = pcf[i].getName();
109                     // exclude serialVersionUID field for jdk1.2.1 on solaris and
110
// exclude JProbe$ for JProbe using
111
if (!pn.equals("serialVersionUID") && !pn.startsWith("JProbe$")) {
112                         FieldDesc fd = (FieldDesc) fieldDesc.get(pn);
113                         if (fd == null) {
114                             throw new DeploymentDescException("public field " + pn + " of primkey-class is not listed as cmp-field in bean " + this.ejbName);
115                         }
116                         fd.setPrimaryKey(true);
117                     }
118                 }
119             }
120         }
121         String JavaDoc packageName = BeanDesc.GENERATED_PREFIX + BeanNaming.getPackageName(getEjbClass().getName());
122         derivedBeanName = new String JavaDoc("JOnAS" + getIdentifier() + "Bean");
123         fullDerivedBeanName = BeanNaming.getClassName(packageName, derivedBeanName);
124
125         // automatic-pk
126
// used of specific tag automatic-pk is deprecated so nothing in this code was about jdbc-mapping
127
String JavaDoc primkeytype = ent.getPrimKeyClass();
128         if ((jm != null) && (jm.getAutomaticPk() != null)) {
129             // optional parameter automatic-pk
130
jdbcAutomaticPk = jm.getAutomaticPk().equalsIgnoreCase("true");
131             if (jdbcAutomaticPk && pkObjectType) {
132                 // You can't use specific tag 'automatic-pk' with prim-key-type=java.lang.Object
133
throw new DeploymentDescException("Don't use specific tag 'automatic-pk' with prim-key-type=java.lang.Object in bean " + ent.getEjbName());
134             }
135         }
136         if (pkObjectType && ent.getPrimkeyField() != null) {
137              throw new DeploymentDescException("'prim-key-field' must not be set if your prim-key-type was java.lang.Object in bean " + ent.getEjbName());
138         }
139         if (this.isAutomaticPk()) { // Check if prim-key-class type is Integer or java.lang.Object
140
if (!(primkeytype.equalsIgnoreCase("java.lang.Integer")
141                     || primkeytype.equalsIgnoreCase("Integer"))) {
142                 throw new DeploymentDescException("You must used java.lang.Integer type for your auto-generate primary key field in bean " + ent.getEjbName());
143             }
144         }
145     }
146
147     /**
148      * Get descriptor for a given field name
149      * Used by GenIC
150      * @param fieldName Name of the field
151      * @return Descriptor for the given field or null
152      */

153     public FieldDesc getCmpFieldDesc(String JavaDoc fieldName) {
154         return (FieldDesc) fieldDesc.get(fieldName);
155     }
156
157     /**
158      * Indicate if the primary key field is only one field with a primary-key-field
159      * defined in the DD.
160      * @return true if the primary key field is only one field
161      * with a primary-key-field defined in the DD.
162      */

163     public boolean hasSimplePkField() {
164         return primaryKeyField != null;
165     }
166
167     // TODO remove this method and keep only the hasSimplePkField method
168
public boolean hasPrimaryKeyField() {
169         return hasSimplePkField();
170     }
171
172     /**
173      * Get the primary key field for the entity.
174      * @return Field for the primary key
175      */

176     public FieldDesc getSimplePkField() {
177         FieldDesc fd = (FieldDesc) fieldDesc.get(getSimplePkFieldName());
178         return fd;
179     }
180
181     /**
182      * Get the primary key field name for the entity.
183      * @return Field for the primary key
184      */

185     public String JavaDoc getSimplePkFieldName() {
186         if (primaryKeyField == null) {
187             throw new Error JavaDoc("No primary key field defined for bean " + this.ejbName);
188         }
189         return primaryKeyField;
190     }
191
192     // TODO remove this method and keep only the getSimplePkFieldName() method
193
public String JavaDoc getPrimaryKeyFieldName() {
194         return getSimplePkFieldName();
195     }
196
197     /**
198      * Get the associated field for auto genarated pk field specify by user
199      * @return Name of the field where automatic pk bean is stored
200      */

201     public String JavaDoc getJdbcAutomaticPkFieldName() {
202         return jdbcAutomaticPkFieldName;
203     }
204
205     public Iterator JavaDoc getCmpFieldDescIterator() {
206         return fieldDesc.values().iterator();
207     }
208     /**
209      * String representation of the object for test purpose
210      * @return String representation of this object
211      */

212     public String JavaDoc toString() {
213         StringBuffer JavaDoc ret = new StringBuffer JavaDoc();
214         ret.append(super.toString());
215         for (Iterator JavaDoc i = fieldDesc.keySet().iterator(); i.hasNext();) {
216             String JavaDoc f = (String JavaDoc) i.next();
217             FieldDesc fd = (FieldDesc) fieldDesc.get(f);
218             ret.append("\ngetCmpFieldDesc(" + f + ")=" + fd.getClass().getName());
219             ret.append(fd.toString());
220         }
221         if (hasPrimaryKeyField()) {
222             ret.append("\ngetPrimaryKeyField()=" + getPrimaryKeyFieldName());
223         }
224         return ret.toString();
225     }
226
227     /**
228      * factory method for field descriptors
229      * @return a FieldDesc.
230      */

231     protected FieldDesc newFieldDescInstance() {
232         return new FieldJdbcDesc();
233     }
234 }
235
236
Popular Tags