KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > iapi > services > diag > DiagnosticUtil


1 /*
2
3    Derby - Class org.apache.derby.iapi.services.diag.DiagnosticUtil
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to you under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21
22 package org.apache.derby.iapi.services.diag;
23
24 /**
25
26 The Diagnostic framework is meant to provide a way to include as much
27 diagnostic capability within the distributed release of the cloudscape
28 product without adversely affecting the runtime speed or foot print of
29 a running configuration that needs not use this information.
30
31 In order to decrease the class size of running objects diagnostic information
32 should be put in "helper" classes. So to provide diagnostic capabiility
33 on the implementation of class Foo.java create a class D_Foo.java. Class
34 D_Foo must implement the Diagnosticable interface.
35
36 This class provide utility functions to get at the information provided by
37 the D_* helper class:
38     findDiagnostic() - given and object "obj", get an instance of D_obj.
39     toDiagString() - return the "best" diagnostic string available about
40                        a given object.
41
42 **/

43
44 public class DiagnosticUtil
45 {
46     /* Constructors for This class: */
47     private DiagnosticUtil()
48     {
49     }
50
51     /* Private/Protected methods of This class: */
52
53     /**
54      * Given an object return instance of the diagnostic object for this class.
55      * <p>
56      * Given an object this routine will determine the classname of the object
57      * and then try to instantiate a new instance of the diagnostic object
58      * for this class by prepending on "D_" to the last element of theclassname.
59        If no matching class is found then the same lookup is made on the super-class
60        of the object, looking all the way up the hierachy until a diagnostic class
61        is found.
62      * <BR>
63        This routine will call "init(ref)" on the new instance and then return the new instance.
64      *
65      * @return A new instance of the diagnostic object for input object, or
66      * null if one could not be found for some reason.
67      *
68      * @param ref The object which to build the diagnostic object for.
69      **/

70     public static Diagnosticable findDiagnostic(Object JavaDoc ref)
71     {
72         Class JavaDoc refClass = ref.getClass();
73
74         for (;;) {
75             try
76             {
77                 String JavaDoc className = refClass.getName();
78                 int lastDot = className.lastIndexOf('.') + 1;
79                 String JavaDoc diagClassName =
80                     className.substring(0, lastDot) +
81                     "D_" + className.substring(lastDot);
82
83                 Class JavaDoc diagClass;
84                 
85                 try {
86                     diagClass = Class.forName(diagClassName);
87                 } catch (ClassNotFoundException JavaDoc cnfe) {
88
89                     // try the super-class of the object
90
refClass = refClass.getSuperclass();
91                     if (refClass == null)
92                         return null;
93
94                     continue;
95                 }
96
97
98                 Diagnosticable diag_obj = (Diagnosticable) diagClass.newInstance();
99
100                 diag_obj.init(ref);
101
102                 return diag_obj;
103             }
104             catch (Exception JavaDoc e)
105             {
106                 return null;
107             }
108         }
109     }
110
111     /**
112      * Return a diagnostic string associated with an object.
113      * <p>
114      * A utility interface to use if you just want to print a single string
115      * that represents the object in question. In following order this routine
116      * will deliver the string to use:
117      *
118      * 1) find diagnostic help class, and use class.diag()
119      * 2) else just use class.toString()
120      *
121      * <p>
122      *
123      * @return The string describing the class input.
124      *
125      * @param obj The object to print out.
126      *
127      **/

128     public static String JavaDoc toDiagString(Object JavaDoc obj)
129     {
130         String JavaDoc ret_string = null;
131
132         if (obj == null) return "null";
133         
134         try
135         {
136             Diagnosticable diag = DiagnosticUtil.findDiagnostic(obj);
137             if (diag != null)
138                 ret_string = diag.diag();
139         }
140         catch (Throwable JavaDoc t)
141         {
142             // do nothing, ret_string should still be null on error
143
}
144
145         if (ret_string == null)
146         {
147             ret_string = obj.toString();
148         }
149
150         return(ret_string);
151     }
152
153     /* Public Methods of This class: */
154     /* Public Methods of XXXX class: */
155 }
156
Popular Tags