| 1 21 package com.db4o.tools; 22 23 import java.io.*; 24 import java.lang.reflect.*; 25 26 import com.db4o.*; 27 import com.db4o.ext.*; 28 import com.db4o.foundation.*; 29 30 36 public class Logger 37 { 38 39 private static final int MAXIMUM_OBJECTS = 20; 40 41 45 public static void main(String [] args) { 46 if(args == null || args.length == 0){ 47 System.out.println("Usage: java com.db4o.tools.Logger <database filename> <class>"); 48 }else{ 49 if(! new File(args[0]).exists()){ 50 System.out.println("A database file with the name '" + args[0] + "' does not exist."); 51 }else{ 52 Db4o.configure().messageLevel(-1); 53 ExtObjectContainer con = null; 54 try{ 55 ObjectContainer c = Db4o.openFile(args[0]); 56 if(c == null){ 57 throw new RuntimeException (); 58 } 59 con = c.ext(); 60 }catch(Exception e){ 61 System.out.println("The database file '" + args[0] + "' could not be opened."); 62 return; 63 } 64 65 if(args.length > 1){ 66 StoredClass sc = con.storedClass(args[1]); 67 if(sc == null){ 68 System.out.println("There is no stored class with the name '" + args[1] + "'."); 69 }else{ 70 long[] ids = sc.getIDs(); 71 for (int i = 0; i < ids.length; i++) { 72 if(i > MAXIMUM_OBJECTS){ 73 break; 74 } 75 Object obj = con.getByID(ids[i]); 76 con.activate(obj, Integer.MAX_VALUE); 77 log(con, obj); 78 } 79 msgCount(ids.length); 80 } 81 }else{ 82 ObjectSet set = con.get(null); 83 int i = 0; 84 while(set.hasNext()){ 85 Object obj = set.next(); 86 con.activate(obj, Integer.MAX_VALUE); 87 log(con, obj); 88 89 if(++i > MAXIMUM_OBJECTS){ 90 break; 91 } 92 } 93 msgCount(set.size()); 94 } 95 con.close(); 96 97 } 98 99 } 100 } 101 102 108 public static void log(ObjectContainer container, Object obj){ 109 if(obj == null){ 110 log("[NULL]"); 111 }else{ 112 log(obj.getClass().getName()); 113 log(container, obj, 0, new Collection4()); 114 } 115 } 116 117 121 public static void log(Object obj){ 122 log(null, obj); 123 } 124 125 126 130 public static void logAll(ObjectContainer container){ 131 ObjectSet set = container.get(null); 132 while(set.hasNext()){ 133 log(container, set.next()); 134 } 135 } 136 137 141 public static void setOut(java.io.PrintStream ps){ 142 out = ps; 143 } 144 145 149 public static void setMaximumDepth(int depth){ 150 maximumDepth = depth; 151 } 152 153 private static void msgCount(int count){ 154 System.out.println("\n\nLog complete.\nObjects: " + count); 155 if(count > MAXIMUM_OBJECTS){ 156 System.out.println("Displayed due to setting of " + Logger.class.getName() + "#MAXIMUM_OBJECTS: " + MAXIMUM_OBJECTS); 157 } 158 } 159 160 161 private static void log(ObjectContainer a_container, Object a_object, int a_depth, Collection4 a_list){ 162 if (a_list.contains(a_object) || a_depth > maximumDepth) { 163 return; 164 } 165 Class clazz = a_object.getClass(); 166 for (int i = 0; i < IGNORE.length; i++) { 167 if(clazz.isAssignableFrom(IGNORE[i])){ 168 return; 169 } 170 } 171 if(Platform4.isSimple(clazz)){ 172 log(a_depth + 1,a_object.getClass().getName(), a_object.toString()); 173 return; 174 } 175 176 a_list.add(a_object); 177 178 Class [] classes = getClassHierarchy(a_object); 179 180 String spaces = ""; 181 for(int i = classes.length - 1; i >= 0; i--){ 182 spaces = spaces + sp; 183 184 String className = spaces; 185 int pos = classes[i].getName().lastIndexOf("."); 186 if(pos > 0){ 187 className += classes[i].getName().substring(pos); 188 }else{ 189 className += classes[i].getName(); 190 } 191 192 if(classes[i] == java.util.Date .class){ 193 String fieldName = className + ".getTime"; 194 Object obj = new Long (((java.util.Date )a_object).getTime()); 195 log(a_container, obj, fieldName, a_depth + 1, -1, a_list); 196 197 }else{ 198 Field[] fields = classes[i].getDeclaredFields(); 199 for (int j = 0; j < fields.length; j++){ 200 Platform4.setAccessible(fields[j]); 201 String fieldName = className + "." + fields[j].getName(); 202 try{ 203 Object obj = fields[j].get(a_object); 204 if(obj.getClass().isArray()){ 205 obj = normalizeNArray(obj); 206 int len = Array.getLength(obj); 207 for (int k = 0 ; k < len; k ++){ 208 Object element = Array.get(obj,k); 209 log(a_container, element,fieldName, a_depth + 1, k, a_list); 210 } 211 }else{ 212 log(a_container, obj, fieldName, a_depth + 1, -1, a_list); 213 } 214 }catch(Exception e){ 215 } 216 } 217 } 218 } 219 } 220 221 private static void log(ObjectContainer a_container, Object a_object, String a_fieldName, int a_depth, int a_arrayElement, Collection4 a_list){ 222 if(a_depth > maximumDepth){ 223 return; 224 } 225 String fieldName = (a_arrayElement > -1) ? a_fieldName + sp + sp + a_arrayElement: a_fieldName; 226 if(a_object != null){ 227 if((a_container == null) || a_container.ext().isStored(a_object)){ 228 if(a_container == null || a_container.ext().isActive(a_object)){ 229 log(a_depth, fieldName, ""); 230 Class clazz = a_object.getClass(); 231 boolean found = false; 232 if(Platform4.isSimple(clazz)){ 233 log(a_depth + 1,a_object.getClass().getName(), a_object.toString()); 234 found = true; 235 } 236 if(!found){ 237 log(a_container, a_object, a_depth, a_list); 238 } 239 }else{ 240 log(a_depth, fieldName, "DEACTIVATED " + a_object.getClass().getName()); 241 } 242 return; 243 } 244 log(a_depth, fieldName, a_object.toString()); 245 }else{ 246 log(a_depth, fieldName, "[NULL]"); 247 } 248 } 249 250 private static void log (String a_msg) { 251 if(! silent){ 252 out.println(a_msg); 253 } 254 } 255 256 private static void log (int indent, String a_property, String a_value) { 257 for (int i = 0; i < indent; i++) { 258 a_property = sp + sp + a_property; 259 } 260 log(a_property, a_value); 261 } 262 263 private static void log (String a_property, String a_value) { 264 if (a_value == null) 265 a_value = "[NULL]"; 266 log(a_property + ": " + a_value); 267 } 268 269 private static Class [] getClassHierarchy(Object a_object){ 270 Class [] classes = new Class [] {a_object.getClass()}; 271 return getClassHierarchy(classes); 272 } 273 274 private static Class [] getClassHierarchy(Class [] a_classes){ 275 Class clazz = a_classes[a_classes.length -1].getSuperclass(); 276 if(clazz.equals(Object .class)){ 277 return a_classes; 278 } 279 Class [] classes = new Class [a_classes.length + 1]; 280 System.arraycopy(a_classes,0,classes,0,a_classes.length); 281 classes[a_classes.length] = clazz; 282 return getClassHierarchy(classes); 283 } 284 285 private static Object normalizeNArray(Object a_object) { 286 if (Array.getLength(a_object) > 0) { 287 Object first = Array.get(a_object, 0); 288 if (first != null && first.getClass().isArray()) { 289 int dim[] = arrayDimensions(a_object); 290 Object all = new Object [arrayElementCount(dim)]; 291 normalizeNArray1(a_object, all, 0, dim, 0); 292 return all; 293 } 294 } 295 return a_object; 296 } 297 298 private static int normalizeNArray1( 299 Object a_object, 300 Object a_all, 301 int a_next, 302 int a_dim[], 303 int a_index) { 304 if (a_index == a_dim.length - 1) { 305 for (int i = 0; i < a_dim[a_index]; i++) { 306 Array.set(a_all, a_next++, Array.get(a_object, i)); 307 } 308 } else { 309 for (int i = 0; i < a_dim[a_index]; i++) { 310 a_next = 311 normalizeNArray1( 312 Array.get(a_object, i), 313 a_all, 314 a_next, 315 a_dim, 316 a_index + 1); 317 } 318 319 } 320 return a_next; 321 } 322 323 private static int[] arrayDimensions(Object a_object) { 324 int count = 0; 325 for (Class clazz = a_object.getClass(); 326 clazz.isArray(); 327 clazz = clazz.getComponentType()) { 328 count++; 329 } 330 int dim[] = new int[count]; 331 for (int i = 0; i < count; i++) { 332 dim[i] = Array.getLength(a_object); 333 a_object = Array.get(a_object, 0); 334 } 335 return dim; 336 } 337 338 private static int arrayElementCount(int a_dim[]) { 339 int elements = a_dim[0]; 340 for (int i = 1; i < a_dim.length; i++) { 341 elements *= a_dim[i]; 342 } 343 return elements; 344 } 345 346 private static final Class [] IGNORE ={ 347 Class .class 348 }; 349 350 private static int maximumDepth = Integer.MAX_VALUE; 351 private static java.io.PrintStream out = System.out; 352 private static String sp = " "; 353 private static boolean silent; 354 355 358 private Logger(){ 359 } 360 } 361 | Popular Tags |