KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > metadata > EntityMetaData


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.metadata;
23
24 import java.util.ArrayList JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.Iterator JavaDoc;
27
28 import org.w3c.dom.Element JavaDoc;
29 import org.jboss.deployment.DeploymentException;
30 import org.jboss.logging.Logger;
31 import org.jboss.util.Strings;
32
33 /**
34  * The meta data information specific to entity beans.
35  *
36  * @author <a HREF="mailto:sebastien.alborini@m4x.org">Sebastien Alborini</a>
37  * @author <a HREF="mailto:scott.stark@jboss.org">Scott Stark</a>
38  * @author <a HREF="mailto:dain@daingroup.com">Dain Sundstrom</a>
39  * @author <a HREF="mailto:bill@burkecentral.com">Bill Burke</a>
40  * @author <a HREF="mailto:criege@riege.com">Christian Riege</a>
41  *
42  * @version $Revision: 58361 $
43  *
44  * <p><b>Revisions:</b><br>
45  * <p><b>2001/10/16: billb</b>
46  * <ol>
47  * <li>Added clustering tags
48  * </ol>
49  */

50 public class EntityMetaData
51    extends BeanMetaData
52 {
53    // Constants -----------------------------------------------------
54
public final static int CMP_VERSION_1 = 1;
55    public final static int CMP_VERSION_2 = 2;
56    public static final String JavaDoc DEFAULT_ENTITY_INVOKER_PROXY_BINDING =
57       "entity-unified-invoker";
58    public static final String JavaDoc DEFAULT_CLUSTERED_ENTITY_INVOKER_PROXY_BINDING =
59       "clustered-entity-rmi-invoker";
60
61    // Attributes ----------------------------------------------------
62
private boolean cmp;
63    private String JavaDoc primaryKeyClass;
64    private boolean reentrant;
65    private int cmpVersion;
66    private String JavaDoc abstractSchemaName;
67    private ArrayList JavaDoc<String JavaDoc> cmpFields = new ArrayList JavaDoc<String JavaDoc>();
68    private String JavaDoc primKeyField;
69    private ArrayList JavaDoc<QueryMetaData> queries = new ArrayList JavaDoc<QueryMetaData>();
70    private boolean readOnly = false;
71    private boolean doDistCachInvalidations = false;
72    private CacheInvalidationConfigMetaData cacheInvalidConfig = null;
73
74    // Static --------------------------------------------------------
75
private static Logger log = Logger.getLogger( EntityMetaData.class );
76
77    // Constructors --------------------------------------------------
78
public EntityMetaData( ApplicationMetaData app )
79    {
80       super(app, BeanMetaData.ENTITY_TYPE);
81    }
82
83    // Public --------------------------------------------------------
84
public boolean isCMP()
85    {
86       return cmp;
87    }
88
89    public void setCmp(boolean cmp)
90    {
91       this.cmp = cmp;
92    }
93
94    public boolean isCMP1x()
95    {
96       return cmp && (cmpVersion==1);
97    }
98
99    public boolean isCMP2x()
100    {
101       return cmp && (cmpVersion==2);
102    }
103
104    public boolean isBMP()
105    {
106       return !cmp;
107    }
108
109
110    public String JavaDoc getAbstractSchemaName()
111    {
112       return abstractSchemaName;
113    }
114
115    public void setAbstractSchemaName(String JavaDoc abstractSchemaName)
116    {
117       this.abstractSchemaName = abstractSchemaName;
118    }
119
120    public CacheInvalidationConfigMetaData getCacheInvalidConfig()
121    {
122       return cacheInvalidConfig;
123    }
124
125    public void setCacheInvalidConfig(
126          CacheInvalidationConfigMetaData cacheInvalidConfig)
127    {
128       this.cacheInvalidConfig = cacheInvalidConfig;
129    }
130
131    public ArrayList JavaDoc<String JavaDoc> getCmpFields()
132    {
133       return cmpFields;
134    }
135
136    public void setCmpFields(ArrayList JavaDoc<String JavaDoc> cmpFields)
137    {
138       this.cmpFields = cmpFields;
139    }
140
141    public int getCmpVersion()
142    {
143       return cmpVersion;
144    }
145
146    public void setCmpVersion(int cmpVersion)
147    {
148       this.cmpVersion = cmpVersion;
149    }
150
151    public boolean isDoDistCachInvalidations()
152    {
153       return doDistCachInvalidations;
154    }
155
156    public void setDoDistCachInvalidations(boolean doDistCachInvalidations)
157    {
158       this.doDistCachInvalidations = doDistCachInvalidations;
159    }
160
161    public String JavaDoc getPrimaryKeyClass()
162    {
163       return primaryKeyClass;
164    }
165
166    public void setPrimaryKeyClass(String JavaDoc primaryKeyClass)
167    {
168       this.primaryKeyClass = primaryKeyClass;
169    }
170
171    public boolean isReadOnly()
172    {
173       return readOnly;
174    }
175
176    public void setReadOnly(boolean readOnly)
177    {
178       this.readOnly = readOnly;
179    }
180
181    public boolean isReentrant()
182    {
183       return reentrant;
184    }
185
186    public void setReentrant(boolean reentrant)
187    {
188       this.reentrant = reentrant;
189    }
190
191    public void setPrimKeyField(String JavaDoc primKeyField)
192    {
193       this.primKeyField = primKeyField;
194    }
195
196    public void setQueries(ArrayList JavaDoc queries)
197    {
198       this.queries = queries;
199    }
200
201    /**
202     * Gets the container managed fields.
203     * @returns iterator over Strings containing names of the fields
204     */

205    public Iterator JavaDoc getCMPFields()
206    {
207       return cmpFields.iterator();
208    }
209
210    public String JavaDoc getPrimKeyField()
211    {
212       return primKeyField;
213    }
214
215    public Iterator JavaDoc getQueries()
216    {
217       return queries.iterator();
218    }
219
220    public void addQuery(QueryMetaData query)
221    {
222       queries.add(query);
223    }
224
225    public String JavaDoc getDefaultConfigurationName()
226    {
227       if (isCMP())
228       {
229          if(getApplicationMetaData().isEJB2x())
230          {
231             if (isClustered())
232             {
233                return ConfigurationMetaData.CLUSTERED_CMP_2x_13;
234             }
235             else
236             {
237                return ConfigurationMetaData.CMP_2x_13;
238             }
239          }
240          else
241          {
242             if (isClustered())
243             {
244                return ConfigurationMetaData.CLUSTERED_CMP_1x_13;
245             }
246             else
247             {
248                return ConfigurationMetaData.CMP_1x_13;
249             }
250          }
251       }
252       else
253       {
254          if (isClustered())
255          {
256             return ConfigurationMetaData.CLUSTERED_BMP_13;
257          }
258          else
259          {
260             return ConfigurationMetaData.BMP_13;
261          }
262       }
263    }
264
265    public boolean doDistributedCacheInvalidations ()
266    {
267       return this.doDistCachInvalidations ;
268    }
269
270    public CacheInvalidationConfigMetaData getDistributedCacheInvalidationConfig ()
271    {
272       return this.cacheInvalidConfig ;
273    }
274
275    public void importEjbJarXml( Element JavaDoc element )
276       throws DeploymentException
277    {
278       super.importEjbJarXml(element);
279
280       // set persistence type
281
String JavaDoc persistenceType = getElementContent(getUniqueChild(element,
282          "persistence-type"));
283       if( persistenceType.equals("Bean") )
284       {
285          cmp = false;
286       }
287       else if( persistenceType.equals("Container") )
288       {
289          cmp = true;
290       }
291       else
292       {
293          throw new DeploymentException( getEjbName() + ": " +
294             "persistence-type must be 'Bean' or 'Container'!" );
295       }
296
297       // set primary key class
298
primaryKeyClass = getElementContent(getUniqueChild(element,
299          "prim-key-class"));
300
301       // set reentrant
302
reentrant = Boolean.valueOf(getElementContent(getUniqueChild(element,
303          "reentrant"))).booleanValue();
304
305       if( isCMP() )
306       {
307          // cmp-version
308
if( getApplicationMetaData().isEJB2x() )
309          {
310             String JavaDoc cmpVersionString = getElementContent(
311                getOptionalChild(element, "cmp-version"));
312
313             if( cmpVersionString == null )
314             {
315                // default for ejb 2.0 apps is cmp 2.x
316
cmpVersion = CMP_VERSION_2;
317             }
318             else
319             {
320                if( "1.x".equals(cmpVersionString) )
321                {
322                   cmpVersion = 1;
323                }
324                else if( "2.x".equals(cmpVersionString) )
325                {
326                   cmpVersion = 2;
327                }
328                else
329                {
330                   throw new DeploymentException( getEjbName() + ": " +
331                      "cmp-version must be '1.x' or '2.x', if specified" );
332                }
333             }
334          }
335          else
336          {
337             // default for 1.0 DTDs is version 2
338
cmpVersion = CMP_VERSION_1;
339          }
340
341          // abstract-schema-name
342
abstractSchemaName = getOptionalChildContent(element,
343             "abstract-schema-name");
344
345          if( cmpVersion == 2 )
346          {
347             // Enforce several restrictions on abstract-schema-name and
348
// ejb-name Elements, see bug #613360
349

350             String JavaDoc ejbName = getEjbName();
351
352             // ejb-name tests
353
if( !Strings.isValidJavaIdentifier(ejbName) )
354             {
355                throw new DeploymentException( "The ejb-name for a CMP" +
356                   "2.x Entity must be a valid Java Identifier" );
357             }
358
359             if( Strings.isEjbQlIdentifier(ejbName) )
360             {
361                log.warn( ejbName + ": The ejb-name for a CMP 2.x Entity " +
362                   "should not be a reserved EJB-QL keyword" );
363             }
364
365             // Test various things for abstract-schema-name
366
if( abstractSchemaName == null )
367             {
368                throw new DeploymentException( "The abstract-schema-name " +
369                   "must be specified for CMP 2.x Beans" );
370             }
371
372             if( !Strings.isValidJavaIdentifier(abstractSchemaName) )
373             {
374                throw new DeploymentException( "The abstract-schema-name " +
375                   "must be a valid Java Identifier '" + abstractSchemaName +
376                   "'");
377             }
378
379             if( Strings.isEjbQlIdentifier(abstractSchemaName) )
380             {
381                log.warn( ejbName + ": The abstract-schema-name should " +
382                   "not be a reserved EJB-QL Identifier '" +
383                   abstractSchemaName + "'" );
384             }
385          }
386
387          // cmp-fields
388
Iterator JavaDoc iterator = getChildrenByTagName( element, "cmp-field" );
389          while( iterator.hasNext() )
390          {
391             Element JavaDoc field = (Element JavaDoc)iterator.next();
392             cmpFields.add(getElementContent(getUniqueChild(field,
393                "field-name")));
394          }
395
396          // set the primary key field
397
primKeyField = getElementContent(getOptionalChild(element,
398             "primkey-field"));
399          if( primKeyField != null && !cmpFields.contains(primKeyField) )
400          {
401             // FIXME: include ejb-name
402
throw new DeploymentException( "primkey-field " + primKeyField +
403                " is not a cmp-field");
404          }
405
406          // queries
407
iterator = getChildrenByTagName(element, "query");
408          while( iterator.hasNext() )
409          {
410             Element JavaDoc queryElement = (Element JavaDoc) iterator.next();
411
412             QueryMetaData queryMetaData = new QueryMetaData();
413             queryMetaData.importEjbJarXml(queryElement);
414
415             queries.add(queryMetaData);
416          }
417       }
418    }
419
420    protected void defaultInvokerBindings()
421    {
422       this.invokerBindings = new HashMap JavaDoc();
423       if( isClustered() )
424       {
425          this.invokerBindings.put(
426             DEFAULT_CLUSTERED_ENTITY_INVOKER_PROXY_BINDING, getJndiName());
427       }
428       else
429       {
430          this.invokerBindings.put(
431             DEFAULT_ENTITY_INVOKER_PROXY_BINDING, getJndiName());
432       }
433    }
434
435    public void importJbossXml( Element JavaDoc element )
436       throws DeploymentException
437    {
438       super.importJbossXml(element);
439       // set readonly
440
String JavaDoc readOnlyString = getElementContent(getOptionalChild(
441          element, "read-only"));
442       if( readOnlyString != null )
443       {
444          readOnly = Boolean.valueOf(readOnlyString).booleanValue();
445       }
446       // Manage distributed cache-invalidation settings
447
//
448
String JavaDoc distCacheInvalidations = getElementContent(getOptionalChild( element,
449          "cache-invalidation"), (this.doDistCachInvalidations ? "True" : "False") );
450       this.doDistCachInvalidations = distCacheInvalidations.equalsIgnoreCase ("True");
451
452       Element JavaDoc cacheInvalidConfigElement = getOptionalChild(element,
453          "cache-invalidation-config");
454
455       this.cacheInvalidConfig = new CacheInvalidationConfigMetaData();
456       this.cacheInvalidConfig.init(this);
457       if (cacheInvalidConfigElement != null)
458       {
459          this.cacheInvalidConfig.importJbossXml(cacheInvalidConfigElement);
460       }
461
462
463    }
464
465    // Package protected ---------------------------------------------
466

467    // Protected -----------------------------------------------------
468

469    // Private -------------------------------------------------------
470

471    // Inner classes -------------------------------------------------
472
}
473 /*
474 vim:ts=3:sw=3:et
475 */

476
Popular Tags