KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > carol > util > perfs > CarolJRMPPerformanceHelper


1 /**
2  * Copyright (C) 2002,2004 - INRIA (www.inria.fr)
3  *
4  * CAROL: Common Architecture for RMI ObjectWeb Layer
5  *
6  * This library is developed inside the ObjectWeb Consortium,
7  * http://www.objectweb.org
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22  * USA
23  *
24  * --------------------------------------------------------------------------
25  * $Id: CarolJRMPPerformanceHelper.java,v 1.3 2004/09/01 11:02:41 benoitf Exp $
26  * --------------------------------------------------------------------------
27  */

28 package org.objectweb.carol.util.perfs;
29
30 import java.io.ByteArrayOutputStream JavaDoc;
31 import java.lang.reflect.Method JavaDoc;
32 import java.rmi.server.RMIClassLoaderSpi JavaDoc;
33
34 import sun.rmi.server.MarshalOutputStream;
35
36 /**
37  * Class <code>CarolJRMPPerformanceHelper</code> Provide an Helper for perfs
38  * mesures
39  * @author Simon Nieuviarts
40  * @version 1.0, 15/03/2003
41  */

42 public class CarolJRMPPerformanceHelper {
43
44     private static RMIClassLoaderSpi JavaDoc defRMISpi;
45
46     static {
47         try {
48             Class JavaDoc cl = Class.forName("java.rmi.server.RMIClassLoader");
49             Method JavaDoc meth = cl.getMethod("getDefaultProviderInstance", new Class JavaDoc[0]);
50             defRMISpi = (RMIClassLoaderSpi JavaDoc) meth.invoke(cl, new Object JavaDoc[0]);
51         } catch (Exception JavaDoc e) {
52             //TraceCarol.error("RemoteClassLoaderSpi error", e);
53
}
54     }
55
56     /**
57      * See a marshalled object
58      * @param obj the object to marchal
59      * @return a visible format of the marshalled object
60      */

61     public static String JavaDoc getMarshalBytes(Object JavaDoc obj) {
62         try {
63             String JavaDoc result = getClassString(obj.getClass()) + "<serialization>\n";
64             // Print the Context value and size
65
ByteArrayOutputStream JavaDoc ostream = new ByteArrayOutputStream JavaDoc();
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 JavaDoc e) {
79             e.printStackTrace();
80             return null;
81         }
82     }
83
84     /**
85      * See the size of a Marchalled object
86      * @param obj the Object to marchal
87      * @return the size of this Marchalled object
88      */

89     public static int getMarshalSize(Object JavaDoc obj) {
90         try {
91             // Print the Context value and size
92
ByteArrayOutputStream JavaDoc ostream = new ByteArrayOutputStream JavaDoc();
93             MarshalOutputStream p = new MarshalOutputStream(ostream);
94             p.writeObject(obj);
95             p.flush();
96             return ostream.size();
97         } catch (Exception JavaDoc e) {
98             e.printStackTrace();
99             return 0;
100         }
101     }
102
103     public static String JavaDoc getClassString(Class JavaDoc cl) {
104         ClassLoader JavaDoc loader = cl.getClassLoader();
105         String JavaDoc 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 JavaDoc getClassAnnotation(Class JavaDoc cl) {
113         return defRMISpi.getClassAnnotation(cl);
114     }
115 }
Popular Tags