1 11 package org.eclipse.jdt.internal.launching; 12 13 14 import java.io.BufferedInputStream ; 15 import java.io.BufferedOutputStream ; 16 import java.io.File ; 17 import java.io.FileInputStream ; 18 import java.io.FileOutputStream ; 19 import java.io.FileWriter ; 20 import java.io.IOException ; 21 import java.io.InputStream ; 22 import java.util.HashMap ; 23 import java.util.Iterator ; 24 import java.util.Map ; 25 26 import org.eclipse.core.resources.IResource; 27 import org.eclipse.core.resources.ResourcesPlugin; 28 import org.eclipse.core.runtime.CoreException; 29 import org.eclipse.core.runtime.IProgressMonitor; 30 import org.eclipse.core.runtime.Path; 31 import org.eclipse.debug.core.DebugEvent; 32 import org.eclipse.debug.core.DebugPlugin; 33 import org.eclipse.debug.core.IDebugEventSetListener; 34 import org.eclipse.debug.core.ILaunch; 35 import org.eclipse.debug.core.ILaunchConfiguration; 36 import org.eclipse.debug.core.model.IDebugTarget; 37 import org.eclipse.debug.core.model.IProcess; 38 import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants; 39 import org.eclipse.jdt.launching.JavaLaunchDelegate; 40 import org.eclipse.jdt.launching.JavaRuntime; 41 42 public class JavaAppletLaunchConfigurationDelegate extends JavaLaunchDelegate implements IDebugEventSetListener { 43 44 49 private static Map fgLaunchToFileMap = new HashMap (); 50 51 54 private ILaunch fLaunch; 55 56 59 public synchronized void launch(ILaunchConfiguration configuration, String mode, ILaunch launch, IProgressMonitor monitor) throws CoreException { 60 try { 61 fLaunch = launch; 62 super.launch(configuration, mode, launch, monitor); 63 } catch (CoreException e) { 64 cleanup(launch); 65 throw e; 66 } 67 fLaunch = null; 68 } 69 70 76 public String getJavaPolicyFile(File workingDir) { 77 File file = new File (workingDir, "java.policy.applet"); if (!file.exists()) { 79 File test = LaunchingPlugin.getFileInPlugin(new Path("java.policy.applet")); BufferedOutputStream outputStream= null; 82 try { 83 byte[] bytes = getFileByteContent(test); 84 outputStream = new BufferedOutputStream (new FileOutputStream (file)); 85 outputStream.write(bytes); 86 } catch (IOException e) { 87 return ""; } finally { 89 if (outputStream != null) { 90 try { 91 outputStream.close(); 92 } catch (IOException e1) { 93 } 94 } 95 } 96 } 97 return "-Djava.security.policy=java.policy.applet"; } 99 100 106 private File buildHTMLFile(ILaunchConfiguration configuration, File dir) { 107 FileWriter writer = null; 108 File tempFile = null; 109 try { 110 String name = getAppletMainTypeName(configuration); 111 tempFile = new File (dir, name + System.currentTimeMillis() + ".html"); writer = new FileWriter (tempFile); 113 writer.write("<html>\n"); writer.write("<body>\n"); writer.write("<applet code="); writer.write(name); 117 writer.write(".class "); String appletName = configuration.getAttribute(IJavaLaunchConfigurationConstants.ATTR_APPLET_NAME, ""); if (appletName.length() != 0) { 120 writer.write("NAME =\"" + appletName + "\" "); } 122 writer.write("width=\""); writer.write(Integer.toString(configuration.getAttribute(IJavaLaunchConfigurationConstants.ATTR_APPLET_WIDTH, 200))); 124 writer.write("\" height=\""); writer.write(Integer.toString(configuration.getAttribute(IJavaLaunchConfigurationConstants.ATTR_APPLET_HEIGHT, 200))); 126 writer.write("\" >\n"); Map parameters = configuration.getAttribute(IJavaLaunchConfigurationConstants.ATTR_APPLET_PARAMETERS, new HashMap ()); 128 if (parameters.size() != 0) { 129 Iterator iterator= parameters.entrySet().iterator(); 130 while(iterator.hasNext()) { 131 Map.Entry next = (Map.Entry ) iterator.next(); 132 writer.write("<param name="); writer.write(getQuotedString((String )next.getKey())); 134 writer.write(" value="); writer.write(getQuotedString((String )next.getValue())); 136 writer.write(">\n"); } 138 } 139 writer.write("</applet>\n"); writer.write("</body>\n"); writer.write("</html>\n"); } catch(IOException e) { 143 } catch(CoreException e) { 144 } finally { 145 if (writer != null) { 146 try { 147 writer.close(); 148 } catch(IOException e) { 149 } 150 } 151 } 152 153 return tempFile; 154 } 155 156 private String getQuotedString(String string) { 157 if (string.indexOf('"') == -1) { 158 return '"' + string + '"'; 159 } 160 return '\'' + string + '\''; 161 } 162 163 166 public void handleDebugEvents(DebugEvent[] events) { 167 for (int i = 0; i < events.length; i++) { 168 DebugEvent event = events[i]; 169 Object eventSource = event.getSource(); 170 switch(event.getKind()) { 171 172 case DebugEvent.TERMINATE : 174 if (eventSource != null) { 175 ILaunch launch = null; 176 if (eventSource instanceof IProcess) { 177 IProcess process = (IProcess) eventSource; 178 launch = process.getLaunch(); 179 } else if (eventSource instanceof IDebugTarget) { 180 IDebugTarget debugTarget = (IDebugTarget) eventSource; 181 launch = debugTarget.getLaunch(); 182 } 183 if (launch != null) { 184 cleanup(launch); 185 } 186 } 187 break; 188 } 189 } 190 } 191 192 197 private void cleanup(ILaunch launch) { 198 File temp = (File ) fgLaunchToFileMap.get(launch); 199 if (temp != null) { 200 try { 201 fgLaunchToFileMap.remove(launch); 202 temp.delete(); 203 } finally { 204 if (fgLaunchToFileMap.isEmpty()) { 205 DebugPlugin.getDefault().removeDebugEventListener(this); 206 } 207 } 208 } 209 } 210 211 215 protected static byte[] getFileByteContent(File file) throws IOException { 216 InputStream stream = null; 217 try { 218 stream = new BufferedInputStream (new FileInputStream (file)); 219 return getInputStreamAsByteArray(stream, (int) file.length()); 220 } finally { 221 if (stream != null) { 222 try { 223 stream.close(); 224 } catch (IOException e) { 225 } 226 } 227 } 228 } 229 230 237 protected static byte[] getInputStreamAsByteArray(InputStream stream, int length) 238 throws IOException { 239 byte[] contents; 240 if (length == -1) { 241 contents = new byte[0]; 242 int contentsLength = 0; 243 int bytesRead = -1; 244 do { 245 int available = stream.available(); 246 247 if (contentsLength + available > contents.length) { 249 System.arraycopy( 250 contents, 251 0, 252 contents = new byte[contentsLength + available], 253 0, 254 contentsLength); 255 } 256 257 bytesRead = stream.read(contents, contentsLength, available); 259 260 if (bytesRead > 0) { 261 contentsLength += bytesRead; 263 } 264 } while (bytesRead > 0); 265 266 if (contentsLength < contents.length) { 268 System.arraycopy( 269 contents, 270 0, 271 contents = new byte[contentsLength], 272 0, 273 contentsLength); 274 } 275 } else { 276 contents = new byte[length]; 277 int len = 0; 278 int readSize = 0; 279 while ((readSize != -1) && (len != length)) { 280 len += readSize; 283 readSize = stream.read(contents, len, length - len); 284 } 285 } 286 287 return contents; 288 } 289 290 293 public String getProgramArguments(ILaunchConfiguration configuration) throws CoreException { 294 File workingDir = verifyWorkingDirectory(configuration); 295 File htmlFile = buildHTMLFile(configuration, workingDir); 297 if (htmlFile == null) { 298 abort(LaunchingMessages.JavaAppletLaunchConfigurationDelegate_Could_not_build_HTML_file_for_applet_launch_1, null, IJavaLaunchConfigurationConstants.ERR_COULD_NOT_BUILD_HTML); 299 } 300 if (fgLaunchToFileMap.isEmpty()) { 302 DebugPlugin.getDefault().addDebugEventListener(this); 303 } 304 fgLaunchToFileMap.put(fLaunch, htmlFile); 306 return htmlFile.getName(); 307 } 308 309 312 public String getVMArguments(ILaunchConfiguration configuration) throws CoreException { 313 StringBuffer arguments = new StringBuffer (super.getVMArguments(configuration)); 314 File workingDir = verifyWorkingDirectory(configuration); 315 String javaPolicyFile = getJavaPolicyFile(workingDir); 316 arguments.append(" "); arguments.append(javaPolicyFile); 318 return arguments.toString(); 319 } 320 321 324 public String getMainTypeName(ILaunchConfiguration configuration) throws CoreException { 325 return configuration.getAttribute(IJavaLaunchConfigurationConstants.ATTR_APPLET_APPLETVIEWER_CLASS, IJavaLaunchConfigurationConstants.DEFAULT_APPLETVIEWER_CLASS); 326 } 327 328 335 protected String getAppletMainTypeName(ILaunchConfiguration configuration) throws CoreException { 336 return super.getMainTypeName(configuration); 337 } 338 339 342 protected File getDefaultWorkingDirectory(ILaunchConfiguration configuration) throws CoreException { 343 String outputDir = JavaRuntime.getProjectOutputDirectory(configuration); 345 if (outputDir == null) { 346 return new File (System.getProperty("user.dir")); } 349 IResource resource = ResourcesPlugin.getWorkspace().getRoot().findMember(outputDir); 350 if (resource == null || !resource.exists()) { 351 return new File (System.getProperty("user.dir")); } 354 return resource.getLocation().toFile(); 355 } 356 357 358 } 359 | Popular Tags |