Changeset 5152
- Timestamp:
- 2003-08-18T13:54:57+12:00 (21 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/gli/src/org/greenstone/gatherer/shell/GShell.java
r5038 r5152 117 117 listeners.remove(GShellListener.class, listener); 118 118 } 119 /** Any threaded class must include this method to allow the thread body to be run. 120 */ 121 private MessageDispatcher dispatcher; 119 /** Any threaded class must include this method to allow the thread body to be run. */ 122 120 public void run() { 123 // Create a new message dispatcher to merge then send off messages at a predetermined rate.124 dispatcher = new MessageDispatcher(250);125 dispatcher.start();126 // Setup127 121 if(progress != null) { 128 122 progress.start(); … … 130 124 // Determine if the user has asked for an outfile. 131 125 String out_name = null; 126 BufferedOutputStream bos = null; 132 127 if(type == IMPORT || type == BUILD) { 133 128 if(type == IMPORT) { … … 138 133 } 139 134 if(out_name != null && out_name.length() > 0) { 140 File out = new File(out_name);141 135 try { 142 if(out.exists()) { 143 boolean append = true; 144 dispatcher.setBOS(new BufferedOutputStream(new FileOutputStream(out, append))); 145 } 136 bos = new BufferedOutputStream(new FileOutputStream(new File(out_name), true)); 146 137 } 147 138 catch (Exception error) { 148 error.printStackTrace();139 Gatherer.printStackTrace(error); 149 140 } 150 141 } … … 159 150 } 160 151 ///ystem.err.println("Command: " + command); 161 dispatcher.standardMessage(type, get("Command") + ": " + command, status);152 fireMessage(type, get("Command") + ": " + command, status); 162 153 163 154 Runtime rt = Runtime.getRuntime(); … … 175 166 progress.parse(eline); 176 167 } 177 dispatcher.errorMessage(type, typeAsString(type) + "> " + eline, status); 168 if(bos != null) { 169 try { 170 bos.write(eline.getBytes(), 0, eline.length()); 171 } 172 catch(Exception error) { 173 Gatherer.printStackTrace(error); 174 } 175 } 176 fireMessage(type, typeAsString(type) + "> " + eline, status); 178 177 } 179 178 if(stdinline != null) { 180 dispatcher.standardMessage(type, typeAsString(type) + "> " + stdinline, status);179 fireMessage(type, typeAsString(type) + "> " + stdinline, status); 181 180 182 181 } … … 189 188 if(prcs.exitValue() == 0) { 190 189 status = OK; 191 dispatcher.standardMessage(type, typeAsString(type) + "> " + get("Success"), status);190 fireMessage(type, typeAsString(type) + "> " + get("Success"), status); 192 191 193 192 } else { 194 193 status = ERROR; 195 dispatcher.standardMessage(type, typeAsString(type) + "> " + get("Failure"), status);194 fireMessage(type, typeAsString(type) + "> " + get("Failure"), status); 196 195 197 196 } … … 199 198 else { 200 199 // I need to somehow kill the child process. Unfortunately Thread.stop() and Process.destroy() both fail to do this. But now, thankx to the magic of Michaels 'close the stream suggestion', it works fine (no it doesn't!) 200 prcs.getInputStream().close(); 201 201 prcs.getOutputStream().close(); 202 202 prcs.destroy(); … … 211 211 // If no error occured, and this was an import process we now extract any new metadata from the archive directory. 212 212 if(status == OK && type == IMPORT) { 213 dispatcher.standardMessage(type, typeAsString(type) + "> " + get("Parsing_Metadata_Start"), status);213 fireMessage(type, typeAsString(type) + "> " + get("Parsing_Metadata_Start"), status); 214 214 new GreenstoneArchiveParser(progress, this); 215 dispatcher.standardMessage(type, typeAsString(type) + "> " + get("Parsing_Metadata_Complete"), status);215 fireMessage(type, typeAsString(type) + "> " + get("Parsing_Metadata_Complete"), status); 216 216 } 217 217 // Tidy up. … … 221 221 // We're done. 222 222 fireProcessComplete(type, status); 223 dispatcher.processComplete(); 224 dispatcher = null; 223 // Close bos 224 if(bos != null) { 225 try { 226 bos.close(); 227 bos = null; 228 } 229 catch(Exception error) { 230 Gatherer.printStackTrace(error); 231 } 232 } 225 233 } 226 234 /** Method for firing a message to all interested listeners. … … 236 244 ((GShellListener)concerned[i+1]).message(event); 237 245 } 238 }239 }240 241 public void message(int type, String message, int status) {242 if(dispatcher != null) {243 dispatcher.standardMessage(type, message, status);244 246 } 245 247 } … … 313 315 return name; 314 316 } 315 316 /** Rather than bombard the progress logs with thousands of individual lines, which can actually slow build times by 200-1000%, we instead fire off quarter second updates with all of the strings retrieved in that time, if any. */317 private class MessageDispatcher318 extends Thread {319 private boolean process_complete;320 private BufferedOutputStream bos;321 private int last_status;322 private int last_type;323 private int wait_time;324 private StringBuffer bos_buffer;325 private StringBuffer buffer;326 public MessageDispatcher(int wait_time) {327 this.bos = bos;328 this.bos_buffer = new StringBuffer();329 this.buffer = new StringBuffer();330 this.process_complete = false;331 this.wait_time = wait_time;332 }333 334 public synchronized void errorMessage(int type, String message, int status) {335 buffer.append(message);336 buffer.append(StaticStrings.NEW_LINE_CHARACTER);337 if(bos != null) {338 bos_buffer.append(message);339 bos_buffer.append(StaticStrings.NEW_LINE_CHARACTER);340 }341 last_status = status;342 last_type = type;343 }344 345 public void processComplete() {346 process_complete = true;347 }348 349 public void run() {350 while(!process_complete) {351 try {352 wait(wait_time);353 }354 catch(Exception error) {355 }356 message();357 }358 // If there is still something in buffer send it359 message();360 // Close bos if it exists361 if(bos != null) {362 try {363 bos.close();364 }365 catch (Exception e) {366 }367 }368 }369 370 public void setBOS(BufferedOutputStream bos) {371 this.bos = bos;372 }373 374 public synchronized void standardMessage(int type, String message, int status) {375 buffer.append(message);376 buffer.append(StaticStrings.NEW_LINE_CHARACTER);377 last_status = status;378 last_type = type;379 }380 381 private synchronized void message() {382 if(bos != null && bos_buffer.length() > 0) {383 try {384 bos.write(bos_buffer.toString().getBytes(), 0, bos_buffer.length());385 }386 catch(Exception error) {387 Gatherer.printStackTrace(error);388 }389 bos_buffer.delete(0, bos_buffer.length());390 }391 if(buffer.length() > 0) {392 fireMessage(last_type, buffer.toString(), last_status);393 buffer.delete(0, buffer.length());394 }395 }396 }397 317 }
Note:
See TracChangeset
for help on using the changeset viewer.