KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > ejb > EJBUtils


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 package com.sun.ejb;
25
26 import java.io.*;
27 import java.security.*;
28 import java.rmi.*;
29 import javax.rmi.PortableRemoteObject JavaDoc;
30 import javax.ejb.*;
31 import javax.naming.*;
32 import java.util.SortedMap JavaDoc;
33 import java.util.Properties JavaDoc;
34 import java.util.TreeMap JavaDoc;
35 import java.util.Map JavaDoc;
36 import java.util.Collection JavaDoc;
37
38 import java.lang.reflect.Method JavaDoc;
39 import java.lang.reflect.Constructor JavaDoc;
40 import java.lang.reflect.Modifier JavaDoc;
41 import java.lang.reflect.Field JavaDoc;
42 import java.lang.reflect.InvocationTargetException JavaDoc;
43
44 import com.sun.enterprise.*;
45 import com.sun.enterprise.log.Log;
46 import com.sun.enterprise.util.ObjectInputStreamWithLoader;
47 import com.sun.ejb.containers.EJBLocalHomeImpl;
48 import com.sun.ejb.containers.EJBLocalObjectImpl;
49 import com.sun.ejb.containers.GenericEJBLocalHome;
50 import com.sun.ejb.containers.RemoteBusinessWrapperBase;
51 import com.sun.ejb.containers.BaseContainer;
52 import com.sun.ejb.containers.RemoteBusinessIntfInvocationHandler;
53 import com.sun.ejb.portable.*;
54 import com.sun.enterprise.deployment.EjbReferenceDescriptor;
55 import com.sun.enterprise.deployment.EjbDescriptor;
56 import com.sun.ejb.codegen.RemoteGenerator;
57 import com.sun.ejb.codegen.Remote30WrapperGenerator;
58 import com.sun.ejb.codegen.SerializableBeanGenerator;
59 import com.sun.ejb.codegen.GenericHomeGenerator;
60 import com.sun.ejb.codegen.ClassGeneratorFactory;
61 import static com.sun.corba.ee.spi.codegen.Wrapper.*;
62
63 import java.util.logging.*;
64 import com.sun.logging.*;
65
66 import com.sun.ejb.base.io.IOUtils;
67
68 /**
69  * A handy class with static utility methods.
70  *
71  */

72 public class EJBUtils {
73     private static Logger _logger=null;
74     static {
75        _logger=LogDomains.getLogger(LogDomains.EJB_LOGGER);
76     }
77
78     // Internal property to force generated ejb container classes to
79
// be created during deployment time instead of dynamically. Note that
80
// this property does *not* cover RMI-IIOP stub generation.
81
// See IASEJBC.java for more details.
82
private static final String JavaDoc EJB_USE_STATIC_CODEGEN_PROP =
83         "com.sun.ejb.UseStaticCodegen";
84
85     private static final String JavaDoc REMOTE30_HOME_JNDI_SUFFIX =
86         "__3_x_Internal_RemoteBusinessHome__";
87
88     private static Boolean JavaDoc ejbUseStaticCodegen_ = null;
89
90     // Initial portion of a corba interoperable naming syntax jndi name.
91
private static final String JavaDoc CORBA_INS_PREFIX = "corbaname:";
92
93     /**
94      * Utility methods for serializing EJBs, primary keys and
95      * container-managed fields, all of which may include Remote EJB
96      * references,
97      * Local refs, JNDI Contexts etc which are not Serializable.
98      * This is not used for normal RMI-IIOP serialization.
99      * It has boolean replaceObject control, whether to call replaceObject
100      * or not
101      */

102     public static final byte[] serializeObject(Object JavaDoc obj,
103                                                boolean replaceObject)
104         throws IOException
105     {
106         return IOUtils.serializeObject(obj, replaceObject);
107     }
108
109     public static final byte[] serializeObject(Object JavaDoc obj)
110         throws IOException
111     {
112         return IOUtils.serializeObject(obj, true);
113     }
114
115     /**
116      * Utility method for deserializing EJBs, primary keys and
117      * container-managed fields, all of which may include Remote
118      * EJB references,
119      * Local refs, JNDI Contexts etc which are not Serializable.
120      */

121     public static final Object JavaDoc deserializeObject(byte[] data,
122             ClassLoader JavaDoc loader, boolean resolveObject)
123         throws Exception JavaDoc
124     {
125         return IOUtils.deserializeObject(data, resolveObject, loader);
126     }
127
128     public static final Object JavaDoc deserializeObject(byte[] data,
129                                                  ClassLoader JavaDoc loader)
130         throws Exception JavaDoc
131     {
132         return IOUtils.deserializeObject(data, true, loader);
133     }
134
135     public static boolean useStaticCodegen() {
136         synchronized (EJBUtils.class) {
137             if( ejbUseStaticCodegen_ == null ) {
138                 String JavaDoc ejbStaticCodegenProp = (String JavaDoc)
139                     java.security.AccessController.doPrivileged
140                     (new java.security.PrivilegedAction JavaDoc() {
141                         public java.lang.Object JavaDoc run() {
142                            return
143                              System.getProperty(EJB_USE_STATIC_CODEGEN_PROP);
144                         }});
145                       
146                 boolean useStaticCodegen =
147                     ( (ejbStaticCodegenProp != null) &&
148                       ejbStaticCodegenProp.equalsIgnoreCase("true"));
149                 
150                 ejbUseStaticCodegen_ = new Boolean JavaDoc(useStaticCodegen);
151
152                 _logger.log(Level.FINE, "EJB Static codegen is " +
153                             (useStaticCodegen ? "ENABLED" : "DISABLED") +
154                             " ejbUseStaticCodegenProp = " +
155                             ejbStaticCodegenProp);
156             }
157         }
158
159         return ejbUseStaticCodegen_.booleanValue();
160         
161     }
162
163     public static boolean isEjbRefCacheable(EjbReferenceDescriptor refDesc) {
164
165         // Ejb-ref is only eligible for caching if it refers to the legacy
166
// Home view and it is resolved to an ejb within the same application.
167
return ( (!isEJB30Ref(refDesc)) &&
168                  (refDesc.getEjbDescriptor() != null) );
169     }
170
171     private static String JavaDoc getClassPackageName(String JavaDoc intf) {
172         int dot = intf.lastIndexOf('.');
173         return (dot == -1) ? null : intf.substring(0, dot);
174     }
175
176     private static String JavaDoc getClassSimpleName(String JavaDoc intf) {
177         int dot = intf.lastIndexOf('.');
178         return (dot == -1) ? intf : intf.substring(dot+1);
179     }
180
181     public static String JavaDoc getGeneratedSerializableClassName(String JavaDoc beanClass) {
182         String JavaDoc packageName = getClassPackageName(beanClass);
183         String JavaDoc simpleName = getClassSimpleName(beanClass);
184         String JavaDoc generatedSimpleName = "_" + simpleName + "_Serializable";
185         return (packageName != null) ?
186             packageName + "." + generatedSimpleName : generatedSimpleName;
187     }
188
189     public static String JavaDoc getGeneratedRemoteIntfName(String JavaDoc businessIntf) {
190         String JavaDoc packageName = getClassPackageName(businessIntf);
191         String JavaDoc simpleName = getClassSimpleName(businessIntf);
192         String JavaDoc generatedSimpleName = "_" + simpleName + "_Remote";
193         return (packageName != null) ?
194             packageName + "." + generatedSimpleName : generatedSimpleName;
195     }
196
197     public static String JavaDoc getGeneratedRemoteWrapperName(String JavaDoc businessIntf) {
198         String JavaDoc packageName = getClassPackageName(businessIntf);
199         String JavaDoc simpleName = getClassSimpleName(businessIntf);
200         String JavaDoc generatedSimpleName = "_" + simpleName + "_Wrapper";
201         return (packageName != null) ?
202             packageName + "." + generatedSimpleName : generatedSimpleName;
203     }
204
205     public static String JavaDoc getGenericEJBHomeClassName() {
206         return "com.sun.ejb.codegen.GenericEJBHome_Generated";
207     }
208
209     /**
210      * Actual jndi-name under which Remote ejb factory depends on
211      * whether it's a Remote Home view or Remote Business view. This is
212      * necessary since a single session bean can expose both views and
213      * the resulting factory objects are different. These semantics are
214      * not exposed to the developer-view to keep things simpler. The
215      * developer can simply deal with a single physical jndi-name. If the
216      * target bean exposes both a Remote Home view and a Remote Business
217      * view, the developer can still use the single physical jndi-name
218      * to resolve remote ejb-refs, and we will handle the distinction
219      * internally. Of course, this is based on the assumption that the
220      * internal name is generated in a way that will not clash with a
221      * separate top-level physical jndi-name chosen by the developer.
222      *
223      * Note that it's better to delay this final jndi name translation as
224      * much as possible and do it right before the NamingManager lookup,
225      * as opposed to changing the jndi-name within the descriptor objects
226      * themselves. This way, the extra indirection will not be exposed
227      * if the descriptors are written out and they won't complicate any
228      * jndi-name equality logic.
229      *
230      */

231     public static String JavaDoc getRemoteEjbJndiName(EjbReferenceDescriptor refDesc) {
232
233         return getRemoteEjbJndiName(refDesc.isEJB30ClientView(),
234                                     refDesc.getEjbInterface(),
235                                     refDesc.getJndiName());
236     }
237
238     public static String JavaDoc getRemote30HomeJndiName(String JavaDoc jndiName) {
239         return jndiName + REMOTE30_HOME_JNDI_SUFFIX;
240     }
241
242     public static String JavaDoc getRemoteEjbJndiName(boolean businessView,
243                                               String JavaDoc interfaceName,
244                                               String JavaDoc jndiName) {
245
246         String JavaDoc returnValue = jndiName;
247
248         if( businessView ) {
249             if( jndiName.startsWith(CORBA_INS_PREFIX) ) {
250                 
251                 // In the case of a corba interoperable naming string, we
252
// need to lookup the internal remote home. We can't rely
253
// on our SerialContext Reference object
254
// (com.sun.ejb.containers.RemoteBusinessObjectFactory)
255
// to do the home lookup because we have to directly access
256
// the CosNaming service.
257
returnValue = getRemote30HomeJndiName(jndiName);
258
259             } else {
260                 if( !jndiName.endsWith("#" + interfaceName) ) {
261                     returnValue = jndiName + "#" + interfaceName;
262                 }
263             }
264         }
265
266         return returnValue;
267     }
268
269     public static Object JavaDoc resolveEjbRefObject(EjbReferenceDescriptor refDesc,
270                                              Object JavaDoc jndiObj)
271         throws NamingException {
272
273         Object JavaDoc returnObject = jndiObj;
274
275         if( refDesc.isLocal() ) {
276
277             EjbDescriptor target = refDesc.getEjbDescriptor();
278             ContainerFactory cf = Switch.getSwitch().getContainerFactory();
279             BaseContainer container = (BaseContainer)
280                 cf.getContainer(target.getUniqueId());
281
282             if( refDesc.isEJB30ClientView() ) {
283                 GenericEJBLocalHome genericLocalHome =
284                     container.getEJBLocalBusinessHome();
285                 returnObject = genericLocalHome.create(refDesc.getEjbInterface());
286             } else {
287                 returnObject = container.getEJBLocalHome();
288             }
289
290         } else {
291
292             // For the Remote case, the only time we have to do
293
// something extra with the given jndiObj is if the lookup
294
// is for a Remote 3.0 object and it was made through a
295
// corba interoperable name. In that case,
296
// the jndiObj refers to the internal Remote 3.0 Home so we
297
// still need to create a remote 30 client wrapper object.
298

299             if ( refDesc.isEJB30ClientView() &&
300                  !(jndiObj instanceof RemoteBusinessWrapperBase) ) {
301                 returnObject = EJBUtils.lookupRemote30BusinessObject
302                     (jndiObj, refDesc.getEjbInterface());
303             }
304
305         }
306
307         return returnObject;
308
309     }
310
311     public static Object JavaDoc lookupRemote30BusinessObject(Object JavaDoc jndiObj,
312                                                       String JavaDoc businessInterface)
313         throws NamingException
314         
315     {
316         Object JavaDoc returnObject = null;
317
318         try {
319             
320             ClassLoader JavaDoc loader =
321                 getBusinessIntfClassLoader(businessInterface);
322             
323             Class JavaDoc genericEJBHome = loadGeneratedGenericEJBHomeClass
324                 (loader);
325             
326             final Object JavaDoc genericHomeObj =
327                 PortableRemoteObject.narrow(jndiObj, genericEJBHome);
328             
329             // The generated remote business interface and the
330
// client wrapper for the business interface are produced
331
// dynamically. The following call must be made before
332
// any EJB 3.0 Remote business interface runtime behavior
333
// is needed in a given JVM.
334
loadGeneratedRemoteBusinessClasses(businessInterface);
335             
336             String JavaDoc generatedRemoteIntfName = EJBUtils.
337                 getGeneratedRemoteIntfName(businessInterface);
338             
339             Method JavaDoc createMethod = genericEJBHome.getMethod
340                 ("create", String JavaDoc.class);
341             
342             final java.rmi.Remote JavaDoc delegate = (java.rmi.Remote JavaDoc)
343                 createMethod.invoke(genericHomeObj,
344                                     generatedRemoteIntfName);
345             
346             returnObject = createRemoteBusinessObject
347                 (loader, businessInterface, delegate);
348             
349         } catch(Exception JavaDoc e) {
350             NamingException ne = new NamingException
351                 ("ejb ref resolution error for remote business interface"
352                  + businessInterface);
353             
354             ne.initCause(e instanceof InvocationTargetException JavaDoc ?
355                          e.getCause() : e);
356             throw ne;
357         }
358
359         return returnObject;
360                
361     }
362
363     public static void loadGeneratedRemoteBusinessClasses
364         (String JavaDoc businessInterfaceName) throws Exception JavaDoc {
365
366         ClassLoader JavaDoc appClassLoader =
367             getBusinessIntfClassLoader(businessInterfaceName);
368         
369         loadGeneratedRemoteBusinessClasses(appClassLoader,
370                                            businessInterfaceName);
371     }
372
373     public static void loadGeneratedRemoteBusinessClasses
374         (ClassLoader JavaDoc appClassLoader, String JavaDoc businessInterfaceName)
375         throws Exception JavaDoc {
376
377         String JavaDoc generatedRemoteIntfName = EJBUtils.
378             getGeneratedRemoteIntfName(businessInterfaceName);
379         
380         String JavaDoc wrapperClassName = EJBUtils.
381             getGeneratedRemoteWrapperName(businessInterfaceName);
382
383         Class JavaDoc generatedRemoteIntf = null;
384         try {
385             generatedRemoteIntf =
386                 appClassLoader.loadClass(generatedRemoteIntfName);
387         } catch(Exception JavaDoc e) {
388         }
389         
390         Class JavaDoc generatedRemoteWrapper = null;
391         try {
392             generatedRemoteWrapper =
393                 appClassLoader.loadClass(wrapperClassName);
394         } catch(Exception JavaDoc e) {
395         }
396         
397         if( (generatedRemoteIntf != null) &&
398             (generatedRemoteWrapper != null) ) {
399             return;
400         }
401         
402         if( generatedRemoteIntf == null ) {
403             
404             RemoteGenerator gen = new RemoteGenerator(appClassLoader,
405                                                       businessInterfaceName);
406
407             Class JavaDoc developerClass = appClassLoader.loadClass(businessInterfaceName);
408             generatedRemoteIntf = generateAndLoad(gen, appClassLoader, developerClass);
409
410         }
411
412         if( generatedRemoteWrapper == null ) {
413             
414             Remote30WrapperGenerator gen = new Remote30WrapperGenerator
415                 (appClassLoader, businessInterfaceName,
416                  generatedRemoteIntfName);
417                                       
418             Class JavaDoc developerClass = appClassLoader.loadClass(businessInterfaceName);
419             generatedRemoteWrapper = generateAndLoad(gen, appClassLoader, developerClass);
420         }
421
422     }
423
424     public static Class JavaDoc loadGeneratedGenericEJBHomeClass
425         (ClassLoader JavaDoc appClassLoader) throws Exception JavaDoc {
426
427         String JavaDoc className = getGenericEJBHomeClassName();
428
429         Class JavaDoc generatedGenericEJBHomeClass = null;
430         
431         try {
432             generatedGenericEJBHomeClass = appClassLoader.loadClass(className);
433         } catch(Exception JavaDoc e) {
434         }
435         
436         if( generatedGenericEJBHomeClass == null ) {
437             
438             GenericHomeGenerator gen =new GenericHomeGenerator(appClassLoader);
439                 
440
441             generatedGenericEJBHomeClass =generateAndLoad(gen, appClassLoader, EJBUtils.class);
442         }
443
444         return generatedGenericEJBHomeClass;
445     }
446
447     public static Class JavaDoc loadGeneratedSerializableClass
448         (ClassLoader JavaDoc appClassLoader,
449             String JavaDoc developerClassName) throws Exception JavaDoc {
450
451         String JavaDoc generatedSerializableClassName =
452             getGeneratedSerializableClassName(developerClassName);
453             
454
455         Class JavaDoc generatedSerializableClass = null;
456         try {
457             generatedSerializableClass =
458                 appClassLoader.loadClass(generatedSerializableClassName);
459
460         } catch(Exception JavaDoc e) {
461         }
462         
463         if( generatedSerializableClass == null ) {
464             
465             SerializableBeanGenerator gen =
466                 new SerializableBeanGenerator(appClassLoader,
467                                               developerClassName);
468
469             Class JavaDoc developerClass = appClassLoader.loadClass(developerClassName);
470             generatedSerializableClass = generateAndLoad(gen, appClassLoader, developerClass);
471
472         }
473
474         return generatedSerializableClass;
475     }
476
477     private static Class JavaDoc generateAndLoad(ClassGeneratorFactory cgf,
478                                          final ClassLoader JavaDoc loader,
479                                          final Class JavaDoc protectionDomainBase) {
480
481         cgf.evaluate();
482
483         final Properties JavaDoc props = new Properties JavaDoc();
484         if( _logger.isLoggable(Level.FINE) ) {
485
486             props.put(DUMP_AFTER_SETUP_VISITOR, "true");
487             props.put(TRACE_BYTE_CODE_GENERATION, "true");
488             props.put(USE_ASM_VERIFIER, "true");
489
490             try {
491
492                 ByteArrayOutputStream baos = new ByteArrayOutputStream();
493                 PrintStream ps = new PrintStream(baos);
494
495                 _sourceCode(ps, props);
496                 _logger.fine(baos.toString());
497
498             } catch(Exception JavaDoc e) {
499                 _logger.log(Level.FINE, "exception generating src", e);
500             }
501
502         }
503
504         return (Class JavaDoc) java.security.AccessController.doPrivileged
505             (new java.security.PrivilegedAction JavaDoc() {
506                     public java.lang.Object JavaDoc run() {
507                         return _generate(loader, protectionDomainBase.getProtectionDomain(), props);
508                     }});
509
510     }
511
512     public static RemoteBusinessWrapperBase createRemoteBusinessObject
513         (String JavaDoc businessInterface, java.rmi.Remote JavaDoc delegate)
514         throws Exception JavaDoc {
515
516         ClassLoader JavaDoc appClassLoader =
517             getBusinessIntfClassLoader(businessInterface);
518         
519         return createRemoteBusinessObject(appClassLoader,
520                                           businessInterface, delegate);
521     }
522
523
524     public static RemoteBusinessWrapperBase createRemoteBusinessObject
525         (ClassLoader JavaDoc loader, String JavaDoc businessInterface,
526          java.rmi.Remote JavaDoc delegate)
527          
528         throws Exception JavaDoc {
529
530         String JavaDoc wrapperClassName = EJBUtils.getGeneratedRemoteWrapperName
531             (businessInterface);
532
533         Class JavaDoc clientWrapperClass = loader.loadClass(wrapperClassName);
534         
535         Constructor JavaDoc ctors[] = clientWrapperClass.getConstructors();
536         
537         Constructor JavaDoc ctor = null;
538         for(Constructor JavaDoc next : ctors) {
539             if (next.getParameterTypes().length > 0 ) {
540                 ctor = next;
541                 break;
542             }
543         }
544         
545         Object JavaDoc obj = ctor.newInstance(new Object JavaDoc[]
546             { delegate, businessInterface } );
547
548         return (RemoteBusinessWrapperBase) obj;
549     }
550
551     private static ClassLoader JavaDoc getBusinessIntfClassLoader
552         (String JavaDoc businessInterface) throws Exception JavaDoc {
553         
554         ClassLoader JavaDoc contextLoader = (ClassLoader JavaDoc)
555             java.security.AccessController.doPrivileged
556             (new java.security.PrivilegedAction JavaDoc() {
557                     public java.lang.Object JavaDoc run() {
558                         // Return context class loader. If there is none,
559
// which could happen within Appclient container,
560
// return system class loader.
561
ClassLoader JavaDoc cl =
562                             Thread.currentThread().getContextClassLoader();
563                         return (cl != null) ? cl :
564                             ClassLoader.getSystemClassLoader();
565
566                     }});
567
568         final Class JavaDoc businessInterfaceClass =
569             contextLoader.loadClass(businessInterface);
570         
571         ClassLoader JavaDoc appClassLoader = (ClassLoader JavaDoc)
572             java.security.AccessController.doPrivileged
573             (new java.security.PrivilegedAction JavaDoc() {
574                     public java.lang.Object JavaDoc run() {
575                         return businessInterfaceClass.getClassLoader();
576                         
577                     }});
578
579         return appClassLoader;
580     }
581
582
583     private static boolean isEJB30Ref(EjbReferenceDescriptor refDesc) {
584         return refDesc.isEJB30ClientView();
585     }
586
587
588     public static void serializeObjectFields(Class JavaDoc clazz,
589                                              Object JavaDoc instance,
590                                              ObjectOutputStream oos)
591         throws IOException {
592
593         final ObjectOutputStream objOutStream = oos;
594
595         // Write out list of fields eligible for serialization in sorted order.
596
for(Field JavaDoc next : getSerializationFields(clazz)) {
597
598             final Field JavaDoc nextField = next;
599             final Object JavaDoc theInstance = instance;
600             try {
601                 Object JavaDoc value =
602                     java.security.AccessController.doPrivileged(
603                      new java.security.PrivilegedExceptionAction JavaDoc() {
604                          public java.lang.Object JavaDoc run() throws Exception JavaDoc {
605                              if( !nextField.isAccessible() ) {
606                                  nextField.setAccessible(true);
607                              }
608                              
609                              return nextField.get(theInstance);
610                          }
611                      });
612                 objOutStream.writeObject(value);
613             } catch(Throwable JavaDoc t) {
614                 IOException ioe = new IOException();
615                 Throwable JavaDoc cause = (t instanceof InvocationTargetException JavaDoc) ?
616                     ((InvocationTargetException JavaDoc)t).getCause() : t;
617                 ioe.initCause( cause );
618                 throw ioe;
619             }
620         }
621     }
622
623     public static void deserializeObjectFields(Class JavaDoc clazz,
624                                                Object JavaDoc instance,
625                                                ObjectInputStream ois)
626         throws IOException {
627
628         final ObjectInputStream objInputStream = ois;
629
630         // Use helper method to get sorted list of fields eligible
631
// for deserialization. This ensures that we correctly match
632
// serialized state with its corresponding field.
633
for(Field JavaDoc next : getSerializationFields(clazz)) {
634
635             try {
636
637                 final Field JavaDoc nextField = next;
638                 final Object JavaDoc value = ois.readObject();
639                 final Object JavaDoc theInstance = instance;
640                 
641                 java.security.AccessController.doPrivileged(
642                     new java.security.PrivilegedExceptionAction JavaDoc() {
643                          public java.lang.Object JavaDoc run() throws Exception JavaDoc {
644                              if( !nextField.isAccessible() ) {
645                                  nextField.setAccessible(true);
646                              }
647                              
648                              nextField.set(theInstance, value);
649                              return null;
650                          }
651                      });
652             } catch(Throwable JavaDoc t) {
653                 IOException ioe = new IOException();
654                 Throwable JavaDoc cause = (t instanceof InvocationTargetException JavaDoc) ?
655                     ((InvocationTargetException JavaDoc)t).getCause() : t;
656                 ioe.initCause( cause );
657                 throw ioe;
658             }
659         }
660     }
661
662     private static Collection JavaDoc<Field JavaDoc> getSerializationFields(Class JavaDoc clazz) {
663
664         Field JavaDoc[] fields = clazz.getDeclaredFields();
665
666         SortedMap JavaDoc<String JavaDoc, Field JavaDoc> sortedMap = new TreeMap JavaDoc<String JavaDoc, Field JavaDoc>();
667
668         for(Field JavaDoc next : fields) {
669
670             int modifiers = next.getModifiers();
671             if( Modifier.isStatic(modifiers) ||
672                 Modifier.isTransient(modifiers) ) {
673                 continue;
674             }
675
676             // All fields come from a single class(not from any superclasses),
677
// so sorting on field name is sufficient. We use natural ordering
678
// of field name java.lang.String object.
679
sortedMap.put(next.getName(), next);
680
681         }
682
683         return (Collection JavaDoc<Field JavaDoc>) sortedMap.values();
684     }
685
686 }
687
Popular Tags