1 31 32 package org.opencms.setup; 33 34 import java.io.BufferedReader ; 35 import java.io.File ; 36 import java.io.FileWriter ; 37 import java.io.IOException ; 38 import java.io.InputStreamReader ; 39 import java.io.LineNumberReader ; 40 import java.io.PipedInputStream ; 41 import java.io.PipedOutputStream ; 42 import java.util.ArrayList ; 43 import java.util.List ; 44 45 56 public class CmsSetupLoggingThread extends Thread { 57 58 private LineNumberReader m_lineReader; 59 private FileWriter m_logWriter; 60 private List m_messages; 61 private PipedInputStream m_pipedIn; 62 private PipedOutputStream m_pipedOut; 63 private boolean m_stopThread; 64 65 71 public CmsSetupLoggingThread(PipedOutputStream pipedOut, String log) { 72 73 super("OpenCms: Setup logging"); 74 75 m_pipedOut = pipedOut; 76 m_messages = new ArrayList (); 77 m_stopThread = false; 78 79 if (log != null) { 80 try { 81 File logFile = new File (log); 82 if (logFile.exists()) { 83 logFile.delete(); 84 } 85 m_logWriter = new FileWriter (logFile); 86 } catch (Throwable t) { 87 m_logWriter = null; 88 } 89 } else { 90 m_logWriter = null; 91 } 92 93 try { 94 m_pipedIn = new PipedInputStream (); 95 m_pipedIn.connect(m_pipedOut); 96 m_lineReader = new LineNumberReader (new BufferedReader (new InputStreamReader (m_pipedIn))); 97 } catch (Exception e) { 98 m_messages.add(e.toString()); 99 } 100 } 101 102 107 public List getMessages() { 108 109 return m_messages; 110 } 111 112 117 public boolean isFinished() { 118 119 return m_stopThread; 120 } 121 122 125 public void run() { 126 127 int lineNr = 0; 128 int lastLineNr = -1; 129 String line = null; 130 while (!m_stopThread) { 131 lineNr = m_lineReader.getLineNumber(); 132 try { 133 line = m_lineReader.readLine(); 134 } catch (IOException e) { 135 } 137 if (line != null) { 138 if (lineNr > lastLineNr) { 139 String content = (lineNr + 1) + ":\t" + line; 141 m_messages.add(content); 142 lastLineNr = lineNr; 143 if (m_logWriter != null) { 144 try { 145 m_logWriter.write(content + "\n"); 146 } catch (IOException e) { 147 m_logWriter = null; 148 } 149 } 150 } 151 } 152 } 153 try { 154 m_pipedIn.close(); 155 } catch (IOException e) { 156 } 158 if (m_logWriter != null) { 159 try { 160 m_logWriter.close(); 161 } catch (IOException e) { 162 m_logWriter = null; 163 } 164 } 165 } 166 167 170 public void stopThread() { 171 172 try { 173 Thread.sleep(1000); 175 } catch (Throwable t) { 176 } 178 m_stopThread = true; 179 } 180 181 } | Popular Tags |