KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > jdo > api > persistence > enhancer > meta > JDOMetaDataPropertyImpl


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24
25 package com.sun.jdo.api.persistence.enhancer.meta;
26
27 import java.util.Map JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.Collection JavaDoc;
30 import java.util.HashSet JavaDoc;
31 import java.util.HashMap JavaDoc;
32 import java.util.ArrayList JavaDoc;
33 import java.util.Iterator JavaDoc;
34 import java.util.Properties JavaDoc;
35 import java.util.StringTokenizer JavaDoc;
36
37 import java.io.PrintWriter JavaDoc;
38
39 import com.sun.jdo.api.persistence.enhancer.meta.JDOMetaDataProperties.JDOClass;
40 import com.sun.jdo.api.persistence.enhancer.meta.JDOMetaDataProperties.JDOField;
41
42 import com.sun.jdo.api.persistence.enhancer.util.Support;
43
44 /**
45  * Provides the JDO meta information based on properties.
46  */

47 //@olsen: new class
48
public class JDOMetaDataPropertyImpl extends Support
49     implements ExtendedJDOMetaData
50 {
51     // misc
52
// static final String nl = System.getProperty("line.separator", "\n");
53
// protected final PrintWriter out;
54

55     // model
56
private static final HashSet JavaDoc transientTypePrefixes = new HashSet JavaDoc();
57     private static final HashSet JavaDoc secondClassObjectTypes = new HashSet JavaDoc();
58     private static final HashSet JavaDoc mutableSecondClassObjectTypes = new HashSet JavaDoc();
59
60
61     /**
62      *
63      */

64     private final JDOMetaDataProperties properties;
65
66
67     /**********************************************************************
68      *
69      *********************************************************************/

70
71     static
72     {
73         transientTypePrefixes.add("java/");//NOI18N
74
transientTypePrefixes.add("javax/");//NOI18N
75
transientTypePrefixes.add("com/sun/jdo/");//NOI18N
76

77         mutableSecondClassObjectTypes.add("java/util/Date");//NOI18N
78
mutableSecondClassObjectTypes.add("com/sun/jdo/spi/persistence/support/sqlstore/sco/Date");//NOI18N
79
mutableSecondClassObjectTypes.add("java/sql/Date");//NOI18N
80
mutableSecondClassObjectTypes.add("com/sun/jdo/spi/persistence/support/sqlstore/sco/SqlTime");//NOI18N
81
mutableSecondClassObjectTypes.add("java/sql/Time");//NOI18N
82
mutableSecondClassObjectTypes.add("com/sun/jdo/spi/persistence/support/sqlstore/sco/SqlDate");//NOI18N
83
mutableSecondClassObjectTypes.add("java/sql/Timestamp");//NOI18N
84
mutableSecondClassObjectTypes.add("com/sun/jdo/spi/persistence/support/sqlstore/sco/SqlTimestamp");//NOI18N
85
mutableSecondClassObjectTypes.add("java/util/Collection");//NOI18N
86
mutableSecondClassObjectTypes.add("java/util/Set");//NOI18N
87
mutableSecondClassObjectTypes.add("java/util/List");//NOI18N
88
mutableSecondClassObjectTypes.add("java/util/HashSet");//NOI18N
89
mutableSecondClassObjectTypes.add("java/util/Vector");//NOI18N
90
mutableSecondClassObjectTypes.add("java/util/ArrayList");//NOI18N
91

92         secondClassObjectTypes.add("java/lang/Boolean");//NOI18N
93
secondClassObjectTypes.add("java/lang/Byte");//NOI18N
94
secondClassObjectTypes.add("java/lang/Short");//NOI18N
95
secondClassObjectTypes.add("java/lang/Integer");//NOI18N
96
secondClassObjectTypes.add("java/lang/Long");//NOI18N
97
secondClassObjectTypes.add("java/lang/Float");//NOI18N
98
secondClassObjectTypes.add("java/lang/Double");//NOI18N
99
secondClassObjectTypes.add("java/lang/Number");//NOI18N
100
secondClassObjectTypes.add("java/lang/Character");//NOI18N
101
secondClassObjectTypes.add("java/lang/String");//NOI18N
102
secondClassObjectTypes.add("java/math/BigInteger");//NOI18N
103
secondClassObjectTypes.add("java/math/BigDecimal");//NOI18N
104
secondClassObjectTypes.addAll(mutableSecondClassObjectTypes);
105
106     } //JDOMetaDataPropertyImpl.<static>
107

108
109     /**
110      * Creates an instance.
111      * @lars: out id not used anymore
112      */

113     public JDOMetaDataPropertyImpl(Properties JavaDoc properties,
114                                    PrintWriter JavaDoc out)
115        throws JDOMetaDataUserException, JDOMetaDataFatalError
116     {
117         // check arguments
118
if (properties == null) {
119             final String JavaDoc msg
120                 = "Initializing meta data: properties == null";//NOI18N
121
throw new JDOMetaDataFatalError(msg);
122         }
123         /*
124         if (out == null) {
125             final String msg
126                 = "Initializing meta data: output stream == null";//NOI18N
127             throw new JDOMetaDataFatalError(msg);
128         }
129         */

130
131         this.properties = new JDOMetaDataProperties (properties);
132         readProperties ();
133         //this.out = out;
134
}
135
136     /**
137      * Creates an instance.
138      * @lars: out id not used anymore
139      */

140     public JDOMetaDataPropertyImpl(Properties JavaDoc properties)
141        throws JDOMetaDataUserException, JDOMetaDataFatalError
142     {
143         this (properties, null);
144     }
145
146     /**
147      * Tests whether a class is known to be persistence-capable.
148      */

149     public boolean isPersistenceCapableClass(String JavaDoc classPath)
150         throws JDOMetaDataUserException, JDOMetaDataFatalError
151     {
152         //check the transient prefixes
153
for (Iterator JavaDoc i = transientTypePrefixes.iterator(); i.hasNext();) {
154             final String JavaDoc typePrefix = (String JavaDoc)i.next();
155             if (classPath.startsWith(typePrefix))
156                 return false;
157         }
158         JDOClass clazz = getJDOClass (classPath);
159         return (clazz != null ? clazz.isPersistent () : false);
160     }
161
162     /**********************************************************************
163      *
164      *********************************************************************/

165
166     public boolean isTransientClass(String JavaDoc classPath)
167         throws JDOMetaDataUserException, JDOMetaDataFatalError
168     {
169         return ! isPersistenceCapableClass (classPath);
170     }
171
172     /**
173      * Tests whether a class is known as a persistence-capable root class.
174      */

175     public boolean isPersistenceCapableRootClass(String JavaDoc classPath)
176         throws JDOMetaDataUserException, JDOMetaDataFatalError
177     {
178         //@lars
179
if ( ! isPersistenceCapableClass (classPath))
180         {
181             return false;
182         }
183         String JavaDoc superclass = getSuperClass (classPath);
184         return (superclass != null ? ! isPersistenceCapableClass (superclass) : true);
185
186         //^olsen: exchange dummy implementation
187
// return isPersistenceCapableClass(classPath);
188
}
189
190     /**
191      * Returns the name of the persistence-capable root class of a class.
192      */

193     public String JavaDoc getPersistenceCapableRootClass(String JavaDoc classPath)
194         throws JDOMetaDataUserException, JDOMetaDataFatalError
195     {
196         //^olsen: exchange dummy implementation
197
return (isPersistenceCapableClass(classPath) ? classPath : null);
198     }
199
200     /**
201      * Returns the superclass of a class.
202      */

203
204     public final String JavaDoc getSuperClass (String JavaDoc classname)
205     {
206
207         JDOClass clazz = getJDOClass (classname);
208         return (clazz != null ? clazz.getSuperClassName () : null);
209
210     } //JDOMetaDataPropertyImpl.getSuperClass()
211

212
213     /**
214      * Tests whether a type is known for Second Class Objects.
215      */

216     public boolean isSecondClassObjectType(String JavaDoc classPath)
217         throws JDOMetaDataUserException, JDOMetaDataFatalError
218     {
219         return secondClassObjectTypes.contains(classPath);
220     }
221
222     /**
223      * Tests whether a type is known for Mutable Second Class Objects.
224      */

225     public boolean isMutableSecondClassObjectType(String JavaDoc classPath)
226         throws JDOMetaDataUserException, JDOMetaDataFatalError
227     {
228         return mutableSecondClassObjectTypes.contains(classPath);
229     }
230
231     /**
232      * Tests whether a field of a class is known to be persistent.
233      */

234     public boolean isPersistentField(String JavaDoc classPath, String JavaDoc fieldName)
235         throws JDOMetaDataUserException, JDOMetaDataFatalError
236     {
237         JDOField field = getJDOField (classPath, fieldName);
238         return (field != null ? field.isPersistent () : false);
239     }
240
241     /**
242      * Tests whether a field of a class is known to be transactional.
243      */

244     public boolean isTransactionalField(String JavaDoc classPath, String JavaDoc fieldName)
245         throws JDOMetaDataUserException, JDOMetaDataFatalError
246     {
247         JDOField field = getJDOField (classPath, fieldName);
248         return (field != null ? field.isTransactional () : false);
249     }
250
251     /**
252      * Tests whether a field of a class is known to be Primary Key.
253      */

254     public boolean isPrimaryKeyField(String JavaDoc classPath, String JavaDoc fieldName)
255         throws JDOMetaDataUserException, JDOMetaDataFatalError
256     {
257         JDOField field = getJDOField (classPath, fieldName);
258         return (field != null ? field.isPk () : false);
259     }
260
261     /**
262      * Tests whether a field of a class is known to be part of the
263      * Default Fetch Group.
264      */

265     public boolean isDefaultFetchGroupField(String JavaDoc classPath, String JavaDoc fieldName)
266         throws JDOMetaDataUserException, JDOMetaDataFatalError
267     {
268         JDOField field = getJDOField (classPath, fieldName);
269         return (field != null ? field.isInDefaultFetchGroup () : false);
270     }
271
272     /**
273      * Returns the unique field index of a declared, persistent field of a
274      * class.
275      */

276     public int getFieldNo(String JavaDoc classPath, String JavaDoc fieldName)
277         throws JDOMetaDataUserException, JDOMetaDataFatalError
278     {
279         JDOClass clazz = getJDOClass (classPath);
280         return (clazz != null ? clazz.getIndexOfField (fieldName) : -1);
281     }
282
283     /**
284      * Returns an array of field names of all declared, persistent fields
285      * of a class.
286      */

287     public String JavaDoc [] getManagedFields (String JavaDoc classname)
288     {
289
290         JDOClass clazz = getJDOClass (classname);
291         return (clazz != null ? clazz.getManagedFieldNames () : new String JavaDoc [] {});
292
293     } //JDOMetaDataPropertyImpl.getManagedFields()
294

295
296     /**********************************************************************
297      * No interface method.
298      *********************************************************************/

299
300     public final String JavaDoc [] getKnownClasses ()
301     {
302
303         return this.properties.getKnownClassNames ();
304
305     } //JDOMetaDataPropertyImpl.getKnownClasses()
306

307
308     /**********************************************************************
309      * Gets all known fields of a class.
310      *********************************************************************/

311
312     public final String JavaDoc [] getKnownFields (String JavaDoc classname)
313     {
314
315         JDOClass clazz = getJDOClass (classname);
316         return (clazz != null ? clazz.getFields () : new String JavaDoc [] {});
317
318     } //JDOMetaDataPropertyImpl.getKnownFields()
319

320
321     /**********************************************************************
322      * Gets the access modifier of a class.
323      *********************************************************************/

324
325     public final int getClassModifiers (String JavaDoc classname)
326     {
327
328         JDOClass clazz = getJDOClass (classname);
329         return (clazz != null ? clazz.getModifiers () : 0);
330
331     } //JDOMetaDataPropertyImpl.getClassModifiers()
332

333
334     /**********************************************************************
335      * Gets the access modifier of a field.
336      *********************************************************************/

337
338     public final int getFieldModifiers (String JavaDoc classname,
339                                         String JavaDoc fieldname)
340     {
341
342         JDOField field = getJDOField (classname, fieldname);
343         return (field != null ? field.getModifiers () : 0);
344
345     } //JDOMetaDataPropertyImpl.getFieldModifiers()
346

347
348     /**********************************************************************
349      *
350      *********************************************************************/

351
352     public final String JavaDoc getFieldType (String JavaDoc classname,
353                                       String JavaDoc fieldname)
354     {
355
356         JDOField field = getJDOField (classname, fieldname);
357         return (field != null ? field.getType () : null);
358
359     } //JDOMetaDataPropertyImpl.getFieldType()
360

361
362     /**********************************************************************
363      *
364      *********************************************************************/

365
366     private final JDOClass getJDOClass (String JavaDoc classname)
367                            throws JDOMetaDataUserException
368     {
369
370         return this.properties.getJDOClass (classname);
371
372     } //JDOMetaDataPropertyImpl.getJDOClass()
373

374
375     /**********************************************************************
376      *
377      *********************************************************************/

378
379     private final void readProperties ()
380     {
381
382         //read all classes
383
String JavaDoc [] classnames = this.properties.getKnownClassNames ();
384         for (int i = classnames.length - 1; i >= 0; i--)
385         {
386             JDOClass clazz = getJDOClass (classnames [i]); //should be always != null
387

388             //if the class is persistence it cannot be a second class object type
389
if (clazz.isPersistent () && secondClassObjectTypes.contains (clazz.getName ()))
390             {
391                 throw new JDOMetaDataUserException ("ERROR: Parsing meta data properties: " +
392                                                     "The persistent-capable class '" + clazz.getName () +
393                                                     "' is second class object type.");
394             }
395         }
396
397     } //JDOMetaDataPropertyImpl.readProperties()
398

399
400     /**********************************************************************
401      *
402      *********************************************************************/

403
404     private final JDOField getJDOField (String JavaDoc classname,
405                                         String JavaDoc fieldname)
406     {
407
408         JDOClass clazz = getJDOClass (classname);
409         return (clazz != null ? clazz.getField (fieldname) : null);
410
411     } //JDOMetaDataPropertyImpl.getJDOField()
412

413
414     /**********************************************************************
415      *
416      *********************************************************************/

417
418     public String JavaDoc getKeyClass(String JavaDoc classPath)
419         throws JDOMetaDataUserException, JDOMetaDataFatalError
420     {
421         final JDOClass clazz = getJDOClass(classPath);
422         return (clazz != null ? clazz.getOidClassName() : null);
423     }
424
425
426     /**********************************************************************
427      *
428      *********************************************************************/

429
430     public boolean isKeyField(String JavaDoc classPath, String JavaDoc fieldName)
431         throws JDOMetaDataUserException, JDOMetaDataFatalError
432     {
433         final JDOField field = getJDOField(classPath, fieldName);
434         return (field != null ? field.isPk() : false);
435     }
436
437
438     /**********************************************************************
439      *
440      *********************************************************************/

441
442     public boolean isKnownNonManagedField(String JavaDoc classPath,
443                                           String JavaDoc fieldName,
444                                           String JavaDoc fieldSig)
445     {
446         final JDOClass clazz = getJDOClass(classPath);
447         if (clazz == null) {
448             return true;
449         }
450         final JDOField field = clazz.getField(fieldName);
451         return (field != null ? field.isKnownTransient() : false);
452     }
453
454
455     /**********************************************************************
456      *
457      *********************************************************************/

458
459     public boolean isManagedField(String JavaDoc classPath, String JavaDoc fieldName)
460         throws JDOMetaDataUserException, JDOMetaDataFatalError
461     {
462         return (isPersistentField(classPath, fieldName)
463                 || isTransactionalField(classPath, fieldName));
464     }
465
466
467     /**********************************************************************
468      *
469      *********************************************************************/

470
471     public int getFieldFlags(String JavaDoc classPath, String JavaDoc fieldName)
472         throws JDOMetaDataUserException, JDOMetaDataFatalError
473     {
474         if (!isManagedField(classPath, fieldName)) {
475             affirm(!isTransactionalField(classPath, fieldName));
476             affirm(!isPersistentField(classPath, fieldName));
477             affirm(!isKeyField(classPath, fieldName));
478             affirm(!isDefaultFetchGroupField(classPath, fieldName));
479             return 0;
480         }
481         //affirm(isManagedField(classPath, fieldName));
482

483         if (isTransactionalField(classPath, fieldName)) {
484             affirm(!isPersistentField(classPath, fieldName));
485             affirm(!isKeyField(classPath, fieldName));
486             // ignore any dfg membership of transactional fields
487
//affirm(!isDefaultFetchGroupField(classPath, fieldName));
488
return CHECK_WRITE;
489         }
490         //affirm(!isTransactionalField(classPath, fieldName));
491
affirm(isPersistentField(classPath, fieldName));
492
493         if (isKeyField(classPath, fieldName)) {
494             // ignore any dfg membership of key fields
495
//affirm(!isDefaultFetchGroupField(classPath, fieldName));
496
return MEDIATE_WRITE;
497         }
498         //affirm(!isKeyField(classPath, fieldName));
499

500         if (isDefaultFetchGroupField(classPath, fieldName)) {
501             return CHECK_READ | CHECK_WRITE;
502         }
503         //affirm(!isDefaultFetchGroupField(classPath, fieldName));
504

505         return MEDIATE_READ | MEDIATE_WRITE;
506     }
507
508
509     /**********************************************************************
510      *
511      *********************************************************************/

512
513     public int[] getFieldFlags(String JavaDoc classPath, String JavaDoc[] fieldNames)
514         throws JDOMetaDataUserException, JDOMetaDataFatalError
515     {
516         final int n = (fieldNames != null ? fieldNames.length : 0);
517         final int[] flags = new int[n];
518         for (int i = 0; i < n; i++) {
519             flags[i] = getFieldFlags(classPath, fieldNames[i]);
520         }
521         return flags;
522     }
523
524
525     /**********************************************************************
526      *
527      *********************************************************************/

528
529     public final String JavaDoc[] getFieldType(String JavaDoc classname,
530                                         String JavaDoc[] fieldnames)
531     {
532         final int n = (fieldnames != null ? fieldnames.length : 0);
533         final String JavaDoc[] types = new String JavaDoc[n];
534         for (int i = 0; i < n; i++) {
535             types[i] = getFieldType(classname, fieldnames[i]);
536         }
537         return types;
538     }
539
540
541     /**********************************************************************
542      *
543      *********************************************************************/

544
545     public int[] getFieldNo(String JavaDoc classPath, String JavaDoc[] fieldNames)
546         throws JDOMetaDataUserException, JDOMetaDataFatalError
547     {
548         final int n = (fieldNames != null ? fieldNames.length : 0);
549         final int[] flags = new int[n];
550         for (int i = 0; i < n; i++) {
551             flags[i] = getFieldNo(classPath, fieldNames[i]);
552         }
553         return flags;
554     }
555
556
557     /**********************************************************************
558      *
559      *********************************************************************/

560
561     public String JavaDoc[] getKeyFields(String JavaDoc classPath)
562         throws JDOMetaDataUserException, JDOMetaDataFatalError
563     {
564         final List JavaDoc keys = new ArrayList JavaDoc();
565         final String JavaDoc[] fieldNames = getManagedFields(classPath);
566         final int n = fieldNames.length;
567         for (int i = 0; i < n; i++) {
568             if (isKeyField(classPath, fieldNames[i])) {
569                 keys.add(fieldNames[i]);
570             }
571         }
572         return (String JavaDoc[])keys.toArray(new String JavaDoc[keys.size()]);
573     }
574
575
576     /**********************************************************************
577      *
578      *********************************************************************/

579
580     public String JavaDoc getPersistenceCapableSuperClass(String JavaDoc classPath)
581         throws JDOMetaDataUserException, JDOMetaDataFatalError
582     {
583         for (String JavaDoc clazz = getSuperClass(classPath);
584              clazz != null;
585              clazz = getSuperClass(clazz)) {
586             if (isPersistenceCapableClass(clazz)) {
587                 return clazz;
588             }
589         }
590         return null;
591     }
592
593
594     /**********************************************************************
595      *
596      *********************************************************************/

597
598     public String JavaDoc getSuperKeyClass(String JavaDoc classPath)
599         throws JDOMetaDataUserException, JDOMetaDataFatalError
600     {
601         for (String JavaDoc superClass = getPersistenceCapableSuperClass(classPath);
602              superClass != null;
603              superClass = getPersistenceCapableSuperClass(superClass)) {
604             final String JavaDoc superKeyClass = getKeyClass(superClass);
605             if (superKeyClass != null) {
606                 return superKeyClass;
607             }
608         }
609         return null;
610     }
611
612
613     /**********************************************************************
614      *
615      *********************************************************************/

616 /*
617     public static void main (String [] argv)
618     {
619
620         if (argv.length != 1)
621         {
622             System.err.println ("No property file specified.");
623             return;
624         }
625         Properties p = new Properties ();
626         try
627         {
628             java.io.InputStream in = new java.io.FileInputStream (new java.io.File (argv [0]));
629             p.load (in);
630             in.close ();
631             System.out.println ("PROPERTIES: " + p);
632             System.out.println ("############");
633             JDOMetaDataProperties props = new JDOMetaDataProperties (p);
634         }
635         catch (Throwable ex)
636         {
637             ex.printStackTrace (System.err);
638         }
639
640         JDOMetaDataPropertyImpl jdo = new JDOMetaDataPropertyImpl (p, new PrintWriter (System.out));
641         String [] classnames = jdo.getKnownClasses ();
642         for (int k = 0; k < classnames.length; k++)
643         {
644             String classname = classnames [k];
645             System.out.println ("CLASSNAME: " + classname);
646             System.out.println ("\tpersistent: " + jdo.isPersistenceCapableClass (classname));
647             System.out.println ("\tpersistent root: " + jdo.isPersistenceCapableRootClass (classname));
648             System.out.println ("\tpersistent root: " + jdo.getPersistenceCapableRootClass (classname));
649             String [] fieldnames = jdo.getKnownFields (classname);
650             for (int j = 0; j < fieldnames.length; j++)
651             {
652                 String fieldname = (String) fieldnames [j];
653                 System.out.println ("FIELDNAME: " + fieldname);
654                 System.out.println ("\tpersistent field: " + jdo.isPersistentField (classname, fieldname));
655                 System.out.println ("\tpk field: " + jdo.isPrimaryKeyField (classname, fieldname));
656                 System.out.println ("\tdfg field: " + jdo.isDefaultFetchGroupField (classname, fieldname));
657                 System.out.println ("\tnumber: " + jdo.getFieldNo (classname, fieldname));
658                 String [] names = jdo.getManagedFields (classname);
659                 final int n = (fieldnames != null ? names.length : 0);
660                 System.out.println ("managed fields: number: " + n);
661                 for (int i = 0; i < n; i++)
662                 {
663                     System.out.println (i + ": " + names [i] +
664                                         " number: " + jdo.getFieldNo (classname, names [i]) +
665                                         " pk: " + jdo.isPrimaryKeyField (classname, names [i]) +
666                                         " dfg: " + jdo.isDefaultFetchGroupField (classname, names [i]));
667                 }
668             }
669         }
670
671     } //JDOMetaDataPropertyImpl.main
672 */

673
674 } //JDOMetaDataPropertyImpl
675

676
677 //JDOMetaDataPropertyImpl
678
Popular Tags