KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > jdo > api > persistence > model > util > DumpMapping


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  * DumpMapping.java
26  *
27  */

28
29 package com.sun.jdo.api.persistence.model.util;
30
31 import java.util.*;
32 import java.io.PrintStream JavaDoc;
33
34 import com.sun.jdo.api.persistence.model.*;
35 import com.sun.jdo.api.persistence.model.mapping.*;
36 import com.sun.jdo.api.persistence.model.mapping.impl.*;
37 import com.sun.jdo.api.persistence.model.jdo.*;
38
39 import org.netbeans.modules.dbschema.ColumnElement;
40 import org.netbeans.modules.dbschema.ColumnPairElement;
41
42 public class DumpMapping
43 {
44     public static Model model = Model.RUNTIME;
45
46     /** Print out the cache of MappingClassElements to the specified PrintStream.
47      * @param stream PrintStream used to dump the info
48      */

49     public static void dumpMappingCache (PrintStream JavaDoc stream)
50     {
51         stream.println("Mapping cache (class names -> MappingClassElements)"); // NOI18N
52
for (Iterator i = model.getMappingCache().entrySet().iterator();
53              i.hasNext();)
54         {
55             Map.Entry entry = (Map.Entry)i.next();
56             String JavaDoc className = (String JavaDoc)entry.getKey();
57             MappingClassElement mce = (MappingClassElement)entry.getValue();
58             String JavaDoc mceRepr = mce.getClass() + "@" + // NOI18N
59
Integer.toHexString(System.identityHashCode(mce));
60             stream.println("\t" + className + " ->\t" + mceRepr); //NOI18N
61
}
62     }
63
64     /** Print out the cache of classLoaders to the specified PrintStream.
65      * @param stream PrintStream used to dump the info
66      */

67     public static void dumpClassLoaderCache (PrintStream JavaDoc stream)
68     {
69         stream.println("ClassLoader cache (class names -> ClassLoaders)"); //NOI18N
70
for (Iterator i = model.getClassLoaderCache().entrySet().iterator();
71              i.hasNext();)
72         {
73             Map.Entry entry = (Map.Entry)i.next();
74             String JavaDoc className = (String JavaDoc)entry.getKey();
75             ClassLoader JavaDoc classLoader = (ClassLoader JavaDoc)entry.getValue();
76             stream.println("\t" + className + " ->\t" + classLoader); //NOI18N
77
}
78     }
79
80     public static void main(String JavaDoc[] args)
81     {
82         for (int i = 0; i < args.length; i++)
83         {
84             String JavaDoc className = args[i];
85             println(0, "\nClass " + className + ":"); //NOI18N
86

87             try
88             {
89                 MappingClassElementImpl mce = (MappingClassElementImpl)model.getMappingClass(className);
90                 if (mce != null)
91                 {
92                     printPersistenceClassElement(mce.getPersistenceElement());
93                     printMappingClassElement(mce);
94                 }
95                 else
96                 {
97                     println(0, "Cannot find mapping info for class " + className + " (getMappingClass returns null)"); //NOI18N
98
}
99             }
100             catch (Exception JavaDoc e)
101             {
102                 println(0, "Problems during accessing mapping info for class " + className); //NOI18N
103
e.printStackTrace();
104             }
105         }
106     }
107
108     // ----- JDO model ------
109

110     public static void printPersistenceClassElement(PersistenceClassElement pce)
111     {
112         println(0, "\n--> PersistenceClassElement "); //NOI18N
113
println(1, "package = " + pce.getPackage()); //NOI18N
114
println(1, "name = " + pce.getName()); //NOI18N
115
println(1, "identity = " + getObjectIdentityTypeRepr(pce.getObjectIdentityType())); //NOI18N
116
println(1, "keyClass = " + pce.getKeyClass()); //NOI18N
117

118         printPersistenceFieldElements(1, pce.getFields());
119         printConcurrencyGroupElements(1, pce.getConcurrencyGroups());
120
121         println(0, "<-- PersistenceClassElement\n "); //NOI18N
122
}
123
124
125     public static void printPersistenceFieldElements(int tabs, PersistenceFieldElement[] fields)
126     {
127         if ((fields != null) && (fields.length > 0))
128         {
129             println(tabs, "--> fields "); //NOI18N
130
for (int i = 0; i < fields.length; i++)
131             {
132                 PersistenceFieldElement pfe = fields[i];
133                 
134                 println(tabs, "[" + i + "] " + pfe.getClass()); //NOI18N
135
println(tabs+1, "name = " + pfe.getName()); //NOI18N
136
println(tabs+1, "declaringClass = " + pfe.getDeclaringClass()); //NOI18N
137
println(tabs+1, "fieldNumber = " + pfe.getFieldNumber()); //NOI18N
138
println(tabs+1, "persistenceType = " + getPersistenceTypeRepr(pfe.getPersistenceType())); //NOI18N
139
println(tabs+1, "read / write = " + pfe.isReadSensitive() + " / " + pfe.isWriteSensitive()); //NOI18N
140
println(tabs+1, "isKey = " + pfe.isKey()); //NOI18N
141

142                 if (pfe instanceof RelationshipElement)
143                 {
144                     RelationshipElement re = (RelationshipElement) pfe;
145                     
146                     println(tabs+1, "bounds = " + re.getLowerBound() + " / " + re.getUpperBound()); //NOI18N
147
println(tabs+1, "deleteAction = " + re.getDeleteAction()); //NOI18N
148
println(tabs+1, "updateAction = " + re.getUpdateAction()); //NOI18N
149
println(tabs+1, "collectionClass = " + re.getCollectionClass()); //NOI18N
150
println(tabs+1, "elementClass = " + re.getElementClass()); //NOI18N
151
println(tabs+1, "isPrefetch = " + re.isPrefetch()); //NOI18N
152
}
153                 printConcurrencyGroupElements(tabs+1, pfe.getConcurrencyGroups());
154             }
155             println(tabs, "<-- fields "); //NOI18N
156
}
157     }
158
159     public static void printConcurrencyGroupElements(int tabs, ConcurrencyGroupElement[] groups)
160     {
161         if ((groups != null) && (groups.length > 0))
162         {
163             println(tabs, "--> concurrency groups"); //NOI18N
164
for (int i = 0; i < groups.length; i++)
165             {
166                 ConcurrencyGroupElement cg = groups[i];
167                 println(tabs, "[" + i + "] " + cg.getClass()); //NOI18N
168
println(tabs+1, "name = " + cg.getName()); //NOI18N
169
println(tabs+1, "declaringClass = " + cg.getDeclaringClass()); //NOI18N
170
println(tabs+1, "fields = " + cg.getFields()); //NOI18N
171
}
172             println(tabs, "<-- concurrency groups"); //NOI18N
173
}
174     }
175
176     // ----- Mapping model ------
177

178     public static void printMappingClassElement(MappingClassElement mce)
179     {
180         println(0, "\n--> MappingClassElement"); //NOI18N
181

182         println(1, "databaseRoot = " + mce.getDatabaseRoot()); //NOI18N
183
printMappingTableElements(1, mce.getTables());
184         printMappingFieldElements(1, mce.getFields());
185
186         println(0, "<-- MappingClassElement"); //NOI18N
187
}
188
189     public static void printMappingTableElements(int tabs, ArrayList tables)
190     {
191         final int count = ((tables != null) ? tables.size() : 0);
192
193         if (count > 0)
194         {
195             println(tabs, "--> tables "); //NOI18N
196
for (int i = 0; i < count; i++)
197             {
198                 MappingTableElementImpl mte = (MappingTableElementImpl) tables.get(i);
199
200                 println(tabs, "[" + i + "] " + mte.getClass()); //NOI18N
201

202                 println(tabs+1, "table = " + mte.getTable()); //NOI18N
203
println(tabs+1, "tableObject = " + mte.getTableObject()); //NOI18N
204
println(tabs+1, "key = " + mte.getKey()); //NOI18N
205
println(tabs+1, "keyObjects = " + mte.getKeyObjects()); //NOI18N
206
printMappingRefKeyElements(tabs+1, mte.getReferencingKeys());
207             }
208             println(tabs, "<-- tables "); //NOI18N
209
}
210      }
211
212     public static void printMappingRefKeyElements(int tabs, ArrayList refKeys)
213     {
214         final int count = ((refKeys != null) ? refKeys.size() : 0);
215
216         if (count > 0)
217         {
218             println(tabs, "--> tables "); //NOI18N
219
for (int i = 0; i < count; i++)
220             {
221                 MappingReferenceKeyElement mrke = (MappingReferenceKeyElement)refKeys.get(i);
222
223                 println(tabs, "[" + i + "] " + mrke.getClass()); //NOI18N
224

225                 println(tabs+1, "table = " + mrke.getDeclaringTable()); //NOI18N
226
println(tabs+1, "pairs = " + mrke.getColumnPairNames()); //NOI18N
227
}
228             println(tabs, "<-- tables "); //NOI18N
229
}
230      }
231
232     public static void printMappingFieldElements(int tabs, ArrayList fields)
233     {
234         final int count = ((fields != null) ? fields.size() : 0);
235
236         if (count > 0)
237         {
238             println(tabs, "--> fields "); //NOI18N
239
for (int i = 0; i < count; i++)
240             {
241                 MappingFieldElementImpl mfe = (MappingFieldElementImpl) fields.get(i);
242                 
243                 println(tabs, "[" + i + "] " + mfe.getClass()); //NOI18N
244
println(tabs+1, "name = " + mfe.getName()); //NOI18N
245
println(tabs+1, "fetchGroup = " + mfe.getFetchGroup()); //NOI18N
246
println(tabs+1, "columns = " + mfe.getColumns()); //NOI18N
247

248                 if (!(mfe instanceof MappingRelationshipElement))
249                 {
250                     println(tabs+1, "columnObjects = " + mfe.getColumnObjects()); //NOI18N
251
}
252                 else
253                 {
254                     MappingRelationshipElementImpl mre = (MappingRelationshipElementImpl) mfe;
255
256                     ArrayList columnObjects = mre.getColumnObjects();
257                     int colCount =
258                         ((columnObjects != null) ? columnObjects.size() : 0);
259                     if (colCount > 0)
260                     {
261                         println(tabs+1, "--> columnsObjects "); //NOI18N
262
for (int j = 0; j < colCount; j++)
263                         {
264                             ColumnPairElement fce = (ColumnPairElement) columnObjects.get(j);
265                             ColumnElement rce = (fce!=null)?fce.getReferencedColumn():null;
266                             println(tabs+1, "[" + j + "] " + fce + " -> " + rce); //NOI18N
267
}
268                         println(tabs+1, "<-- columnsObjects "); //NOI18N
269
}
270                     
271                     println(tabs+1, "associatedColumns = " + mre.getAssociatedColumns()); //NOI18N
272

273                     ArrayList associatedColumnObjects = mre.getAssociatedColumnObjects();
274                     colCount = ((associatedColumnObjects != null) ?
275                         associatedColumnObjects.size() : 0);
276                     if (colCount > 0)
277                     {
278                         println(tabs+1, "--> associatedColumnObjects "); //NOI18N
279
for (int j = 0; j < colCount; j++)
280                         {
281                             ColumnPairElement fce = (ColumnPairElement) associatedColumnObjects.get(j);
282                             ColumnElement rce = (fce!=null)?fce.getReferencedColumn():null;
283                             println(tabs+1, "[" + j + "] " + fce + " -> " + rce); //NOI18N
284
}
285                         println(tabs+1, "<-- associatedColumnObjects "); //NOI18N
286
}
287                 }
288             }
289             println(tabs, "<-- fields "); //NOI18N
290
}
291
292     }
293
294    // ----- helper methods -----
295

296     static String JavaDoc getObjectIdentityTypeRepr(int objectIdentityType)
297     {
298         String JavaDoc repr;
299         switch (objectIdentityType)
300         {
301         case PersistenceClassElement.APPLICATION_IDENTITY:
302             return "APPLICATION_IDENTITY"; //NOI18N
303
case PersistenceClassElement.DATABASE_IDENTITY:
304             return "DATABASE_IDENTITY_IDENTITY"; //NOI18N
305
case PersistenceClassElement.UNMANAGED_IDENTITY:
306             return "UNMANAGED_IDENTITY"; //NOI18N
307
default:
308             return "UNKNOWN"; //NOI18N
309
}
310     }
311     
312     static String JavaDoc getPersistenceTypeRepr(int persistenceType)
313     {
314         String JavaDoc repr;
315         switch (persistenceType)
316         {
317         case PersistenceFieldElement.PERSISTENT:
318             return "PERSISTENT"; //NOI18N
319
case PersistenceFieldElement.DERIVED:
320             return "DERIVED"; //NOI18N
321
case PersistenceFieldElement.TRANSIENT:
322             return "TRANSIENT"; //NOI18N
323
default:
324             return "UNKNOWN"; //NOI18N
325
}
326     }
327     
328     static void println(int indent, String JavaDoc text)
329     {
330         for (int i = 0; i < indent; i++)
331         {
332             System.out.print("\t"); //NOI18N
333
}
334         
335         System.out.println(text);
336     }
337 }
338
Popular Tags