KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > proactive > core > process > AbstractExternalProcessDecorator


1 /*
2 * ################################################################
3 *
4 * ProActive: The Java(TM) library for Parallel, Distributed,
5 * Concurrent computing with Security and Mobility
6 *
7 * Copyright (C) 1997-2002 INRIA/University of Nice-Sophia Antipolis
8 * Contact: proactive-support@inria.fr
9 *
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or any later version.
14 *
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
23 * USA
24 *
25 * Initial developer(s): The ProActive Team
26 * http://www.inria.fr/oasis/ProActive/contacts.html
27 * Contributor(s):
28 *
29 * ################################################################
30 */

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   //
42
// -- CONSTRUCTORS -----------------------------------------------
43
//
44

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   //
61
// -- PUBLIC METHODS -----------------------------------------------
62
//
63

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   //
90
// -- PROTECTED METHODS -----------------------------------------------
91
//
92

93   protected void toString(StringBuffer JavaDoc 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 JavaDoc 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 JavaDoc internalBuildCommand();
119
120
121   protected void internalStartProcess(String JavaDoc command) throws java.io.IOException JavaDoc {
122     //System.out.println("---------------Internal start process of AbstractExternalProcessDecorator "+command);
123
super.internalStartProcess(command);
124     if (compositionType == SEND_TO_OUTPUT_STREAM_COMPOSITION) {
125       try {
126         Thread.sleep(3000);
127         //System.out.println("---------------Internal start process of AbstractExternalProcessDecorator");
128
} catch (InterruptedException JavaDoc e) {}
129       // the masterProcess is started, now we feed the output with the slave command
130
outputMessageSink.setMessage(targetProcess.getCommand());
131     }
132   }
133
134
135   protected void handleOutput(java.io.BufferedWriter JavaDoc 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   //
144
// -- PRIVATE METHODS -----------------------------------------------
145
//
146

147
148
149   //
150
// -- INNER CLASSES -----------------------------------------------
151
//
152

153   /**
154    * Implementation of a MessageLogger that feeds two MessageLoggers
155    */

156   public static class CompositeMessageLogger implements MessageLogger,java.io.Serializable JavaDoc {
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 JavaDoc message) {
167       messageLogger1.log(message);
168       messageLogger2.log(message);
169     }
170
171     public void log(Throwable JavaDoc t) {
172       messageLogger1.log(t);
173       messageLogger2.log(t);
174     }
175
176     public void log(String JavaDoc message, Throwable JavaDoc t) {
177       messageLogger1.log(message, t);
178       messageLogger2.log(message, t);
179     }
180
181   } // end inner class CompositeMessageLogger
182

183
184
185   /**
186    * Implementation of a MessageSink that can receive one message at a time
187    */

188   public static class CompositeMessageSink implements MessageSink {
189
190     private String JavaDoc 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 JavaDoc getMessage() {
202       while ((! hasMessage()) && isActive()) {
203         try {
204           wait(1000);
205         } catch (InterruptedException JavaDoc 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 JavaDoc 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   } // end inner class CompositeMessageSink
229

230 }
231
Popular Tags