001: package com.dwipal;
002:
003: import javax.swing.*;
004: import java.io.*;
005: import java.util.*;
006:
007: public class DwSnmpMibOutputHandler {
008:
009: JTextArea outputText;
010: JTextArea outputError;
011: boolean doLog = false;
012: boolean autoScroll = true;
013: static BufferedWriter outfile;
014:
015: DwSnmpMibOutputHandler() {
016: }
017:
018: public void setAutoScroll(boolean as) {
019: autoScroll = as;
020: }
021:
022: public void setOutput(JTextArea o) {
023: outputText = o;
024:
025: }
026:
027: public void setOutputError(JTextArea e) {
028: outputError = e;
029: }
030:
031: void showErrorMessage(String s) {
032: JOptionPane.showMessageDialog(outputText, s, "Mib Browser",
033: JOptionPane.OK_OPTION);
034: }
035:
036: public void setLogging(boolean log) {
037:
038: try {
039: if (log == true) {
040: String strFileName = getLogFileName();
041: outfile = new BufferedWriter(new FileWriter(
042: strFileName, true));
043: outfile
044: .write("\n**********************************************************\n");
045: outfile.write("MIB Browser Started at : " + new Date());
046: outfile
047: .write("\n**********************************************************\n");
048: System.out.println("Output log file: " + strFileName);
049: this .doLog = true;
050:
051: java.util.Timer tmr = new java.util.Timer(true);
052: class SnmpTimerTask extends java.util.TimerTask {
053: public void run() {
054: try {
055: outfile.flush();
056: } catch (Exception e) {
057: System.out
058: .println("Error in writing to log file: "
059: + e);
060: }
061: }
062: }
063: ;
064: long lFlushTime = getFlushTime();
065: System.out.println("Log will be refreshed every "
066: + lFlushTime / 1000 + " seconds.");
067: tmr.schedule(new SnmpTimerTask(), lFlushTime,
068: lFlushTime);
069:
070: Thread thrdFlush = new Thread(new Runnable() {
071: public void run() {
072: try {
073: System.out.println("Have a nice day !!");
074: outfile
075: .write("\n**********************************************************\n");
076: outfile.write("MIB Browser Stopped at : "
077: + new Date());
078: outfile
079: .write("\n**********************************************************\n");
080: outfile.flush();
081: outfile.close();
082: } catch (Exception e) {
083: System.out
084: .println("Error while writing to log file: "
085: + e);
086: }
087: }
088: });
089: Runtime.getRuntime().addShutdownHook(thrdFlush);
090:
091: } else
092: outfile.close();
093: } catch (Exception e) {
094: System.out.println("Error : Cannot log" + e.toString());
095: // doLog=false;
096: return;
097: }
098: doLog = true;
099: }
100:
101: private String getLogFileName() {
102: String strFileName = System.getProperty("logfilename");
103: if (strFileName == null) {
104: strFileName = "mibbrowser.log";
105: }
106: return strFileName;
107: }
108:
109: private long getFlushTime() {
110: long lTime = 0;
111: String strTime = System.getProperty("logrefreshtime");
112: if (strTime != null) {
113: try {
114: lTime = Long.parseLong(strTime);
115: lTime = lTime * 1000;
116: } catch (Exception e) {
117: System.out
118: .println("Invalid value for log refresh time. default will be used.");
119: }
120: }
121:
122: if (lTime < 1000) { // minimum must be 1 second.
123: lTime = 60 * 1000; // default is 1 minute.
124: }
125: return lTime;
126: }
127:
128: public void println(String s) {
129: if (outputText != null) {
130: outputText.append("\n" + s);
131: if (autoScroll == true)
132: outputText.setCaretPosition(outputText.getDocument()
133: .getLength() - 1);
134: }
135: //else System.out.println(s);
136: try {//if(doLog==true)
137: {
138: outfile.write(s + "\n");
139: }
140: } catch (Exception e) {
141: System.out.println(e.toString());
142: }
143: }
144:
145: public void print(String s) {
146: if (outputText != null) {
147: outputText.append(s);
148: if (autoScroll == true)
149: outputText.setCaretPosition(outputText.getDocument()
150: .getLength() - 1);
151: } else
152: System.out.println(s);
153: try {//if(doLog==true)
154: outfile.write(s);
155: } catch (Exception e) {
156: System.out.println(e.toString());
157: }
158:
159: }
160:
161: public void printError(String e) {
162: if (outputError != null)
163: outputError.append("\n" + e);
164: else
165: System.err.println(e);
166: try {//if(doLog==true)
167: outfile.write("\n" + e + "\n");
168: } catch (Exception ex) {
169: System.out.println(e.toString());
170: }
171:
172: }
173: }
|