1 31 package org.objectweb.proactive.core.process; 32 33 import org.objectweb.proactive.core.util.MessageLogger; 34 35 public abstract class AbstractExternalProcessDecorator extends AbstractExternalProcess implements ExternalProcessDecorator { 36 37 protected ExternalProcess targetProcess; 38 39 private int compositionType = APPEND_TO_COMMAND_COMPOSITION; 40 41 45 public AbstractExternalProcessDecorator() { 46 super(); 47 } 48 49 public AbstractExternalProcessDecorator(ExternalProcess targetProcess) { 50 this(targetProcess, APPEND_TO_COMMAND_COMPOSITION); 51 } 52 53 public AbstractExternalProcessDecorator(ExternalProcess targetProcess, int compositionType) { 54 super(); 55 setTargetProcess(targetProcess); 56 this.compositionType = compositionType; 57 } 58 59 60 64 public ExternalProcess getTargetProcess() { 65 return targetProcess; 66 } 67 68 69 public void setTargetProcess(ExternalProcess targetProcess) { 70 checkStarted(); 71 this.targetProcess = targetProcess; 72 setInputMessageLogger(targetProcess.getInputMessageLogger()); 73 setErrorMessageLogger(targetProcess.getErrorMessageLogger()); 74 setOutputMessageSink(targetProcess.getOutputMessageSink()); 75 } 76 77 78 public int getCompositionType() { 79 return compositionType; 80 } 81 82 83 public void setCompositionType(int compositionType) { 84 checkStarted(); 85 this.compositionType = compositionType; 86 } 87 88 89 93 protected void toString(StringBuffer sb) { 94 super.toString(sb); 95 sb.append(" ---- Target Process ----- \n"); 96 if (targetProcess == null) { 97 sb.append(" NOT DEFINED \n"); 98 } else { 99 sb.append(targetProcess.toString()); 100 } 101 sb.append(" -------------- \n"); 102 } 103 104 105 protected String buildCommand() { 106 if (compositionType == SEND_TO_OUTPUT_STREAM_COMPOSITION || compositionType == GIVE_COMMAND_AS_PARAMETER) { 107 return internalBuildCommand(); 108 } else { 109 if (targetProcess != null) { 110 return internalBuildCommand()+targetProcess.getCommand(); 111 } else { 112 return internalBuildCommand(); 113 } 114 } 115 } 116 117 118 protected abstract String internalBuildCommand(); 119 120 121 protected void internalStartProcess(String command) throws java.io.IOException { 122 super.internalStartProcess(command); 124 if (compositionType == SEND_TO_OUTPUT_STREAM_COMPOSITION) { 125 try { 126 Thread.sleep(3000); 127 } catch (InterruptedException e) {} 129 outputMessageSink.setMessage(targetProcess.getCommand()); 131 } 132 } 133 134 135 protected void handleOutput(java.io.BufferedWriter out) { 136 if (compositionType == SEND_TO_OUTPUT_STREAM_COMPOSITION) { 137 if (outputMessageSink == null) outputMessageSink = new SimpleMessageSink(); 138 } 139 super.handleOutput(out); 140 } 141 142 143 147 148 149 153 156 public static class CompositeMessageLogger implements MessageLogger,java.io.Serializable { 157 158 private MessageLogger messageLogger1; 159 private MessageLogger messageLogger2; 160 161 public CompositeMessageLogger(MessageLogger messageLogger1, MessageLogger messageLogger2) { 162 this.messageLogger1 = messageLogger1; 163 this.messageLogger2 = messageLogger2; 164 } 165 166 public void log(String message) { 167 messageLogger1.log(message); 168 messageLogger2.log(message); 169 } 170 171 public void log(Throwable t) { 172 messageLogger1.log(t); 173 messageLogger2.log(t); 174 } 175 176 public void log(String message, Throwable t) { 177 messageLogger1.log(message, t); 178 messageLogger2.log(message, t); 179 } 180 181 } 183 184 185 188 public static class CompositeMessageSink implements MessageSink { 189 190 private String message; 191 private boolean isActive = true; 192 193 private MessageSink messageSink1; 194 private MessageSink messageSink2; 195 196 public CompositeMessageSink(MessageSink messageSink1, MessageSink messageSink2) { 197 this.messageSink1 = messageSink1; 198 this.messageSink2 = messageSink2; 199 } 200 201 public synchronized String getMessage() { 202 while ((! hasMessage()) && isActive()) { 203 try { 204 wait(1000); 205 } catch (InterruptedException e) {} 206 } 207 if (messageSink1.hasMessage()) { 208 return messageSink1.getMessage(); 209 } else if (messageSink2.hasMessage()) { 210 return messageSink1.getMessage(); 211 } 212 return null; 213 } 214 215 216 public synchronized void setMessage(String messageToPost) { 217 messageSink1.setMessage(messageToPost); 218 notifyAll(); 219 } 220 221 public synchronized boolean hasMessage() { 222 return messageSink1.hasMessage() || messageSink2.hasMessage(); 223 } 224 225 public synchronized boolean isActive() { 226 return messageSink1.isActive() || messageSink2.isActive(); 227 } 228 } 230 } 231 | Popular Tags |