1 28 package org.objectweb.carol.util.perfs; 29 30 import java.io.ByteArrayOutputStream ; 31 import java.lang.reflect.Method ; 32 import java.rmi.server.RMIClassLoaderSpi ; 33 34 import sun.rmi.server.MarshalOutputStream; 35 36 42 public class CarolJRMPPerformanceHelper { 43 44 private static RMIClassLoaderSpi defRMISpi; 45 46 static { 47 try { 48 Class cl = Class.forName("java.rmi.server.RMIClassLoader"); 49 Method meth = cl.getMethod("getDefaultProviderInstance", new Class [0]); 50 defRMISpi = (RMIClassLoaderSpi ) meth.invoke(cl, new Object [0]); 51 } catch (Exception e) { 52 } 54 } 55 56 61 public static String getMarshalBytes(Object obj) { 62 try { 63 String result = getClassString(obj.getClass()) + "<serialization>\n"; 64 ByteArrayOutputStream ostream = new ByteArrayOutputStream (); 66 MarshalOutputStream p = new MarshalOutputStream(ostream); 67 p.writeObject(obj); 68 p.flush(); 69 byte[] b = ostream.toByteArray(); 70 for (int i = 0; i < b.length; i++) { 71 if ((b[i] >= 0) && (b[i] < 32)) { 72 result += "<" + b[i] + ">"; 73 } else 74 result += (char) b[i]; 75 } 76 result += "</serialization>\n"; 77 return result; 78 } catch (Exception e) { 79 e.printStackTrace(); 80 return null; 81 } 82 } 83 84 89 public static int getMarshalSize(Object obj) { 90 try { 91 ByteArrayOutputStream ostream = new ByteArrayOutputStream (); 93 MarshalOutputStream p = new MarshalOutputStream(ostream); 94 p.writeObject(obj); 95 p.flush(); 96 return ostream.size(); 97 } catch (Exception e) { 98 e.printStackTrace(); 99 return 0; 100 } 101 } 102 103 public static String getClassString(Class cl) { 104 ClassLoader loader = cl.getClassLoader(); 105 String result = "<class>\n"; 106 result = result + "<classloader>" + loader.getClass().getName() + "</classloader>\n"; 107 result = result + "<annotations>" + defRMISpi.getClassAnnotation(cl) + "</annotaions>\n"; 108 result += "</class>"; 109 return result; 110 } 111 112 public static String getClassAnnotation(Class cl) { 113 return defRMISpi.getClassAnnotation(cl); 114 } 115 } | Popular Tags |