KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > launcher > SWTLaunchConfiguration


1 /*******************************************************************************
2  * Copyright (c) 2005, 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.pde.internal.ui.launcher;
12
13 import java.io.File JavaDoc;
14 import java.io.IOException JavaDoc;
15 import java.io.InputStream JavaDoc;
16 import com.ibm.icu.text.MessageFormat;
17 import java.util.ArrayList JavaDoc;
18 import java.util.Enumeration JavaDoc;
19 import java.util.Map JavaDoc;
20 import java.util.zip.ZipEntry JavaDoc;
21 import java.util.zip.ZipException JavaDoc;
22 import java.util.zip.ZipFile JavaDoc;
23
24 import org.eclipse.core.resources.IProject;
25 import org.eclipse.core.resources.IResource;
26 import org.eclipse.core.runtime.CoreException;
27 import org.eclipse.core.runtime.IPath;
28 import org.eclipse.core.runtime.IProgressMonitor;
29 import org.eclipse.core.runtime.NullProgressMonitor;
30 import org.eclipse.core.runtime.Path;
31 import org.eclipse.core.runtime.Platform;
32 import org.eclipse.debug.core.DebugPlugin;
33 import org.eclipse.debug.core.ILaunch;
34 import org.eclipse.debug.core.ILaunchConfiguration;
35 import org.eclipse.jdt.core.IClasspathEntry;
36 import org.eclipse.jdt.core.IJavaProject;
37 import org.eclipse.jdt.core.JavaCore;
38 import org.eclipse.jdt.launching.AbstractJavaLaunchConfigurationDelegate;
39 import org.eclipse.jdt.launching.ExecutionArguments;
40 import org.eclipse.jdt.launching.IVMInstall;
41 import org.eclipse.jdt.launching.IVMRunner;
42 import org.eclipse.jdt.launching.JavaRuntime;
43 import org.eclipse.jdt.launching.VMRunnerConfiguration;
44 import org.eclipse.osgi.service.environment.Constants;
45 import org.eclipse.osgi.service.resolver.BundleDescription;
46 import org.eclipse.pde.core.plugin.IFragmentModel;
47 import org.eclipse.pde.core.plugin.IPluginLibrary;
48 import org.eclipse.pde.core.plugin.IPluginModelBase;
49 import org.eclipse.pde.internal.core.ClasspathUtilCore;
50 import org.eclipse.pde.internal.core.PDECore;
51 import org.eclipse.pde.internal.core.PluginModelManager;
52 import org.eclipse.pde.internal.core.TargetPlatform;
53 import org.eclipse.pde.internal.core.util.CoreUtility;
54 import org.eclipse.pde.internal.ui.PDEPlugin;
55 import org.osgi.framework.Version;
56
57 public class SWTLaunchConfiguration extends
58         AbstractJavaLaunchConfigurationDelegate {
59
60     private boolean fShouldDelete;
61
62     public void launch(ILaunchConfiguration configuration, String JavaDoc mode, ILaunch launch, IProgressMonitor monitor) throws CoreException {
63         if (monitor == null) {
64             monitor = new NullProgressMonitor();
65         }
66         fShouldDelete = true;
67         
68         monitor.beginTask(MessageFormat.format("{0}...", new String JavaDoc[]{configuration.getName()}), 3); //$NON-NLS-1$
69
// check for cancellation
70
if (monitor.isCanceled()) {
71             return;
72         }
73         
74         String JavaDoc mainTypeName = verifyMainTypeName(configuration);
75
76         IVMInstall vm = verifyVMInstall(configuration);
77
78         IVMRunner runner = vm.getVMRunner(mode);
79         if (runner == null) {
80             monitor.setCanceled(true);
81         }
82
83         File JavaDoc workingDir = verifyWorkingDirectory(configuration);
84         String JavaDoc workingDirName = null;
85         if (workingDir != null) {
86             workingDirName = workingDir.getAbsolutePath();
87         }
88         
89         // Environment variables
90
String JavaDoc[] envp= DebugPlugin.getDefault().getLaunchManager().getEnvironment(configuration);
91         
92         // Program & VM args
93
String JavaDoc pgmArgs = getProgramArguments(configuration);
94         String JavaDoc vmArgs = getVMArguments(configuration);
95         ExecutionArguments execArgs = new ExecutionArguments(vmArgs, pgmArgs);
96         
97         // Find SWT Fragments from the target platform
98
BundleDescription[] fragments = findFragments();
99         
100         // VM-specific attributes
101
Map JavaDoc vmAttributesMap = getVMSpecificAttributesMap(configuration);
102         
103         // Classpath
104
String JavaDoc[] classpath = getClasspath(fragments, configuration);
105         
106         // Create VM config
107
VMRunnerConfiguration runConfig = new VMRunnerConfiguration(mainTypeName, classpath);
108         runConfig.setProgramArguments(execArgs.getProgramArgumentsArray());
109         runConfig.setEnvironment(envp);
110         runConfig.setVMArguments(getVMArguments(fragments, execArgs));
111         runConfig.setWorkingDirectory(workingDirName);
112         runConfig.setVMSpecificAttributesMap(vmAttributesMap);
113
114         // Bootpath
115
runConfig.setBootClassPath(getBootpath(configuration));
116         
117         // check for cancellation
118
if (monitor.isCanceled()) {
119             return;
120         }
121         
122         // stop in main
123
prepareStopInMain(configuration);
124         
125         // done the verification phase
126
monitor.worked(1);
127         
128         // set the default source locator if required
129
setDefaultSourceLocator(launch, configuration);
130         monitor.worked(1);
131         
132         // Launch the configuration - 1 unit of work
133
runner.run(runConfig, launch, monitor);
134         
135         // check for cancellation
136
if (monitor.isCanceled()) {
137             return;
138         }
139         
140         monitor.done();
141     }
142     
143     private String JavaDoc[] getVMArguments(BundleDescription[] fragments, ExecutionArguments execArgs) {
144         if (fragments.length == 0)
145             return execArgs.getVMArgumentsArray();
146         
147         String JavaDoc location = getNativeLibrariesLocations(fragments);
148         if (location == null)
149             return execArgs.getVMArgumentsArray();
150         
151         String JavaDoc[] vmArgs = execArgs.getVMArgumentsArray();
152         for (int i = vmArgs.length - 1; i >= 0; i--) {
153             if (vmArgs[i].startsWith("-Djava.library.path")) { //$NON-NLS-1$
154
vmArgs[i] += File.pathSeparatorChar + location;
155                 return vmArgs;
156             }
157         }
158         String JavaDoc[] all = new String JavaDoc[vmArgs.length + 1];
159         all[0] = "-Djava.library.path=" + location; //$NON-NLS-1$
160
System.arraycopy(vmArgs, 0, all, 1, vmArgs.length);
161         return all;
162     }
163     
164     private String JavaDoc getNativeLibrariesLocations(BundleDescription[] bundles) {
165         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
166         for (int i = 0; i < bundles.length; i++) {
167             String JavaDoc location = getNativeLibrariesLocation(bundles[i]);
168             if (location != null) {
169                 if (buffer.length() > 0)
170                     buffer.append(File.pathSeparatorChar);
171                 buffer.append(location);
172             }
173         }
174         return buffer.length() == 0 ? null : buffer.toString();
175     }
176
177     protected static BundleDescription[] findFragments() {
178         IPluginModelBase model = PDECore.getDefault().getModelManager().findModel("org.eclipse.swt"); //$NON-NLS-1$
179
if (model != null && model.isEnabled()) {
180             BundleDescription desc = model.getBundleDescription();
181             if (desc.getContainingState() != null)
182                 return desc.getFragments();
183         }
184         return new BundleDescription[0];
185     }
186
187     private String JavaDoc getNativeLibrariesLocation(BundleDescription fragment) {
188         if (!fragment.isResolved())
189             return null;
190         Version version = fragment.getVersion();
191         if (version.getMajor() < 3 || version.getMinor() < 1)
192             return getLegacyNativeLibrariesLocation(fragment);
193         
194         File JavaDoc file = new File JavaDoc(fragment.getLocation());
195         return file.isDirectory() ? fragment.getLocation() : getExtractionLocation(file);
196     }
197     
198     private String JavaDoc getExtractionLocation(File JavaDoc file) {
199         long timestamp = file.lastModified() ^ file.getAbsolutePath().hashCode();
200         File JavaDoc metadata = PDEPlugin.getDefault().getStateLocation().toFile();
201         File JavaDoc cache = new File JavaDoc(metadata, Long.toString(timestamp) + ".swt"); //$NON-NLS-1$
202
if (!cache.exists()){
203             if (fShouldDelete) {
204                 deleteStaleCache(metadata);
205                 fShouldDelete = false;
206             }
207             cache.mkdirs();
208             extractZipFile(file, cache);
209         }
210         return cache.getAbsolutePath();
211     }
212     
213     private void deleteStaleCache(File JavaDoc metadata) {
214         if (!metadata.exists())
215             return;
216         
217         File JavaDoc[] children = metadata.listFiles();
218         if (children == null)
219             return;
220         for (int i = 0; i < children.length; i++) {
221             if (children[i].isDirectory() && children[i].getName().endsWith(".swt")) { //$NON-NLS-1$
222
CoreUtility.deleteContent(children[i]);
223             }
224         }
225     }
226     
227     private void extractZipFile(File JavaDoc fragment, File JavaDoc destination) {
228         ZipFile JavaDoc zipFile = null;
229         try {
230             zipFile = new ZipFile JavaDoc(fragment);
231             for (Enumeration JavaDoc zipEntries = zipFile.entries(); zipEntries.hasMoreElements();) {
232                 ZipEntry JavaDoc zipEntry = (ZipEntry JavaDoc) zipEntries.nextElement();
233                 if (zipEntry.isDirectory())
234                     continue;
235                 if (isInterestingFile(zipEntry.getName())) {
236                     InputStream JavaDoc in = null;
237                     try {
238                         in = zipFile.getInputStream(zipEntry);
239                         if (in != null) {
240                             File JavaDoc file = new File JavaDoc(destination, zipEntry.getName());
241                             CoreUtility.readFile(in, file);
242                             if (!Platform.getOS().equals(Constants.OS_WIN32))
243                                 Runtime.getRuntime().exec(new String JavaDoc[] {"chmod", "755", file.getAbsolutePath()}).waitFor(); //$NON-NLS-1$ //$NON-NLS-2$
244
}
245                     } catch (IOException JavaDoc e) {
246                     } catch (InterruptedException JavaDoc e) {
247                     } finally {
248                         try {
249                             if (in != null)
250                                 in.close();
251                         } catch (IOException JavaDoc e1) {
252                         }
253                     }
254                 }
255             }
256         } catch (ZipException JavaDoc e) {
257         } catch (IOException JavaDoc e) {
258         } finally {
259             try {
260                 if (zipFile != null)
261                     zipFile.close();
262             } catch (IOException JavaDoc e) {
263             }
264         }
265     }
266     
267     private boolean isInterestingFile(String JavaDoc name) {
268         Path path = new Path(name);
269         if (path.segmentCount() > 1)
270             return false;
271         return name.endsWith(".dll") //$NON-NLS-1$
272
|| name.endsWith(".jnilib") //$NON-NLS-1$
273
|| name.endsWith(".sl") //$NON-NLS-1$
274
|| name.endsWith(".a") //$NON-NLS-1$
275
|| name.indexOf(".so") != -1; //$NON-NLS-1$
276
}
277     
278     private String JavaDoc getLegacyNativeLibrariesLocation(BundleDescription fragment) {
279         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
280         IPath path = new Path(fragment.getLocation());
281         buffer.append(path.removeTrailingSeparator().toString());
282         buffer.append(IPath.SEPARATOR);
283         buffer.append("os"); //$NON-NLS-1$
284
buffer.append(IPath.SEPARATOR);
285         buffer.append(TargetPlatform.getOS());
286         buffer.append(IPath.SEPARATOR);
287         buffer.append(TargetPlatform.getOSArch());
288         return buffer.toString();
289     }
290         
291     private String JavaDoc[] getClasspath(BundleDescription[] fragments, ILaunchConfiguration configuration) throws CoreException {
292         String JavaDoc[] entries = getClasspath(configuration);
293         
294         PluginModelManager manager = PDECore.getDefault().getModelManager();
295         ArrayList JavaDoc extra = new ArrayList JavaDoc();
296         for (int i = 0; i < fragments.length; i++) {
297             IFragmentModel fragment = manager.findFragmentModel(fragments[i].getSymbolicName());
298             if (fragment == null)
299                 continue;
300             
301             IResource resource = fragment.getUnderlyingResource();
302             if (resource != null) {
303                 IProject project = resource.getProject();
304                 if (project.hasNature(JavaCore.NATURE_ID)) {
305                     IJavaProject jProject = JavaCore.create(project);
306                     extra.add(JavaRuntime.newProjectRuntimeClasspathEntry(jProject).getPath());
307                     IClasspathEntry[] classEntries = jProject.getRawClasspath();
308                     for (int j = 0; j < classEntries.length; j++) {
309                         int kind = classEntries[j].getEntryKind();
310                         if (kind == IClasspathEntry.CPE_LIBRARY) {
311                             extra.add(JavaRuntime.newArchiveRuntimeClasspathEntry(classEntries[j].getPath()).getLocation());
312                         }
313                     }
314                 }
315             } else {
316                 IPluginLibrary[] libraries = fragment.getFragment().getLibraries();
317                 String JavaDoc location = fragment.getInstallLocation();
318                 for (int j = 0; j < libraries.length; j++) {
319                     String JavaDoc name = ClasspathUtilCore.expandLibraryName(libraries[j].getName());
320                     extra.add(new Path(location).append(name).toOSString());
321                 }
322             }
323         }
324         if (extra.size() > 0) {
325             String JavaDoc[] all = new String JavaDoc[entries.length + extra.size()];
326             System.arraycopy(entries, 0, all, 0, entries.length);
327             for (int i = 0; i < extra.size(); i++) {
328                 all[i+entries.length] = extra.get(i).toString();
329             }
330             return all;
331         }
332         return entries;
333     }
334 }
335
Popular Tags