Source code: org/apache/derby/iapi/services/diag/DiagnosticUtil.java
1 /*
2
3 Derby - Class org.apache.derby.iapi.services.diag.DiagnosticUtil
4
5 Copyright 1998, 2004 The Apache Software Foundation or its licensors, as applicable.
6
7 Licensed under the Apache License, Version 2.0 (the "License");
8 you may not use this file except in compliance with the License.
9 You may obtain a copy of the License at
10
11 http://www.apache.org/licenses/LICENSE-2.0
12
13 Unless required by applicable law or agreed to in writing, software
14 distributed under the License is distributed on an "AS IS" BASIS,
15 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 See the License for the specific language governing permissions and
17 limitations under the License.
18
19 */
20
21 package org.apache.derby.iapi.services.diag;
22
23 /**
24
25 The Diagnostic framework is meant to provide a way to include as much
26 diagnostic capability within the distributed release of the cloudscape
27 product without adversely affecting the runtime speed or foot print of
28 a running configuration that needs not use this information.
29
30 In order to decrease the class size of running objects diagnostic information
31 should be put in "helper" classes. So to provide diagnostic capabiility
32 on the implementation of class Foo.java create a class D_Foo.java. Class
33 D_Foo must implement the Diagnosticable interface.
34
35 This class provide utility functions to get at the information provided by
36 the D_* helper class:
37 findDiagnostic() - given and object "obj", get an instance of D_obj.
38 toDiagString() - return the "best" diagnostic string available about
39 a given object.
40
41 **/
42
43 public class DiagnosticUtil
44 {
45 /* Constructors for This class: */
46 private DiagnosticUtil()
47 {
48 }
49
50 /* Private/Protected methods of This class: */
51
52 /**
53 * Given an object return instance of the diagnostic object for this class.
54 * <p>
55 * Given an object this routine will determine the classname of the object
56 * and then try to instantiate a new instance of the diagnostic object
57 * for this class by prepending on "D_" to the last element of theclassname.
58 If no matching class is found then the same lookup is made on the super-class
59 of the object, looking all the way up the hierachy until a diagnostic class
60 is found.
61 * <BR>
62 This routine will call "init(ref)" on the new instance and then return the new instance.
63 *
64 * @return A new instance of the diagnostic object for input object, or
65 * null if one could not be found for some reason.
66 *
67 * @param ref The object which to build the diagnostic object for.
68 **/
69 public static Diagnosticable findDiagnostic(Object ref)
70 {
71 Class refClass = ref.getClass();
72
73 for (;;) {
74 try
75 {
76 String className = refClass.getName();
77 int lastDot = className.lastIndexOf('.') + 1;
78 String diagClassName =
79 className.substring(0, lastDot) +
80 "D_" + className.substring(lastDot);
81
82 Class diagClass;
83
84 try {
85 diagClass = Class.forName(diagClassName);
86 } catch (ClassNotFoundException cnfe) {
87
88 // try the super-class of the object
89 refClass = refClass.getSuperclass();
90 if (refClass == null)
91 return null;
92
93 continue;
94 }
95
96
97 Diagnosticable diag_obj = (Diagnosticable) diagClass.newInstance();
98
99 diag_obj.init(ref);
100
101 return diag_obj;
102 }
103 catch (Exception e)
104 {
105 return null;
106 }
107 }
108 }
109
110 /**
111 * Return a diagnostic string associated with an object.
112 * <p>
113 * A utility interface to use if you just want to print a single string
114 * that represents the object in question. In following order this routine
115 * will deliver the string to use:
116 *
117 * 1) find diagnostic help class, and use class.diag()
118 * 2) else just use class.toString()
119 *
120 * <p>
121 *
122 * @return The string describing the class input.
123 *
124 * @param obj The object to print out.
125 *
126 **/
127 public static String toDiagString(Object obj)
128 {
129 String ret_string = null;
130
131 if (obj == null) return "null";
132
133 try
134 {
135 Diagnosticable diag = DiagnosticUtil.findDiagnostic(obj);
136 if (diag != null)
137 ret_string = diag.diag();
138 }
139 catch (Throwable t)
140 {
141 // do nothing, ret_string should still be null on error
142 }
143
144 if (ret_string == null)
145 {
146 ret_string = obj.toString();
147 }
148
149 return(ret_string);
150 }
151
152 /* Public Methods of This class: */
153 /* Public Methods of XXXX class: */
154 }