001: /*
002:
003: Derby - Class org.apache.derby.iapi.services.diag.DiagnosticUtil
004:
005: Licensed to the Apache Software Foundation (ASF) under one or more
006: contributor license agreements. See the NOTICE file distributed with
007: this work for additional information regarding copyright ownership.
008: The ASF licenses this file to you under the Apache License, Version 2.0
009: (the "License"); you may not use this file except in compliance with
010: the License. You may obtain a copy of the License at
011:
012: http://www.apache.org/licenses/LICENSE-2.0
013:
014: Unless required by applicable law or agreed to in writing, software
015: distributed under the License is distributed on an "AS IS" BASIS,
016: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: See the License for the specific language governing permissions and
018: limitations under the License.
019:
020: */
021:
022: package org.apache.derby.iapi.services.diag;
023:
024: /**
025:
026: The Diagnostic framework is meant to provide a way to include as much
027: diagnostic capability within the distributed release of the cloudscape
028: product without adversely affecting the runtime speed or foot print of
029: a running configuration that needs not use this information.
030:
031: In order to decrease the class size of running objects diagnostic information
032: should be put in "helper" classes. So to provide diagnostic capabiility
033: on the implementation of class Foo.java create a class D_Foo.java. Class
034: D_Foo must implement the Diagnosticable interface.
035:
036: This class provide utility functions to get at the information provided by
037: the D_* helper class:
038: findDiagnostic() - given and object "obj", get an instance of D_obj.
039: toDiagString() - return the "best" diagnostic string available about
040: a given object.
041:
042: **/
043:
044: public class DiagnosticUtil {
045: /* Constructors for This class: */
046: private DiagnosticUtil() {
047: }
048:
049: /* Private/Protected methods of This class: */
050:
051: /**
052: * Given an object return instance of the diagnostic object for this class.
053: * <p>
054: * Given an object this routine will determine the classname of the object
055: * and then try to instantiate a new instance of the diagnostic object
056: * for this class by prepending on "D_" to the last element of theclassname.
057: If no matching class is found then the same lookup is made on the super-class
058: of the object, looking all the way up the hierachy until a diagnostic class
059: is found.
060: * <BR>
061: This routine will call "init(ref)" on the new instance and then return the new instance.
062: *
063: * @return A new instance of the diagnostic object for input object, or
064: * null if one could not be found for some reason.
065: *
066: * @param ref The object which to build the diagnostic object for.
067: **/
068: public static Diagnosticable findDiagnostic(Object ref) {
069: Class refClass = ref.getClass();
070:
071: for (;;) {
072: try {
073: String className = refClass.getName();
074: int lastDot = className.lastIndexOf('.') + 1;
075: String diagClassName = className.substring(0, lastDot)
076: + "D_" + className.substring(lastDot);
077:
078: Class diagClass;
079:
080: try {
081: diagClass = Class.forName(diagClassName);
082: } catch (ClassNotFoundException cnfe) {
083:
084: // try the super-class of the object
085: refClass = refClass.getSuperclass();
086: if (refClass == null)
087: return null;
088:
089: continue;
090: }
091:
092: Diagnosticable diag_obj = (Diagnosticable) diagClass
093: .newInstance();
094:
095: diag_obj.init(ref);
096:
097: return diag_obj;
098: } catch (Exception e) {
099: return null;
100: }
101: }
102: }
103:
104: /**
105: * Return a diagnostic string associated with an object.
106: * <p>
107: * A utility interface to use if you just want to print a single string
108: * that represents the object in question. In following order this routine
109: * will deliver the string to use:
110: *
111: * 1) find diagnostic help class, and use class.diag()
112: * 2) else just use class.toString()
113: *
114: * <p>
115: *
116: * @return The string describing the class input.
117: *
118: * @param obj The object to print out.
119: *
120: **/
121: public static String toDiagString(Object obj) {
122: String ret_string = null;
123:
124: if (obj == null)
125: return "null";
126:
127: try {
128: Diagnosticable diag = DiagnosticUtil.findDiagnostic(obj);
129: if (diag != null)
130: ret_string = diag.diag();
131: } catch (Throwable t) {
132: // do nothing, ret_string should still be null on error
133: }
134:
135: if (ret_string == null) {
136: ret_string = obj.toString();
137: }
138:
139: return (ret_string);
140: }
141:
142: /* Public Methods of This class: */
143: /* Public Methods of XXXX class: */
144: }
|