1 19 package org.netbeans.modules.j2ee.oc4j.ide; 20 21 import java.io.File ; 22 import java.io.FileInputStream ; 23 import java.io.FileNotFoundException ; 24 import java.io.IOException ; 25 import java.io.InputStream ; 26 import java.io.InputStreamReader ; 27 import java.util.HashMap ; 28 import java.util.Map ; 29 import org.netbeans.modules.j2ee.deployment.plugins.api.UISupport; 30 import org.openide.ErrorManager; 31 import org.openide.util.RequestProcessor; 32 import org.openide.windows.InputOutput; 33 34 41 public class OC4JLogger { 42 43 47 private static final int delay = 1000; 48 49 52 private static Map <String , OC4JLogger> instances = new HashMap <String , OC4JLogger>(); 53 54 57 private InputOutput io; 58 59 64 private OC4JLogger(String uri) { 65 io = UISupport.getServerIO(uri); 66 67 if (io == null) { 68 return; } 70 71 try { 73 io.getOut().reset(); 74 } catch (IOException ioe) { 75 } 77 78 io.select(); 79 } 80 81 87 public static OC4JLogger getInstance(String uri) { 88 if (!instances.containsKey(uri)) 89 instances.put(uri, new OC4JLogger(uri)); 90 91 return instances.get(uri); 92 } 93 94 99 public void readInputStreams(InputStream [] inputStreams) { 100 for(InputStream inputStream : inputStreams) 101 RequestProcessor.getDefault().post(new OC4JLoggerRunnable(inputStream)); 102 } 103 104 109 public void readFiles(File [] files) { 110 for(InputStream inputStream : getInputStreamsFromFiles(files)) 111 RequestProcessor.getDefault().post(new OC4JLoggerRunnable(inputStream)); 112 } 113 114 119 public synchronized void write(String s) { 120 io.getOut().println(s); 121 } 122 123 126 public synchronized void selectIO() { 127 io.select(); 128 } 129 130 private static InputStream [] getInputStreamsFromFiles(File [] files) { 131 InputStream [] inputStreams = new InputStream [files.length]; 132 133 try { 134 for(int i=0 ; i<files.length ; i++) 135 inputStreams[i] = new FileInputStream (files[i]); 136 } catch(FileNotFoundException ex) { 137 return new InputStream [] {}; 138 } 139 140 return inputStreams; 141 } 142 143 private class OC4JLoggerRunnable implements Runnable { 144 145 private InputStream inputStream; 146 147 public OC4JLoggerRunnable(InputStream inputStream) { 148 this.inputStream = inputStream; 149 } 150 151 155 public void run() { 156 try { 157 InputStreamReader reader = new InputStreamReader (inputStream); 159 160 char[] chars = new char[1024]; 163 while (true) { 164 while (reader.ready()) { 166 write(new String (chars, 0, reader.read(chars))); 167 selectIO(); 168 } 169 170 try { 172 Thread.sleep(delay); 173 } catch (InterruptedException e) { 174 } 176 } 177 } catch (IOException e) { 178 ErrorManager.getDefault().notify(ErrorManager.EXCEPTION, e); 179 } finally { 180 try { 182 inputStream.close(); 183 } catch (IOException e) { 184 ErrorManager.getDefault().notify(ErrorManager.EXCEPTION, e); 185 } 186 } 187 } 188 } 189 } | Popular Tags |