1 /* 2 * $Id$ 3 * 4 * Copyright (c) 1996, 2014, Oracle and/or its affiliates. All rights reserved. 5 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 6 * 7 * This code is free software; you can redistribute it and/or modify it 8 * under the terms of the GNU General Public License version 2 only, as 9 * published by the Free Software Foundation. Oracle designates this 10 * particular file as subject to the "Classpath" exception as provided 11 * by Oracle in the LICENSE file that accompanied this code. 12 * 13 * This code is distributed in the hope that it will be useful, but WITHOUT 14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 15 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 16 * version 2 for more details (a copy is included in the LICENSE file that 17 * accompanied this code). 18 * 19 * You should have received a copy of the GNU General Public License version 20 * 2 along with this work; if not, write to the Free Software Foundation, 21 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 22 * 23 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 24 * or visit www.oracle.com if you need additional information or have any 25 * questions. 26 */ 27 package com.sun.javatest.agent; 28 29 import java.applet.Applet; 30 import java.applet.AppletContext; 31 import java.awt.Component; 32 import java.awt.Container; 33 import java.awt.Frame; 34 import java.awt.GridBagConstraints; 35 import java.awt.GridBagLayout; 36 import java.awt.GridLayout; 37 import java.awt.Label; 38 import java.awt.Panel; 39 import java.io.IOException; 40 import java.lang.reflect.InvocationTargetException; 41 import java.lang.reflect.Method; 42 import java.net.URL; 43 44 import com.sun.javatest.Status; 45 import com.sun.javatest.util.MainAppletContext; 46 import com.sun.javatest.util.MainFrame; 47 48 /** 49 * An applet that displays and controls a Agent. 50 * 51 * @see Agent 52 * @see AgentMain 53 * @see AgentFrame 54 * 55 **/ 56 public class AgentApplet extends Applet implements Agent.Observer 57 { 58 59 /** 60 * Initialize the applet, based on the applet parameters. 61 * 62 * The following applet parameters are recognized 63 * <table> 64 * <tr><td>mode <em>mode</em> <td> set mode to be "active" or "passive" 65 * <tr><td>activeHost <em>hostname</em> <td> set the host for active connections 66 * <tr><td>activePort <em>port</em> <td> set the port for active connections 67 * <tr><td>passivePort <em>port</em> <td> set the port for passive connections 68 * <tr><td>map <em>url</em> <td> map file for translating arguments of incoming requests; the url is relative to the applet's document base 69 * <tr><td>concurrency <em>number</em> <td> set the maximum number of simultaneous connections 70 * <tr><td>history <em>number</em> <td> set the size of the execution history 71 * <tr><td>trace <em>boolean</em> <td> trace the execution of the agent 72 * <tr><td>observer <em>classname</em> <td> add an observer to the agent that is used 73 * </table> 74 */ 75 public void init() { 76 77 String mode = getParameter("mode", "active"); 78 URL docBase = getDocumentBase(); // may be null if run standalone, sigh 79 String defaultActiveHost = (docBase == null ? "localhost" : docBase.getHost()); 80 String activeHost = getParameter("activeHost", defaultActiveHost); 81 int activePort = getIntParameter("activePort", Agent.defaultActivePort); 82 int passivePort = getIntParameter("passivePort", Agent.defaultPassivePort); 83 String serialPort = getParameter("serialPort"); 84 int concurrency = getIntParameter("concurrency", 1); 85 int history = getIntParameter("history", -1); 86 int delay = getIntParameter("retryDelay", -1); 87 String mapFile = getParameter("map"); 88 String usac = getParameter("useSharedAppletContext"); 89 shareAppletContext = (usac != null && usac.equals("true")); 90 String usf = getParameter("useSharedFrame"); 91 boolean shareFrame = (usf == null || usf.equals("true")); 92 final boolean tracing = "true".equals(getParameter("trace")); 93 boolean autostart = "true".equals(getParameter("start")); 94 String observerClassName = getParameter("observer"); 95 String appletName; 96 97 if (shareAppletContext) { 98 appletName = getParameter("appletName"); 99 // do checks for use with MainAppletContext 100 if (appletName == null) { 101 // the following normally executes on the test platform, so is not i18n 102 String line1 = "Error: Applet parameter \"appletName\" must be defined"; 103 String line2 = "and match the applet's \"name\" attribute."; 104 showStatus("Error starting Applet: Applet parameter \"appletName\" must be defined."); 105 Panel p = new Panel(new GridLayout(0,1)); 106 Label label1 = new Label(line1); 107 Label label2 = new Label(line2); 108 p.add(label1); 109 p.add(label2); 110 add(p); 111 return; 112 } 113 } 114 else 115 appletName = null; 116 117 setLayout(new GridBagLayout()); 118 119 GridBagConstraints c = new GridBagConstraints(); 120 121 ActiveModeOptions amo = new ActiveModeOptions(); 122 if (activeHost != null) 123 amo.setHost(activeHost); 124 amo.setPort(activePort); 125 126 PassiveModeOptions pmo = new PassiveModeOptions(); 127 pmo.setPort(passivePort); 128 129 ModeOptions smo = null; 130 try { 131 Class<?> serial = Class.forName("com.sun.javatest.agent.SerialPortModeOptions"); 132 smo = (ModeOptions)serial.newInstance(); 133 } catch (Exception e) { 134 System.err.println("There is no support for serial port"); 135 } 136 if (serialPort != null){ 137 try { 138 Method setPortMethod = smo.getClass().getMethod("setPort", String.class); 139 setPortMethod.invoke(smo, serialPort); 140 } 141 catch (SecurityException | NoSuchMethodException | 142 IllegalArgumentException| InvocationTargetException | 143 IllegalAccessException e) { 144 System.err.println("Could not set serial port:"); 145 e.printStackTrace(); 146 } 147 } 148 149 ModeOptions[] modeOptions = new ModeOptions[] {amo, pmo, smo}; 150 151 AgentPanel.MapReader mapReader = new AgentPanel.MapReader() { 152 public Map read(String name) throws IOException { 153 if (name == null || name.length() == 0) 154 return null; 155 else { 156 Map m = Map.readURL(new URL(getDocumentBase(), name)); 157 m.setTracing(tracing, System.out); 158 return m; 159 } 160 } 161 }; 162 AgentPanel ap = new AgentPanel(modeOptions, mapReader); 163 164 if (observerClassName != null) { 165 try { 166 Class<?> observerClass = Class.forName(observerClassName); 167 Agent.Observer observer = (Agent.Observer)(observerClass.newInstance()); 168 ap.addObserver(observer); 169 } 170 catch (ClassCastException e) { 171 showStatus("observer is not of type " + 172 Agent.Observer.class.getName() + ": " + observerClassName); 173 } 174 catch (ClassNotFoundException e) { 175 showStatus("cannot find observer class: " + observerClassName); 176 } 177 catch (IllegalAccessException e) { 178 showStatus("problem instantiating observer: " + e); 179 } 180 catch (InstantiationException e) { 181 showStatus("problem instantiating observer: " + e); 182 } 183 } 184 185 ap.setMode(mode); 186 ap.setConcurrency(concurrency); 187 ap.setTracing(tracing, System.out); 188 189 if (mapFile != null) 190 ap.setMapFile(mapFile); 191 192 if (history != -1) 193 ap.setHistoryLimit(history); 194 195 if (delay != -1) 196 ap.setRetryDelay(delay); 197 198 c.weightx = 1; 199 c.weighty = 1; 200 c.fill = GridBagConstraints.BOTH; 201 add(ap, c); 202 203 if (shareFrame) { 204 try { 205 Container x = this; 206 while (x != null && !(x instanceof Frame)) 207 x = x.getParent(); 208 if (x != null) 209 MainFrame.setFrame((Frame)x); 210 } 211 catch (SecurityException e) { 212 System.err.println("Security Exception occurred while attempting to access shared frame; " + e); 213 } 214 } 215 216 if (shareAppletContext) { 217 AppletContext context = getAppletContext(); 218 MainAppletContext.setAppletContext(context); 219 MainAppletContext.putApplet(appletName, this); 220 MainAppletContext.setAgentApplet(this); 221 } 222 223 if (autostart) 224 ap.start(); 225 } 226 227 public void start() { 228 if (shareAppletContext) 229 MainAppletContext.setStarted(true); 230 super.start(); 231 } 232 233 public void destroy() { 234 Component c = getComponent(0); 235 if (c instanceof AgentPanel) { 236 AgentPanel ap = (AgentPanel) c; 237 ap.stop(); 238 } 239 } 240 241 /** 242 * Returns a string containing information about 243 * the author, version and copyright of the applet. 244 */ 245 public String getAppletInfo() { 246 return (Agent.productName + " " + 247 Agent.productVersion + " " + 248 Agent.productCopyright); 249 } 250 251 /** 252 * Returns an array of strings describing the 253 * parameters that are understood by this 254 * applet. 255 */ 256 public String[][] getParameterInfo() { 257 String[][] pinfo = { 258 {"mode", "\"active\" or \"passive\"", 259 "the mode for the agent"}, 260 {"activeHost", "hostname", "the host for active connections"}, 261 {"activePort", "port", "the port for active connections"}, 262 {"passivePort", "port", "the port for passive connections"}, 263 {"map", "url", "map file for translating arguments of incoming requests"}, 264 {"concurrency", "number", "the maximum number of simultaneous connections"}, 265 {"history", "int", "the size of the execution history"}, 266 {"trace", "boolean", "trace the execution of the agent"} 267 }; 268 return pinfo; 269 } 270 271 private int getIntParameter(String name, int dflt) { 272 try { 273 String s = getParameter(name); 274 if (s != null) 275 return Integer.parseInt(s); 276 } 277 catch (NumberFormatException e) { 278 // ignore 279 } 280 return dflt; 281 } 282 283 private String getParameter(String name, String dflt) { 284 String s = getParameter(name); 285 return (s == null ? dflt : s); 286 } 287 288 public void started(Agent sl) { 289 showStatus("agent started"); 290 } 291 292 public void errorOpeningConnection(Agent sl, Exception e) { 293 showStatus("error opening connection: " + e.getMessage()); 294 } 295 296 public void finished(Agent sl) { 297 showStatus("agent stopped"); 298 } 299 300 public synchronized void openedConnection(Agent sl, Connection c) { 301 showStatus("OPENED SOCKET"); 302 } 303 304 public synchronized void execTest(Agent sl, Connection c, String tag, String className, String[] args) { 305 showStatus("EXEC"); 306 } 307 308 public synchronized void execCommand(Agent sl, Connection c, String tag, String className, String[] args) { 309 showStatus("EXEC"); 310 } 311 312 public synchronized void execMain(Agent sl, Connection c, String tag, String className, String[] args) { 313 showStatus("EXEC"); 314 } 315 316 public synchronized void result(Agent sl, Connection c, Status r) { 317 showStatus("RESULT"); 318 } 319 320 public synchronized void exception(Agent sl, Connection c, Throwable t) { 321 showStatus("EXCEPTION (NYI)"); 322 } 323 324 public synchronized void completed(Agent sl, Connection c) { 325 showStatus("COMPLETED (NYI)"); 326 } 327 328 private boolean shareAppletContext; 329 330 }