1 31 package org.objectweb.proactive.ic2d.gui.process; 32 33 import java.net.InetAddress ; 34 import java.net.UnknownHostException ; 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 { 45 46 private static final String [] DEFAULT_ENVIRONMENT = { "DISPLAY="+RSHProcess.DEFAULT_HOSTNAME+":0" }; 47 private static final java.awt.Color FINISHED_PROCESS_COLOR = new java.awt.Color (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 mainSplitPanel; 54 55 59 public ProcessControlPanel() { 60 processesListModel = new ProcessesListModel(); 61 62 setLayout(new java.awt.GridLayout (1,1)); 63 processListPanel = new ProcessListPanel(); 65 processDefinitionPanel = new ProcessDefinitionPanel(); 66 processOutputPanel = new ProcessOutputPanel(); 67 javax.swing.JSplitPane topSplitPanel = new javax.swing.JSplitPane (javax.swing.JSplitPane.HORIZONTAL_SPLIT, false, processListPanel, processDefinitionPanel); 68 topSplitPanel.setDividerLocation(200); 69 topSplitPanel.setOneTouchExpandable(true); 70 71 mainSplitPanel = new javax.swing.JSplitPane (javax.swing.JSplitPane.VERTICAL_SPLIT, false, topSplitPanel, processOutputPanel); 73 mainSplitPanel.setOneTouchExpandable(true); 75 add(mainSplitPanel); 76 } 77 78 82 83 84 88 89 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 107 private interface MonitoredRSHProcessObserver { 108 public void processChanged(JVMProcessWrapper process); 109 } 110 111 private interface MonitoredRSHProcessManager { 112 public JVMProcessWrapper getCurrentProcess(); 113 } 114 115 124 private class ProcessesListModel extends javax.swing.AbstractListModel { 125 126 private java.util.ArrayList processesList; 127 128 132 public ProcessesListModel() { 133 processesList = new java.util.ArrayList (); 134 } 135 136 140 public int getSize() { 144 return processesList.size(); 145 } 146 147 public Object 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 } 165 166 167 170 private class ProcessListCellRenderer extends javax.swing.DefaultListCellRenderer { 171 public ProcessListCellRenderer() {} 172 public java.awt.Component getListCellRendererComponent(javax.swing.JList list, Object o, int index, boolean isSelected, boolean cellHasFocus) { 173 java.awt.Component 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 194 private class ProcessListPanel extends javax.swing.JPanel implements MonitoredRSHProcessObserver, MonitoredRSHProcessManager { 195 private javax.swing.JButton stopProcessButton; 196 private javax.swing.JList processesJList; 197 public ProcessListPanel() { 198 setBorder(javax.swing.BorderFactory.createTitledBorder("List of current processes")); 199 setLayout(new java.awt.BorderLayout ()); 200 processesJList = new javax.swing.JList (processesListModel); 202 processesJList.setCellRenderer(new ProcessListCellRenderer()); 203 processesJList.setVisibleRowCount(10); 204 processesJList.addListSelectionListener(new javax.swing.event.ListSelectionListener () { 205 208 public void valueChanged(javax.swing.event.ListSelectionEvent e) { 209 if (e.getValueIsAdjusting()) return; 210 currentProcessChanged(); 211 } 212 } 213 ); 214 javax.swing.JScrollPane scrollPane = new javax.swing.JScrollPane (processesJList); 215 scrollPane.setHorizontalScrollBarPolicy(javax.swing.JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); 216 add(scrollPane, java.awt.BorderLayout.CENTER); 217 stopProcessButton = new javax.swing.JButton ("Stop selected process"); 219 stopProcessButton.setEnabled(false); 220 stopProcessButton.addActionListener(new java.awt.event.ActionListener () { 221 public void actionPerformed(java.awt.event.ActionEvent 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 } 242 243 244 245 254 private class ProcessDefinitionPanel extends javax.swing.JPanel implements MonitoredRSHProcessObserver { 255 257 String localRuntimeUrl; 258 private javax.swing.JTextField hostnameField = new javax.swing.JTextField (RSHProcess.DEFAULT_HOSTNAME); 259 private javax.swing.JTextField usernameField = new javax.swing.JTextField (RSHProcess.DEFAULT_USERNAME); 260 private javax.swing.JTextField javaPathField = new javax.swing.JTextField (JVMProcessImpl.DEFAULT_JAVAPATH); 261 private javax.swing.JTextField policyFileField = new javax.swing.JTextField (JVMProcessImpl.DEFAULT_POLICY_FILE); 262 private javax.swing.JTextField classnameField = new javax.swing.JTextField ("org.objectweb.proactive.core.runtime.StartRuntime"); 263 private javax.swing.JTextField parametersField; 264 private javax.swing.JTextArea classpathField = new javax.swing.JTextArea (JVMProcessImpl.DEFAULT_CLASSPATH, 4, 0); 265 private javax.swing.JTextArea environmentField = new javax.swing.JTextArea (stringArrayToString(DEFAULT_ENVIRONMENT), 4, 0); 266 267 public ProcessDefinitionPanel() { 268 setBorder(javax.swing.BorderFactory.createTitledBorder("Create new process")); 269 setLayout(new java.awt.BorderLayout ()); 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 ("ic2d "+localRuntimeUrl+" rmi: 1 jvm"); 278 279 280 { 282 javax.swing.JPanel northPanel = new javax.swing.JPanel (new java.awt.BorderLayout ()); 283 javax.swing.JPanel labelPanel = new javax.swing.JPanel (new java.awt.GridLayout (0,1)); 284 javax.swing.JPanel fieldPanel = new javax.swing.JPanel (new java.awt.GridLayout (0,1)); 285 labelPanel.add(new javax.swing.JLabel ("hostname ", javax.swing.JLabel.RIGHT)); 286 fieldPanel.add(hostnameField); 287 labelPanel.add(new javax.swing.JLabel ("username ", javax.swing.JLabel.RIGHT)); 288 fieldPanel.add(usernameField); 289 labelPanel.add(new javax.swing.JLabel ("java command path ", javax.swing.JLabel.RIGHT)); 290 fieldPanel.add(javaPathField); 291 labelPanel.add(new javax.swing.JLabel ("policy file path ", javax.swing.JLabel.RIGHT)); 292 fieldPanel.add(policyFileField); 293 labelPanel.add(new javax.swing.JLabel ("classname to start ", javax.swing.JLabel.RIGHT)); 294 fieldPanel.add(classnameField); 295 labelPanel.add(new javax.swing.JLabel ("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 javax.swing.JPanel labelPanel = new javax.swing.JPanel (new java.awt.GridLayout (0,1)); 305 javax.swing.JPanel fieldPanel = new javax.swing.JPanel (new java.awt.GridLayout (0,1)); 306 labelPanel.add(new javax.swing.JLabel ("classpath ", javax.swing.JLabel.RIGHT)); 307 fieldPanel.add(createScrollWrapper(classpathField)); 308 classpathField.setLineWrap(true); 309 labelPanel.add(new javax.swing.JLabel ("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 { 318 javax.swing.JButton b = new javax.swing.JButton ("Start new process"); 319 b.addActionListener(new java.awt.event.ActionListener () { 320 public void actionPerformed(java.awt.event.ActionEvent e) { 321 String hostname = filterEmptyString(hostnameField.getText()); 322 String username = filterEmptyString(usernameField.getText()); 323 String javaPath = filterEmptyString(javaPathField.getText()); 324 String policyFile = filterEmptyString(policyFileField.getText()); 325 String classname = filterEmptyString(classnameField.getText()); 326 String classpath = filterEmptyString(classpathField.getText()); 327 String parameters = filterEmptyString(parametersField.getText()); 328 String [] 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 [] environment, String javaPath, String policyFile, String classpath, 352 String classname, String parameters, String hostname, String 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 e) { 370 messagePanel.getMessageLogger().log("Unknown Host",e); 373 374 } 375 return new JVMProcessWrapper(process, messagePanel, javaPath, policyFile, classpath, classname, parameters); 376 } 377 378 private String filterNull(String s) { 379 if (s == null) return ""; 380 return s; 381 } 382 383 private String filterEmptyString(String s) { 384 if (s.length() == 0) return null; 385 return s; 386 } 387 388 private String stringArrayToString(String [] stringArray) { 389 if (stringArray == null) return ""; 390 StringBuffer sb = new StringBuffer (); 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 [] stringToStringArray(String string) { 399 java.util.StringTokenizer st = new java.util.StringTokenizer (string, "\n"); 400 java.util.ArrayList result = new java.util.ArrayList (); 401 while (st.hasMoreTokens()) { 402 String s = st.nextToken().trim(); 403 if (s.length() > 0) { 404 result.add(s); 405 } 406 } 407 if (result.size() == 0) return null; 408 String [] stringArray = new String [result.size()]; 409 result.toArray(stringArray); 410 return stringArray; 411 } 412 413 private javax.swing.JScrollPane createScrollWrapper(javax.swing.JTextArea textArea) { 414 javax.swing.JScrollPane areaScrollPane = new javax.swing.JScrollPane (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 } 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 message) { 433 logger.log(message); 434 } 435 436 public synchronized void log(String message, Throwable e) { 437 logger.log(message, e); 438 } 439 440 public synchronized void log(Throwable e) { 441 logger.log(e); 442 } 443 444 } 445 446 447 456 private class ProcessOutputPanel extends javax.swing.JPanel implements MonitoredRSHProcessObserver { 457 458 private javax.swing.JPanel emptyProcessPanel; 459 460 public ProcessOutputPanel() { 461 setLayout(new java.awt.GridLayout (1,1)); 462 emptyProcessPanel = new javax.swing.JPanel (); 464 emptyProcessPanel.add(new javax.swing.JLabel ("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 } 482 } 483 484 | Popular Tags |