KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > proactive > ic2d > gui > process > ProcessControlPanel


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.ic2d.gui.process;
32
33 import java.net.InetAddress JavaDoc;
34 import java.net.UnknownHostException JavaDoc;
35
36 import org.objectweb.proactive.core.ProActiveException;
37 import org.objectweb.proactive.core.process.JVMProcessImpl;
38 import org.objectweb.proactive.core.process.rsh.RSHProcess;
39 import org.objectweb.proactive.core.runtime.ProActiveRuntime;
40 import org.objectweb.proactive.core.runtime.RuntimeFactory;
41 import org.objectweb.proactive.core.util.MessageLogger;
42 import org.objectweb.proactive.ic2d.gui.util.MessagePanel;
43
44 public class ProcessControlPanel extends javax.swing.JPanel JavaDoc {
45
46   private static final String JavaDoc[] DEFAULT_ENVIRONMENT = { "DISPLAY="+RSHProcess.DEFAULT_HOSTNAME+":0" };
47   private static final java.awt.Color JavaDoc FINISHED_PROCESS_COLOR = new java.awt.Color JavaDoc(211,32,47);
48   
49   private ProcessesListModel processesListModel;
50   private ProcessListPanel processListPanel;
51   private ProcessDefinitionPanel processDefinitionPanel;
52   private ProcessOutputPanel processOutputPanel;
53   private javax.swing.JSplitPane JavaDoc mainSplitPanel;
54
55   //
56
// -- CONSTRUCTORS -----------------------------------------------
57
//
58

59   public ProcessControlPanel() {
60     processesListModel = new ProcessesListModel();
61     
62     setLayout(new java.awt.GridLayout JavaDoc(1,1));
63     // create the top split panel
64
processListPanel = new ProcessListPanel();
65     processDefinitionPanel = new ProcessDefinitionPanel();
66     processOutputPanel = new ProcessOutputPanel();
67     javax.swing.JSplitPane JavaDoc topSplitPanel = new javax.swing.JSplitPane JavaDoc(javax.swing.JSplitPane.HORIZONTAL_SPLIT, false, processListPanel, processDefinitionPanel);
68     topSplitPanel.setDividerLocation(200);
69     topSplitPanel.setOneTouchExpandable(true);
70     
71     //Create the full split pane
72
mainSplitPanel = new javax.swing.JSplitPane JavaDoc(javax.swing.JSplitPane.VERTICAL_SPLIT, false, topSplitPanel, processOutputPanel);
73     //mainSplitPanel.setDividerLocation(getHeight() - 200);
74
mainSplitPanel.setOneTouchExpandable(true);
75     add(mainSplitPanel);
76   }
77
78   //
79
// -- PUBLIC METHODS -----------------------------------------------
80
//
81

82   
83
84   //
85
// -- PROTECTED METHODS -----------------------------------------------
86
//
87

88   
89   //
90
// -- PRIVATE METHODS -----------------------------------------------
91
//
92

93   private void processChanged(JVMProcessWrapper process) {
94     processListPanel.processChanged(process);
95     processDefinitionPanel.processChanged(process);
96     processOutputPanel.processChanged(process);
97   }
98   
99   private void currentProcessChanged() {
100     processChanged(processListPanel.getCurrentProcess());
101   }
102   
103   //
104
// -- INNER CLASSES -----------------------------------------------
105
//
106

107   private interface MonitoredRSHProcessObserver {
108     public void processChanged(JVMProcessWrapper process);
109   }
110   
111   private interface MonitoredRSHProcessManager {
112     public JVMProcessWrapper getCurrentProcess();
113   }
114   
115  /**
116   *
117   *
118   *
119   * ProcessList
120   *
121   *
122   *
123   */

124   private class ProcessesListModel extends javax.swing.AbstractListModel JavaDoc {
125   
126     private java.util.ArrayList JavaDoc processesList;
127   
128     //
129
// -- CONSTRUCTORS -----------------------------------------------
130
//
131

132     public ProcessesListModel() {
133       processesList = new java.util.ArrayList JavaDoc();
134     }
135
136     //
137
// -- Public methods -----------------------------------------------
138
//
139

140     //
141
// -- implements ListModel -----------------------------------------------
142
//
143
public int getSize() {
144       return processesList.size();
145     }
146
147     public Object JavaDoc getElementAt(int index) {
148       return processesList.get(index);
149     }
150     
151     public void addProcess(JVMProcessWrapper wrapper) {
152       int n = processesList.size();
153       processesList.add(wrapper);
154       fireIntervalAdded(this, n, n);
155     }
156
157     public void removeProcess(int index) {
158       JVMProcessWrapper wrapper = (JVMProcessWrapper) processesList.remove(index);
159       wrapper.stopProcess();
160       fireIntervalRemoved(this, index, index);
161     }
162
163   } // end inner class ProcessListModel
164

165
166   
167   /**
168    * Cell Renderer
169    */

170   private class ProcessListCellRenderer extends javax.swing.DefaultListCellRenderer JavaDoc {
171     public ProcessListCellRenderer() {}
172     public java.awt.Component JavaDoc getListCellRendererComponent(javax.swing.JList JavaDoc list, Object JavaDoc o, int index, boolean isSelected, boolean cellHasFocus) {
173       java.awt.Component JavaDoc c = super.getListCellRendererComponent(list, o, index, isSelected, cellHasFocus);
174       if (! (o instanceof JVMProcessWrapper)) return c;
175       JVMProcessWrapper wrapper = (JVMProcessWrapper) o;
176       if (wrapper.getProcess().isFinished()) {
177         c.setForeground(FINISHED_PROCESS_COLOR);
178       }
179       return c;
180     }
181   }
182
183
184
185   /**
186    *
187    *
188    *
189    * ProcessListPanel
190    *
191    *
192    *
193    */

194   private class ProcessListPanel extends javax.swing.JPanel JavaDoc implements MonitoredRSHProcessObserver, MonitoredRSHProcessManager {
195     private javax.swing.JButton JavaDoc stopProcessButton;
196     private javax.swing.JList JavaDoc processesJList;
197     public ProcessListPanel() {
198       setBorder(javax.swing.BorderFactory.createTitledBorder("List of current processes"));
199       setLayout(new java.awt.BorderLayout JavaDoc());
200       // list
201
processesJList = new javax.swing.JList JavaDoc(processesListModel);
202       processesJList.setCellRenderer(new ProcessListCellRenderer());
203       processesJList.setVisibleRowCount(10);
204       processesJList.addListSelectionListener(new javax.swing.event.ListSelectionListener JavaDoc() {
205            /**
206             * When an item is selected, this method is fired and updates the display area
207             */

208             public void valueChanged(javax.swing.event.ListSelectionEvent JavaDoc e) {
209               if (e.getValueIsAdjusting()) return;
210               currentProcessChanged();
211             }
212           }
213         );
214       javax.swing.JScrollPane JavaDoc scrollPane = new javax.swing.JScrollPane JavaDoc(processesJList);
215       scrollPane.setHorizontalScrollBarPolicy(javax.swing.JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
216       add(scrollPane, java.awt.BorderLayout.CENTER);
217       // stop button
218
stopProcessButton = new javax.swing.JButton JavaDoc("Stop selected process");
219       stopProcessButton.setEnabled(false);
220       stopProcessButton.addActionListener(new java.awt.event.ActionListener JavaDoc() {
221           public void actionPerformed(java.awt.event.ActionEvent JavaDoc e) {
222             int n = processesJList.getSelectedIndex();
223             if (n > -1) {
224               processesListModel.removeProcess(n);
225               currentProcessChanged();
226             }
227           }
228         });
229       stopProcessButton.setToolTipText("Stop the selected process in the list");
230       add(stopProcessButton, java.awt.BorderLayout.SOUTH);
231     }
232       
233     public JVMProcessWrapper getCurrentProcess() {
234       return (JVMProcessWrapper) processesJList.getSelectedValue();
235     }
236     
237     public void processChanged(JVMProcessWrapper process) {
238       stopProcessButton.setEnabled(process != null);
239     }
240   } // end inner class ProcessListPanel
241

242
243
244
245  /**
246   *
247   *
248   *
249   * ProcessDefinitionPanel
250   *
251   *
252   *
253   */

254   private class ProcessDefinitionPanel extends javax.swing.JPanel JavaDoc implements MonitoredRSHProcessObserver {
255     //ProActiveRuntimeImpl part = (ProActiveRuntimeImpl) ProActiveRuntimeImpl.getProActiveRuntime();
256

257     String JavaDoc localRuntimeUrl;
258     private javax.swing.JTextField JavaDoc hostnameField = new javax.swing.JTextField JavaDoc(RSHProcess.DEFAULT_HOSTNAME);
259     private javax.swing.JTextField JavaDoc usernameField = new javax.swing.JTextField JavaDoc(RSHProcess.DEFAULT_USERNAME);
260     private javax.swing.JTextField JavaDoc javaPathField = new javax.swing.JTextField JavaDoc(JVMProcessImpl.DEFAULT_JAVAPATH);
261     private javax.swing.JTextField JavaDoc policyFileField = new javax.swing.JTextField JavaDoc(JVMProcessImpl.DEFAULT_POLICY_FILE);
262     private javax.swing.JTextField JavaDoc classnameField = new javax.swing.JTextField JavaDoc("org.objectweb.proactive.core.runtime.StartRuntime");
263     private javax.swing.JTextField JavaDoc parametersField;
264     private javax.swing.JTextArea JavaDoc classpathField = new javax.swing.JTextArea JavaDoc(JVMProcessImpl.DEFAULT_CLASSPATH, 4, 0);
265     private javax.swing.JTextArea JavaDoc environmentField = new javax.swing.JTextArea JavaDoc(stringArrayToString(DEFAULT_ENVIRONMENT), 4, 0);
266     
267     public ProcessDefinitionPanel() {
268       setBorder(javax.swing.BorderFactory.createTitledBorder("Create new process"));
269       setLayout(new java.awt.BorderLayout JavaDoc());
270       ProActiveRuntime part;
271     try {
272         part = RuntimeFactory.getDefaultRuntime();
273         localRuntimeUrl = part.getURL();
274     } catch (ProActiveException e1) {
275         e1.printStackTrace();
276     }
277     parametersField = new javax.swing.JTextField JavaDoc("ic2d "+localRuntimeUrl+" rmi: 1 jvm");
278       
279       
280       // defines fields in the north panel
281
{
282       javax.swing.JPanel JavaDoc northPanel = new javax.swing.JPanel JavaDoc(new java.awt.BorderLayout JavaDoc());
283       javax.swing.JPanel JavaDoc labelPanel = new javax.swing.JPanel JavaDoc(new java.awt.GridLayout JavaDoc(0,1));
284       javax.swing.JPanel JavaDoc fieldPanel = new javax.swing.JPanel JavaDoc(new java.awt.GridLayout JavaDoc(0,1));
285       labelPanel.add(new javax.swing.JLabel JavaDoc("hostname ", javax.swing.JLabel.RIGHT));
286       fieldPanel.add(hostnameField);
287       labelPanel.add(new javax.swing.JLabel JavaDoc("username ", javax.swing.JLabel.RIGHT));
288       fieldPanel.add(usernameField);
289       labelPanel.add(new javax.swing.JLabel JavaDoc("java command path ", javax.swing.JLabel.RIGHT));
290       fieldPanel.add(javaPathField);
291       labelPanel.add(new javax.swing.JLabel JavaDoc("policy file path ", javax.swing.JLabel.RIGHT));
292       fieldPanel.add(policyFileField);
293       labelPanel.add(new javax.swing.JLabel JavaDoc("classname to start ", javax.swing.JLabel.RIGHT));
294       fieldPanel.add(classnameField);
295       labelPanel.add(new javax.swing.JLabel JavaDoc("parameters of the class ", javax.swing.JLabel.RIGHT));
296       fieldPanel.add(parametersField);
297       northPanel.add(labelPanel, java.awt.BorderLayout.WEST);
298       northPanel.add(fieldPanel, java.awt.BorderLayout.CENTER);
299       add(northPanel, java.awt.BorderLayout.NORTH);
300       }
301       
302       {
303       // defines text areas in the center panel
304
javax.swing.JPanel JavaDoc labelPanel = new javax.swing.JPanel JavaDoc(new java.awt.GridLayout JavaDoc(0,1));
305       javax.swing.JPanel JavaDoc fieldPanel = new javax.swing.JPanel JavaDoc(new java.awt.GridLayout JavaDoc(0,1));
306       labelPanel.add(new javax.swing.JLabel JavaDoc("classpath ", javax.swing.JLabel.RIGHT));
307       fieldPanel.add(createScrollWrapper(classpathField));
308       classpathField.setLineWrap(true);
309       labelPanel.add(new javax.swing.JLabel JavaDoc("environment ", javax.swing.JLabel.RIGHT));
310       fieldPanel.add(createScrollWrapper(environmentField));
311       environmentField.setLineWrap(false);
312       add(labelPanel, java.awt.BorderLayout.WEST);
313       add(fieldPanel, java.awt.BorderLayout.CENTER);
314       }
315       
316       // defines start button in the south panel
317
{
318       javax.swing.JButton JavaDoc b = new javax.swing.JButton JavaDoc("Start new process");
319       b.addActionListener(new java.awt.event.ActionListener JavaDoc() {
320           public void actionPerformed(java.awt.event.ActionEvent JavaDoc e) {
321             String JavaDoc hostname = filterEmptyString(hostnameField.getText());
322             String JavaDoc username = filterEmptyString(usernameField.getText());
323             String JavaDoc javaPath = filterEmptyString(javaPathField.getText());
324             String JavaDoc policyFile = filterEmptyString(policyFileField.getText());
325             String JavaDoc classname = filterEmptyString(classnameField.getText());
326             String JavaDoc classpath = filterEmptyString(classpathField.getText());
327             String JavaDoc parameters = filterEmptyString(parametersField.getText());
328             String JavaDoc[] environment = stringToStringArray(environmentField.getText());
329             JVMProcessWrapper w = createJVMProcess(environment, javaPath, policyFile, classpath, classname, parameters, hostname, username);
330             processesListModel.addProcess(w);
331             w.startProcess();
332           }
333         });
334       b.setToolTipText("Start a process based on the above specs");
335       add(b, java.awt.BorderLayout.SOUTH);
336       }
337     }
338     
339     public void processChanged(JVMProcessWrapper wrapper) {
340       if (wrapper == null) return;
341       hostnameField.setText(filterNull(wrapper.getHostname()));
342       usernameField.setText(filterNull(wrapper.getUsername()));
343       javaPathField.setText(filterNull(wrapper.getJavaPath()));
344       policyFileField.setText(filterNull(wrapper.getPolicyFile()));
345       classnameField.setText(filterNull(wrapper.getClassname()));
346       parametersField.setText(filterNull(wrapper.getParameters()));
347       classpathField.setText(filterNull(wrapper.getClasspath()));
348       environmentField.setText(stringArrayToString(wrapper.getProcess().getEnvironment()));
349     }
350     
351     private JVMProcessWrapper createJVMProcess(String JavaDoc[] environment, String JavaDoc javaPath, String JavaDoc policyFile, String JavaDoc classpath,
352                                                String JavaDoc classname, String JavaDoc parameters, String JavaDoc hostname, String JavaDoc username) {
353       MessagePanel messagePanel = new MessagePanel("Messages for process running "+classname);
354       JVMProcessImpl process = new JVMProcessImpl(new SynchronizedMessageLogger(messagePanel.getMessageLogger()));
355       process.setEnvironment(environment);
356       process.setJavaPath(javaPath);
357       process.setPolicyFile(policyFile);
358       process.setClasspath(classpath);
359       process.setClassname(classname);
360       process.setParameters(parameters);
361       try {
362         if (!hostname.equals(InetAddress.getLocalHost().getHostName())) {
363             RSHProcess rshProcess = new RSHProcess(process);
364             rshProcess.setHostname(hostname);
365             rshProcess.setUsername(username);
366             process.setParameters("ic2d "+localRuntimeUrl+" rmi: 1 rsh-jvm");
367             return new JVMProcessWrapper(rshProcess, messagePanel, javaPath, policyFile, classpath, classname, parameters, hostname, username);
368           }
369     } catch (UnknownHostException JavaDoc e) {
370         // TODO Auto-generated catch block
371
//e.printStackTrace();
372
messagePanel.getMessageLogger().log("Unknown Host",e);
373         
374     }
375       return new JVMProcessWrapper(process, messagePanel, javaPath, policyFile, classpath, classname, parameters);
376     }
377     
378     private String JavaDoc filterNull(String JavaDoc s) {
379       if (s == null) return "";
380       return s;
381     }
382     
383     private String JavaDoc filterEmptyString(String JavaDoc s) {
384       if (s.length() == 0) return null;
385       return s;
386     }
387     
388     private String JavaDoc stringArrayToString(String JavaDoc[] stringArray) {
389       if (stringArray == null) return "";
390       StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
391       for (int i=0; i<stringArray.length; i++) {
392         sb.append(stringArray[i]);
393         sb.append("\n");
394       }
395       return sb.toString();
396     }
397     
398     private String JavaDoc[] stringToStringArray(String JavaDoc string) {
399       java.util.StringTokenizer JavaDoc st = new java.util.StringTokenizer JavaDoc(string, "\n");
400       java.util.ArrayList JavaDoc result = new java.util.ArrayList JavaDoc();
401       while (st.hasMoreTokens()) {
402         String JavaDoc s = st.nextToken().trim();
403         if (s.length() > 0) {
404           result.add(s);
405         }
406       }
407       if (result.size() == 0) return null;
408       String JavaDoc[] stringArray = new String JavaDoc[result.size()];
409       result.toArray(stringArray);
410       return stringArray;
411     }
412     
413     private javax.swing.JScrollPane JavaDoc createScrollWrapper(javax.swing.JTextArea JavaDoc textArea) {
414       javax.swing.JScrollPane JavaDoc areaScrollPane = new javax.swing.JScrollPane JavaDoc(textArea);
415       areaScrollPane.setVerticalScrollBarPolicy(javax.swing.JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
416       areaScrollPane.setBorder(javax.swing.BorderFactory.createLineBorder(java.awt.Color.black));
417       return areaScrollPane;
418     }
419
420   } // end inner class ProcessDefinitionPanel
421

422
423
424   public class SynchronizedMessageLogger implements MessageLogger {
425     
426     private MessageLogger logger;
427     
428     public SynchronizedMessageLogger(MessageLogger logger) {
429       this.logger = logger;
430     }
431
432     public synchronized void log(String JavaDoc message) {
433       logger.log(message);
434     }
435
436     public synchronized void log(String JavaDoc message, Throwable JavaDoc e) {
437       logger.log(message, e);
438     }
439
440     public synchronized void log(Throwable JavaDoc e) {
441       logger.log(e);
442     }
443
444   }
445
446
447  /**
448   *
449   *
450   *
451   * ProcessOutputPanel
452   *
453   *
454   *
455   */

456   private class ProcessOutputPanel extends javax.swing.JPanel JavaDoc implements MonitoredRSHProcessObserver {
457   
458     private javax.swing.JPanel JavaDoc emptyProcessPanel;
459
460     public ProcessOutputPanel() {
461       setLayout(new java.awt.GridLayout JavaDoc(1,1));
462       // create the empty Process Panel
463
emptyProcessPanel = new javax.swing.JPanel JavaDoc();
464       emptyProcessPanel.add(new javax.swing.JLabel JavaDoc("no process selected"));
465       add(emptyProcessPanel);
466     }
467     
468     public void processChanged(JVMProcessWrapper process) {
469       removeAll();
470       if (process == null) {
471         add(emptyProcessPanel);
472       } else {
473         add(process.getPanel());
474       }
475       revalidate();
476       repaint();
477     }
478   
479   
480   } // end inner class ProcessOutputPanel
481

482 }
483
484
Popular Tags