1 19 package org.netbeans.modules.j2ee.websphere6.util; 20 21 import java.io.*; 22 import java.util.Collections ; 23 import java.util.HashMap ; 24 import java.util.Map ; 25 26 import org.openide.*; 27 import org.openide.windows.*; 28 29 36 public class WSTailer extends Thread { 37 38 42 private static final int delay = 1000; 43 44 private static Map isIOPanelOpen = Collections.synchronizedMap((Map )new HashMap (2,1)); 45 46 49 private File file; 50 51 54 private InputStream inputStream; 55 56 59 private InputOutput io; 60 61 67 public WSTailer(File file, String ioPanelName) { 68 this.file = file; 70 this.io = IOProvider.getDefault().getIO(ioPanelName, false); 71 } 72 73 79 public WSTailer(InputStream inputStream, String ioPanelName) { 80 this.inputStream = inputStream; 82 this.io = IOProvider.getDefault().getIO(ioPanelName, false); 83 } 84 85 89 public void run() { 90 if(isIOPanelOpen.containsKey(io)) { 91 return; 92 } 93 94 isIOPanelOpen.put(io, new Object ()); 95 try { 96 97 if(io.isClosed()) { 98 io.getOut().reset(); 99 } 100 this.io.select(); 101 102 if (file != null) { 105 while (inputStream == null) { 106 try { 107 inputStream = new FileInputStream(file); 108 } catch (IOException e) { 109 try { 110 Thread.sleep(delay); 111 } catch (InterruptedException ex) { 112 continue; 113 } 114 } 115 } 116 } 117 BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); 119 String line; 122 while (!io.isClosed()) { 123 while (reader.ready()) { 125 line = reader.readLine(); 126 if(line!=null) { 127 io.getOut().println(line); 128 io.getOut().flush(); 129 } 130 } 131 132 try { 134 Thread.sleep(delay); 135 } catch (InterruptedException e) { 136 } 138 } 139 } catch (FileNotFoundException e) { 140 ErrorManager.getDefault().notify(ErrorManager.EXCEPTION, e); 141 return; 142 } catch (IOException e) { 143 ErrorManager.getDefault().notify(ErrorManager.EXCEPTION, e); 144 } finally { 145 try { 147 isIOPanelOpen.remove(io); 148 inputStream.close(); 149 } catch (IOException e) { 150 ErrorManager.getDefault().notify(ErrorManager.EXCEPTION, e); 151 } 152 } 153 } 154 155 } 156 | Popular Tags |