KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > launching > JavaAppletLaunchConfigurationDelegate


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.launching;
12
13
14 import java.io.BufferedInputStream JavaDoc;
15 import java.io.BufferedOutputStream JavaDoc;
16 import java.io.File JavaDoc;
17 import java.io.FileInputStream JavaDoc;
18 import java.io.FileOutputStream JavaDoc;
19 import java.io.FileWriter JavaDoc;
20 import java.io.IOException JavaDoc;
21 import java.io.InputStream JavaDoc;
22 import java.util.HashMap JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.Map JavaDoc;
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     /**
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 JavaDoc fgLaunchToFileMap = new HashMap JavaDoc();
50     
51     /**
52      * Used to map temp file to launch obejct.
53      */

54     private ILaunch fLaunch;
55     
56     /* (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      */

59     public synchronized void launch(ILaunchConfiguration configuration, String JavaDoc 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     /**
71      * Returns the system property string for the policy file
72      *
73      * @param workingDir the working directory
74      * @return system property for the policy file
75      */

76     public String JavaDoc getJavaPolicyFile(File JavaDoc workingDir) {
77             File JavaDoc file = new File JavaDoc(workingDir, "java.policy.applet");//$NON-NLS-1$
78
if (!file.exists()) {
79                 // copy it to the working directory
80
File JavaDoc test = LaunchingPlugin.getFileInPlugin(new Path("java.policy.applet")); //$NON-NLS-1$
81
BufferedOutputStream JavaDoc outputStream= null;
82                 try {
83                     byte[] bytes = getFileByteContent(test);
84                     outputStream = new BufferedOutputStream JavaDoc(new FileOutputStream JavaDoc(file));
85                     outputStream.write(bytes);
86                 } catch (IOException JavaDoc e) {
87                     return "";//$NON-NLS-1$
88
} finally {
89                     if (outputStream != null) {
90                         try {
91                             outputStream.close();
92                         } catch (IOException JavaDoc e1) {
93                         }
94                     }
95                 }
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      *
104      * @param dir the directory in which to make the file
105      */

106     private File JavaDoc buildHTMLFile(ILaunchConfiguration configuration, File JavaDoc dir) {
107         FileWriter JavaDoc writer = null;
108         File JavaDoc tempFile = null;
109         try {
110             String JavaDoc name = getAppletMainTypeName(configuration);
111             tempFile = new File JavaDoc(dir, name + System.currentTimeMillis() + ".html"); //$NON-NLS-1$
112
writer = new FileWriter JavaDoc(tempFile);
113             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 JavaDoc 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
writer.write(Integer.toString(configuration.getAttribute(IJavaLaunchConfigurationConstants.ATTR_APPLET_WIDTH, 200)));
124             writer.write("\" height=\""); //$NON-NLS-1$
125
writer.write(Integer.toString(configuration.getAttribute(IJavaLaunchConfigurationConstants.ATTR_APPLET_HEIGHT, 200)));
126             writer.write("\" >\n"); //$NON-NLS-1$
127
Map JavaDoc parameters = configuration.getAttribute(IJavaLaunchConfigurationConstants.ATTR_APPLET_PARAMETERS, new HashMap JavaDoc());
128             if (parameters.size() != 0) {
129                 Iterator JavaDoc iterator= parameters.entrySet().iterator();
130                 while(iterator.hasNext()) {
131                     Map.Entry JavaDoc next = (Map.Entry JavaDoc) iterator.next();
132                     writer.write("<param name="); //$NON-NLS-1$
133
writer.write(getQuotedString((String JavaDoc)next.getKey()));
134                     writer.write(" value="); //$NON-NLS-1$
135
writer.write(getQuotedString((String JavaDoc)next.getValue()));
136                     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 JavaDoc e) {
143         } catch(CoreException e) {
144         } finally {
145             if (writer != null) {
146                 try {
147                     writer.close();
148                 } catch(IOException JavaDoc e) {
149                 }
150             }
151         }
152         
153         return tempFile;
154     }
155     
156     private String JavaDoc getQuotedString(String JavaDoc string) {
157         if (string.indexOf('"') == -1) {
158             return '"' + string + '"';
159         }
160         return '\'' + string + '\'';
161     }
162     
163     /* (non-Javadoc)
164      * @see org.eclipse.debug.core.IDebugEventSetListener#handleDebugEvents(org.eclipse.debug.core.DebugEvent[])
165      */

166     public void handleDebugEvents(DebugEvent[] events) {
167         for (int i = 0; i < events.length; i++) {
168             DebugEvent event = events[i];
169             Object JavaDoc 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                         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     /**
193      * Cleans up event listener and temp file for the launch.
194      *
195      * @param launch
196      */

197     private void cleanup(ILaunch launch) {
198         File JavaDoc temp = (File JavaDoc) 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     /**
212      * Returns the contents of the given file as a byte array.
213      * @throws IOException if a problem occured reading the file.
214      */

215     protected static byte[] getFileByteContent(File JavaDoc file) throws IOException JavaDoc {
216         InputStream JavaDoc stream = null;
217         try {
218             stream = new BufferedInputStream JavaDoc(new FileInputStream JavaDoc(file));
219             return getInputStreamAsByteArray(stream, (int) file.length());
220         } finally {
221             if (stream != null) {
222                 try {
223                     stream.close();
224                 } catch (IOException JavaDoc 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      * @throws IOException if a problem occured reading the stream.
236      */

237     protected static byte[] getInputStreamAsByteArray(InputStream JavaDoc stream, int length)
238         throws IOException JavaDoc {
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     /* (non-Javadoc)
291      * @see org.eclipse.jdt.launching.AbstractJavaLaunchConfigurationDelegate#getProgramArguments(org.eclipse.debug.core.ILaunchConfiguration)
292      */

293     public String JavaDoc getProgramArguments(ILaunchConfiguration configuration) throws CoreException {
294         File JavaDoc workingDir = verifyWorkingDirectory(configuration);
295         // Construct the HTML file and set its name as a program argument
296
File JavaDoc 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         // 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 JavaDoc getVMArguments(ILaunchConfiguration configuration) throws CoreException {
313         StringBuffer JavaDoc arguments = new StringBuffer JavaDoc(super.getVMArguments(configuration));
314         File JavaDoc workingDir = verifyWorkingDirectory(configuration);
315         String JavaDoc 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 JavaDoc 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 JavaDoc getAppletMainTypeName(ILaunchConfiguration configuration) throws CoreException {
336         return super.getMainTypeName(configuration);
337     }
338
339     /* (non-Javadoc)
340      * @see org.eclipse.jdt.launching.AbstractJavaLaunchConfigurationDelegate#getDefaultWorkingDirectory(org.eclipse.debug.core.ILaunchConfiguration)
341      */

342     protected File JavaDoc getDefaultWorkingDirectory(ILaunchConfiguration configuration) throws CoreException {
343         // default working dir for applets is the project's output directory
344
String JavaDoc outputDir = JavaRuntime.getProjectOutputDirectory(configuration);
345         if (outputDir == null) {
346             // if no project attribute, default to eclipse directory
347
return new File JavaDoc(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 JavaDoc(System.getProperty("user.dir")); //$NON-NLS-1$
353
}
354         return resource.getLocation().toFile();
355     }
356     
357     
358 }
359
Popular Tags