KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > archive > util > DevUtils


1 /* DevUtils
2  *
3  * Created on Oct 29, 2003
4  *
5  * Copyright (C) 2003 Internet Archive.
6  *
7  * This file is part of the Heritrix web crawler (crawler.archive.org).
8  *
9  * Heritrix is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU Lesser Public License as published by
11  * the Free Software Foundation; either version 2.1 of the License, or
12  * any later version.
13  *
14  * Heritrix 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
17  * GNU Lesser Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser Public License
20  * along with Heritrix; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22  */

23 package org.archive.util;
24
25 import java.io.BufferedReader JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.io.InputStreamReader JavaDoc;
28 import java.io.PrintWriter JavaDoc;
29 import java.io.StringWriter JavaDoc;
30 import java.util.logging.Logger JavaDoc;
31
32
33 /**
34  * Write a message and stack trace to the 'org.archive.util.DevUtils' logger.
35  *
36  * @author gojomo
37  * @version $Revision: 1.11.12.1 $ $Date: 2007/01/13 01:31:39 $
38  */

39 public class DevUtils {
40     public static Logger JavaDoc logger =
41         Logger.getLogger(DevUtils.class.getName());
42
43     /**
44      * Log a warning message to the logger 'org.archive.util.DevUtils' made of
45      * the passed 'note' and a stack trace based off passed exception.
46      *
47      * @param ex Exception we print a stacktrace on.
48      * @param note Message to print ahead of the stacktrace.
49      */

50     public static void warnHandle(Throwable JavaDoc ex, String JavaDoc note) {
51         logger.warning(TextUtils.exceptionToString(note, ex));
52     }
53
54     /**
55      * @return Extra information gotten from current Thread. May not
56      * always be available in which case we return empty string.
57      */

58     public static String JavaDoc extraInfo() {
59         StringWriter JavaDoc sw = new StringWriter JavaDoc();
60         PrintWriter JavaDoc pw = new PrintWriter JavaDoc(sw);
61         final Thread JavaDoc current = Thread.currentThread();
62         if (current instanceof Reporter) {
63             Reporter tt = (Reporter)current;
64             try {
65                 tt.reportTo(pw);
66             } catch (IOException JavaDoc e) {
67                 // Not really possible w/ a StringWriter
68
e.printStackTrace();
69             }
70         }
71         if (current instanceof ProgressStatisticsReporter) {
72             ProgressStatisticsReporter tt = (ProgressStatisticsReporter)current;
73             try {
74                 tt.progressStatisticsLegend(pw);
75                 tt.progressStatisticsLine(pw);
76             } catch (IOException JavaDoc e) {
77                 // Not really possible w/ a StringWriter
78
e.printStackTrace();
79             }
80         }
81         pw.flush();
82         return sw.toString();
83     }
84
85     /**
86      * Nothing to see here, move along.
87      * @deprecated This method was never used.
88      */

89     @Deprecated JavaDoc
90     public static void betterPrintStack(RuntimeException JavaDoc re) {
91         re.printStackTrace(System.err);
92     }
93     
94     /**
95      * Send this JVM process a SIGQUIT; giving a thread dump and possibly
96      * a heap histogram (if using -XX:+PrintClassHistogram).
97      *
98      * Used to automatically dump info, for example when a serious error
99      * is encountered. Would use 'jmap'/'jstack', but have seen JVM
100      * lockups -- perhaps due to lost thread wake signals -- when using
101      * those against Sun 1.5.0+03 64bit JVM.
102      */

103     public static void sigquitSelf() {
104         try {
105             Process JavaDoc p = Runtime.getRuntime().exec(
106                     new String JavaDoc[] {"perl", "-e", "print getppid(). \"\n\";"});
107             BufferedReader JavaDoc br =
108                 new BufferedReader JavaDoc(new InputStreamReader JavaDoc(p.getInputStream()));
109             String JavaDoc ppid = br.readLine();
110             Runtime.getRuntime().exec(
111                     new String JavaDoc[] {"sh", "-c", "kill -3 "+ppid}).waitFor();
112         } catch (IOException JavaDoc e) {
113             // TODO Auto-generated catch block
114
e.printStackTrace();
115         } catch (InterruptedException JavaDoc e) {
116             // TODO Auto-generated catch block
117
e.printStackTrace();
118         }
119     }
120 }
121
Popular Tags