org.eclipse.jdt.launching/launching/org/eclipse/jdt/internal/launching/JavaAppletLaunchConfigurationDelegate.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.33 - (view) (download)

1 : droberts 1.11 /*******************************************************************************
2 : mrennie 1.33 * Copyright (c) 2000, 2007 IBM Corporation and others.
3 : darins 1.25 * All rights reserved. This program and the accompanying materials
4 :     * are made available under the terms of the Eclipse Public License v1.0
5 : droberts 1.11 * which accompanies this distribution, and is available at
6 : darins 1.25 * http://www.eclipse.org/legal/epl-v10.html
7 : darin 1.31 *
8 : droberts 1.11 * Contributors:
9 :     * IBM Corporation - initial API and implementation
10 :     *******************************************************************************/
11 : jszursze 1.1 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 : jszursze 1.7 import org.eclipse.core.resources.IResource;
27 :     import org.eclipse.core.resources.ResourcesPlugin;
28 : jszursze 1.1 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 : darin 1.27 import org.eclipse.jdt.launching.JavaLaunchDelegate;
40 : jszursze 1.1 import org.eclipse.jdt.launching.JavaRuntime;
41 :    
42 : darin 1.27 public class JavaAppletLaunchConfigurationDelegate extends JavaLaunchDelegate implements IDebugEventSetListener {
43 : jszursze 1.1
44 : jszursze 1.6 /**
45 :     * Mapping of ILaunch objects to File objects that represent the .html file
46 :     * used to initiate the applet launch. This is used to delete the .html
47 :     * file when the launch terminates.
48 :     */
49 :     private static Map fgLaunchToFileMap = new HashMap();
50 : jszursze 1.1
51 : darin 1.27 /**
52 :     * Used to map temp file to launch obejct.
53 :     */
54 :     private ILaunch fLaunch;
55 :    
56 : darins 1.17 /* (non-Javadoc)
57 :     * @see org.eclipse.debug.core.model.ILaunchConfigurationDelegate#launch(org.eclipse.debug.core.ILaunchConfiguration, java.lang.String, org.eclipse.debug.core.ILaunch, org.eclipse.core.runtime.IProgressMonitor)
58 : jszursze 1.1 */
59 : darin 1.27 public synchronized void launch(ILaunchConfiguration configuration, String mode, ILaunch launch, IProgressMonitor monitor) throws CoreException {
60 : jszursze 1.6 try {
61 : darin 1.27 fLaunch = launch;
62 :     super.launch(configuration, mode, launch, monitor);
63 :     } catch (CoreException e) {
64 :     cleanup(launch);
65 :     throw e;
66 : jszursze 1.6 }
67 : darin 1.27 fLaunch = null;
68 : jszursze 1.1 }
69 :    
70 :     /**
71 : darin 1.8 * Returns the system property string for the policy file
72 : jszursze 1.1 *
73 : darin 1.8 * @param workingDir the working directory
74 :     * @return system property for the policy file
75 : jszursze 1.1 */
76 : darins 1.14 public String getJavaPolicyFile(File workingDir) {
77 : darin 1.8 File file = new File(workingDir, "java.policy.applet");//$NON-NLS-1$
78 : jszursze 1.1 if (!file.exists()) {
79 :     // copy it to the working directory
80 :     File test = LaunchingPlugin.getFileInPlugin(new Path("java.policy.applet")); //$NON-NLS-1$
81 : darins 1.23 BufferedOutputStream outputStream= null;
82 : jszursze 1.1 try {
83 :     byte[] bytes = getFileByteContent(test);
84 : darins 1.23 outputStream = new BufferedOutputStream(new FileOutputStream(file));
85 : jszursze 1.1 outputStream.write(bytes);
86 :     } catch (IOException e) {
87 :     return "";//$NON-NLS-1$
88 : darins 1.23 } finally {
89 :     if (outputStream != null) {
90 :     try {
91 :     outputStream.close();
92 :     } catch (IOException e1) {
93 :     }
94 :     }
95 : jszursze 1.1 }
96 :     }
97 :     return "-Djava.security.policy=java.policy.applet";//$NON-NLS-1$
98 :     }
99 :    
100 :     /**
101 :     * Using the specified launch configuration, build an HTML file that specifies the
102 :     * applet to launch. Return the name of the HTML file.
103 : darin 1.8 *
104 : darins 1.24 * @param dir the directory in which to make the file
105 : jszursze 1.1 */
106 : darin 1.8 private File buildHTMLFile(ILaunchConfiguration configuration, File dir) {
107 : jszursze 1.1 FileWriter writer = null;
108 : jszursze 1.6 File tempFile = null;
109 : jszursze 1.1 try {
110 : darin 1.27 String name = getAppletMainTypeName(configuration);
111 : darin 1.29 tempFile = new File(dir, name + System.currentTimeMillis() + ".html"); //$NON-NLS-1$
112 : jszursze 1.6 writer = new FileWriter(tempFile);
113 : jszursze 1.1 writer.write("<html>\n"); //$NON-NLS-1$
114 :     writer.write("<body>\n"); //$NON-NLS-1$
115 :     writer.write("<applet code="); //$NON-NLS-1$
116 :     writer.write(name);
117 :     writer.write(".class "); //$NON-NLS-1$
118 :     String appletName = configuration.getAttribute(IJavaLaunchConfigurationConstants.ATTR_APPLET_NAME, ""); //$NON-NLS-1$
119 :     if (appletName.length() != 0) {
120 :     writer.write("NAME =\"" + appletName + "\" "); //$NON-NLS-1$ //$NON-NLS-2$
121 :     }
122 :     writer.write("width=\""); //$NON-NLS-1$
123 : darin 1.29 writer.write(Integer.toString(configuration.getAttribute(IJavaLaunchConfigurationConstants.ATTR_APPLET_WIDTH, 200)));
124 : jszursze 1.1 writer.write("\" height=\""); //$NON-NLS-1$
125 : darin 1.29 writer.write(Integer.toString(configuration.getAttribute(IJavaLaunchConfigurationConstants.ATTR_APPLET_HEIGHT, 200)));
126 : jszursze 1.1 writer.write("\" >\n"); //$NON-NLS-1$
127 :     Map parameters = configuration.getAttribute(IJavaLaunchConfigurationConstants.ATTR_APPLET_PARAMETERS, new HashMap());
128 :     if (parameters.size() != 0) {
129 : lbourlier 1.19 Iterator iterator= parameters.entrySet().iterator();
130 : jszursze 1.1 while(iterator.hasNext()) {
131 : lbourlier 1.19 Map.Entry next = (Map.Entry) iterator.next();
132 : jszursze 1.1 writer.write("<param name="); //$NON-NLS-1$
133 : lbourlier 1.19 writer.write(getQuotedString((String)next.getKey()));
134 : jszursze 1.1 writer.write(" value="); //$NON-NLS-1$
135 : lbourlier 1.19 writer.write(getQuotedString((String)next.getValue()));
136 : jszursze 1.1 writer.write(">\n"); //$NON-NLS-1$
137 :     }
138 :     }
139 :     writer.write("</applet>\n"); //$NON-NLS-1$
140 :     writer.write("</body>\n"); //$NON-NLS-1$
141 :     writer.write("</html>\n"); //$NON-NLS-1$
142 :     } 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 : darins 1.26
153 : jszursze 1.6 return tempFile;
154 : lbourlier 1.19 }
155 :    
156 :     private String getQuotedString(String string) {
157 :     if (string.indexOf('"') == -1) {
158 :     return '"' + string + '"';
159 : darins 1.21 }
160 :     return '\'' + string + '\'';
161 : jszursze 1.1 }
162 :    
163 : darins 1.17 /* (non-Javadoc)
164 :     * @see org.eclipse.debug.core.IDebugEventSetListener#handleDebugEvents(org.eclipse.debug.core.DebugEvent[])
165 : jszursze 1.1 */
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 :     // Delete the HTML file used for the launch
173 :     case DebugEvent.TERMINATE :
174 :     if (eventSource != null) {
175 : jszursze 1.6 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 : darin 1.27 if (launch != null) {
184 :     cleanup(launch);
185 : jszursze 1.1 }
186 :     }
187 :     break;
188 :     }
189 :     }
190 :     }
191 : darin 1.27
192 :     /**
193 :     * Cleans up event listener and temp file for the launch.
194 :     *
195 :     * @param launch
196 :     */
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 : jszursze 1.1
211 :     /**
212 :     * Returns the contents of the given file as a byte array.
213 : mrennie 1.32 * @throws IOException if a problem occurred reading the file.
214 : jszursze 1.1 */
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 :     /**
231 :     * Returns the given input stream's contents as a byte array.
232 :     * If a length is specified (ie. if length != -1), only length bytes
233 :     * are returned. Otherwise all bytes in the stream are returned.
234 :     * Note this doesn't close the stream.
235 : mrennie 1.32 * @throws IOException if a problem occurred reading the stream.
236 : jszursze 1.1 */
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 :     // resize contents if needed
248 :     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 :     // read as many bytes as possible
258 :     bytesRead = stream.read(contents, contentsLength, available);
259 :    
260 :     if (bytesRead > 0) {
261 :     // remember length of contents
262 :     contentsLength += bytesRead;
263 :     }
264 :     } while (bytesRead > 0);
265 :    
266 :     // resize contents if necessary
267 :     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 :     // See PR 1FMS89U
281 :     // We record first the read size. In this case len is the actual read size.
282 :     len += readSize;
283 :     readSize = stream.read(contents, len, length - len);
284 :     }
285 :     }
286 :    
287 :     return contents;
288 :     }
289 :    
290 : darins 1.17 /* (non-Javadoc)
291 : darin 1.27 * @see org.eclipse.jdt.launching.AbstractJavaLaunchConfigurationDelegate#getProgramArguments(org.eclipse.debug.core.ILaunchConfiguration)
292 :     */
293 :     public String getProgramArguments(ILaunchConfiguration configuration) throws CoreException {
294 :     File workingDir = verifyWorkingDirectory(configuration);
295 :     // Construct the HTML file and set its name as a program argument
296 :     File htmlFile = buildHTMLFile(configuration, workingDir);
297 :     if (htmlFile == null) {
298 : darin 1.29 abort(LaunchingMessages.JavaAppletLaunchConfigurationDelegate_Could_not_build_HTML_file_for_applet_launch_1, null, IJavaLaunchConfigurationConstants.ERR_COULD_NOT_BUILD_HTML);
299 : darin 1.27 }
300 :     // Add a debug listener if necessary
301 :     if (fgLaunchToFileMap.isEmpty()) {
302 :     DebugPlugin.getDefault().addDebugEventListener(this);
303 :     }
304 :     // Add a mapping of the launch to the html file
305 :     fgLaunchToFileMap.put(fLaunch, htmlFile);
306 :     return htmlFile.getName();
307 :     }
308 :    
309 :     /* (non-Javadoc)
310 :     * @see org.eclipse.jdt.launching.AbstractJavaLaunchConfigurationDelegate#getVMArguments(org.eclipse.debug.core.ILaunchConfiguration)
311 :     */
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(" "); //$NON-NLS-1$
317 :     arguments.append(javaPolicyFile);
318 :     return arguments.toString();
319 :     }
320 :    
321 :     /* (non-Javadoc)
322 :     * @see org.eclipse.jdt.launching.AbstractJavaLaunchConfigurationDelegate#getMainTypeName(org.eclipse.debug.core.ILaunchConfiguration)
323 :     */
324 :     public String getMainTypeName(ILaunchConfiguration configuration) throws CoreException {
325 :     return configuration.getAttribute(IJavaLaunchConfigurationConstants.ATTR_APPLET_APPLETVIEWER_CLASS, IJavaLaunchConfigurationConstants.DEFAULT_APPLETVIEWER_CLASS);
326 :     }
327 :    
328 :     /**
329 :     * Returns the applet's main type name.
330 :     *
331 :     * @param configuration
332 :     * @return
333 :     * @throws CoreException
334 :     */
335 :     protected String getAppletMainTypeName(ILaunchConfiguration configuration) throws CoreException {
336 :     return super.getMainTypeName(configuration);
337 :     }
338 : darin 1.30
339 :     /* (non-Javadoc)
340 :     * @see org.eclipse.jdt.launching.AbstractJavaLaunchConfigurationDelegate#getDefaultWorkingDirectory(org.eclipse.debug.core.ILaunchConfiguration)
341 :     */
342 :     protected File getDefaultWorkingDirectory(ILaunchConfiguration configuration) throws CoreException {
343 :     // default working dir for applets is the project's output directory
344 :     String outputDir = JavaRuntime.getProjectOutputDirectory(configuration);
345 :     if (outputDir == null) {
346 :     // if no project attribute, default to eclipse directory
347 :     return new File(System.getProperty("user.dir")); //$NON-NLS-1$
348 :     }
349 :     IResource resource = ResourcesPlugin.getWorkspace().getRoot().findMember(outputDir);
350 :     if (resource == null || !resource.exists()) {
351 :     //default to eclipse directory
352 :     return new File(System.getProperty("user.dir")); //$NON-NLS-1$
353 :     }
354 :     return resource.getLocation().toFile();
355 :     }
356 :    
357 :    
358 : droberts 1.11 }